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 4.5 kB

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