1# Copyright 2020 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 18 19import mindspore.context as context 20import mindspore.nn as nn 21from mindspore import Tensor 22from mindspore.ops import operations as P 23 24 25class Net(nn.Cell): 26 def __init__(self): 27 super(Net, self).__init__() 28 self.ops = P.Pow() 29 30 def construct(self, x, y): 31 return self.ops(x, y) 32 33 34@pytest.mark.level0 35@pytest.mark.platform_x86_cpu 36@pytest.mark.env_onecard 37def test_net(): 38 x0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32) 39 y0_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32) 40 x1_np = np.random.randint(1, 5, (2, 3, 4, 4)).astype(np.float32) 41 y1_np = np.array(3).astype(np.float32) 42 43 x0 = Tensor(x0_np) 44 y0 = Tensor(y0_np) 45 x1 = Tensor(x1_np) 46 y1 = Tensor(y1_np) 47 48 context.set_context(mode=context.GRAPH_MODE, device_target='CPU') 49 net = Net() 50 out = net(x0, y0).asnumpy() 51 expect = np.power(x0_np, y0_np) 52 assert np.all(out == expect) 53 assert out.shape == expect.shape 54 55 out = net(x1, y1).asnumpy() 56 expect = np.power(x1_np, y1_np) 57 assert np.all(out == expect) 58 assert out.shape == expect.shape 59