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): 25 super(Net, self).__init__() 26 self.pool = nn.LPPool2d(norm_type=1, kernel_size=3, stride=1) 27 28 def construct(self, x): 29 out = self.pool(x) 30 return out 31 32 33@pytest.mark.level2 34@pytest.mark.platform_x86_cpu 35@pytest.mark.platform_arm_cpu 36@pytest.mark.platform_x86_gpu_training 37@pytest.mark.env_onecard 38@pytest.mark.parametrize('mode', [ms.GRAPH_MODE, ms.PYNATIVE_MODE]) 39def test_lppool2d_normal(mode): 40 """ 41 Feature: LPPool2d 42 Description: Verify the result of LPPool2d 43 Expectation: success 44 """ 45 ms.set_context(mode=mode) 46 net = Net() 47 x = ms.Tensor(np.arange(2 * 3 * 4 * 5).reshape((2, 3, 4, 5)), dtype=ms.float32) 48 out = net(x) 49 expect_out = np.array([[[[54., 63., 72.], 50 [99., 108., 117.]], 51 [[234., 243., 252.], 52 [279., 288., 297.]], 53 [[414., 423., 432.], 54 [459., 468., 477.]]], 55 [[[594., 603., 612.], 56 [639., 648., 657.]], 57 [[774., 783., 792.], 58 [819., 828., 837.]], 59 [[954., 963., 972.], 60 [999., 1008., 1017.]]]]) 61 assert np.allclose(out.asnumpy(), expect_out) 62