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

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