1# Copyright 2024 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 pytest 16import numpy as np 17import mindspore.context as context 18from mindspore import Tensor 19from mindspore import ops 20import tests.st.utils.test_utils as test_utils 21 22 23@test_utils.run_with_cell 24def forward_func(x): 25 return ops.matrix_band_part(x, 2, 1) 26 27 28@pytest.mark.level0 29@pytest.mark.env_onecard 30@pytest.mark.platform_arm_ascend_training 31@pytest.mark.platform_x86_ascend_training 32@pytest.mark.parametrize("context_mode", [context.GRAPH_MODE, context.PYNATIVE_MODE]) 33def test_matrixbandpart_float32(context_mode): 34 """ 35 Feature: matrixbandpart 36 Description: test matrixbandpart forward 37 Expectation: expect correct result. 38 """ 39 context.set_context(mode=context_mode, device_target="Ascend") 40 x = Tensor(np.ones([2, 4, 4]).astype(np.float32)) 41 output = forward_func(x) 42 expected = np.array( 43 [[[1., 1., 0., 0.], 44 [1., 1., 1., 0.], 45 [1., 1., 1., 1.], 46 [0., 1., 1., 1.]], 47 [[1., 1., 0., 0.], 48 [1., 1., 1., 0.], 49 [1., 1., 1., 1.], 50 [0., 1., 1., 1.]]], np.float32) 51 np.testing.assert_allclose(output.asnumpy(), expected, rtol=1e-3) 52