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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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().shard(strategy1).add_prim_attr("primitive_target", target)
  44. self.mm = P.BatchMatMul().shard(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. net.set_train()
  57. _executor.compile(net, x, y)
  58. def test_embeddinglookup_reducescatter_true():
  59. shape = [8, 8]
  60. offset = 8
  61. net = NetWithLoss(Net(shape, offset))
  62. net.set_auto_parallel()
  63. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  64. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  65. net.set_train()
  66. _executor.compile(net, x, y)
  67. def test_embeddinglookup_reducescatter_false_grad():
  68. shape = [8, 8]
  69. offset = 8
  70. net = GradWrap(NetWithLoss(Net(shape, offset)))
  71. net.set_auto_parallel()
  72. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  73. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  74. net.set_train()
  75. _executor.compile(net, x, y)
  76. def test_embeddinglookup_reducescatter_true_grad():
  77. context.set_context(save_graphs=True)
  78. shape = [8, 8]
  79. offset = 8
  80. net = GradWrap(NetWithLoss(Net(shape, offset)))
  81. net.set_auto_parallel()
  82. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  83. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  84. net.set_train()
  85. _executor.compile(net, x, y)
  86. def test_embeddinglookup_semi_auto1():
  87. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  88. shape = [64, 32]
  89. offset = 0
  90. strategy1 = ((8, 1), (1, 1))
  91. strategy2 = ((4, 1, 2), (4, 2, 1))
  92. net = GradWrap(NetWithLoss(Net(shape, offset, strategy1, strategy2, "CPU")))
  93. net.set_auto_parallel()
  94. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  95. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  96. net.set_train()
  97. _executor.compile(net, x, y)
  98. def test_embeddinglookup_semi_auto2():
  99. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  100. shape = [64, 32]
  101. offset = 0
  102. strategy1 = ((1, 8), (1, 1))
  103. strategy2 = ((4, 1, 2), (4, 2, 1))
  104. net = GradWrap(NetWithLoss(Net(shape, offset, strategy1, strategy2, "CPU")))
  105. net.set_auto_parallel()
  106. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  107. y = Tensor(np.ones([64, 64, 64]), dtype=ms.float32)
  108. net.set_train()
  109. _executor.compile(net, x, y)