已合并
test: add Backend.Options API coverage #43164
test: add Backend.Options API coverage #43164
已合并
nwww创建于 7月28日
1 个文件变更+180-0
@@ -0,0 +1,180 @@
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_group_metadata_properties(self):
83+ options = Backend.Options("hccl")
84+ 
85+ self.assertEqual(options.global_ranks_in_group, [])
86+ self.assertEqual(options.group_name, "")
87+ 
88+ options.global_ranks_in_group = [0, 2, 4]
89+ options.group_name = "test_group"
90+ 
91+ self.assertEqual(
92+ options.global_ranks_in_group,
93+ [0, 2, 4],
94+ )
95+ self.assertEqual(options.group_name, "test_group")
96+ 
97+ def test_property_access_and_instance_independence(self):
98+ first = Backend.Options(
99+ "hccl",
100+ timedelta(seconds=1),
101+ )
102+ second = Backend.Options(
103+ "gloo",
104+ timedelta(seconds=2),
105+ )
106+ 
107+ with self.assertRaises(AttributeError):
108+ first.backend = "gloo"
109+ 
110+ first._timeout = timedelta(seconds=20)
111+ 
112+ self.assertEqual(first.backend, "hccl")
113+ self.assertEqual(first._timeout, timedelta(seconds=20))
114+ self.assertEqual(second.backend, "gloo")
115+ self.assertEqual(second._timeout, timedelta(seconds=2))
116+ self.assertIsNot(first, second)
117+ 
118+ def test_invalid_constructor_arguments(self):
119+ with self.assertRaises(TypeError):
120+ Backend.Options()
121+ 
122+ invalid_backends = [
123+ None,
124+ 1,
125+ [],
126+ {},
127+ ]
128+ 
129+ for backend in invalid_backends:
130+ with self.subTest(backend=backend):
131+ with self.assertRaises(TypeError):
132+ Backend.Options(backend)
133+ 
134+ invalid_timeouts = [
135+ None,
136+ 1,
137+ "1 second",
138+ [],
139+ {},
140+ ]
141+ 
142+ for timeout in invalid_timeouts:
143+ with self.subTest(timeout=timeout):
144+ with self.assertRaises(TypeError):
145+ Backend.Options("hccl", timeout)
146+ 
147+ with self.assertRaises(TypeError):
148+ Backend.Options(
149+ "hccl",
150+ timedelta(seconds=1),
151+ "unexpected",
152+ )
153+ 
154+ with self.assertRaises(TypeError):
155+ Backend.Options(
156+ "hccl",
157+ unexpected=True,
158+ )
159+ 
160+ def test_invalid_timeout_assignment(self):
161+ options = Backend.Options("hccl")
162+ 
163+ invalid_timeouts = [
164+ None,
165+ 1,
166+ "1 second",
167+ [],
168+ {},
169+ ]
170+ 
171+ for timeout in invalid_timeouts:
172+ with self.subTest(timeout=timeout):
173+ with self.assertRaises(TypeError):
174+ options._timeout = timeout
175+ 
176+ self.assertEqual(options._timeout, timedelta(minutes=30))
177+ 
178+ 
179+if __name__ == "__main__":
180+ run_tests()