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# ============================================================================ 15import numpy as np 16import pytest 17 18import mindspore.context as context 19import mindspore.nn as nn 20import mindspore.common.dtype as mstype 21from mindspore.common.initializer import Normal 22from mindspore import Tensor 23 24 25context.set_context(mode=context.GRAPH_MODE, device_target="Ascend") 26 27 28@pytest.mark.level1 29@pytest.mark.platform_x86_ascend_training 30@pytest.mark.platform_arm_ascend_training 31@pytest.mark.env_onecard 32def test_conv2d_depthwiseconv2d_str(): 33 net = nn.Conv2d(128, 128, (2, 3), stride=4, pad_mode='valid', padding=0, group=128, weight_init='normal') 34 input_data = Tensor(np.ones([3, 128, 127, 114]), dtype=mstype.float32) 35 output = net(input_data) 36 assert output.shape == (3, 128, 32, 28) 37 38 39@pytest.mark.level1 40@pytest.mark.platform_x86_ascend_training 41@pytest.mark.platform_arm_ascend_training 42@pytest.mark.env_onecard 43def test_conv2d_depthwiseconv2d_initializer(): 44 net = nn.Conv2d(128, 128, (2, 3), stride=4, pad_mode='valid', padding=0, group=128, weight_init=Normal()) 45 input_data = Tensor(np.ones([3, 128, 127, 114]), dtype=mstype.float32) 46 output = net(input_data) 47 assert output.shape == (3, 128, 32, 28) 48 49 50@pytest.mark.level0 51@pytest.mark.platform_x86_ascend_training 52@pytest.mark.platform_arm_ascend_training 53@pytest.mark.env_onecard 54def test_conv2d_depthwiseconv2d_tensor(): 55 weight_init = Tensor(np.random.randn(128, 1, 2, 3).astype(np.float32)) 56 net = nn.Conv2d(128, 128, (2, 3), stride=4, pad_mode='valid', padding=0, group=128, weight_init=weight_init) 57 input_data = Tensor(np.ones([3, 128, 127, 114]), dtype=mstype.float32) 58 output = net(input_data) 59 assert output.shape == (3, 128, 32, 28) 60