test_matmul.py 1.3 KB
Newer Older
Z
zhunaipan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
# Copyright 2019 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
import numpy as np
J
jinyaohui 已提交
16

Z
zhunaipan 已提交
17
import mindspore.context as context
J
jinyaohui 已提交
18 19 20 21
import mindspore.nn as nn
from mindspore import Tensor
from mindspore.common.api import ms_function
from mindspore.ops import operations as P
22

Z
zhunaipan 已提交
23
context.set_context(device_target="Ascend")
24 25


Z
zhunaipan 已提交
26 27 28 29 30 31
class Net(nn.Cell):
    def __init__(self):
        super(Net, self).__init__()
        self.matmul = P.MatMul()

    @ms_function
32 33
    def construct(self, x1_, x2_):
        return self.matmul(x1_, x2_)
Z
zhunaipan 已提交
34

35 36 37 38

x1 = np.random.randn(1, 3).astype(np.float32)
x2 = np.random.randn(3, 4).astype(np.float32)

Z
zhunaipan 已提交
39 40 41 42 43 44 45

def test_net():
    matmul = Net()
    output = matmul(Tensor(x1), Tensor(x2))
    print(x1)
    print(x2)
    print(output.asnumpy())