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_adam_fusion.py 3.9 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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 pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore import Tensor
  20. from mindspore.common.api import ms_function
  21. from mindspore.ops import operations as P
  22. from mindspore.ops import functional as F
  23. from mindspore.common import dtype as mstype
  24. from mindspore.common.parameter import Parameter
  25. context.set_context(mode=context.GRAPH_MODE, device_target="GPU", save_graphs=True)
  26. class Net(nn.Cell):
  27. def __init__(self, decay_flag=True):
  28. super(Net, self).__init__()
  29. self.decay_flag = decay_flag
  30. self.op_mul = P.Mul()
  31. self.op_square = P.Square()
  32. self.op_sqrt = P.Sqrt()
  33. self.op_cast = P.Cast()
  34. self.op_reshape = P.Reshape()
  35. self.op_shape = P.Shape()
  36. self.param = Parameter(Tensor(np.array([0.1, 0.3, 0.5]).astype(np.float32)), name='param')
  37. self.m = Parameter(Tensor(np.array([0.1, 0.3, 0.5]).astype(np.float32)), name='m')
  38. self.v = Parameter(Tensor(np.array([0.1, 0.3, 0.5]).astype(np.float32)), name='v')
  39. @ms_function
  40. def construct(self, beta1, beta2, gradient, eps, weight_decay_tensor, lr):
  41. param_fp32 = self.op_cast(self.param, mstype.float32)
  42. m_fp32 = self.op_cast(self.m, mstype.float32)
  43. v_fp32 = self.op_cast(self.v, mstype.float32)
  44. gradient_fp32 = self.op_cast(gradient, mstype.float32)
  45. next_m = self.op_mul(beta1, m_fp32) + \
  46. self.op_mul(self.op_cast(F.tuple_to_array((1.0,)), mstype.float32) - beta1, gradient_fp32)
  47. next_v = self.op_mul(beta2, v_fp32) + self.op_mul(self.op_cast(F.tuple_to_array((1.0,)), mstype.float32) - \
  48. beta2, self.op_square(gradient_fp32))
  49. update = next_m / (eps + self.op_sqrt(next_v))
  50. if self.decay_flag:
  51. update = self.op_mul(weight_decay_tensor, param_fp32) + update
  52. update_with_lr = self.op_mul(lr, update)
  53. next_param = param_fp32 - self.op_reshape(update_with_lr, self.op_shape(param_fp32))
  54. next_v = F.depend(next_v, F.assign(self.param, next_param))
  55. next_v = F.depend(next_v, F.assign(self.m, next_m))
  56. next_v = F.depend(next_v, F.assign(self.v, next_v))
  57. return next_v
  58. @pytest.mark.level0
  59. @pytest.mark.platform_x86_gpu_training
  60. @pytest.mark.env_onecard
  61. def test_adam_fusion():
  62. beta1 = Tensor(np.array([0.9]).astype(np.float32))
  63. beta2 = Tensor(np.array([0.999]).astype(np.float32))
  64. lr = Tensor(np.array([0.001]).astype(np.float32))
  65. eps = Tensor(np.array([1e-6]).astype(np.float32))
  66. weight_decay_tensor = Tensor(np.array([0.001]).astype(np.float32))
  67. gradient = Tensor(np.array([0.01, 0.03, 0.05]).astype(np.float32))
  68. opt = Net(True)
  69. _ = opt(beta1, beta2, gradient, eps, weight_decay_tensor, lr)
  70. param_expect = np.array([0.09971199, 0.29950103, 0.4993557]).astype(np.float32)
  71. m_expect = np.array([0.091, 0.273, 0.45499998]).astype(np.float32)
  72. v_expect = np.array([0.0999001, 0.29970092, 0.4995025]).astype(np.float32)
  73. assert np.allclose(opt.param.data.asnumpy(), param_expect)
  74. assert np.allclose(opt.m.data.asnumpy(), m_expect)
  75. assert np.allclose(opt.v.data.asnumpy(), v_expect)