已合并
test(nn): Add test cases for torch.nn.parameter.is_lazy #42612
m0_45651774创建于 27 天前
test(nn): Add test cases for torch.nn.parameter.is_lazy #42612
已合并
共 1 个文件变更+63-0
| @@ -0,0 +1,63 @@ | |||
| 1 | +# Copyright (c) 2026 Huawei Technologies Co., Ltd | ||
| 2 | +# All rights reserved. | ||
| 3 | +# | ||
| 4 | +# Licensed under the BSD 3-Clause License (the "License"); | ||
| 5 | +# you may not use this file except in compliance with the License. | ||
| 6 | +# You may obtain a copy of the License at | ||
| 7 | +# | ||
| 8 | +# https://opensource.org/licenses/BSD-3-Clause | ||
| 9 | +# | ||
| 10 | +# Unless required by applicable law or agreed to in writing, software | ||
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, | ||
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| 13 | +# See the License for the specific language governing permissions and | ||
| 14 | +# limitations under the License. | ||
| 15 | +""" | ||
群 | |||
| 16 | +Add validation cases for torch.nn.parameter.is_lazy on NPU: | ||
| 17 | +1. PyTorch community lacks sufficient and direct API validations for some APIs, so this file is added. | ||
| 18 | +2. This file validates torch.nn.parameter.is_lazy (extendable). | ||
| 19 | +""" | ||
| 20 | + | ||
| 21 | +import torch | ||
| 22 | +from torch.testing._internal.common_utils import TestCase, run_tests | ||
| 23 | + | ||
| 24 | +# Get device command to ensure tensors run on NPU | ||
| 25 | +device_type = acc.type if (acc := torch.accelerator.current_accelerator()) else "cpu" | ||
| 26 | + | ||
| 27 | + | ||
| 28 | +class TestIsLazy(TestCase): | ||
| 29 | + | ||
| 30 | + def test_is_lazy_uninitialized_parameter(self): | ||
| 31 | + """Test is_lazy returns True for UninitializedParameter""" | ||
| 32 | + uninit_param = torch.nn.parameter.UninitializedParameter(device=device_type) | ||
| 33 | + self.assertTrue(torch.nn.parameter.is_lazy(uninit_param)) | ||
| 34 | + | ||
| 35 | + def test_is_lazy_uninitialized_buffer(self): | ||
| 36 | + """Test is_lazy returns True for UninitializedBuffer""" | ||
| 37 | + uninit_buffer = torch.nn.parameter.UninitializedBuffer(device=device_type) | ||
| 38 | + self.assertTrue(torch.nn.parameter.is_lazy(uninit_buffer)) | ||
| 39 | + | ||
| 40 | + def test_is_lazy_regular_parameter(self): | ||
| 41 | + """Test is_lazy returns False for regular Parameter""" | ||
| 42 | + regular_param = torch.nn.Parameter(torch.randn(3, 3).to(device_type)) | ||
| 43 | + self.assertFalse(torch.nn.parameter.is_lazy(regular_param)) | ||
| 44 | + | ||
| 45 | + def test_is_lazy_regular_tensor(self): | ||
| 46 | + """Test is_lazy returns False for regular Tensor""" | ||
| 47 | + regular_tensor = torch.randn(3, 3).to(device_type) | ||
| 48 | + self.assertFalse(torch.nn.parameter.is_lazy(regular_tensor)) | ||
| 49 | + | ||
| 50 | + def test_is_lazy_none(self): | ||
| 51 | + """Test is_lazy returns False for None""" | ||
| 52 | + self.assertFalse(torch.nn.parameter.is_lazy(None)) | ||
| 53 | + | ||
| 54 | + def test_is_lazy_after_materialize(self): | ||
| 55 | + """Test is_lazy returns False after materialize""" | ||
| 56 | + uninit_param = torch.nn.parameter.UninitializedParameter(device=device_type) | ||
| 57 | + self.assertTrue(torch.nn.parameter.is_lazy(uninit_param)) | ||
| 58 | + uninit_param.materialize(shape=(3, 3)) | ||
| 59 | + self.assertFalse(torch.nn.parameter.is_lazy(uninit_param)) | ||
| 60 | + | ||
| 61 | + | ||
| 62 | +if __name__ == "__main__": | ||
| 63 | + run_tests() | ||
加上copyright