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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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.ops.operations import _inner_ops as inner
  22. from mindspore import Tensor, context
  23. from tests.ut.python.ops.test_math_ops import VirtualLoss
  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 C.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, reduce_scatter_flag, split_num):
  40. super().__init__()
  41. self.index = Tensor(np.ones(shape), dtype=ms.int32)
  42. self.offset = offset
  43. self.reduce_scatter_flag = reduce_scatter_flag
  44. self.split_num = split_num
  45. self.elu = inner.EmbeddingLookup()
  46. self.mm = P.BatchMatMul()
  47. def construct(self, x, y):
  48. out = self.elu(x, self.index, self.offset, self.reduce_scatter_flag, self.split_num)
  49. out = self.mm(out, y)
  50. return out
  51. def test_embeddinglookup_reducescatter_false():
  52. shape = [8, 8]
  53. offset = 8
  54. reduce_scatter_flag = False
  55. split_num = 1
  56. net = NetWithLoss(Net(shape, offset, reduce_scatter_flag, split_num))
  57. net.set_auto_parallel()
  58. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  59. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  60. _executor.compile(net, x, y)
  61. def test_embeddinglookup_reducescatter_true():
  62. shape = [64, 8]
  63. offset = 8
  64. reduce_scatter_flag = True
  65. split_num = 8
  66. net = NetWithLoss(Net(shape, offset, reduce_scatter_flag, split_num))
  67. net.set_auto_parallel()
  68. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  69. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  70. _executor.compile(net, x, y)
  71. def test_embeddinglookup_reducescatter_false_grad():
  72. shape = [8, 8]
  73. offset = 8
  74. reduce_scatter_flag = False
  75. split_num = 1
  76. net = GradWrap(NetWithLoss(Net(shape, offset, reduce_scatter_flag, split_num)))
  77. net.set_auto_parallel()
  78. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  79. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  80. _executor.compile(net, x, y)
  81. def test_embeddinglookup_reducescatter_true_grad():
  82. context.set_context(save_graphs=True)
  83. shape = [64, 8]
  84. offset = 8
  85. reduce_scatter_flag = True
  86. split_num = 8
  87. net = GradWrap(NetWithLoss(Net(shape, offset, reduce_scatter_flag, split_num)))
  88. net.set_auto_parallel()
  89. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  90. y = Tensor(np.ones([8, 32, 8]), dtype=ms.float32)
  91. _executor.compile(net, x, y)