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 4.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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.common.dtype as mstype
  18. import mindspore.nn as nn
  19. from mindspore import Tensor, context
  20. from mindspore.common.api import ms_function
  21. from mindspore.ops import operations as P
  22. context.set_context(mode=context.GRAPH_MODE, device_target='CPU')
  23. class Net(nn.Cell):
  24. def __init__(self):
  25. super(Net, self).__init__()
  26. self.mul = P.Mul()
  27. @ms_function
  28. def construct(self, x, y):
  29. return self.mul(x, y)
  30. @pytest.mark.level0
  31. @pytest.mark.platform_x86_cpu
  32. @pytest.mark.env_onecard
  33. def test_mul():
  34. x0 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32))
  35. y0 = Tensor(np.random.uniform(-2, 2, (1, 1, 1, 1)).astype(np.float32))
  36. x1 = Tensor(np.random.uniform(-2, 2, (1, 3, 1, 4)).astype(np.float32))
  37. y1 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32))
  38. x2 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.float32))
  39. y2 = Tensor(2, mstype.float32)
  40. x3 = Tensor(2, mstype.float32)
  41. y3 = Tensor(2, mstype.float32)
  42. x4 = Tensor(np.random.uniform(-2, 2, (4)).astype(np.float32))
  43. y4 = Tensor(np.random.uniform(-2, 2, (4, 4)).astype(np.float32))
  44. mul = Net()
  45. out = mul(x0, y0).asnumpy()
  46. exp = x0.asnumpy() * y0.asnumpy()
  47. diff = np.abs(out - exp)
  48. err = np.ones(shape=exp.shape) * 1.0e-5
  49. assert np.all(diff < err)
  50. assert out.shape == exp.shape
  51. out = mul(x1, y1).asnumpy()
  52. exp = x1.asnumpy() * y1.asnumpy()
  53. diff = np.abs(out - exp)
  54. err = np.ones(shape=exp.shape) * 1.0e-5
  55. assert np.all(diff < err)
  56. assert out.shape == exp.shape
  57. out = mul(x2, y2).asnumpy()
  58. exp = x2.asnumpy() * y2.asnumpy()
  59. diff = np.abs(out - exp)
  60. err = np.ones(shape=exp.shape) * 1.0e-5
  61. assert np.all(diff < err)
  62. assert out.shape == exp.shape
  63. out = mul(x3, y3).asnumpy()
  64. exp = x3.asnumpy() * y3.asnumpy()
  65. diff = np.abs(out - exp)
  66. err = np.ones(shape=exp.shape) * 1.0e-5
  67. assert np.all(diff < err)
  68. assert out.shape == exp.shape
  69. out = mul(x4, y4).asnumpy()
  70. exp = x4.asnumpy() * y4.asnumpy()
  71. diff = np.abs(out - exp)
  72. err = np.ones(shape=exp.shape) * 1.0e-5
  73. assert np.all(diff < err)
  74. assert out.shape == exp.shape
  75. @pytest.mark.level0
  76. @pytest.mark.platform_x86_cpu
  77. @pytest.mark.env_onecard
  78. def test_mul_int32():
  79. x0 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.int32))
  80. y0 = Tensor(np.random.uniform(-2, 2, (1, 1, 1, 1)).astype(np.int32))
  81. x1 = Tensor(np.random.uniform(-2, 2, (1, 3, 1, 4)).astype(np.int32))
  82. y1 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.int32))
  83. x2 = Tensor(np.random.uniform(-2, 2, (2, 3, 4, 4)).astype(np.int32))
  84. y2 = Tensor(2, mstype.int32)
  85. x3 = Tensor(2, mstype.int32)
  86. y3 = Tensor(2, mstype.int32)
  87. x4 = Tensor(np.random.uniform(-2, 2, (4)).astype(np.int32))
  88. y4 = Tensor(np.random.uniform(-2, 2, (4, 4)).astype(np.int32))
  89. mul = Net()
  90. out = mul(x0, y0).asnumpy()
  91. exp = x0.asnumpy() * y0.asnumpy()
  92. diff = np.abs(out - exp)
  93. err = np.ones(shape=exp.shape) * 1.0e-5
  94. assert np.all(diff < err)
  95. assert out.shape == exp.shape
  96. out = mul(x1, y1).asnumpy()
  97. exp = x1.asnumpy() * y1.asnumpy()
  98. diff = np.abs(out - exp)
  99. err = np.ones(shape=exp.shape) * 1.0e-5
  100. assert np.all(diff < err)
  101. assert out.shape == exp.shape
  102. out = mul(x2, y2).asnumpy()
  103. exp = x2.asnumpy() * y2.asnumpy()
  104. diff = np.abs(out - exp)
  105. err = np.ones(shape=exp.shape) * 1.0e-5
  106. assert np.all(diff < err)
  107. assert out.shape == exp.shape
  108. out = mul(x3, y3).asnumpy()
  109. exp = x3.asnumpy() * y3.asnumpy()
  110. diff = np.abs(out - exp)
  111. err = np.ones(shape=exp.shape) * 1.0e-5
  112. assert np.all(diff < err)
  113. assert out.shape == exp.shape
  114. out = mul(x4, y4).asnumpy()
  115. exp = x4.asnumpy() * y4.asnumpy()
  116. diff = np.abs(out - exp)
  117. err = np.ones(shape=exp.shape) * 1.0e-5
  118. assert np.all(diff < err)
  119. assert out.shape == exp.shape