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

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