• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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
16import numpy as np
17import pytest
18import mindspore.context as context
19from mindspore import Tensor
20from mindspore.nn import Cell
21import mindspore.ops.operations as P
22
23class Net(Cell):
24    def __init__(self):
25        super(Net, self).__init__()
26        self.matmul = P.MatMul(transpose_a=True, transpose_b=True)
27
28    def construct(self, x, y):
29        return self.matmul(x, y)
30
31class Net1(Cell):
32    def __init__(self):
33        super(Net1, self).__init__()
34        self.matmul = P.MatMul(transpose_a=True, transpose_b=True)
35        self.add = P.BiasAdd()
36
37    def construct(self, x, y, bias):
38        res = self.matmul(x, y)
39        return self.add(res, bias)
40
41def get_output(i0, i1, enable_graph_kernel=False):
42    context.set_context(enable_graph_kernel=enable_graph_kernel)
43    net = Net()
44    output = net(i0, i1)
45    return output
46
47def get_output1(i0, i1, i2, enable_graph_kernel=False):
48    context.set_context(enable_graph_kernel=enable_graph_kernel)
49    net = Net1()
50    output = net(i0, i1, i2)
51    return output
52
53def test_basic():
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    expect = get_output(i0, i1, False)
57    output = get_output(i0, i1, True)
58    expect_np = expect.asnumpy().copy()
59    output_np = output.asnumpy().copy()
60    assert np.allclose(expect_np, output_np, 1.e-4, 1.e-7)
61
62def test_basic1():
63    i0 = Tensor(np.random.normal(1, 0.01, [800, 96]).astype(np.float16))
64    i1 = Tensor(np.random.normal(1, 0.01, [128, 800]).astype(np.float16))
65    i2 = Tensor(np.random.normal(100, 0.01, [128,]).astype(np.float16))
66    expect = get_output1(i0, i1, i2, False)
67    output = get_output1(i0, i1, i2, True)
68    expect_np = expect.asnumpy().copy()
69    output_np = output.asnumpy().copy()
70    assert np.allclose(expect_np, output_np, 6.e-4, 6.e-4)
71
72@pytest.mark.level0
73@pytest.mark.platform_arm_ascend_training
74@pytest.mark.platform_x86_ascend_training
75@pytest.mark.env_onecard
76def test_basic_ascend():
77    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
78    test_basic()
79
80@pytest.mark.level0
81@pytest.mark.platform_arm_ascend_training
82@pytest.mark.platform_x86_ascend_training
83@pytest.mark.env_onecard
84def test_basic_ascend1():
85    context.set_context(mode=context.GRAPH_MODE, device_target="Ascend")
86    test_basic1()
87
88@pytest.mark.level0
89@pytest.mark.platform_x86_gpu_training
90@pytest.mark.env_onecard
91def test_basic_gpu():
92    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
93    test_basic()
94