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 5.2 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  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. import numpy as np
  17. from mindspore import Tensor, context, nn, Parameter
  18. from mindspore import dtype as mstype
  19. from mindspore.ops import operations as P
  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.Add()
  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.Add()
  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.Gather()
  85. self.yy = Tensor(np.ones([8], dtype=np.int32))
  86. def construct(self, x, y):
  87. shp = P.Shape()(self.yy)
  88. y = P.Reshape()(y, shp)
  89. u, _ = self.unq(y)
  90. u_shp = P.DynamicShape()(u)
  91. z = self.gather(x, u, 0)
  92. return z, u_shp
  93. x = Tensor(np.ones([20, 12], dtype=np.float32))
  94. y = Tensor(np.ones([2, 4], dtype=np.int32))
  95. net = Net()
  96. net(x, y)
  97. def test_segmentsum():
  98. class Net(nn.Cell):
  99. def __init__(self):
  100. super(Net, self).__init__()
  101. self.unq = P.Unique()
  102. self.segment_ids = Tensor([0, 0, 1, 2, 1, 1, 1, 1], mstype.int32)
  103. self.sum = P.UnsortedSegmentSum()
  104. def construct(self, x):
  105. u, _ = self.unq(x)
  106. shp = P.DynamicShape()(u)
  107. z = self.sum(x, self.segment_ids, shp[0])
  108. return z, shp[0]
  109. x = Tensor(np.ones([8], dtype=np.int32))
  110. net = Net()
  111. net(x)
  112. def test_addn():
  113. class Net(nn.Cell):
  114. def __init__(self):
  115. super(Net, self).__init__()
  116. self.unq = P.Unique()
  117. self.addn = P.AddN()
  118. def construct(self, x):
  119. u, _ = self.unq(x)
  120. u = self.addn((u, u, u))
  121. z = self.addn([u, u])
  122. return z
  123. y = Tensor(np.ones([8], dtype=np.int32))
  124. net = Net()
  125. net(y)