• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 matrix_inverseress or implied.
12# See the License for the specific language governing permissions and
13# limitations under the License.
14# ============================================================================
15
16import numpy as np
17from numpy.linalg import inv
18import pytest
19
20import mindspore.context as context
21import mindspore.nn as nn
22from mindspore import Tensor
23from mindspore.ops import operations as P
24
25np.random.seed(1)
26
27class NetMatrixInverse(nn.Cell):
28    def __init__(self):
29        super(NetMatrixInverse, self).__init__()
30        self.matrix_inverse = P.MatrixInverse()
31
32    def construct(self, x):
33        return self.matrix_inverse(x)
34
35
36@pytest.mark.level0
37@pytest.mark.platform_x86_gpu_training
38@pytest.mark.env_onecard
39def test_matrix_inverse():
40    x0_np = np.random.uniform(-2, 2, (3, 4, 4)).astype(np.float32)
41    x0 = Tensor(x0_np)
42    expect0 = inv(x0_np)
43    error0 = np.ones(shape=expect0.shape) * 1.0e-3
44
45    context.set_context(mode=context.GRAPH_MODE, device_target="GPU")
46    matrix_inverse = NetMatrixInverse()
47    output0 = matrix_inverse(x0)
48    diff0 = output0.asnumpy() - expect0
49    assert np.all(diff0 < error0)
50    assert output0.shape == expect0.shape
51
52    context.set_context(mode=context.PYNATIVE_MODE, device_target="GPU")
53    matrix_inverse = NetMatrixInverse()
54    output0 = matrix_inverse(x0)
55    diff0 = output0.asnumpy() - expect0
56    assert np.all(diff0 < error0)
57    assert output0.shape == expect0.shape
58