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

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