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_batchnorm.py 3.8 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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 numpy as np
  16. import pytest
  17. import mindspore.context as context
  18. import mindspore.nn as nn
  19. from mindspore.common.tensor import Tensor
  20. from mindspore.common.parameter import Parameter
  21. from mindspore.ops import operations as P
  22. class Net(nn.Cell):
  23. def __init__(self, input_scale, input_bias, input_mean, input_variance, is_training):
  24. super(Net, self).__init__()
  25. self.fused_bn_ex = P.BatchNorm(is_training=is_training, epsilon=1e-5, momentum=0.9)
  26. self.scale = Parameter(input_scale, name='scale')
  27. self.bias = Parameter(input_bias, name='b')
  28. self.mean = Parameter(input_mean, name='mean')
  29. self.variance = Parameter(input_variance, name='variance')
  30. def construct(self, input_x):
  31. return self.fused_bn_ex(input_x, self.scale, self.bias, self.mean, self.variance)
  32. def get_output(x, weight, bias, moving_mean, moving_var, is_training, enable_graph_kernel=False):
  33. context.set_context(enable_graph_kernel=enable_graph_kernel)
  34. net = Net(Tensor(weight), Tensor(bias), Tensor(moving_mean), Tensor(moving_var), is_training)
  35. output = net(Tensor(x))
  36. return output, net.mean, net.variance
  37. def test_bn_train():
  38. x = np.random.normal(0, 1, [1, 2, 4, 4]).astype(np.float32)
  39. weight = np.random.normal(0, 1, [2,]).astype(np.float32)
  40. bias = np.random.normal(0, 1, [2,]).astype(np.float32)
  41. moving_mean = np.random.normal(0, 1, [2,]).astype(np.float32)
  42. moving_var = np.random.normal(0, 1, [2,]).astype(np.float32)
  43. train_expect = get_output(x, weight, bias, moving_mean, moving_var, True, False)
  44. train_output = get_output(x, weight, bias, moving_mean, moving_var, True, True)
  45. assert np.allclose(train_expect[0][0].asnumpy(), train_output[0][0].asnumpy(), 0.0001, 0.0001)
  46. assert np.allclose(train_expect[0][3].asnumpy(), train_output[0][3].asnumpy(), 0.0001, 0.0001)
  47. assert np.allclose(train_expect[0][4].asnumpy(), train_output[0][4].asnumpy(), 0.0001, 0.0001)
  48. assert np.allclose(train_expect[1].data.asnumpy(), train_output[1].data.asnumpy(), 0.0001, 0.0001)
  49. assert np.allclose(train_expect[2].data.asnumpy(), train_output[2].data.asnumpy(), 0.0001, 0.0001)
  50. def test_bn_infer():
  51. x = np.random.normal(5, 1, [1, 2, 4, 4]).astype(np.float32)
  52. weight = np.random.normal(5, 1, [2,]).astype(np.float32)
  53. bias = np.random.normal(5, 1, [2,]).astype(np.float32)
  54. moving_mean = np.random.normal(5, 1, [2,]).astype(np.float32)
  55. moving_var = np.random.normal(5, 1, [2,]).astype(np.float32)
  56. infer_expect = get_output(x, weight, bias, moving_mean, moving_var, False, False)
  57. infer_output = get_output(x, weight, bias, moving_mean, moving_var, False, True)
  58. assert np.allclose(infer_expect[0][0].asnumpy(), infer_output[0][0].asnumpy(), 0.0001, 0.0001)
  59. @pytest.mark.level0
  60. @pytest.mark.platform_x86_gpu_training
  61. @pytest.mark.env_onecard
  62. def test_bn_train_gpu():
  63. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  64. test_bn_train()
  65. @pytest.mark.level0
  66. @pytest.mark.platform_x86_gpu_training
  67. @pytest.mark.env_onecard
  68. def test_bn_infer_gpu():
  69. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  70. test_bn_infer()