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_sub_op.py 4.6 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. # Copyright 2019-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 import Tensor
  20. from mindspore.ops import operations as P
  21. class Net(nn.Cell):
  22. def __init__(self):
  23. super(Net, self).__init__()
  24. self.sub = P.Sub()
  25. def construct(self, x, y):
  26. return self.sub(x, y)
  27. def sub(nptype):
  28. np_x0 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype)
  29. np_y0 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype)
  30. np_x1 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype)
  31. np_y1 = np.random.uniform(-2, 2, (2, 1, 4, 4)).astype(nptype)
  32. np_x2 = np.random.uniform(-2, 2, (2, 1, 1, 4)).astype(nptype)
  33. np_y2 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(nptype)
  34. np_x3 = np.random.uniform(-2, 2, 1).astype(nptype)
  35. np_y3 = np.random.uniform(-2, 2, 1).astype(nptype)
  36. np_x4 = np.array(768).astype(nptype)
  37. np_y4 = np.array(3072.5).astype(nptype)
  38. x0 = Tensor(np_x0)
  39. y0 = Tensor(np_y0)
  40. x1 = Tensor(np_x1)
  41. y1 = Tensor(np_y1)
  42. x2 = Tensor(np_x2)
  43. y2 = Tensor(np_y2)
  44. x3 = Tensor(np_x3)
  45. y3 = Tensor(np_y3)
  46. x4 = Tensor(np_x4)
  47. y4 = Tensor(np_y4)
  48. expect0 = np.subtract(np_x0, np_y0)
  49. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  50. expect1 = np.subtract(np_x1, np_y1)
  51. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  52. expect2 = np.subtract(np_x2, np_y2)
  53. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  54. expect3 = np.subtract(np_x3, np_y3)
  55. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  56. expect4 = np.subtract(np_x4, np_y4)
  57. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  58. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  59. sub_net = Net()
  60. output0 = sub_net(x0, y0)
  61. output1 = sub_net(x1, y1)
  62. output2 = sub_net(x2, y2)
  63. output3 = sub_net(x3, y3)
  64. output4 = sub_net(x4, y4)
  65. diff0 = output0.asnumpy() - expect0
  66. assert np.all(diff0 < error0)
  67. assert output0.shape == expect0.shape
  68. diff1 = output1.asnumpy() - expect1
  69. assert np.all(diff1 < error1)
  70. assert output1.shape == expect1.shape
  71. diff2 = output2.asnumpy() - expect2
  72. assert np.all(diff2 < error2)
  73. assert output2.shape == expect2.shape
  74. diff3 = output3.asnumpy() - expect3
  75. assert np.all(diff3 < error3)
  76. assert output3.shape == expect3.shape
  77. diff4 = output4.asnumpy() - expect4
  78. assert np.all(diff4 < error4)
  79. assert output4.shape == expect4.shape
  80. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  81. sub_net = Net()
  82. output0 = sub_net(x0, y0)
  83. output1 = sub_net(x1, y1)
  84. output2 = sub_net(x2, y2)
  85. output3 = sub_net(x3, y3)
  86. output4 = sub_net(x4, y4)
  87. diff0 = output0.asnumpy() - expect0
  88. assert np.all(diff0 < error0)
  89. assert output0.shape == expect0.shape
  90. diff1 = output1.asnumpy() - expect1
  91. assert np.all(diff1 < error1)
  92. assert output1.shape == expect1.shape
  93. diff2 = output2.asnumpy() - expect2
  94. assert np.all(diff2 < error2)
  95. assert output2.shape == expect2.shape
  96. diff3 = output3.asnumpy() - expect3
  97. assert np.all(diff3 < error3)
  98. assert output3.shape == expect3.shape
  99. diff4 = output4.asnumpy() - expect4
  100. assert np.all(diff4 < error4)
  101. assert output4.shape == expect4.shape
  102. @pytest.mark.level0
  103. @pytest.mark.platform_x86_gpu_training
  104. @pytest.mark.env_onecard
  105. def test_sub_float64():
  106. sub(np.float64)
  107. @pytest.mark.level0
  108. @pytest.mark.platform_x86_gpu_training
  109. @pytest.mark.env_onecard
  110. def test_sub_float32():
  111. sub(np.float32)
  112. @pytest.mark.level0
  113. @pytest.mark.platform_x86_gpu_training
  114. @pytest.mark.env_onecard
  115. def test_sub_float16():
  116. sub(np.float16)
  117. @pytest.mark.level0
  118. @pytest.mark.platform_x86_gpu_training
  119. @pytest.mark.env_onecard
  120. def test_sub_int64():
  121. sub(np.int64)
  122. @pytest.mark.level0
  123. @pytest.mark.platform_x86_gpu_training
  124. @pytest.mark.env_onecard
  125. def test_sub_int32():
  126. sub(np.int32)