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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. import numpy as np
  15. import mindspore as ms
  16. import mindspore.nn as nn
  17. from mindspore import Tensor
  18. from mindspore import context
  19. from mindspore.common.api import _executor
  20. from mindspore.common.parameter import Parameter
  21. from mindspore.ops import composite as C
  22. from mindspore.ops import operations as P
  23. from mindspore.common.initializer import initializer
  24. from mindspore.nn import TrainOneStepCell, Momentum
  25. from tests.ut.python.ops.test_math_ops import VirtualLoss
  26. grad_all = C.GradOperation(get_all=True)
  27. class NetWithLoss(nn.Cell):
  28. def __init__(self, network):
  29. super(NetWithLoss, self).__init__()
  30. self.loss = VirtualLoss()
  31. self.network = network
  32. def construct(self, x):
  33. predict = self.network(x)
  34. return self.loss(predict)
  35. class GradWrap(nn.Cell):
  36. def __init__(self, network):
  37. super(GradWrap, self).__init__()
  38. self.network = network
  39. def construct(self, x):
  40. return grad_all(self.network)(x)
  41. def test_unique_column_split():
  42. class Net(nn.Cell):
  43. def __init__(self):
  44. super().__init__()
  45. self.unique = P.Unique().shard(((1,),))
  46. self.relu = P.ReLU()
  47. self.mul = P.Mul()
  48. self.embedding_lookp = P.GatherV2().shard(((1, 8), (1,)))
  49. self.embedding_table = Parameter(initializer('normal', [2000, 128]),
  50. name='embedding_table')
  51. self.gatherv2 = P.GatherV2().shard(((1, 8), (1,)))
  52. self.reshape = P.Reshape()
  53. self.matmul = P.MatMul()
  54. self.mul_weight = Parameter(Tensor(np.full([32, 64, 1], 0.5, dtype=np.float32)), name="mul_weight")
  55. def construct(self, indices):
  56. indices_flatten = self.reshape(indices, (-1,))
  57. unique_id, unique_idx = self.unique(indices_flatten)
  58. unique_id_weight = self.embedding_lookp(self.embedding_table, unique_id, 0)
  59. weight_flatten = self.gatherv2(unique_id_weight, unique_idx, 0)
  60. weight = self.reshape(weight_flatten, (32, 64, 128))
  61. vx = self.mul(weight, self.mul_weight)
  62. return vx
  63. size = 8
  64. context.set_auto_parallel_context(device_num=size, global_rank=0, parallel_mode="auto_parallel")
  65. x = Tensor(np.ones([32, 64]), dtype=ms.int32)
  66. net = Net()
  67. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  68. train_net = TrainOneStepCell(net, optimizer)
  69. train_net.set_auto_parallel()
  70. train_net.set_train()
  71. _executor.compile(train_net, x)
  72. def test_unique_row_split():
  73. class Net(nn.Cell):
  74. def __init__(self):
  75. super().__init__()
  76. self.unique = P.Unique().shard(((1,),))
  77. self.relu = P.ReLU()
  78. self.mul = P.Mul()
  79. self.embedding_lookp = P.GatherV2().shard(((8, 1), (1,)))
  80. self.embedding_table = Parameter(initializer('normal', [2000, 128]),
  81. name='embedding_table')
  82. self.gatherv2 = P.GatherV2().shard(((1, 1), (1,)))
  83. self.reshape = P.Reshape()
  84. self.matmul = P.MatMul()
  85. self.mul_weight = Parameter(Tensor(np.full([32, 64, 1], 0.5, dtype=np.float32)), name="mul_weight")
  86. def construct(self, indices):
  87. indices_flatten = self.reshape(indices, (-1,))
  88. unique_id, unique_idx = self.unique(indices_flatten)
  89. unique_id_weight = self.embedding_lookp(self.embedding_table, unique_id, 0)
  90. weight_flatten = self.gatherv2(unique_id_weight, unique_idx, 0)
  91. weight = self.reshape(weight_flatten, (32, 64, 128))
  92. vx = self.mul(weight, self.mul_weight)
  93. return vx
  94. size = 8
  95. context.set_auto_parallel_context(device_num=size, global_rank=0, parallel_mode="semi_auto_parallel")
  96. x = Tensor(np.ones([32, 64]), dtype=ms.int32)
  97. net = Net()
  98. optimizer = Momentum(net.trainable_params(), learning_rate=0.1, momentum=0.9)
  99. train_net = TrainOneStepCell(net, optimizer)
  100. train_net.set_auto_parallel()
  101. train_net.set_train()
  102. _executor.compile(train_net, x)