已合并
[Task-100][v2.7.1] API Consistency: Tensor.ndimension #41290
Yhw050920创建于 7月11日
[Task-100][v2.7.1] API Consistency: Tensor.ndimension #41290
已合并
共 1 个文件变更+82-0
| @@ -0,0 +1,82 @@ | |||
| 1 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd | ||
| 2 | +# All rights reserved. | ||
| 3 | +# Licensed under the BSD 3-Clause 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 | +# https://opensource.org/licenses/BSD-3-Clause | ||
| 7 | +# Unless required by applicable law or agreed to in writing, software | ||
| 8 | +# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 9 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 10 | +# See the License for the specific language governing permissions and | ||
| 11 | +# limitations under the License. | ||
| 12 | + | ||
| 13 | +""" | ||
| 14 | +Add validation cases for Tensor.ndimension API on Ascend NPU: | ||
| 15 | + | ||
| 16 | +PyTorch community lacks sufficient and direct API validations for this API, so this file is added. | ||
| 17 | +This file validates Tensor.ndimension() (extendable). | ||
| 18 | + | ||
| 19 | +Test command: | ||
| 20 | + python test/npu/test_tensor_ndimension.py | ||
| 21 | +""" | ||
| 22 | + | ||
| 23 | +import torch | ||
| 24 | +from torch.testing._internal.common_utils import TestCase, run_tests | ||
| 25 | + | ||
| 26 | +import torch_npu | ||
| 27 | +from torch_npu.testing.common_utils import SupportedDevices | ||
| 28 | + | ||
| 29 | +assert torch_npu is not None # NPU backend registration | ||
| 30 | + | ||
| 31 | + | ||
| 32 | +class TestTensorNdimension(TestCase): | ||
| 33 | + | ||
| 34 | + | ||
| 35 | + def test_ndimension_0d_tensor(self): | ||
| 36 | + x = torch.tensor(5).npu() | ||
| 37 | + self.assertEqual(x.ndimension(), 0) | ||
| 38 | + | ||
| 39 | + | ||
| 40 | + def test_ndimension_1d_tensor(self): | ||
| 41 | + x = torch.randn(5).npu() | ||
| 42 | + self.assertEqual(x.ndimension(), 1) | ||
| 43 | + | ||
| 44 | + | ||
| 45 | + def test_ndimension_2d_tensor(self): | ||
| 46 | + x = torch.randn(3, 4).npu() | ||
| 47 | + self.assertEqual(x.ndimension(), 2) | ||
| 48 | + | ||
| 49 | + | ||
| 50 | + def test_ndimension_3d_tensor(self): | ||
| 51 | + x = torch.randn(2, 3, 4).npu() | ||
| 52 | + self.assertEqual(x.ndimension(), 3) | ||
| 53 | + | ||
| 54 | + | ||
| 55 | + def test_ndimension_4d_tensor(self): | ||
| 56 | + x = torch.randn(2, 3, 4, 5).npu() | ||
| 57 | + self.assertEqual(x.ndimension(), 4) | ||
| 58 | + | ||
| 59 | + | ||
| 60 | + def test_ndimension_cpu_npu_consistency(self): | ||
| 61 | + for shape in [(5,), (3, 4), (2, 3, 4), (2, 3, 4, 5)]: | ||
| 62 | + cpu_tensor = torch.randn(*shape) | ||
| 63 | + npu_tensor = cpu_tensor.npu() | ||
| 64 | + self.assertEqual(cpu_tensor.ndimension(), npu_tensor.ndimension()) | ||
| 65 | + | ||
| 66 | + | ||
| 67 | + def test_ndimension_after_operations(self): | ||
| 68 | + x = torch.randn(3, 4).npu() | ||
| 69 | + y = torch.randn(3, 4).npu() | ||
| 70 | + z = x + y | ||
| 71 | + self.assertEqual(z.ndimension(), 2) | ||
| 72 | + w = z.sum(dim=1) | ||
| 73 | + self.assertEqual(w.ndimension(), 1) | ||
| 74 | + | ||
| 75 | + | ||
| 76 | + def test_ndimension_equals_ndim(self): | ||
| 77 | + x = torch.randn(3, 4).npu() | ||
| 78 | + self.assertEqual(x.ndimension(), x.ndim) | ||
| 79 | + | ||
| 80 | + | ||
| 81 | +if __name__ == "__main__": | ||
| 82 | + run_tests() | ||