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_sparse_feature_bprop.py 4.6 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. """ test sparse feature bprop """
  16. import pytest
  17. import numpy as np
  18. import mindspore as ms
  19. import mindspore.nn as nn
  20. from mindspore import context
  21. from mindspore.common.parameter import Parameter
  22. from mindspore.common.tensor import Tensor
  23. from mindspore.ops import composite as C, operations as P
  24. from mindspore.ops.operations.comm_ops import AllReduce
  25. from mindspore.common.api import _executor
  26. from mindspore.nn import TrainOneStepCell, Adam
  27. grad_all = C.GradOperation(get_all=True)
  28. @pytest.fixture(name="test_context")
  29. def _test_context():
  30. context.set_context(enable_sparse=True)
  31. yield
  32. context.set_context(enable_sparse=False)
  33. context.reset_auto_parallel_context()
  34. class GradWrap(nn.Cell):
  35. def __init__(self, network):
  36. super(GradWrap, self).__init__()
  37. self.network = network
  38. def construct(self, x):
  39. return grad_all(self.network)(x)
  40. def test_bprop_with_sparse_feature_allreduce(test_context):
  41. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="hybrid_parallel")
  42. class Net(nn.Cell):
  43. def __init__(self, axis=0, shape=None):
  44. super(Net, self).__init__()
  45. if shape is None:
  46. shape = [8, 8]
  47. self.all_reduce = AllReduce()
  48. self.gatherv2 = P.SparseGatherV2()
  49. self.index = Tensor(np.ones(shape), dtype=ms.int32)
  50. self.axis = axis
  51. def construct(self, x):
  52. out = self.all_reduce(x)
  53. out = self.gatherv2(out, self.index, self.axis)
  54. return out
  55. net = GradWrap(Net())
  56. x = Tensor(np.ones([64, 64]), dtype=ms.float32)
  57. net.set_train()
  58. _executor.compile(net, x)
  59. def test_bprop_with_sparse_feature_mirror(test_context):
  60. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="semi_auto_parallel")
  61. class Net(nn.Cell):
  62. def __init__(self, shape=None):
  63. super(Net, self).__init__()
  64. if shape is None:
  65. shape = [8, 8]
  66. self.index = Tensor(np.ones(shape), dtype=ms.int32)
  67. self.embeddinglookup = nn.EmbeddingLookup(64, 64, param_init='ones')
  68. self.embeddinglookup.embeddinglookup.shard(((1, 1), (8, 1)))
  69. def construct(self, x, b):
  70. out = self.embeddinglookup(self.index)
  71. return out
  72. _x = Tensor(np.ones([126, 64, 32]), dtype=ms.float32)
  73. _b = Tensor(np.ones([126, 64, 32]), dtype=ms.float32)
  74. def compile_net(net):
  75. optimizer = Adam(net.trainable_params(), learning_rate=0.1, loss_scale=1024.0, weight_decay=0.9)
  76. train_net = TrainOneStepCell(net, optimizer)
  77. train_net.set_train()
  78. _executor.compile(train_net, _x, _b)
  79. net = Net()
  80. compile_net(net)
  81. def test_bprop_with_sparse_feature_dataparallel(test_context):
  82. context.set_auto_parallel_context(device_num=8, global_rank=0, parallel_mode="data_parallel")
  83. class Net(nn.Cell):
  84. def __init__(self, axis=0, shape=None):
  85. super(Net, self).__init__()
  86. if shape is None:
  87. shape = [8, 8]
  88. weight = Tensor(np.ones([64, 64]), dtype=ms.float32)
  89. self.weight = Parameter(weight, "w")
  90. self.index = Tensor(np.ones(shape), dtype=ms.int32)
  91. self.axis = axis
  92. self.gatherv2 = P.SparseGatherV2()
  93. def construct(self, x, b):
  94. out = self.gatherv2(self.weight, self.index, self.axis)
  95. return out
  96. _x = Tensor(np.ones([126, 64, 32]), dtype=ms.float32)
  97. _b = Tensor(np.ones([126, 64, 32]), dtype=ms.float32)
  98. def compile_net(net):
  99. optimizer = Adam(net.trainable_params(), learning_rate=0.1, loss_scale=1024.0, weight_decay=0.9)
  100. train_net = TrainOneStepCell(net, optimizer)
  101. train_net.set_train()
  102. _executor.compile(train_net, _x, _b)
  103. net = Net()
  104. compile_net(net)