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