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 2.6 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
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. mul = Net()
  43. out = mul(x0, y0).asnumpy()
  44. exp = x0.asnumpy() * y0.asnumpy()
  45. diff = np.abs(out - exp)
  46. err = np.ones(shape=exp.shape) * 1.0e-5
  47. assert np.all(diff < err)
  48. assert out.shape == exp.shape
  49. out = mul(x1, y1).asnumpy()
  50. exp = x1.asnumpy() * y1.asnumpy()
  51. diff = np.abs(out - exp)
  52. err = np.ones(shape=exp.shape) * 1.0e-5
  53. assert np.all(diff < err)
  54. assert out.shape == exp.shape
  55. out = mul(x2, y2).asnumpy()
  56. exp = x2.asnumpy() * y2.asnumpy()
  57. diff = np.abs(out - exp)
  58. err = np.ones(shape=exp.shape) * 1.0e-5
  59. assert np.all(diff < err)
  60. assert out.shape == exp.shape
  61. out = mul(x3, y3).asnumpy()
  62. exp = x3.asnumpy() * y3.asnumpy()
  63. diff = np.abs(out - exp)
  64. err = np.ones(shape=exp.shape) * 1.0e-5
  65. assert np.all(diff < err)
  66. assert out.shape == exp.shape