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_sum_as_loss.py 3.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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. import numpy as np
  15. from mindspore import context
  16. import mindspore.nn as nn
  17. from mindspore.ops import operations as P
  18. from mindspore import Tensor
  19. from tests.ut.python.ops.test_math_ops import VirtualLoss
  20. import mindspore as ms
  21. from mindspore.common.api import _executor
  22. from mindspore.ops import composite as C
  23. class GradWrap(nn.Cell):
  24. def __init__(self, network):
  25. super(GradWrap, self).__init__()
  26. self.network = network
  27. def construct(self, x, y, bias):
  28. return C.grad_all(self.network)(x, y, bias)
  29. def compile(net, x, y, bias):
  30. net.set_auto_parallel()
  31. _executor.compile(net, x, y, bias)
  32. def test_sum_as_loss():
  33. class Net(nn.Cell):
  34. def __init__(self, strategy0, strategy1):
  35. super().__init__()
  36. self.fc_nobias = P.MatMul(transpose_b=True).set_strategy(strategy0)
  37. self.reduce_sum = P.ReduceSum(keep_dims=False).set_strategy(strategy1)
  38. def construct(self, x, y, bias):
  39. out = self.fc_nobias(x, y)
  40. out = self.reduce_sum(out, (0, 1))
  41. return out
  42. context.set_auto_parallel_context(device_num=16, global_rank=0)
  43. strategy0 = ((4, 1), (4, 1))
  44. strategy1 = ((4, 1), )
  45. net = GradWrap(Net(strategy0, strategy1))
  46. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  47. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  48. y = Tensor(np.ones([64, 32]), dtype=ms.float32)
  49. bias = Tensor(np.ones([64]), dtype=ms.float32)
  50. compile(net, x, y, bias)
  51. def test_sum_as_loss2():
  52. class Net(nn.Cell):
  53. def __init__(self, strategy0, strategy1):
  54. super().__init__()
  55. self.fc_nobias = P.MatMul(transpose_b=True).set_strategy(strategy0)
  56. self.reduce_sum = P.ReduceSum(keep_dims=False).set_strategy(strategy1)
  57. def construct(self, x, y, bias):
  58. out = self.fc_nobias(x, y)
  59. out = self.reduce_sum(out, (0, 1))
  60. return out
  61. context.set_auto_parallel_context(device_num=16, global_rank=0)
  62. strategy0 = ((4, 1), (4, 1))
  63. strategy1 = ((1, 1), )
  64. net = GradWrap(Net(strategy0, strategy1))
  65. context.set_auto_parallel_context(parallel_mode="semi_auto_parallel")
  66. x = Tensor(np.ones([64, 32]), dtype=ms.float32)
  67. y = Tensor(np.ones([64, 32]), dtype=ms.float32)
  68. bias = Tensor(np.ones([64]), dtype=ms.float32)
  69. compile(net, x, y, bias)