| @@ -0,0 +1,74 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +""" | ||
| 3 | +测试目的:验证 torch.utils.data.ConcatDataset 接口功能正确性 | ||
| 4 | +API 名称:torch.utils.data.ConcatDataset | ||
| 5 | +API 签名:torch.utils.data.ConcatDataset(datasets) | ||
| 6 | + | ||
| 7 | +覆盖维度表: | ||
| 8 | +| 覆盖维度 | 说明 | 覆盖情况 | | ||
| 9 | +|------------------|-------------------------------------|-------------------------| | ||
| 10 | +| 基础调用 | 拼接多个 Dataset 不报错 | 已覆盖 | | ||
| 11 | +| 长度 | 长度等于各子集之和 | 已覆盖 | | ||
| 12 | +| getitem | 索引访问跨数据集 | 已覆盖 | | ||
| 13 | + | ||
| 14 | +未覆盖项及原因: | ||
| 15 | +- 无 | ||
| 16 | + | ||
| 17 | +注意:本测试仅验证功能正确性,不做精度和数值正确性校验。 | ||
| 18 | +""" | ||
| 19 | +import torch | ||
| 20 | +import torch_npu # noqa: F401 | ||
| 21 | +from torch.utils.data import Dataset, ConcatDataset | ||
| 22 | + | ||
| 23 | +try: | ||
OO 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() | |||
| 24 | + from torch_npu.testing.testcase import TestCase, run_tests | ||
| 25 | +except ImportError: | ||
| 26 | + import sys | ||
| 27 | + import unittest | ||
| 28 | + from unittest import TestCase | ||
| 29 | + def run_tests(): | ||
| 30 | + unittest.main(argv=sys.argv) | ||
| 31 | + | ||
| 32 | + | ||
| 33 | +class _ListDataset(Dataset): | ||
| 34 | + def __init__(self, data): | ||
| 35 | + self.data = data | ||
| 36 | + | ||
| 37 | + def __len__(self): | ||
| 38 | + return len(self.data) | ||
| 39 | + | ||
| 40 | + def __getitem__(self, idx): | ||
| 41 | + return self.data[idx] | ||
| 42 | + | ||
| 43 | + | ||
| 44 | +class TestUtilsDataConcatDataset(TestCase): | ||
| 45 | + def setUp(self): | ||
| 46 | + super().setUp() | ||
| 47 | + self.device_name = torch._C._get_privateuse1_backend_name() | ||
| 48 | + self.assertEqual(self.device_name, 'npu', | ||
| 49 | + f"Expected device 'npu', got '{self.device_name}'") | ||
| 50 | + | ||
| 51 | + def test_concat_two_datasets(self): | ||
| 52 | + """Verify concatenated length equals sum of sub-datasets.""" | ||
| 53 | + ds1 = _ListDataset([1, 2, 3]) | ||
| 54 | + ds2 = _ListDataset([4, 5]) | ||
| 55 | + concat = ConcatDataset([ds1, ds2]) | ||
| 56 | + self.assertEqual(len(concat), 5) | ||
| 57 | + | ||
| 58 | + def test_getitem_across_datasets(self): | ||
| 59 | + """Verify index access spans across sub-datasets.""" | ||
| 60 | + ds1 = _ListDataset([10, 20]) | ||
| 61 | + ds2 = _ListDataset([30, 40]) | ||
| 62 | + concat = ConcatDataset([ds1, ds2]) | ||
| 63 | + self.assertEqual(concat[0], 10) | ||
| 64 | + self.assertEqual(concat[2], 30) | ||
| 65 | + | ||
| 66 | + def test_single_dataset(self): | ||
| 67 | + """Verify single-dataset concat preserves original length.""" | ||
| 68 | + ds = _ListDataset([1, 2, 3]) | ||
| 69 | + concat = ConcatDataset([ds]) | ||
| 70 | + self.assertEqual(len(concat), 3) | ||
| 71 | + | ||
| 72 | + | ||
| 73 | +if __name__ == "__main__": | ||
| 74 | + run_tests() | ||
| @@ -0,0 +1,97 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +""" | ||
| 3 | +测试目的:验证 torch.utils.data.DataLoader 接口功能正确性 | ||
| 4 | +API 名称:torch.utils.data.DataLoader | ||
| 5 | +API 签名:torch.utils.data.DataLoader(dataset, batch_size=1, shuffle=False, ...) | ||
| 6 | + | ||
| 7 | +覆盖维度表: | ||
| 8 | +| 覆盖维度 | 说明 | 覆盖情况 | | ||
| 9 | +|------------------|-------------------------------------|-------------------------| | ||
| 10 | +| 基础调用 | 创建 DataLoader 不报错 | 已覆盖 | | ||
| 11 | +| 迭代 | 迭代返回 batch | 已覆盖 | | ||
| 12 | +| batch_size | batch 维度正确 | 已覆盖 | | ||
| 13 | +| shuffle | shuffle=True/False | 已覆盖 | | ||
| 14 | +| num_workers=0 | 单进程加载 | 已覆盖 | | ||
| 15 | + | ||
| 16 | +未覆盖项及原因: | ||
| 17 | +- num_workers>0:多进程环境复杂 | ||
| 18 | +- pin_memory/collate_fn 等高级参数 | ||
| 19 | + | ||
| 20 | +注意:本测试仅验证功能正确性,不做精度和数值正确性校验。 | ||
| 21 | +""" | ||
| 22 | +import torch | ||
| 23 | +import torch_npu # noqa: F401 | ||
| 24 | +from torch.utils.data import Dataset, DataLoader | ||
| 25 | + | ||
| 26 | +try: | ||
OO 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() | |||
| 27 | + from torch_npu.testing.testcase import TestCase, run_tests | ||
| 28 | +except ImportError: | ||
| 29 | + import sys | ||
| 30 | + import unittest | ||
| 31 | + from unittest import TestCase | ||
| 32 | + | ||
| 33 | + def run_tests(): | ||
| 34 | + unittest.main(argv=sys.argv) | ||
| 35 | + | ||
| 36 | + | ||
| 37 | +class _TensorDataset(Dataset): | ||
| 38 | + def __init__(self, size=10): | ||
| 39 | + self.data = torch.randn(size, 4) | ||
| 40 | + | ||
| 41 | + def __len__(self): | ||
| 42 | + return len(self.data) | ||
| 43 | + | ||
| 44 | + def __getitem__(self, idx): | ||
| 45 | + return self.data[idx] | ||
| 46 | + | ||
| 47 | + | ||
| 48 | +class TestUtilsDataDataLoader(TestCase): | ||
| 49 | + def setUp(self): | ||
| 50 | + super().setUp() | ||
| 51 | + self.device_name = torch._C._get_privateuse1_backend_name() | ||
| 52 | + self.assertEqual(self.device_name, 'npu', | ||
| 53 | + f"Expected device 'npu', got '{self.device_name}'") | ||
| 54 | + | ||
| 55 | + def test_create_dataloader(self): | ||
| 56 | + """Verify DataLoader can be created with a dataset and batch_size.""" | ||
| 57 | + ds = _TensorDataset() | ||
| 58 | + loader = DataLoader(ds, batch_size=2) | ||
| 59 | + self.assertIsInstance(loader, DataLoader) | ||
| 60 | + | ||
| 61 | + def test_iter_returns_batches(self): | ||
| 62 | + """Verify iteration yields correct number of batches (ceil division).""" | ||
| 63 | + ds = _TensorDataset(10) | ||
| 64 | + loader = DataLoader(ds, batch_size=3, num_workers=0) | ||
| 65 | + batches = list(loader) | ||
| 66 | + self.assertEqual(len(batches), 4) # ceil(10/3) = 4 | ||
| 67 | + | ||
| 68 | + def test_batch_shape(self): | ||
| 69 | + """Verify first batch has expected shape.""" | ||
| 70 | + ds = _TensorDataset(10) | ||
| 71 | + loader = DataLoader(ds, batch_size=4, num_workers=0) | ||
| 72 | + first_batch = next(iter(loader)) | ||
| 73 | + self.assertEqual(first_batch.shape[0], 4) | ||
| 74 | + self.assertEqual(first_batch.shape[1], 4) | ||
| 75 | + | ||
| 76 | + def test_shuffle_true(self): | ||
| 77 | + """Verify DataLoader with shuffle=True produces correct shape.""" | ||
| 78 | + ds = _TensorDataset(10) | ||
| 79 | + loader = DataLoader(ds, batch_size=10, shuffle=True, num_workers=0) | ||
| 80 | + batch = next(iter(loader)) | ||
| 81 | + self.assertEqual(batch.shape, torch.Size([10, 4])) | ||
| 82 | + | ||
| 83 | + def test_npu_tensor_dataset(self): | ||
| 84 | + """Verify DataLoader works with NPU tensor-returning dataset.""" | ||
| 85 | + class _NpuDataset(Dataset): | ||
| 86 | + def __len__(self): | ||
| 87 | + return 8 | ||
| 88 | + def __getitem__(self, idx): | ||
| 89 | + return torch.tensor(idx, device='npu') | ||
| 90 | + ds = _NpuDataset() | ||
| 91 | + loader = DataLoader(ds, batch_size=4, num_workers=0) | ||
| 92 | + batch = next(iter(loader)) | ||
| 93 | + self.assertEqual(batch.shape, torch.Size([4])) | ||
| 94 | + | ||
| 95 | + | ||
| 96 | +if __name__ == "__main__": | ||
| 97 | + run_tests() | ||
| @@ -0,0 +1,60 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +""" | ||
| 3 | +测试目的:验证 torch.utils.data.Dataset 接口功能正确性 | ||
| 4 | +API 名称:torch.utils.data.Dataset | ||
| 5 | +API 签名:torch.utils.data.Dataset (abstract base class) | ||
| 6 | + | ||
| 7 | +覆盖维度表: | ||
| 8 | +| 覆盖维度 | 说明 | 覆盖情况 | | ||
| 9 | +|------------------|-------------------------------------|-------------------------| | ||
| 10 | +| 子类化 | 可以继承并实现 __len__/__getitem__ | 已覆盖 | | ||
| 11 | +| 实例化后调用 | __len__ 和 __getitem__ 返回正确类型 | 已覆盖 | | ||
| 12 | + | ||
| 13 | +未覆盖项及原因: | ||
| 14 | +- 无 | ||
| 15 | + | ||
| 16 | +注意:本测试仅验证功能正确性,不做精度和数值正确性校验。 | ||
| 17 | +""" | ||
| 18 | +import torch | ||
| 19 | +import torch_npu # noqa: F401 | ||
| 20 | +from torch.utils.data import Dataset | ||
| 21 | + | ||
| 22 | +try: | ||
OO 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() | |||
| 23 | + from torch_npu.testing.testcase import TestCase, run_tests | ||
| 24 | +except ImportError: | ||
| 25 | + import sys | ||
| 26 | + import unittest | ||
| 27 | + from unittest import TestCase | ||
| 28 | + def run_tests(): | ||
| 29 | + unittest.main(argv=sys.argv) | ||
| 30 | + | ||
| 31 | + | ||
| 32 | +class _SimpleDataset(Dataset): | ||
| 33 | + def __len__(self): | ||
| 34 | + return 10 | ||
| 35 | + | ||
| 36 | + def __getitem__(self, idx): | ||
| 37 | + return idx | ||
| 38 | + | ||
| 39 | + | ||
| 40 | +class TestUtilsDataDataset(TestCase): | ||
| 41 | + def setUp(self): | ||
| 42 | + super().setUp() | ||
| 43 | + self.device_name = torch._C._get_privateuse1_backend_name() | ||
| 44 | + self.assertEqual(self.device_name, 'npu', | ||
| 45 | + f"Expected device 'npu', got '{self.device_name}'") | ||
| 46 | + | ||
| 47 | + def test_subclass_len(self): | ||
| 48 | + """Verify Dataset subclass __len__ returns correct length.""" | ||
| 49 | + ds = _SimpleDataset() | ||
| 50 | + self.assertEqual(len(ds), 10) | ||
| 51 | + | ||
| 52 | + def test_subclass_getitem(self): | ||
| 53 | + """Verify Dataset subclass __getitem__ returns correct item.""" | ||
| 54 | + ds = _SimpleDataset() | ||
| 55 | + self.assertEqual(ds[0], 0) | ||
| 56 | + self.assertEqual(ds[9], 9) | ||
| 57 | + | ||
| 58 | + | ||
| 59 | +if __name__ == "__main__": | ||
| 60 | + run_tests() | ||
| @@ -0,0 +1,72 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +""" | ||
| 3 | +测试目的:验证 torch.utils.data.RandomSampler 接口功能正确性 | ||
| 4 | +API 名称:torch.utils.data.RandomSampler | ||
| 5 | +API 签名:torch.utils.data.RandomSampler(data_source, replacement=False, num_samples=None, generator=None) | ||
| 6 | + | ||
| 7 | +覆盖维度表: | ||
| 8 | +| 覆盖维度 | 说明 | 覆盖情况 | | ||
| 9 | +|------------------|-------------------------------------|-------------------------| | ||
| 10 | +| 基础调用 | 创建 RandomSampler 不报错 | 已覆盖 | | ||
| 11 | +| 迭代 | 迭代返回索引列表 | 已覆盖 | | ||
| 12 | +| 长度 | __len__ 等于数据源长度 | 已覆盖 | | ||
| 13 | +| replacement | replacement=True/False | 已覆盖 | | ||
| 14 | + | ||
| 15 | +未覆盖项及原因: | ||
| 16 | +- 无 | ||
| 17 | + | ||
| 18 | +注意:本测试仅验证功能正确性,不做精度和数值正确性校验。 | ||
| 19 | +""" | ||
| 20 | +import torch | ||
| 21 | +import torch_npu # noqa: F401 | ||
| 22 | +from torch.utils.data import RandomSampler | ||
| 23 | + | ||
| 24 | +try: | ||
OO 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() | |||
| 25 | + from torch_npu.testing.testcase import TestCase, run_tests | ||
| 26 | +except ImportError: | ||
| 27 | + import sys | ||
| 28 | + import unittest | ||
| 29 | + from unittest import TestCase | ||
| 30 | + def run_tests(): | ||
| 31 | + unittest.main(argv=sys.argv) | ||
| 32 | + | ||
| 33 | + | ||
| 34 | +class TestUtilsDataRandomSampler(TestCase): | ||
| 35 | + def setUp(self): | ||
| 36 | + super().setUp() | ||
| 37 | + self.device_name = torch._C._get_privateuse1_backend_name() | ||
| 38 | + self.assertEqual(self.device_name, 'npu', | ||
| 39 | + f"Expected device 'npu', got '{self.device_name}'") | ||
| 40 | + | ||
| 41 | + def test_create_sampler(self): | ||
| 42 | + """Verify RandomSampler can be created with a data source.""" | ||
| 43 | + data = [1, 2, 3, 4, 5] | ||
| 44 | + sampler = RandomSampler(data) | ||
| 45 | + self.assertIsInstance(sampler, RandomSampler) | ||
| 46 | + | ||
| 47 | + def test_iter_returns_indices(self): | ||
| 48 | + """Verify iteration returns all indices exactly once (no replacement).""" | ||
| 49 | + data = [1, 2, 3, 4, 5] | ||
| 50 | + sampler = RandomSampler(data) | ||
| 51 | + indices = list(sampler) | ||
| 52 | + self.assertEqual(len(indices), 5) | ||
| 53 | + self.assertEqual(set(indices), {0, 1, 2, 3, 4}) | ||
| 54 | + | ||
| 55 | + def test_len(self): | ||
| 56 | + """Verify sampler length matches data source length.""" | ||
| 57 | + data = [1, 2, 3] | ||
| 58 | + sampler = RandomSampler(data) | ||
| 59 | + self.assertEqual(len(sampler), 3) | ||
| 60 | + | ||
| 61 | + def test_replacement_true(self): | ||
| 62 | + """Verify replacement=True allows repeated indices with custom num_samples.""" | ||
| 63 | + data = [1, 2, 3] | ||
| 64 | + sampler = RandomSampler(data, replacement=True, num_samples=10) | ||
| 65 | + indices = list(sampler) | ||
| 66 | + self.assertEqual(len(indices), 10) | ||
| 67 | + for idx in indices: | ||
| 68 | + self.assertIn(idx, [0, 1, 2]) | ||
| 69 | + | ||
| 70 | + | ||
| 71 | +if __name__ == "__main__": | ||
| 72 | + run_tests() | ||
| @@ -0,0 +1,64 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +""" | ||
| 3 | +测试目的:验证 torch.utils.data.Sampler 接口功能正确性 | ||
| 4 | +API 名称:torch.utils.data.Sampler | ||
| 5 | +API 签名:torch.utils.data.Sampler(data_source=None) | ||
| 6 | + | ||
| 7 | +覆盖维度表: | ||
| 8 | +| 覆盖维度 | 说明 | 覆盖情况 | | ||
| 9 | +|------------------|-------------------------------------|-------------------------| | ||
| 10 | +| 子类化 | 可以继承并实现 __iter__ | 已覆盖 | | ||
| 11 | +| 实例化 | 无参数实例化不报错 | 已覆盖 | | ||
| 12 | + | ||
| 13 | +未覆盖项及原因: | ||
| 14 | +- 无 | ||
| 15 | + | ||
| 16 | +注意:本测试仅验证功能正确性,不做精度和数值正确性校验。 | ||
| 17 | +""" | ||
| 18 | +import torch | ||
| 19 | +import torch_npu # noqa: F401 | ||
| 20 | +from torch.utils.data import Sampler | ||
| 21 | + | ||
| 22 | +try: | ||
OO 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() | |||
| 23 | + from torch_npu.testing.testcase import TestCase, run_tests | ||
| 24 | +except ImportError: | ||
| 25 | + import sys | ||
| 26 | + import unittest | ||
| 27 | + from unittest import TestCase | ||
| 28 | + | ||
| 29 | + def run_tests(): | ||
| 30 | + unittest.main(argv=sys.argv) | ||
| 31 | + | ||
| 32 | + | ||
| 33 | +class _RangeSampler(Sampler): | ||
| 34 | + def __init__(self, n): | ||
| 35 | + self.n = n | ||
| 36 | + | ||
| 37 | + def __iter__(self): | ||
| 38 | + return iter(range(self.n)) | ||
| 39 | + | ||
| 40 | + def __len__(self): | ||
| 41 | + return self.n | ||
| 42 | + | ||
| 43 | + | ||
| 44 | +class TestUtilsDataSampler(TestCase): | ||
| 45 | + def setUp(self): | ||
| 46 | + super().setUp() | ||
| 47 | + self.device_name = torch._C._get_privateuse1_backend_name() | ||
| 48 | + self.assertEqual(self.device_name, 'npu', | ||
| 49 | + f"Expected device 'npu', got '{self.device_name}'") | ||
| 50 | + | ||
| 51 | + def test_subclass_iter(self): | ||
| 52 | + """Verify Sampler subclass __iter__ yields correct range.""" | ||
| 53 | + sampler = _RangeSampler(5) | ||
| 54 | + result = list(sampler) | ||
| 55 | + self.assertEqual(result, [0, 1, 2, 3, 4]) | ||
| 56 | + | ||
| 57 | + def test_subclass_len(self): | ||
| 58 | + """Verify Sampler subclass __len__ returns correct value.""" | ||
| 59 | + sampler = _RangeSampler(3) | ||
| 60 | + self.assertEqual(len(sampler), 3) | ||
| 61 | + | ||
| 62 | + | ||
| 63 | +if __name__ == "__main__": | ||
| 64 | + run_tests() | ||
| @@ -0,0 +1,61 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +""" | ||
| 3 | +测试目的:验证 torch.utils.data.SequentialSampler 接口功能正确性 | ||
| 4 | +API 名称:torch.utils.data.SequentialSampler | ||
| 5 | +API 签名:torch.utils.data.SequentialSampler(data_source) | ||
| 6 | + | ||
| 7 | +覆盖维度表: | ||
| 8 | +| 覆盖维度 | 说明 | 覆盖情况 | | ||
| 9 | +|------------------|-------------------------------------|-------------------------| | ||
| 10 | +| 基础调用 | 创建不报错 | 已覆盖 | | ||
| 11 | +| 顺序迭代 | 返回 0..N-1 顺序索引 | 已覆盖 | | ||
| 12 | +| 长度 | __len__ 等于数据源长度 | 已覆盖 | | ||
| 13 | + | ||
| 14 | +未覆盖项及原因: | ||
| 15 | +- 无 | ||
| 16 | + | ||
| 17 | +注意:本测试仅验证功能正确性,不做精度和数值正确性校验。 | ||
| 18 | +""" | ||
| 19 | +import torch | ||
| 20 | +import torch_npu # noqa: F401 | ||
| 21 | +from torch.utils.data import SequentialSampler | ||
| 22 | + | ||
| 23 | +try: | ||
OO 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() | |||
| 24 | + from torch_npu.testing.testcase import TestCase, run_tests | ||
| 25 | +except ImportError: | ||
| 26 | + import sys | ||
| 27 | + import unittest | ||
| 28 | + from unittest import TestCase | ||
| 29 | + def run_tests(): | ||
| 30 | + unittest.main(argv=sys.argv) | ||
| 31 | + | ||
| 32 | + | ||
| 33 | +class TestUtilsDataSequentialSampler(TestCase): | ||
| 34 | + def setUp(self): | ||
| 35 | + super().setUp() | ||
| 36 | + self.device_name = torch._C._get_privateuse1_backend_name() | ||
| 37 | + self.assertEqual(self.device_name, 'npu', | ||
| 38 | + f"Expected device 'npu', got '{self.device_name}'") | ||
| 39 | + | ||
| 40 | + def test_create_sampler(self): | ||
| 41 | + """Verify SequentialSampler can be created with a data source.""" | ||
| 42 | + data = [1, 2, 3, 4, 5] | ||
| 43 | + sampler = SequentialSampler(data) | ||
| 44 | + self.assertIsInstance(sampler, SequentialSampler) | ||
| 45 | + | ||
| 46 | + def test_sequential_order(self): | ||
| 47 | + """Verify iteration yields indices in sequential order.""" | ||
| 48 | + data = [10, 20, 30] | ||
| 49 | + sampler = SequentialSampler(data) | ||
| 50 | + indices = list(sampler) | ||
| 51 | + self.assertEqual(indices, [0, 1, 2]) | ||
| 52 | + | ||
| 53 | + def test_len(self): | ||
| 54 | + """Verify sampler length matches data source length.""" | ||
| 55 | + data = [1, 2, 3, 4, 5] | ||
| 56 | + sampler = SequentialSampler(data) | ||
| 57 | + self.assertEqual(len(sampler), 5) | ||
| 58 | + | ||
| 59 | + | ||
| 60 | +if __name__ == "__main__": | ||
| 61 | + run_tests() | ||
| @@ -0,0 +1,72 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +""" | ||
| 3 | +测试目的:验证 torch.utils.data.Subset 接口功能正确性 | ||
| 4 | +API 名称:torch.utils.data.Subset | ||
| 5 | +API 签名:torch.utils.data.Subset(dataset, indices) | ||
| 6 | + | ||
| 7 | +覆盖维度表: | ||
| 8 | +| 覆盖维度 | 说明 | 覆盖情况 | | ||
| 9 | +|------------------|-------------------------------------|-------------------------| | ||
| 10 | +| 基础调用 | 创建子集不报错 | 已覆盖 | | ||
| 11 | +| 长度 | 长度等于 indices 长度 | 已覆盖 | | ||
| 12 | +| getitem | 按子集索引访问 | 已覆盖 | | ||
| 13 | + | ||
| 14 | +未覆盖项及原因: | ||
| 15 | +- 无 | ||
| 16 | + | ||
| 17 | +注意:本测试仅验证功能正确性,不做精度和数值正确性校验。 | ||
| 18 | +""" | ||
| 19 | +import torch | ||
| 20 | +import torch_npu # noqa: F401 | ||
| 21 | +from torch.utils.data import Dataset, Subset | ||
| 22 | + | ||
| 23 | +try: | ||
OO 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() | |||
| 24 | + from torch_npu.testing.testcase import TestCase, run_tests | ||
| 25 | +except ImportError: | ||
| 26 | + import sys | ||
| 27 | + import unittest | ||
| 28 | + from unittest import TestCase | ||
| 29 | + def run_tests(): | ||
| 30 | + unittest.main(argv=sys.argv) | ||
| 31 | + | ||
| 32 | + | ||
| 33 | +class _ListDataset(Dataset): | ||
| 34 | + def __init__(self, data): | ||
| 35 | + self.data = data | ||
| 36 | + | ||
| 37 | + def __len__(self): | ||
| 38 | + return len(self.data) | ||
| 39 | + | ||
| 40 | + def __getitem__(self, idx): | ||
| 41 | + return self.data[idx] | ||
| 42 | + | ||
| 43 | + | ||
| 44 | +class TestUtilsDataSubset(TestCase): | ||
| 45 | + def setUp(self): | ||
| 46 | + super().setUp() | ||
| 47 | + self.device_name = torch._C._get_privateuse1_backend_name() | ||
| 48 | + self.assertEqual(self.device_name, 'npu', | ||
| 49 | + f"Expected device 'npu', got '{self.device_name}'") | ||
| 50 | + | ||
| 51 | + def test_subset_len(self): | ||
| 52 | + """Verify Subset length equals indices list length.""" | ||
| 53 | + ds = _ListDataset([10, 20, 30, 40, 50]) | ||
| 54 | + subset = Subset(ds, [0, 2, 4]) | ||
| 55 | + self.assertEqual(len(subset), 3) | ||
| 56 | + | ||
| 57 | + def test_subset_getitem(self): | ||
| 58 | + """Verify Subset __getitem__ maps to correct parent dataset elements.""" | ||
| 59 | + ds = _ListDataset([10, 20, 30, 40, 50]) | ||
| 60 | + subset = Subset(ds, [1, 3]) | ||
| 61 | + self.assertEqual(subset[0], 20) | ||
| 62 | + self.assertEqual(subset[1], 40) | ||
| 63 | + | ||
| 64 | + def test_subset_empty_indices(self): | ||
| 65 | + """Verify Subset with empty indices has length 0.""" | ||
| 66 | + ds = _ListDataset([10, 20, 30]) | ||
| 67 | + subset = Subset(ds, []) | ||
| 68 | + self.assertEqual(len(subset), 0) | ||
| 69 | + | ||
| 70 | + | ||
| 71 | +if __name__ == "__main__": | ||
| 72 | + run_tests() | ||
| @@ -0,0 +1,70 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +""" | ||
| 3 | +测试目的:验证 torch.utils.data.default_collate 接口功能正确性 | ||
| 4 | +API 名称:torch.utils.data.default_collate | ||
| 5 | +API 签名:torch.utils.data.default_collate(batch) | ||
| 6 | + | ||
| 7 | +覆盖维度表: | ||
| 8 | +| 覆盖维度 | 说明 | 覆盖情况 | | ||
| 9 | +|------------------|-------------------------------------|-------------------------| | ||
| 10 | +| 基础调用 | 合并 tensor 列表不报错 | 已覆盖 | | ||
| 11 | +| 返回类型 | 返回 Tensor | 已覆盖 | | ||
| 12 | +| shape | batch 维度在第 0 维 | 已覆盖 | | ||
| 13 | +| 空列表 | 空列表应报错或返回空 | 已覆盖 | | ||
| 14 | + | ||
| 15 | +未覆盖项及原因: | ||
| 16 | +- 无 | ||
| 17 | + | ||
| 18 | +注意:本测试仅验证功能正确性,不做精度和数值正确性校验。 | ||
| 19 | +""" | ||
| 20 | +import torch | ||
| 21 | +import torch_npu # noqa: F401 | ||
| 22 | +from torch.utils.data import default_collate | ||
| 23 | + | ||
| 24 | +try: | ||
OO 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() | |||
| 25 | + from torch_npu.testing.testcase import TestCase, run_tests | ||
| 26 | +except ImportError: | ||
| 27 | + import sys | ||
| 28 | + import unittest | ||
| 29 | + from unittest import TestCase | ||
| 30 | + def run_tests(): | ||
| 31 | + unittest.main(argv=sys.argv) | ||
| 32 | + | ||
| 33 | + | ||
| 34 | +class TestUtilsDataDefaultCollate(TestCase): | ||
| 35 | + def setUp(self): | ||
| 36 | + super().setUp() | ||
| 37 | + self.device_name = torch._C._get_privateuse1_backend_name() | ||
| 38 | + self.assertEqual(self.device_name, 'npu', | ||
| 39 | + f"Expected device 'npu', got '{self.device_name}'") | ||
| 40 | + | ||
| 41 | + def test_collate_tensors(self): | ||
| 42 | + """Verify default_collate stacks tensors into a batched Tensor.""" | ||
| 43 | + batch = [torch.tensor([1, 2]), torch.tensor([3, 4])] | ||
| 44 | + result = default_collate(batch) | ||
| 45 | + self.assertIsInstance(result, torch.Tensor) | ||
| 46 | + self.assertEqual(result.shape, torch.Size([2, 2])) | ||
| 47 | + | ||
| 48 | + def test_collate_float_tensors(self): | ||
| 49 | + """Verify collated float tensors have correct batch shape.""" | ||
| 50 | + batch = [torch.randn(3) for _ in range(4)] | ||
| 51 | + result = default_collate(batch) | ||
| 52 | + self.assertEqual(result.shape, torch.Size([4, 3])) | ||
| 53 | + | ||
| 54 | + def test_collate_dicts(self): | ||
| 55 | + """Verify default_collate merges list of dicts into dict of batched tensors.""" | ||
| 56 | + batch = [{'a': torch.tensor(1), 'b': torch.tensor(2)}, | ||
| 57 | + {'a': torch.tensor(3), 'b': torch.tensor(4)}] | ||
| 58 | + result = default_collate(batch) | ||
| 59 | + self.assertIsInstance(result, dict) | ||
| 60 | + self.assertIn('a', result) | ||
| 61 | + self.assertIn('b', result) | ||
| 62 | + | ||
| 63 | + def test_collate_empty_raises(self): | ||
| 64 | + """Verify empty list raises an error.""" | ||
| 65 | + with self.assertRaises(IndexError): | ||
| 66 | + default_collate([]) | ||
| 67 | + | ||
| 68 | + | ||
| 69 | +if __name__ == "__main__": | ||
| 70 | + run_tests() | ||
| @@ -0,0 +1,82 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +""" | ||
| 3 | +测试目的:验证 torch.utils.data.distributed.DistributedSampler 接口功能正确性 | ||
| 4 | +API 名称:torch.utils.data.distributed.DistributedSampler | ||
| 5 | +API 签名:torch.utils.data.distributed.DistributedSampler(dataset, num_replicas=None, rank=None, shuffle=True, seed=0, drop_last=False) | ||
| 6 | + | ||
| 7 | +覆盖维度表: | ||
| 8 | +| 覆盖维度 | 说明 | 覆盖情况 | | ||
| 9 | +|------------------|-------------------------------------|-------------------------| | ||
| 10 | +| 基础创建 | 创建 sampler 不报错 | 已覆盖 | | ||
| 11 | +| 单进程模拟 | num_replicas=1, rank=0 | 已覆盖 | | ||
| 12 | +| set_epoch | set_epoch 不报错 | 已覆盖 | | ||
| 13 | +| 迭代 | 迭代返回索引 | 已覆盖 | | ||
| 14 | + | ||
| 15 | +未覆盖项及原因: | ||
| 16 | +- 真实多进程:需 torch.distributed 初始化,此处单进程模拟 | ||
| 17 | + | ||
| 18 | +注意:本测试仅验证功能正确性,不做精度和数值正确性校验。 | ||
| 19 | +""" | ||
| 20 | +import torch | ||
| 21 | +import torch_npu # noqa: F401 | ||
| 22 | +from torch.utils.data import Dataset | ||
| 23 | +from torch.utils.data.distributed import DistributedSampler | ||
OO 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() | |||
| 24 | + | ||
| 25 | +try: | ||
| 26 | + from torch_npu.testing.testcase import TestCase, run_tests | ||
| 27 | +except ImportError: | ||
| 28 | + import sys | ||
| 29 | + import unittest | ||
| 30 | + from unittest import TestCase | ||
| 31 | + | ||
| 32 | + def run_tests(): | ||
| 33 | + unittest.main(argv=sys.argv) | ||
| 34 | + | ||
| 35 | + | ||
| 36 | +class _ListDataset(Dataset): | ||
| 37 | + def __init__(self, size=10): | ||
| 38 | + self.data = list(range(size)) | ||
| 39 | + | ||
| 40 | + def __len__(self): | ||
| 41 | + return len(self.data) | ||
| 42 | + | ||
| 43 | + def __getitem__(self, idx): | ||
| 44 | + return self.data[idx] | ||
| 45 | + | ||
| 46 | + | ||
| 47 | +class TestUtilsDataDistributedDistributedSampler(TestCase): | ||
| 48 | + def setUp(self): | ||
| 49 | + super().setUp() | ||
| 50 | + self.device_name = torch._C._get_privateuse1_backend_name() | ||
| 51 | + self.assertEqual(self.device_name, 'npu', | ||
| 52 | + f"Expected device 'npu', got '{self.device_name}'") | ||
| 53 | + | ||
| 54 | + def test_create_sampler(self): | ||
| 55 | + """Verify DistributedSampler can be created with single-process config.""" | ||
| 56 | + ds = _ListDataset(10) | ||
| 57 | + sampler = DistributedSampler(ds, num_replicas=1, rank=0) | ||
| 58 | + self.assertIsInstance(sampler, DistributedSampler) | ||
| 59 | + | ||
| 60 | + def test_iter_single_process(self): | ||
| 61 | + """Verify single-process iteration covers all indices exactly once.""" | ||
| 62 | + ds = _ListDataset(10) | ||
| 63 | + sampler = DistributedSampler(ds, num_replicas=1, rank=0) | ||
| 64 | + indices = list(sampler) | ||
| 65 | + self.assertEqual(len(indices), 10) | ||
| 66 | + self.assertEqual(set(indices), set(range(10))) | ||
| 67 | + | ||
| 68 | + def test_set_epoch(self): | ||
| 69 | + """Verify set_epoch does not raise.""" | ||
| 70 | + ds = _ListDataset(10) | ||
| 71 | + sampler = DistributedSampler(ds, num_replicas=1, rank=0) | ||
| 72 | + sampler.set_epoch(0) | ||
| 73 | + | ||
| 74 | + def test_len(self): | ||
| 75 | + """Verify sampler length matches dataset size for single replica.""" | ||
| 76 | + ds = _ListDataset(10) | ||
| 77 | + sampler = DistributedSampler(ds, num_replicas=1, rank=0) | ||
| 78 | + self.assertEqual(len(sampler), 10) | ||
| 79 | + | ||
| 80 | + | ||
| 81 | +if __name__ == "__main__": | ||
| 82 | + run_tests() | ||
| @@ -0,0 +1,45 @@ | |||
| 1 | +# -*- coding: utf-8 -*- | ||
| 2 | +""" | ||
| 3 | +测试目的:验证 torch.utils.data.get_worker_info 接口功能正确性 | ||
| 4 | +API 名称:torch.utils.data.get_worker_info | ||
| 5 | +API 签名:torch.utils.data.get_worker_info() -> WorkerInfo or None | ||
| 6 | + | ||
| 7 | +覆盖维度表: | ||
| 8 | +| 覆盖维度 | 说明 | 覆盖情况 | | ||
| 9 | +|------------------|-------------------------------------|-------------------------| | ||
| 10 | +| 主进程调用 | 返回 None | 已覆盖 | | ||
| 11 | + | ||
| 12 | +未覆盖项及原因: | ||
| 13 | +- worker 内调用需 DataLoader(num_workers>0) 环境 | ||
| 14 | + | ||
| 15 | +注意:本测试仅验证功能正确性,不做精度和数值正确性校验。 | ||
| 16 | +""" | ||
| 17 | +import torch | ||
| 18 | +import torch_npu # noqa: F401 | ||
| 19 | +from torch.utils.data import get_worker_info | ||
| 20 | + | ||
| 21 | +try: | ||
OO 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() 【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。 ![]() ![]() | |||
| 22 | + from torch_npu.testing.testcase import TestCase, run_tests | ||
| 23 | +except ImportError: | ||
| 24 | + import sys | ||
| 25 | + import unittest | ||
| 26 | + from unittest import TestCase | ||
| 27 | + def run_tests(): | ||
| 28 | + unittest.main(argv=sys.argv) | ||
| 29 | + | ||
| 30 | + | ||
| 31 | +class TestUtilsDataGetWorkerInfo(TestCase): | ||
| 32 | + def setUp(self): | ||
| 33 | + super().setUp() | ||
| 34 | + self.device_name = torch._C._get_privateuse1_backend_name() | ||
| 35 | + self.assertEqual(self.device_name, 'npu', | ||
| 36 | + f"Expected device 'npu', got '{self.device_name}'") | ||
| 37 | + | ||
| 38 | + def test_main_process_returns_none(self): | ||
| 39 | + """Verify get_worker_info returns None in main process.""" | ||
| 40 | + result = get_worker_info() | ||
| 41 | + self.assertIsNone(result) | ||
| 42 | + | ||
| 43 | + | ||
| 44 | +if __name__ == "__main__": | ||
| 45 | + run_tests() | ||


此条代码评论区间+18至+23
【openlibing.ci】识别到代码检查告警抑制注释,匹配工具:flake8,请Committer检视其合理性。