You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_dynamic_shape.py 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """ test dynamic shape """
  16. from mindspore import Tensor, context, nn, Parameter
  17. from mindspore.ops import operations as P
  18. from mindspore import dtype as mstype
  19. import numpy as np
  20. context.set_context(mode=context.GRAPH_MODE, save_graphs=False)
  21. def test_sparse_apply_proximal_ada_grad():
  22. class Net(nn.Cell):
  23. def __init__(self):
  24. super(Net, self).__init__()
  25. self.sparse_apply_proximal_adagrad = P.SparseApplyProximalAdagrad()
  26. self.var = Parameter(Tensor(np.random.rand(7800, 80).astype(np.float32)), name="var")
  27. self.accum = Parameter(Tensor(np.random.rand(7800, 80).astype(np.float32)), name="accum")
  28. self.lr = 0.01
  29. self.l1 = 0.0
  30. self.l2 = 0.0
  31. def construct(self, grad, indices):
  32. out = self.sparse_apply_proximal_adagrad(self.var, self.accum, self.lr, self.l1, self.l2, grad, indices)
  33. return out[0]
  34. class NetWrapper(nn.Cell):
  35. def __init__(self):
  36. super(NetWrapper, self).__init__()
  37. self.unq = P.Unique()
  38. self.add = P.TensorAdd()
  39. self.expand_dims = P.ExpandDims()
  40. self.cast = P.Cast()
  41. self.net = Net()
  42. def construct(self, grad, inp):
  43. ids, _ = self.unq(inp)
  44. new_grad = self.expand_dims(ids, 1)
  45. new_grad = self.cast(new_grad, mstype.float32) + grad
  46. return self.net(new_grad, ids)
  47. net = NetWrapper()
  48. grad = Tensor(np.random.rand(1, 80).astype(np.float32))
  49. indices = Tensor(np.ones([7800]), mstype.int32)
  50. net(grad, indices)
  51. def test_sparse_apply_ftrl():
  52. class SparseApplyFtrlNet(nn.Cell):
  53. def __init__(self):
  54. super(SparseApplyFtrlNet, self).__init__()
  55. self.sparse_apply_ftrl = P.SparseApplyFtrl(lr=0.01, l1=0.0, l2=0.0, lr_power=-0.5)
  56. self.var = Parameter(Tensor(np.random.rand(7800, 80).astype(np.float32)), name="var")
  57. self.accum = Parameter(Tensor(np.random.rand(7800, 80).astype(np.float32)), name="accum")
  58. self.linear = Parameter(Tensor(np.random.rand(7800, 80).astype(np.float32)), name="linear")
  59. def construct(self, grad, indices):
  60. out = self.sparse_apply_ftrl(self.var, self.accum, self.linear, grad, indices)
  61. return out[0]
  62. class NetWrapper(nn.Cell):
  63. def __init__(self):
  64. super(NetWrapper, self).__init__()
  65. self.unq = P.Unique()
  66. self.add = P.TensorAdd()
  67. self.expand_dims = P.ExpandDims()
  68. self.cast = P.Cast()
  69. self.net = SparseApplyFtrlNet()
  70. def construct(self, grad, inp):
  71. ids, _ = self.unq(inp)
  72. new_grad = self.expand_dims(ids, 1)
  73. new_grad = self.cast(new_grad, mstype.float32) + grad
  74. return self.net(new_grad, ids)
  75. net = NetWrapper()
  76. grad = Tensor(np.random.rand(1, 80).astype(np.float32))
  77. indices = Tensor(np.ones([7800]), mstype.int32)
  78. net(grad, indices)
  79. def test_gatherv2():
  80. class Net(nn.Cell):
  81. def __init__(self):
  82. super(Net, self).__init__()
  83. self.unq = P.Unique()
  84. self.gather = P.GatherV2()
  85. def construct(self, x, y):
  86. u, _ = self.unq(y)
  87. z = self.gather(x, u, 0)
  88. return z
  89. x = Tensor(np.ones([20, 12], dtype=np.float32))
  90. y = Tensor(np.ones([8], dtype=np.int32))
  91. net = Net()
  92. net(x, y)