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_lamb.py 5.5 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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.context as context
  18. from mindspore import Tensor, Parameter
  19. from mindspore.nn import Cell
  20. from mindspore.nn.graph_kernels import LambUpdateWithLR, LambNextMV
  21. class LambNet(Cell):
  22. def __init__(self, i2, i5, x6):
  23. super(LambNet, self).__init__()
  24. self.i2 = Parameter(i2, name='i2')
  25. self.i5 = Parameter(i5, name='i5')
  26. self.x6 = Parameter(x6, name='x6')
  27. self.lamb_next = LambNextMV()
  28. self.lamb_update = LambUpdateWithLR()
  29. def construct(self, i1, i3, i4, i6, i7, i8, i9, ix0, ix1, ix2, ix3,
  30. x1, x2, x3, x4, x5, gy, se, my):
  31. i1_ = i1 + i3
  32. return self.lamb_next(i1_, self.i2, i3, i4, self.i5, i6, i7, i8, i9, ix0,
  33. ix1, ix2, ix3), \
  34. self.lamb_update(x1, x2, x3, x4, x5, self.x6, gy, se, my)
  35. def LambUpdateNumpy(x1, x2, x3, x4, x5, x6, gy, se, my):
  36. trust_ratio = np.where(np.greater(x2, gy),
  37. np.where(np.greater(x1, gy), np.divide(x2, x3), se),
  38. se)
  39. trust_ratio = np.maximum(np.minimum(trust_ratio, my), gy)
  40. update_with_lr = trust_ratio * x4 * x5
  41. next_param = x6 - np.reshape(update_with_lr, x6.shape)
  42. return next_param
  43. def LambNextMVNumpy(i1, i2, i3, i4, i5, i6, i7, i8, i9, x0, x1, x2, x3):
  44. m_fp32 = i5.astype(np.float32)
  45. v_fp32 = i2.astype(np.float32)
  46. next_m = i8 * m_fp32 + i9 * i4
  47. next_v = x0 * v_fp32 + x1 * i1
  48. next_mm = next_m / i6
  49. next_vv = next_v / i3
  50. update = next_mm / (np.sqrt(next_vv) + x3)
  51. add3 = next_mm / np.sqrt(next_vv + x3) + x2 * i7
  52. return add3, next_m, next_v, update
  53. def tensor_all(*args):
  54. res = [Tensor(a) for a in args]
  55. return res
  56. def test_graph_kernel_lamb():
  57. shape = [1, 16]
  58. oshape = [1]
  59. np.random.seed(0)
  60. x1 = np.random.normal(0, 1, oshape).astype(np.float32)
  61. x2 = np.random.normal(0, 1, oshape).astype(np.float32)
  62. x3 = np.random.normal(0, 1, oshape).astype(np.float32)
  63. x4 = np.random.normal(0, 1, oshape).astype(np.float32)
  64. x5 = np.random.normal(0, 1, shape).astype(np.float32)
  65. x6 = np.random.normal(0, 1, shape).astype(np.float32)
  66. gy = np.random.normal(0, 1, oshape).astype(np.float32)
  67. se = np.random.normal(0, 1, oshape).astype(np.float32)
  68. my = np.random.normal(0, 1, oshape).astype(np.float32)
  69. tx1, tx2, tx3, tx4, tx5, tx6, tgy, tse, tmy = tensor_all(
  70. x1, x2, x3, x4, x5, x6, gy, se, my)
  71. np.random.seed(1)
  72. i1 = np.abs(np.random.normal(0, 1, shape)).astype(np.float32)
  73. i2 = np.abs(np.random.normal(0, 1, shape)).astype(np.float32)
  74. i3 = np.abs(np.random.normal(0, 1, shape)).astype(np.float32)
  75. i4 = np.random.normal(0, 1, shape).astype(np.float32)
  76. i5 = np.random.normal(0, 1, shape).astype(np.float32)
  77. i6 = np.abs(np.random.normal(0, 1, shape)).astype(np.float32)
  78. i7 = np.random.normal(0, 1, shape).astype(np.float32)
  79. i8 = np.random.normal(0, 1, shape).astype(np.float32)
  80. i9 = np.random.normal(0, 1, shape).astype(np.float32)
  81. ix0 = np.abs(np.random.normal(0, 1, shape)).astype(np.float32)
  82. ix1 = np.abs(np.random.normal(0, 1, shape)).astype(np.float32)
  83. ix2 = np.random.normal(0, 1, shape).astype(np.float32)
  84. ix3 = np.ones(shape).astype(np.float32) * 1e-6
  85. ti1, ti2, ti3, ti4, ti5, ti6, ti7, ti8, ti9, tix0, tix1, tix2, tix3 = \
  86. tensor_all(i1, i2, i3, i4, i5, i6, i7, i8, i9, ix0, ix1, ix2, ix3)
  87. context.set_context(enable_graph_kernel=True)
  88. net = LambNet(ti2, ti5, tx6)
  89. (wa3, wup), _ = net(ti1, ti3, ti4, ti6, ti7, ti8, ti9, tix0, tix1, tix2, tix3,
  90. tx1, tx2, tx3, tx4, tx5, tgy, tse, tmy)
  91. wi2 = net.i2.data.asnumpy().copy()
  92. wi5 = net.i5.data.asnumpy().copy()
  93. ares = net.x6.data.asnumpy().copy()
  94. context.set_context(enable_graph_kernel=False)
  95. i1_ = i1 + i3
  96. a3, a0, a1, up = LambNextMVNumpy(i1_, i2, i3, i4, i5, i6, i7, i8, i9, ix0,
  97. ix1, ix2, ix3)
  98. np_res = LambUpdateNumpy(x1, x2, x3, x4, x5, x6, gy, se, my)
  99. rtol = 0.0001
  100. atol = 0.0001
  101. wres = (wa3.asnumpy().copy(), wi5, wi2, wup.asnumpy().copy())
  102. bres = (a3, a0, a1, up)
  103. cmp_res = list(map(lambda x, y: np.allclose(x, y, rtol, atol),
  104. wres, bres))
  105. assert all(cmp_res) and np.allclose(ares, np_res, rtol, atol)
  106. def test_graph_kernel_lamb_gpu():
  107. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="GPU")
  108. test_graph_kernel_lamb()
  109. @pytest.mark.level0
  110. @pytest.mark.platform_arm_ascend_training
  111. @pytest.mark.platform_x86_ascend_training
  112. @pytest.mark.env_onecard
  113. def test_graph_kernel_lamb_ascend():
  114. context.set_context(mode=context.GRAPH_MODE, enable_graph_kernel=True, device_target="Ascend")
  115. test_graph_kernel_lamb()