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

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