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

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. # Copyright 2019 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. from mindspore import Tensor
  17. from mindspore.ops import operations as P
  18. import mindspore.nn as nn
  19. import numpy as np
  20. import mindspore.context as context
  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. @pytest.mark.level0
  28. @pytest.mark.platform_x86_gpu_training
  29. @pytest.mark.env_onecard
  30. def test_Sub():
  31. np_x0 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)
  32. np_y0 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)
  33. np_x1 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)
  34. np_y1 = np.random.uniform(-2, 2, (2, 1, 4, 4)).astype(np.float32)
  35. np_x2 = np.random.uniform(-2, 2, (2, 1, 1, 4)).astype(np.float32)
  36. np_y2 = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)
  37. np_x3 = np.random.uniform(-2, 2, 1).astype(np.float32)
  38. np_y3 = np.random.uniform(-2, 2, 1).astype(np.float32)
  39. np_x4 = np.array(768).astype(np.float32)
  40. np_y4 = np.array(3072.5).astype(np.float32)
  41. x0 = Tensor(np_x0)
  42. y0 = Tensor(np_y0)
  43. x1 = Tensor(np_x1)
  44. y1 = Tensor(np_y1)
  45. x2 = Tensor(np_x2)
  46. y2 = Tensor(np_y2)
  47. x3 = Tensor(np_x3)
  48. y3 = Tensor(np_y3)
  49. x4 = Tensor(np_x4)
  50. y4 = Tensor(np_y4)
  51. expect0 = np.subtract(np_x0, np_y0)
  52. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  53. expect1 = np.subtract(np_x1, np_y1)
  54. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  55. expect2 = np.subtract(np_x2, np_y2)
  56. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  57. expect3 = np.subtract(np_x3, np_y3)
  58. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  59. expect4 = np.subtract(np_x4, np_y4)
  60. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  61. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  62. sub = Net()
  63. output0 = sub(x0, y0)
  64. output1 = sub(x1, y1)
  65. output2 = sub(x2, y2)
  66. output3 = sub(x3, y3)
  67. output4 = sub(x4, y4)
  68. diff0 = output0.asnumpy() - expect0
  69. assert np.all(diff0 < error0)
  70. assert (output0.shape() == expect0.shape)
  71. diff1 = output1.asnumpy() - expect1
  72. assert np.all(diff1 < error1)
  73. assert (output1.shape() == expect1.shape)
  74. diff2 = output2.asnumpy() - expect2
  75. assert np.all(diff2 < error2)
  76. assert (output2.shape() == expect2.shape)
  77. diff3 = output3.asnumpy() - expect3
  78. assert np.all(diff3 < error3)
  79. assert (output3.shape() == expect3.shape)
  80. diff4 = output4.asnumpy() - expect4
  81. assert np.all(diff4 < error4)
  82. assert (output4.shape() == expect4.shape)
  83. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  84. sub = Net()
  85. output0 = sub(x0, y0)
  86. output1 = sub(x1, y1)
  87. output2 = sub(x2, y2)
  88. output3 = sub(x3, y3)
  89. output4 = sub(x4, y4)
  90. diff0 = output0.asnumpy() - expect0
  91. assert np.all(diff0 < error0)
  92. assert (output0.shape() == expect0.shape)
  93. diff1 = output1.asnumpy() - expect1
  94. assert np.all(diff1 < error1)
  95. assert (output1.shape() == expect1.shape)
  96. diff2 = output2.asnumpy() - expect2
  97. assert np.all(diff2 < error2)
  98. assert (output2.shape() == expect2.shape)
  99. diff3 = output3.asnumpy() - expect3
  100. assert np.all(diff3 < error3)
  101. assert (output3.shape() == expect3.shape)
  102. diff4 = output4.asnumpy() - expect4
  103. assert np.all(diff4 < error4)
  104. assert (output4.shape() == expect4.shape)