已合并
test: add Backend.Options API coverage #43162
nwww创建于 7月28日
test: add Backend.Options API coverage #43162
已合并
共 1 个文件变更+165-0
| @@ -0,0 +1,165 @@ | |||
| 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 | ||
| 13 | +# implied. | ||
| 14 | +# See the License for the specific language governing permissions and | ||
| 15 | +# limitations under the License. | ||
| 16 | + | ||
| 17 | +""" | ||
| 18 | +Add validation cases for torch._C._distributed_c10d.Backend.Options on NPU: | ||
| 19 | + | ||
| 20 | +1. PyTorch community lacks sufficient and direct API validations for | ||
| 21 | + this API, so this file is added. | ||
| 22 | +2. This file validates | ||
| 23 | + torch._C._distributed_c10d.Backend.Options (extendable). | ||
| 24 | +""" | ||
| 25 | + | ||
| 26 | +from datetime import timedelta | ||
| 27 | + | ||
| 28 | +from torch._C._distributed_c10d import Backend | ||
| 29 | +from torch.testing._internal.common_utils import TestCase, run_tests | ||
| 30 | + | ||
| 31 | + | ||
| 32 | +class TestBackendOptions(TestCase): | ||
| 33 | + def test_init_with_default_and_explicit_timeout(self): | ||
| 34 | + default_options = Backend.Options("hccl") | ||
| 35 | + | ||
| 36 | + self.assertIsInstance(default_options, Backend.Options) | ||
| 37 | + self.assertEqual(default_options.backend, "hccl") | ||
| 38 | + self.assertEqual(default_options._timeout, timedelta(minutes=30)) | ||
| 39 | + | ||
| 40 | + explicit_timeout = timedelta(seconds=7) | ||
| 41 | + keyword_options = Backend.Options( | ||
| 42 | + backend="hccl", | ||
| 43 | + timeout=explicit_timeout, | ||
| 44 | + ) | ||
| 45 | + | ||
| 46 | + self.assertEqual(keyword_options.backend, "hccl") | ||
| 47 | + self.assertEqual(keyword_options._timeout, explicit_timeout) | ||
| 48 | + | ||
| 49 | + positional_options = Backend.Options( | ||
| 50 | + "gloo", | ||
| 51 | + timedelta(seconds=11), | ||
| 52 | + ) | ||
| 53 | + | ||
| 54 | + self.assertEqual(positional_options.backend, "gloo") | ||
| 55 | + self.assertEqual( | ||
| 56 | + positional_options._timeout, | ||
| 57 | + timedelta(seconds=11), | ||
| 58 | + ) | ||
| 59 | + | ||
| 60 | + def test_init_with_boundary_values(self): | ||
| 61 | + empty_backend_options = Backend.Options("") | ||
| 62 | + zero_timeout_options = Backend.Options( | ||
| 63 | + "hccl", | ||
| 64 | + timedelta(0), | ||
| 65 | + ) | ||
| 66 | + negative_timeout_options = Backend.Options( | ||
| 67 | + "hccl", | ||
| 68 | + timedelta(seconds=-1), | ||
| 69 | + ) | ||
| 70 | + | ||
| 71 | + self.assertEqual(empty_backend_options.backend, "") | ||
| 72 | + self.assertEqual( | ||
| 73 | + empty_backend_options._timeout, | ||
| 74 | + timedelta(minutes=30), | ||
| 75 | + ) | ||
| 76 | + self.assertEqual(zero_timeout_options._timeout, timedelta(0)) | ||
| 77 | + self.assertEqual( | ||
| 78 | + negative_timeout_options._timeout, | ||
| 79 | + timedelta(seconds=-1), | ||
| 80 | + ) | ||
| 81 | + | ||
| 82 | + def test_property_access_and_instance_independence(self): | ||
| 83 | + first = Backend.Options( | ||
| 84 | + "hccl", | ||
| 85 | + timedelta(seconds=1), | ||
| 86 | + ) | ||
| 87 | + second = Backend.Options( | ||
| 88 | + "gloo", | ||
| 89 | + timedelta(seconds=2), | ||
| 90 | + ) | ||
| 91 | + | ||
| 92 | + with self.assertRaises(AttributeError): | ||
| 93 | + first.backend = "gloo" | ||
| 94 | + | ||
| 95 | + first._timeout = timedelta(seconds=20) | ||
| 96 | + | ||
| 97 | + self.assertEqual(first.backend, "hccl") | ||
| 98 | + self.assertEqual(first._timeout, timedelta(seconds=20)) | ||
| 99 | + self.assertEqual(second.backend, "gloo") | ||
| 100 | + self.assertEqual(second._timeout, timedelta(seconds=2)) | ||
| 101 | + self.assertIsNot(first, second) | ||
| 102 | + | ||
| 103 | + def test_invalid_constructor_arguments(self): | ||
| 104 | + with self.assertRaises(TypeError): | ||
| 105 | + Backend.Options() | ||
| 106 | + | ||
| 107 | + invalid_backends = [ | ||
| 108 | + None, | ||
| 109 | + 1, | ||
| 110 | + [], | ||
| 111 | + {}, | ||
| 112 | + ] | ||
| 113 | + | ||
| 114 | + for backend in invalid_backends: | ||
| 115 | + with self.subTest(backend=backend): | ||
| 116 | + with self.assertRaises(TypeError): | ||
| 117 | + Backend.Options(backend) | ||
| 118 | + | ||
| 119 | + invalid_timeouts = [ | ||
| 120 | + None, | ||
| 121 | + 1, | ||
| 122 | + "1 second", | ||
| 123 | + [], | ||
| 124 | + {}, | ||
| 125 | + ] | ||
| 126 | + | ||
| 127 | + for timeout in invalid_timeouts: | ||
| 128 | + with self.subTest(timeout=timeout): | ||
| 129 | + with self.assertRaises(TypeError): | ||
| 130 | + Backend.Options("hccl", timeout) | ||
| 131 | + | ||
| 132 | + with self.assertRaises(TypeError): | ||
| 133 | + Backend.Options( | ||
| 134 | + "hccl", | ||
| 135 | + timedelta(seconds=1), | ||
| 136 | + "unexpected", | ||
| 137 | + ) | ||
| 138 | + | ||
| 139 | + with self.assertRaises(TypeError): | ||
| 140 | + Backend.Options( | ||
| 141 | + "hccl", | ||
| 142 | + unexpected=True, | ||
| 143 | + ) | ||
| 144 | + | ||
| 145 | + def test_invalid_timeout_assignment(self): | ||
| 146 | + options = Backend.Options("hccl") | ||
| 147 | + | ||
| 148 | + invalid_timeouts = [ | ||
| 149 | + None, | ||
| 150 | + 1, | ||
| 151 | + "1 second", | ||
| 152 | + [], | ||
| 153 | + {}, | ||
| 154 | + ] | ||
| 155 | + | ||
| 156 | + for timeout in invalid_timeouts: | ||
| 157 | + with self.subTest(timeout=timeout): | ||
| 158 | + with self.assertRaises(TypeError): | ||
| 159 | + options._timeout = timeout | ||
| 160 | + | ||
| 161 | + self.assertEqual(options._timeout, timedelta(minutes=30)) | ||
| 162 | + | ||
| 163 | + | ||
| 164 | +if __name__ == "__main__": | ||
| 165 | + run_tests() | ||