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_embeddinglookup.py 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. # Copyright 2019 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. import numpy as np
  16. import mindspore as ms
  17. import mindspore.nn as nn
  18. from mindspore.common.api import _executor
  19. from mindspore.ops import operations as P
  20. from mindspore.ops import composite as C
  21. from mindspore import Tensor, context
  22. from tests.ut.python.ops.test_math_ops import VirtualLoss
  23. class GradWrap(nn.Cell):
  24. def __init__(self, network):
  25. super(GradWrap, self).__init__()
  26. self.network = network
  27. def construct(self, x, y):
  28. return C.grad_all(self.network)(x, y)
  29. class NetWithLoss(nn.Cell):
  30. def __init__(self, network):
  31. super(NetWithLoss, self).__init__()
  32. self.loss = VirtualLoss()
  33. self.network = network
  34. def construct(self, x, y):
  35. predict = self.network(x, y)
  36. return self.loss(predict)
  37. class Net(nn.Cell):
  38. def __init__(self, shape, offset, strategy1=None, strategy2=None, target="Device"):
  39. super().__init__()
  40. self.index = Tensor(np.ones(shape), dtype=ms.int32)
  41. self.offset = offset
  42. self.elu = P.EmbeddingLookup().set_strategy(strategy1).add_prim_attr("primitive_target", target)
  43. self.mm = P.BatchMatMul().set_strategy(strategy2)
  44. def construct(self, x, y):
  45. out = self.elu(x, self.index, self.offset)
  46. out = self.mm(out, y)
  47. return out
  48. def test_embeddinglookup_reducescatter_false():
  49. shape = [8, 8]
  50. offset = 8
  51. net = NetWithLoss(Net(shape, offset))
  52. net.set_auto_parallel()
  53. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  54. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  55. _executor.compile(net, x, y)
  56. def test_embeddinglookup_reducescatter_true():
  57. shape = [8, 8]
  58. offset = 8
  59. net = NetWithLoss(Net(shape, offset))
  60. net.set_auto_parallel()
  61. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  62. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  63. _executor.compile(net, x, y)
  64. def test_embeddinglookup_reducescatter_false_grad():
  65. shape = [8, 8]
  66. offset = 8
  67. net = GradWrap(NetWithLoss(Net(shape, offset)))
  68. net.set_auto_parallel()
  69. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  70. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  71. _executor.compile(net, x, y)
  72. def test_embeddinglookup_reducescatter_true_grad():
  73. context.set_context(save_graphs=True)
  74. shape = [8, 8]
  75. offset = 8
  76. net = GradWrap(NetWithLoss(Net(shape, offset)))
  77. net.set_auto_parallel()
  78. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  79. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  80. _executor.compile(net, x, y)
  81. def test_embeddinglookup_semi_auto1():
  82. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  83. shape = [64, 32]
  84. offset = 0
  85. strategy1 = ((8, 1), (1, 1))
  86. strategy2 = ((4, 1, 2), (4, 2, 1))
  87. net = GradWrap(NetWithLoss(Net(shape, offset, strategy1, strategy2, "CPU")))
  88. net.set_auto_parallel()
  89. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  90. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  91. _executor.compile(net, x, y)
  92. def test_embeddinglookup_semi_auto2():
  93. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  94. shape = [64, 32]
  95. offset = 0
  96. strategy1 = ((1, 8), (1, 1))
  97. strategy2 = ((4, 1, 2), (4, 2, 1))
  98. net = GradWrap(NetWithLoss(Net(shape, offset, strategy1, strategy2, "CPU")))
  99. net.set_auto_parallel()
  100. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  101. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  102. _executor.compile(net, x, y)