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_matmul.py 3.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright 2021 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. from mindspore import Tensor
  19. from mindspore.nn import Cell
  20. import mindspore.ops.operations as P
  21. class Net(Cell):
  22. def __init__(self):
  23. super(Net, self).__init__()
  24. self.matmul = P.MatMul(transpose_a=True, transpose_b=True)
  25. def construct(self, x, y):
  26. return self.matmul(x, y)
  27. class Net1(Cell):
  28. def __init__(self):
  29. super(Net1, self).__init__()
  30. self.matmul = P.MatMul(transpose_a=True, transpose_b=True)
  31. self.add = P.BiasAdd()
  32. def construct(self, x, y, bias):
  33. res = self.matmul(x, y)
  34. return self.add(res, bias)
  35. def get_output(i0, i1, enable_graph_kernel=False):
  36. context.set_context(enable_graph_kernel=enable_graph_kernel)
  37. net = Net()
  38. output = net(i0, i1)
  39. return output
  40. def get_output1(i0, i1, i2, enable_graph_kernel=False):
  41. context.set_context(enable_graph_kernel=enable_graph_kernel)
  42. net = Net1()
  43. output = net(i0, i1, i2)
  44. return output
  45. def test_basic():
  46. i0 = Tensor(np.random.normal(1, 0.01, [800, 96]).astype(np.float16))
  47. i1 = Tensor(np.random.normal(1, 0.01, [128, 800]).astype(np.float16))
  48. expect = get_output(i0, i1, False)
  49. output = get_output(i0, i1, True)
  50. expect_np = expect.asnumpy().copy()
  51. output_np = output.asnumpy().copy()
  52. assert np.allclose(expect_np, output_np, 1.e-4, 1.e-7)
  53. def test_basic1():
  54. i0 = Tensor(np.random.normal(1, 0.01, [800, 96]).astype(np.float16))
  55. i1 = Tensor(np.random.normal(1, 0.01, [128, 800]).astype(np.float16))
  56. i2 = Tensor(np.random.normal(100, 0.01, [128,]).astype(np.float16))
  57. expect = get_output1(i0, i1, i2, False)
  58. output = get_output1(i0, i1, i2, True)
  59. expect_np = expect.asnumpy().copy()
  60. output_np = output.asnumpy().copy()
  61. assert np.allclose(expect_np, output_np, 6.e-4, 6.e-4)
  62. @pytest.mark.level0
  63. @pytest.mark.platform_arm_ascend_training
  64. @pytest.mark.platform_x86_ascend_training
  65. @pytest.mark.env_onecard
  66. def test_basic_ascend():
  67. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  68. test_basic()
  69. @pytest.mark.level0
  70. @pytest.mark.platform_arm_ascend_training
  71. @pytest.mark.platform_x86_ascend_training
  72. @pytest.mark.env_onecard
  73. def test_basic_ascend1():
  74. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  75. test_basic1()