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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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 import Tensor
  19. from mindspore.common.api import _executor
  20. from mindspore.ops import operations as P
  21. from mindspore.ops.operations import _inner_ops as inner
  22. from tests.ut.python.ops.test_math_ops import VirtualLoss
  23. class NetWithLoss(nn.Cell):
  24. def __init__(self, network):
  25. super(NetWithLoss, self).__init__()
  26. self.loss = VirtualLoss()
  27. self.network = network
  28. def construct(self, x, y):
  29. predict = self.network(x, y)
  30. return self.loss(predict)
  31. class Net(nn.Cell):
  32. def __init__(self, shape, offset, reduce_scatter_flag, split_num):
  33. super().__init__()
  34. self.index = Tensor(np.ones(shape), dtype=ms.int32)
  35. self.offset = offset
  36. self.reduce_scatter_flag = reduce_scatter_flag
  37. self.split_num = split_num
  38. self.elu = inner.EmbeddingLookup()
  39. self.mm = P.BatchMatMul()
  40. def construct(self, x, y):
  41. out = self.elu(x, self.index, self.offset, self.reduce_scatter_flag, self.split_num)
  42. out = self.mm(out, y)
  43. return out
  44. def test_embeddinglookup_reducescatter_false():
  45. shape = [8, 8]
  46. offset = 8
  47. reduce_scatter_flag = False
  48. split_num = 1
  49. net = NetWithLoss(Net(shape, offset, reduce_scatter_flag, split_num))
  50. net.set_auto_parallel()
  51. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  52. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  53. _executor.compile(net, x, y)
  54. def test_embeddinglookup_reducescatter_true():
  55. shape = [64, 8]
  56. offset = 8
  57. reduce_scatter_flag = True
  58. split_num = 8
  59. net = NetWithLoss(Net(shape, offset, reduce_scatter_flag, split_num))
  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)