1# Copyright 2022 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 as ms 20import mindspore.nn as nn 21import mindspore.ops as ops 22 23 24class Net(nn.Cell): 25 def __init__(self, kernel_size=1, stride=1, pad_mode="valid", padding=0, ceil_mode=False, count_include_pad=True, 26 divisor_override=None, data_format='NCHW'): 27 super(Net, self).__init__() 28 self.pool = nn.AvgPool2d(kernel_size=kernel_size, stride=stride, pad_mode=pad_mode, padding=padding, 29 ceil_mode=ceil_mode, count_include_pad=count_include_pad, 30 divisor_override=divisor_override, data_format=data_format) 31 32 def construct(self, x): 33 out = self.pool(x) 34 return out 35 36 37@pytest.mark.level2 38@pytest.mark.platform_x86_gpu_training 39@pytest.mark.platform_x86_cpu 40@pytest.mark.platform_arm_cpu 41@pytest.mark.env_onecard 42@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE]) 43def test_avgpool2d_normal(mode): 44 """ 45 Feature: AvgPool2d 46 Description: Verify the result of AvgPool2d 47 Expectation: success 48 """ 49 ms.set_context(mode=mode) 50 x1 = ms.Tensor(np.random.randint(0, 10, [1, 2, 4, 4]), ms.float32) 51 pool1 = Net(kernel_size=3, stride=1) 52 output1 = pool1(x1) 53 54 x2 = ops.randn(6, 6, 8, 8) 55 pool2 = Net(kernel_size=4, stride=1, pad_mode='pad', padding=2, divisor_override=5) 56 output2 = pool2(x2) 57 58 assert output1.shape == (1, 2, 2, 2) 59 assert output2.shape == (6, 6, 9, 9) 60