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_div_op.py 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. # Copyright 2020 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 NetDiv(nn.Cell):
  22. def __init__(self):
  23. super(NetDiv, self).__init__()
  24. self.div = P.Div()
  25. def construct(self, x, y):
  26. return self.div(x, y)
  27. @pytest.mark.level0
  28. @pytest.mark.platform_x86_gpu_training
  29. @pytest.mark.env_onecard
  30. def test_div():
  31. x0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
  32. y0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
  33. x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
  34. y1_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.float32)
  35. x2_np = np.random.randint(1, 5, (2, 1, 1, 4)).astype(np.float32)
  36. y2_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
  37. x3_np = np.random.randint(1, 5, 1).astype(np.float32)
  38. y3_np = np.random.randint(1, 5, 1).astype(np.float32)
  39. x4_np = np.array(768).astype(np.float32)
  40. y4_np = np.array(3072.5).astype(np.float32)
  41. x5_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float16)
  42. y5_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float16)
  43. x6_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.int32)
  44. y6_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.int32)
  45. x0 = Tensor(x0_np)
  46. y0 = Tensor(y0_np)
  47. x1 = Tensor(x1_np)
  48. y1 = Tensor(y1_np)
  49. x2 = Tensor(x2_np)
  50. y2 = Tensor(y2_np)
  51. x3 = Tensor(x3_np)
  52. y3 = Tensor(y3_np)
  53. x4 = Tensor(x4_np)
  54. y4 = Tensor(y4_np)
  55. x5 = Tensor(x5_np)
  56. y5 = Tensor(y5_np)
  57. x6 = Tensor(x6_np)
  58. y6 = Tensor(y6_np)
  59. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  60. div = NetDiv()
  61. output0 = div(x0, y0)
  62. expect0 = np.divide(x0_np, y0_np)
  63. diff0 = output0.asnumpy() - expect0
  64. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  65. assert np.all(diff0 < error0)
  66. assert output0.shape == expect0.shape
  67. output1 = div(x1, y1)
  68. expect1 = np.divide(x1_np, y1_np)
  69. diff1 = output1.asnumpy() - expect1
  70. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  71. assert np.all(diff1 < error1)
  72. assert output1.shape == expect1.shape
  73. output2 = div(x2, y2)
  74. expect2 = np.divide(x2_np, y2_np)
  75. diff2 = output2.asnumpy() - expect2
  76. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  77. assert np.all(diff2 < error2)
  78. assert output2.shape == expect2.shape
  79. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  80. output3 = div(x3, y3)
  81. expect3 = np.divide(x3_np, y3_np)
  82. diff3 = output3.asnumpy() - expect3
  83. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  84. assert np.all(diff3 < error3)
  85. assert output3.shape == expect3.shape
  86. output4 = div(x4, y4)
  87. expect4 = np.divide(x4_np, y4_np)
  88. diff4 = output4.asnumpy() - expect4
  89. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  90. assert np.all(diff4 < error4)
  91. assert output4.shape == expect4.shape
  92. output5 = div(x5, y5)
  93. expect5 = np.divide(x5_np, y5_np)
  94. diff5 = output5.asnumpy() - expect5
  95. error5 = np.ones(shape=expect5.shape) * 1.0e-5
  96. assert np.all(diff5 < error5)
  97. assert output5.shape == expect5.shape
  98. output6 = div(x6, y6)
  99. expect6 = np.divide(x6_np, y6_np)
  100. diff6 = output6.asnumpy() - expect6
  101. error6 = np.ones(shape=expect6.shape) * 1.0e-5
  102. assert np.all(diff6 < error6)
  103. assert output6.shape == expect6.shape