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_realdiv_op.py 4.7 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  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. from mindspore.common.api import ms_function
  20. import numpy as np
  21. import mindspore.context as context
  22. from mindspore.common.initializer import initializer
  23. from mindspore.common.parameter import Parameter
  24. class NetRealDiv(nn.Cell):
  25. def __init__(self):
  26. super(NetRealDiv, self).__init__()
  27. self.divide = P.RealDiv()
  28. def construct(self, x, y):
  29. return self.divide(x, y)
  30. @pytest.mark.level0
  31. @pytest.mark.platform_x86_gpu_training
  32. @pytest.mark.env_onecard
  33. def test_real_div():
  34. x0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
  35. y0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
  36. x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
  37. y1_np = np.random.randint(1, 5, (2, 1, 4, 4)).astype(np.float32)
  38. x2_np = np.random.randint(1, 5, (2, 1, 1, 4)).astype(np.float32)
  39. y2_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32)
  40. x3_np = np.random.randint(1, 5, 1).astype(np.float32)
  41. y3_np = np.random.randint(1, 5, 1).astype(np.float32)
  42. x4_np = np.array(768).astype(np.float32)
  43. y4_np = np.array(3072.5).astype(np.float32)
  44. x0 = Tensor(x0_np)
  45. y0 = Tensor(y0_np)
  46. x1 = Tensor(x1_np)
  47. y1 = Tensor(y1_np)
  48. x2 = Tensor(x2_np)
  49. y2 = Tensor(y2_np)
  50. x3 = Tensor(x3_np)
  51. y3 = Tensor(y3_np)
  52. x4 = Tensor(x4_np)
  53. y4 = Tensor(y4_np)
  54. context.set_context(mode=context.GRAPH_MODE, device_target='GPU')
  55. real_div = NetRealDiv()
  56. output0 = real_div(x0, y0)
  57. expect0 = np.divide(x0_np, y0_np)
  58. diff0 = output0.asnumpy() - expect0
  59. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  60. assert np.all(diff0 < error0)
  61. assert (output0.shape() == expect0.shape)
  62. output1 = real_div(x1, y1)
  63. expect1 = np.divide(x1_np, y1_np)
  64. diff1 = output1.asnumpy() - expect1
  65. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  66. assert np.all(diff1 < error1)
  67. assert (output1.shape() == expect1.shape)
  68. output2 = real_div(x2, y2)
  69. expect2 = np.divide(x2_np, y2_np)
  70. diff2 = output2.asnumpy() - expect2
  71. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  72. assert np.all(diff2 < error2)
  73. assert (output2.shape() == expect2.shape)
  74. output3 = real_div(x3, y3)
  75. expect3 = np.divide(x3_np, y3_np)
  76. diff3 = output3.asnumpy() - expect3
  77. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  78. assert np.all(diff3 < error3)
  79. assert (output3.shape() == expect3.shape)
  80. output4 = real_div(x4, y4)
  81. expect4 = np.divide(x4_np, y4_np)
  82. diff4 = output4.asnumpy() - expect4
  83. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  84. assert np.all(diff4 < error4)
  85. assert (output4.shape() == expect4.shape)
  86. context.set_context(mode=context.PYNATIVE_MODE, device_target='GPU')
  87. real_div = NetRealDiv()
  88. output0 = real_div(x0, y0)
  89. expect0 = np.divide(x0_np, y0_np)
  90. diff0 = output0.asnumpy() - expect0
  91. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  92. assert np.all(diff0 < error0)
  93. assert (output0.shape() == expect0.shape)
  94. output1 = real_div(x1, y1)
  95. expect1 = np.divide(x1_np, y1_np)
  96. diff1 = output1.asnumpy() - expect1
  97. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  98. assert np.all(diff1 < error1)
  99. assert (output1.shape() == expect1.shape)
  100. output2 = real_div(x2, y2)
  101. expect2 = np.divide(x2_np, y2_np)
  102. diff2 = output2.asnumpy() - expect2
  103. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  104. assert np.all(diff2 < error2)
  105. assert (output2.shape() == expect2.shape)
  106. output3 = real_div(x3, y3)
  107. expect3 = np.divide(x3_np, y3_np)
  108. diff3 = output3.asnumpy() - expect3
  109. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  110. assert np.all(diff3 < error3)
  111. assert (output3.shape() == expect3.shape)
  112. output4 = real_div(x4, y4)
  113. expect4 = np.divide(x4_np, y4_np)
  114. diff4 = output4.asnumpy() - expect4
  115. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  116. assert np.all(diff4 < error4)
  117. assert (output4.shape() == expect4.shape)