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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  1. # Copyright 2020 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, field_size=10, slice_mode=nn.EmbeddingLookup.BATCH_SLICE, target="Device",
  41. operator='SUM'):
  42. super().__init__()
  43. self.embedding = nn.MultiFieldEmbeddingLookup(vocab_size=32, embedding_size=64, target=target,
  44. field_size=field_size, slice_mode=slice_mode, operator=operator)
  45. self.reshape = P.Reshape().shard(((8, 1, 1),))
  46. self.batch_size = shape[0]
  47. def construct(self, x, y, z):
  48. out = self.embedding(x, y, z)
  49. out = self.reshape(out, (self.batch_size, -1))
  50. return out
  51. def compile_net(net, shape):
  52. context.set_context(enable_sparse=True)
  53. x = Tensor(np.ones(shape), dtype=ms.int32)
  54. y = Tensor(np.ones(shape), dtype=ms.float32)
  55. z = Tensor(np.ones(shape), dtype=ms.int32)
  56. optimizer = Adam(net.trainable_params(), learning_rate=0.1)
  57. train_net = TrainOneStepCell(net, optimizer)
  58. train_net.set_auto_parallel()
  59. train_net.set_train()
  60. _executor.compile(train_net, x, y, z)
  61. context.reset_auto_parallel_context()
  62. def test_embeddinglookup_batch_parallel_sum():
  63. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  64. shape = [64, 64]
  65. net = NetWithLoss(Net(shape, field_size=10, target='DEVICE'))
  66. compile_net(net, shape)
  67. def test_embeddinglookup_row_parallel_sum():
  68. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  69. shape = [64, 64]
  70. net = NetWithLoss(Net(shape, field_size=9, slice_mode=nn.EmbeddingLookup.TABLE_ROW_SLICE, target='DEVICE'))
  71. compile_net(net, shape)
  72. def test_embeddinglookup_column_parallel_sum():
  73. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  74. shape = [64, 64]
  75. net = NetWithLoss(Net(shape, field_size=10, slice_mode=nn.EmbeddingLookup.TABLE_COLUMN_SLICE, target='DEVICE'))
  76. compile_net(net, shape)
  77. def test_embeddinglookup_batch_parallel_mean():
  78. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  79. shape = [64, 64]
  80. net = NetWithLoss(Net(shape, field_size=1, target='DEVICE', operator='MEAN'))
  81. compile_net(net, shape)
  82. def test_embeddinglookup_column_parallel_mean():
  83. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  84. shape = [64, 64]
  85. net = NetWithLoss(Net(shape, target='DEVICE', slice_mode=nn.EmbeddingLookup.TABLE_COLUMN_SLICE, operator='MEAN'))
  86. compile_net(net, shape)
  87. def test_embeddinglookup_row_parallel_mean():
  88. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  89. shape = [64, 64]
  90. net = NetWithLoss(Net(shape, target='DEVICE', slice_mode=nn.EmbeddingLookup.TABLE_ROW_SLICE, operator='MEAN'))
  91. compile_net(net, shape)
  92. def test_embeddinglookup_batch_parallel_max():
  93. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  94. shape = [64, 64]
  95. net = NetWithLoss(Net(shape, target='DEVICE', operator='MAX'))
  96. compile_net(net, shape)
  97. def test_embeddinglookup_column_parallel_max():
  98. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  99. shape = [64, 64]
  100. net = NetWithLoss(Net(shape, target='DEVICE', slice_mode=nn.EmbeddingLookup.TABLE_COLUMN_SLICE, operator='MAX'))
  101. compile_net(net, shape)
  102. def test_embeddinglookup_row_parallel_max():
  103. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  104. shape = [64, 64]
  105. net = NetWithLoss(Net(shape, target='DEVICE', slice_mode=nn.EmbeddingLookup.TABLE_ROW_SLICE, operator='MAX'))
  106. compile_net(net, shape)