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.1 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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. if enable_graph_kernel:
  37. context.set_context(enable_graph_kernel=True, save_graphs=False)
  38. net = Net()
  39. output = net(i0, i1)
  40. return output
  41. def get_output1(i0, i1, i2, enable_graph_kernel=False):
  42. if enable_graph_kernel:
  43. context.set_context(enable_graph_kernel=True, save_graphs=False)
  44. net = Net1()
  45. output = net(i0, i1, i2)
  46. return output
  47. def test_basic():
  48. i0 = Tensor(np.random.normal(1, 0.01, [800, 96]).astype(np.float16))
  49. i1 = Tensor(np.random.normal(1, 0.01, [128, 800]).astype(np.float16))
  50. expect = get_output(i0, i1, False)
  51. output = get_output(i0, i1, True)
  52. expect_np = expect.asnumpy().copy()
  53. output_np = output.asnumpy().copy()
  54. assert np.allclose(expect_np, output_np, 1.e-4, 1.e-7)
  55. def test_basic1():
  56. i0 = Tensor(np.random.normal(1, 0.01, [800, 96]).astype(np.float16))
  57. i1 = Tensor(np.random.normal(1, 0.01, [128, 800]).astype(np.float16))
  58. i2 = Tensor(np.random.normal(100, 0.01, [128,]).astype(np.float16))
  59. expect = get_output1(i0, i1, i2, False)
  60. output = get_output1(i0, i1, i2, True)
  61. expect_np = expect.asnumpy().copy()
  62. output_np = output.asnumpy().copy()
  63. assert np.allclose(expect_np, output_np, 6.e-4, 6.e-4)
  64. @pytest.mark.level0
  65. @pytest.mark.platform_arm_ascend_training
  66. @pytest.mark.platform_x86_ascend_training
  67. @pytest.mark.env_onecard
  68. def test_basic_ascend():
  69. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  70. test_basic()
  71. @pytest.mark.level0
  72. @pytest.mark.platform_arm_ascend_training
  73. @pytest.mark.platform_x86_ascend_training
  74. @pytest.mark.env_onecard
  75. def test_basic_ascend1():
  76. context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
  77. test_basic1()