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

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