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_multi_field_embedding.py 5.3 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 mindspore.nn import TrainOneStepCell, Adam
  23. from tests.ut.python.ops.test_math_ops import VirtualLoss
  24. grad_all = C.GradOperation(get_all=True)
  25. class GradWrap(nn.Cell):
  26. def __init__(self, network):
  27. super(GradWrap, self).__init__()
  28. self.network = network
  29. def construct(self, x, y, z):
  30. return grad_all(self.network)(x, y, z)
  31. class NetWithLoss(nn.Cell):
  32. def __init__(self, network):
  33. super(NetWithLoss, self).__init__()
  34. self.loss = VirtualLoss()
  35. self.network = network
  36. def construct(self, x, y, z):
  37. predict = self.network(x, y, z)
  38. return self.loss(predict)
  39. class Net(nn.Cell):
  40. def __init__(self, shape, slice_mode=nn.EmbeddingLookup.BATCH_SLICE, target="Device", operator='SUM'):
  41. super().__init__()
  42. self.embedding = nn.MultiFieldEmbeddingLookup(vocab_size=32, embedding_size=64, target=target,
  43. field_size=shape[1], slice_mode=slice_mode, operator=operator)
  44. self.reshape = P.Reshape().shard(((8, 1, 1),))
  45. self.batch_size = shape[0]
  46. def construct(self, x, y, z):
  47. out = self.embedding(x, y, z)
  48. out = self.reshape(out, (self.batch_size, -1))
  49. return out
  50. def compile_net(net, shape):
  51. context.set_context(enable_sparse=True)
  52. x = Tensor(np.ones(shape), dtype=ms.int32)
  53. y = Tensor(np.ones(shape), dtype=ms.float32)
  54. z = Tensor(np.ones(shape), dtype=ms.int32)
  55. optimizer = Adam(net.trainable_params(), learning_rate=0.1)
  56. train_net = TrainOneStepCell(net, optimizer)
  57. train_net.set_auto_parallel()
  58. train_net.set_train()
  59. _executor.compile(train_net, x, y, z)
  60. context.reset_auto_parallel_context()
  61. def test_embeddinglookup_batch_parallel_sum():
  62. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  63. shape = [64, 64]
  64. net = NetWithLoss(Net(shape, target='DEVICE'))
  65. compile_net(net, shape)
  66. def test_embeddinglookup_row_parallel_sum():
  67. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  68. shape = [64, 64]
  69. net = NetWithLoss(Net(shape, slice_mode=nn.EmbeddingLookup.TABLE_ROW_SLICE, target='DEVICE'))
  70. compile_net(net, shape)
  71. def test_embeddinglookup_column_parallel_sum():
  72. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  73. shape = [64, 64]
  74. net = NetWithLoss(Net(shape, slice_mode=nn.EmbeddingLookup.TABLE_COLUMN_SLICE, target='DEVICE'))
  75. compile_net(net, shape)
  76. def test_embeddinglookup_batch_parallel_mean():
  77. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  78. shape = [64, 64]
  79. net = NetWithLoss(Net(shape, target='DEVICE', operator='MEAN'))
  80. compile_net(net, shape)
  81. def test_embeddinglookup_column_parallel_mean():
  82. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  83. shape = [64, 64]
  84. net = NetWithLoss(Net(shape, target='DEVICE', slice_mode=nn.EmbeddingLookup.TABLE_COLUMN_SLICE, operator='MEAN'))
  85. compile_net(net, shape)
  86. def test_embeddinglookup_row_parallel_mean():
  87. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  88. shape = [64, 64]
  89. net = NetWithLoss(Net(shape, target='DEVICE', slice_mode=nn.EmbeddingLookup.TABLE_ROW_SLICE, operator='MEAN'))
  90. compile_net(net, shape)
  91. def test_embeddinglookup_batch_parallel_max():
  92. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  93. shape = [64, 64]
  94. net = NetWithLoss(Net(shape, target='DEVICE', operator='MAX'))
  95. compile_net(net, shape)
  96. def test_embeddinglookup_column_parallel_max():
  97. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  98. shape = [64, 64]
  99. net = NetWithLoss(Net(shape, target='DEVICE', slice_mode=nn.EmbeddingLookup.TABLE_COLUMN_SLICE, operator='MAX'))
  100. compile_net(net, shape)
  101. def test_embeddinglookup_row_parallel_max():
  102. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  103. shape = [64, 64]
  104. net = NetWithLoss(Net(shape, target='DEVICE', slice_mode=nn.EmbeddingLookup.TABLE_ROW_SLICE, operator='MAX'))
  105. compile_net(net, shape)