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_lstm_op.py 7.7 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179
  1. # Copyright 2021 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 math
  16. import pytest
  17. import numpy as np
  18. from mindspore import context
  19. from mindspore import nn
  20. from mindspore import Tensor
  21. from mindspore.common.initializer import initializer
  22. from mindspore.common.parameter import ParameterTuple
  23. from mindspore.common.parameter import Parameter
  24. from mindspore.ops import composite as c
  25. class GradOfAllInputsAndParams(nn.Cell):
  26. def __init__(self, network, sens_param):
  27. super().__init__()
  28. self.grad = c.GradOperation(get_all=True, get_by_list=True, sens_param=sens_param)
  29. self.network = network
  30. self.params = ParameterTuple(self.network.trainable_params())
  31. def construct(self, *inputs):
  32. gout = self.grad(self.network, self.params)(*inputs)
  33. return gout
  34. class LSTM(nn.Cell):
  35. def __init__(self, input_s, hidden_s, num_layers, has_bias, batch_first, bidirectional, dropout):
  36. super().__init__()
  37. self.lstm = nn.LSTM(input_size=input_s, hidden_size=hidden_s, num_layers=num_layers, has_bias=has_bias,
  38. batch_first=batch_first, bidirectional=bidirectional, dropout=dropout)
  39. def construct(self, inp, h0, c0):
  40. return self.lstm(inp, (h0, c0))
  41. class LSTMWeightBias():
  42. def __init__(self, num_layers, has_bias, input_s, num_directions, hidden_s, bidirectional):
  43. self.num_layers = num_layers
  44. self.has_bias = has_bias
  45. self.input_s = input_s
  46. self.num_directions = num_directions
  47. self.hidden_s = hidden_s
  48. self.bidirectional = bidirectional
  49. def get_weight_bias(self):
  50. stdv = 1 / math.sqrt(self.hidden_s)
  51. gate_size = 4 * self.hidden_s
  52. w_list_value = []
  53. b_list_value = []
  54. for i in range(self.num_layers):
  55. b0 = np.zeros(gate_size, dtype=np.float16)
  56. w_shape = self.input_s if i == 0 else (self.num_directions * self.hidden_s)
  57. w_np = np.random.uniform(-stdv, stdv, (w_shape + self.hidden_s, gate_size)).astype(np.float16)
  58. w_list_value.append(Parameter(initializer(Tensor(w_np), [w_shape + self.hidden_s, gate_size]),
  59. name="weight_fw" + str(i)))
  60. if self.has_bias:
  61. b_np = np.random.uniform(-stdv, stdv, gate_size).astype(np.float16)
  62. b_list_value.append(Parameter(initializer(Tensor(b_np), [gate_size]), name="bias_fw" + str(i)))
  63. else:
  64. b_list_value.append(Parameter(initializer(Tensor(b0), [gate_size]), name="bias_fw" + str(i)))
  65. if self.bidirectional:
  66. w_bw_np = np.random.uniform(-stdv, stdv, (w_shape + self.hidden_s, gate_size)).astype(np.float16)
  67. b_list_value.append(Parameter(initializer(Tensor(w_bw_np), [w_shape + self.hidden_s, gate_size]),
  68. name="weight_bw" + str(i)))
  69. b_bw_np = np.random.uniform(-stdv, stdv, (4 * self.hidden_s)).astype(
  70. np.float16) if self.has_bias else b0
  71. b_list_value.append(Parameter(initializer(Tensor(b_bw_np), [gate_size]), name="bias_bw" + str(i)))
  72. w_list_value = ParameterTuple(w_list_value)
  73. b_list_value = ParameterTuple(b_list_value)
  74. return w_list_value, b_list_value
  75. @pytest.mark.level0
  76. @pytest.mark.platform_arm_ascend_training
  77. @pytest.mark.platform_x86_ascend_training
  78. @pytest.mark.env_onecard
  79. def test_sit_lstm_forward_input_3_32_32_is_32_hs_16():
  80. input_s = 32
  81. hidden_s = 16
  82. has_bias = True
  83. bidirectional = False
  84. num_layers = 1
  85. num_directions = 1
  86. fact = LSTMWeightBias(num_layers, has_bias, input_s, num_directions, hidden_s, bidirectional)
  87. w_list_value, b_list_value = fact.get_weight_bias()
  88. h0 = Tensor(np.random.randn(num_layers * 1, 32, 16).astype(np.float32))
  89. c0 = Tensor(np.random.randn(num_layers * 1, 32, 16).astype(np.float32))
  90. input_ms = Tensor(np.random.randn(3, 32, 32).astype(np.float32))
  91. # graph mode
  92. context.set_context(mode=context.GRAPH_MODE)
  93. net = LSTM(input_s=input_s, hidden_s=16, num_layers=num_layers, has_bias=has_bias, batch_first=False,
  94. bidirectional=bidirectional, dropout=0.0)
  95. net.lstm.w_list = w_list_value
  96. net.lstm.b_list = b_list_value
  97. out, (hy, cy) = net(input_ms, h0, c0)
  98. # pynative mode
  99. context.set_context(mode=context.PYNATIVE_MODE)
  100. net_pynative = LSTM(input_s=input_s, hidden_s=16, num_layers=num_layers, has_bias=has_bias, batch_first=False,
  101. bidirectional=bidirectional, dropout=0.0)
  102. net_pynative.lstm.w_list = w_list_value
  103. net_pynative.lstm.b_list = b_list_value
  104. out_pynative, (hy_pynative, cy_pynative) = net_pynative(input_ms, h0, c0)
  105. assert np.allclose(out.asnumpy(), out_pynative.asnumpy(), 0.0001, 0.0001)
  106. assert np.allclose(hy.asnumpy(), hy_pynative.asnumpy(), 0.0001, 0.0001)
  107. assert np.allclose(cy.asnumpy(), cy_pynative.asnumpy(), 0.0001, 0.0001)
  108. @pytest.mark.level0
  109. @pytest.mark.platform_arm_ascend_training
  110. @pytest.mark.platform_x86_ascend_training
  111. @pytest.mark.env_onecard
  112. def test_sit_lstm_grad_input_3_32_32_is_32_hs_16():
  113. input_s = 32
  114. hidden_s = 16
  115. has_bias = True
  116. bidirectional = False
  117. num_layers = 1
  118. num_directions = 1
  119. fact = LSTMWeightBias(num_layers, has_bias, input_s, num_directions, hidden_s, bidirectional)
  120. w_list_value, b_list_value = fact.get_weight_bias()
  121. h0 = Tensor(np.random.randn(num_layers * 1, 32, 16).astype(np.float32))
  122. c0 = Tensor(np.random.randn(num_layers * 1, 32, 16).astype(np.float32))
  123. input_ms = Tensor(np.random.randn(3, 32, 32).astype(np.float32))
  124. # graph mode
  125. context.set_context(mode=context.GRAPH_MODE)
  126. net = LSTM(input_s=input_s, hidden_s=16, num_layers=num_layers, has_bias=has_bias, batch_first=False,
  127. bidirectional=bidirectional, dropout=0.0)
  128. net.lstm.w_list = w_list_value
  129. net.lstm.b_list = b_list_value
  130. grad_net_inp = GradOfAllInputsAndParams(net, sens_param=False)
  131. grad_net_inp.set_train()
  132. out_grad, _ = grad_net_inp(input_ms, h0, c0)
  133. x_grad = out_grad[0].asnumpy()
  134. h_grad = out_grad[1].asnumpy()
  135. c_grad = out_grad[2].asnumpy()
  136. # pynative mode
  137. context.set_context(mode=context.PYNATIVE_MODE)
  138. net_pynative = LSTM(input_s=input_s, hidden_s=16, num_layers=num_layers, has_bias=has_bias, batch_first=False,
  139. bidirectional=bidirectional, dropout=0.0)
  140. net_pynative.lstm.w_list = w_list_value
  141. net_pynative.lstm.b_list = b_list_value
  142. grad_net_inp_pynative = GradOfAllInputsAndParams(net_pynative, sens_param=False)
  143. grad_net_inp_pynative.set_train()
  144. out_grad_pynative, _ = grad_net_inp_pynative(input_ms, h0, c0)
  145. x_grad_pynative = out_grad_pynative[0].asnumpy()
  146. h_grad_pynative = out_grad_pynative[1].asnumpy()
  147. c_grad_pynative = out_grad_pynative[2].asnumpy()
  148. assert np.allclose(x_grad, x_grad_pynative, 0.0001, 0.0001)
  149. assert np.allclose(h_grad, h_grad_pynative, 0.0001, 0.0001)
  150. assert np.allclose(c_grad, c_grad_pynative, 0.0001, 0.0001)