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_mul_op.py 5.9 kB

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175
  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 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. from mindspore.ops.operations import _inner_ops as inner
  22. class NetMul(nn.Cell):
  23. def __init__(self):
  24. super(NetMul, self).__init__()
  25. self.mul = P.Mul()
  26. def construct(self, x, y):
  27. return self.mul(x, y)
  28. @pytest.mark.level0
  29. @pytest.mark.platform_x86_gpu_training
  30. @pytest.mark.env_onecard
  31. def test_mul():
  32. x0_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)
  33. y0_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)
  34. x1_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)
  35. y1_np = np.random.uniform(-2, 2, (2, 1, 4, 4)).astype(np.float32)
  36. x2_np = np.random.uniform(-2, 2, (2, 1, 1, 4)).astype(np.float32)
  37. y2_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)
  38. x3_np = np.random.uniform(-2, 2, 1).astype(np.float32)
  39. y3_np = np.random.uniform(-2, 2, 1).astype(np.float32)
  40. x4_np = np.array(768).astype(np.float32)
  41. y4_np = np.array(3072.5).astype(np.float32)
  42. x0 = Tensor(x0_np)
  43. y0 = Tensor(y0_np)
  44. x1 = Tensor(x1_np)
  45. y1 = Tensor(y1_np)
  46. x2 = Tensor(x2_np)
  47. y2 = Tensor(y2_np)
  48. x3 = Tensor(x3_np)
  49. y3 = Tensor(y3_np)
  50. x4 = Tensor(x4_np)
  51. y4 = Tensor(y4_np)
  52. context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
  53. mul = NetMul()
  54. output0 = mul(x0, y0)
  55. expect0 = np.multiply(x0_np, y0_np)
  56. diff0 = output0.asnumpy() - expect0
  57. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  58. assert np.all(diff0 < error0)
  59. assert output0.shape == expect0.shape
  60. output1 = mul(x1, y1)
  61. expect1 = np.multiply(x1_np, y1_np)
  62. diff1 = output1.asnumpy() - expect1
  63. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  64. assert np.all(diff1 < error1)
  65. assert output1.shape == expect1.shape
  66. output2 = mul(x2, y2)
  67. expect2 = np.multiply(x2_np, y2_np)
  68. diff2 = output2.asnumpy() - expect2
  69. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  70. assert np.all(diff2 < error2)
  71. assert output2.shape == expect2.shape
  72. output3 = mul(x3, y3)
  73. expect3 = np.multiply(x3_np, y3_np)
  74. diff3 = output3.asnumpy() - expect3
  75. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  76. assert np.all(diff3 < error3)
  77. assert output3.shape == expect3.shape
  78. output4 = mul(x4, y4)
  79. expect4 = np.multiply(x4_np, y4_np)
  80. diff4 = output4.asnumpy() - expect4
  81. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  82. assert np.all(diff4 < error4)
  83. assert output4.shape == expect4.shape
  84. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  85. mul = NetMul()
  86. output0 = mul(x0, y0)
  87. expect0 = np.multiply(x0_np, y0_np)
  88. diff0 = output0.asnumpy() - expect0
  89. error0 = np.ones(shape=expect0.shape) * 1.0e-5
  90. assert np.all(diff0 < error0)
  91. assert output0.shape == expect0.shape
  92. output1 = mul(x1, y1)
  93. expect1 = np.multiply(x1_np, y1_np)
  94. diff1 = output1.asnumpy() - expect1
  95. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  96. assert np.all(diff1 < error1)
  97. assert output1.shape == expect1.shape
  98. output2 = mul(x2, y2)
  99. expect2 = np.multiply(x2_np, y2_np)
  100. diff2 = output2.asnumpy() - expect2
  101. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  102. assert np.all(diff2 < error2)
  103. assert output2.shape == expect2.shape
  104. output3 = mul(x3, y3)
  105. expect3 = np.multiply(x3_np, y3_np)
  106. diff3 = output3.asnumpy() - expect3
  107. error3 = np.ones(shape=expect3.shape) * 1.0e-5
  108. assert np.all(diff3 < error3)
  109. assert output3.shape == expect3.shape
  110. output4 = mul(x4, y4)
  111. expect4 = np.multiply(x4_np, y4_np)
  112. diff4 = output4.asnumpy() - expect4
  113. error4 = np.ones(shape=expect4.shape) * 1.0e-5
  114. assert np.all(diff4 < error4)
  115. assert output4.shape == expect4.shape
  116. class NetMul_dynamic(nn.Cell):
  117. def __init__(self):
  118. super(NetMul_dynamic, self).__init__()
  119. self.mul = P.Mul()
  120. self.test_dynamic = inner.GpuConvertToDynamicShape()
  121. def construct(self, x, y):
  122. x = self.test_dynamic(x)
  123. y = self.test_dynamic(y)
  124. out = self.mul(x, y)
  125. return out
  126. @pytest.mark.level0
  127. @pytest.mark.platform_x86_gpu_training
  128. @pytest.mark.env_onecard
  129. def test_mul_dynamic():
  130. x1_np = np.array([768]).astype(np.float32)
  131. y1_np = np.array([3072.5]).astype(np.float32)
  132. x2_np = np.random.uniform(-2, 2, (2, 1, 1, 4)).astype(np.float32)
  133. y2_np = np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32)
  134. x1 = Tensor(x1_np)
  135. y1 = Tensor(y1_np)
  136. x2 = Tensor(x2_np)
  137. y2 = Tensor(y2_np)
  138. context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
  139. mul = NetMul_dynamic()
  140. output1 = mul(x1, y1)
  141. output2 = mul(x2, y2)
  142. expect1 = np.multiply(x1_np, y1_np)
  143. expect2 = np.multiply(x2_np, y2_np)
  144. diff1 = output1.asnumpy() - expect1
  145. diff2 = output2.asnumpy() - expect2
  146. error1 = np.ones(shape=expect1.shape) * 1.0e-5
  147. assert np.all(diff1 < error1)
  148. assert output1.shape == expect1.shape
  149. error2 = np.ones(shape=expect2.shape) * 1.0e-5
  150. assert np.all(diff2 < error2)
  151. assert output2.shape == expect2.shape