Co-authored-by: mortalzz<2283012562@qq.com> # message auto-generated for no-merge-commit merge: !44274 merge test-api-consistency-master into master test: add NPU coverage for distributed_c10d._tensor_to_object Created-by: mortalzz Commit-by: mortalzz Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 - https://gitcode.com/Ascend/pytorch/issues/3194 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 ## 一、API 功能 `torch.distributed.distributed_c10d._tensor_to_object(tensor, tensor_size, group)` 根据序列化 ByteTensor、对象有效长度和可选 ProcessGroup 反序列化并恢复 Python 对象。该私有 API 已支持,无需修改功能源码。 ## 二、PyTorch 社区用例与完整性 PyTorch master 的 object collective 用例只通过上层路径间接调用目标 API,没有直接覆盖 NPU ByteTensor、`group=None`/有效 ProcessGroup、`tensor_size` 输入类型和边界,以及非法输入异常一致性。 社区直接用例覆盖不完整,且目标涉及 NPU Tensor。因此按照当前验收标准,在 `test/distributed/test_distributed_c10d_api.py` 中保留直接测试。 ## 三、NPU 测试适配 - 在 `setUp` 中建立单卡 `HashStore + HCCL` 默认进程组并统一清理。 - 输入类型用例覆盖 CPU/NPU tensor、多种 size 类型、`group=None` 与有效 `dist.group.WORLD`。 - 边界用例覆盖精确、超长、零、截断和负值。 - 非法输入用例覆盖非法 size、dtype 和非 Tensor 输入,并比较 CPU/NPU 异常一致性。 - 不显式导入 `torch_npu`。 # 【资料变更】 无。目标为 PyTorch 私有实现接口,无需补充公开 API 文档。 # 【接口变更】 无客户可见接口变更。本 PR 只新增目标 API 的直接测试。 # 【功能验证】 master 当前没有可用的匹配 torch-npu wheel。本次使用 PyTorch/torch-npu 2.12.0、Ascend910B3 对 master 分支中的相同测试内容进行 NPU 运行验证,并对 master 提交执行静态检查;不将其表述为 master 对应 PyTorch 运行时的原生验证。 ```bash /workspace/venv-2.12/bin/python /workspace/pytorch-pr44274/test/distributed/test_distributed_c10d_api.py -v python -m py_compile test/distributed/test_distributed_c10d_api.py git diff --check upstream/master...HEAD ``` ```text test_tensor_to_object_input_types (__main__.TestDistributedC10dAPIs) ... ok test_tensor_to_object_invalid_inputs (__main__.TestDistributedC10dAPIs) ... ok test_tensor_to_object_tensor_size_boundaries (__main__.TestDistributedC10dAPIs) ... ok ---------------------------------------------------------------------- Ran 3 tests in 0.878s OK ``` 目标测试内容执行通过,无测试级 skip;`py_compile` 和 `git diff --check` 通过。master 对应运行时验证由 PR CI 补充。 # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] 本地 NPU 内容验证与 master 静态检查通过 See merge request: Ascend/pytorch!44274
162 lines
5.7 KiB
Python
162 lines
5.7 KiB
Python
# Copyright (c) 2026 Huawei Technologies Co., Ltd
|
|
# All rights reserved.
|
|
#
|
|
# Licensed under the BSD 3-Clause License (the "License");
|
|
# you may not use this file except in compliance with the License.
|
|
# You may obtain a copy of the License at
|
|
#
|
|
# https://opensource.org/licenses/BSD-3-Clause
|
|
#
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
# See the License for the specific language governing permissions and
|
|
# limitations under the License.
|
|
|
|
"""Validate distributed_c10d APIs without direct PyTorch community coverage."""
|
|
|
|
import pickle
|
|
|
|
import torch
|
|
import torch.distributed as dist
|
|
import torch.distributed.distributed_c10d as c10d
|
|
from torch.testing._internal.common_utils import TestCase, run_tests
|
|
|
|
|
|
TEST_NPU = torch.npu.is_available()
|
|
|
|
|
|
class TestDistributedC10dAPIs(TestCase):
|
|
|
|
def setUp(self):
|
|
super().setUp()
|
|
if not TEST_NPU:
|
|
self.skipTest("NPU unavailable")
|
|
|
|
torch.npu.set_device(0)
|
|
store = dist.HashStore()
|
|
dist.init_process_group("hccl", store=store, rank=0, world_size=1)
|
|
self.addCleanup(dist.destroy_process_group)
|
|
|
|
@staticmethod
|
|
def _tensor_to_object_inputs():
|
|
payload = {
|
|
"api": "torch.distributed.distributed_c10d._tensor_to_object",
|
|
"values": [1, None, "npu"],
|
|
}
|
|
tensor, tensor_size = c10d._object_to_tensor(
|
|
payload, torch.device("npu"), None
|
|
)
|
|
return payload, tensor, tensor_size
|
|
|
|
def _assert_tensor_to_object_error(
|
|
self,
|
|
cpu_tensor,
|
|
npu_tensor,
|
|
cpu_tensor_size,
|
|
npu_tensor_size,
|
|
exception_type,
|
|
error_regex,
|
|
):
|
|
messages = []
|
|
for tensor, tensor_size in (
|
|
(cpu_tensor, cpu_tensor_size),
|
|
(npu_tensor, npu_tensor_size),
|
|
):
|
|
with self.assertRaisesRegex(exception_type, error_regex) as error:
|
|
c10d._tensor_to_object(tensor, tensor_size, None)
|
|
messages.append(str(error.exception))
|
|
self.assertEqual(messages[0], messages[1])
|
|
|
|
def test_tensor_to_object_input_types(self):
|
|
payload, npu_tensor, npu_tensor_size = self._tensor_to_object_inputs()
|
|
cpu_tensor = npu_tensor.cpu()
|
|
cpu_tensor_size = npu_tensor_size.cpu()
|
|
tensor_size = int(npu_tensor_size.item())
|
|
|
|
self.assertEqual(npu_tensor.device.type, "npu")
|
|
self.assertEqual(npu_tensor.dtype, torch.uint8)
|
|
self.assertEqual(npu_tensor_size.device.type, "npu")
|
|
self.assertEqual(npu_tensor_size.dtype, torch.int64)
|
|
self.assertEqual(npu_tensor_size.shape, torch.Size([1]))
|
|
valid_inputs = (
|
|
(npu_tensor, npu_tensor_size, None),
|
|
(npu_tensor, cpu_tensor_size, None),
|
|
(npu_tensor, npu_tensor_size[0], None),
|
|
(npu_tensor, tensor_size, None),
|
|
(cpu_tensor, tensor_size, None),
|
|
(npu_tensor, npu_tensor_size, dist.group.WORLD),
|
|
)
|
|
for tensor, size, group in valid_inputs:
|
|
with self.subTest(
|
|
tensor_device=tensor.device.type,
|
|
tensor_size_type=type(size).__name__,
|
|
group_is_none=group is None,
|
|
):
|
|
self.assertEqual(
|
|
c10d._tensor_to_object(tensor, size, group), payload
|
|
)
|
|
|
|
def test_tensor_to_object_tensor_size_boundaries(self):
|
|
payload, npu_tensor, npu_tensor_size = self._tensor_to_object_inputs()
|
|
cpu_tensor = npu_tensor.cpu()
|
|
tensor_size = int(npu_tensor_size.item())
|
|
|
|
for tensor in (cpu_tensor, npu_tensor):
|
|
with self.subTest(tensor_device=tensor.device.type, boundary="exact"):
|
|
self.assertEqual(
|
|
c10d._tensor_to_object(tensor, tensor_size, None), payload
|
|
)
|
|
with self.subTest(tensor_device=tensor.device.type, boundary="oversized"):
|
|
self.assertEqual(
|
|
c10d._tensor_to_object(tensor, tensor_size + 1, None), payload
|
|
)
|
|
|
|
self._assert_tensor_to_object_error(
|
|
cpu_tensor, npu_tensor, 0, 0, EOFError, "Ran out of input"
|
|
)
|
|
for boundary in (tensor_size - 1, -1):
|
|
self._assert_tensor_to_object_error(
|
|
cpu_tensor,
|
|
npu_tensor,
|
|
boundary,
|
|
boundary,
|
|
pickle.UnpicklingError,
|
|
"pickle data was truncated",
|
|
)
|
|
|
|
def test_tensor_to_object_invalid_inputs(self):
|
|
_, npu_tensor, npu_tensor_size = self._tensor_to_object_inputs()
|
|
cpu_tensor = npu_tensor.cpu()
|
|
tensor_size = int(npu_tensor_size.item())
|
|
|
|
self._assert_tensor_to_object_error(
|
|
cpu_tensor,
|
|
npu_tensor,
|
|
float(tensor_size),
|
|
float(tensor_size),
|
|
TypeError,
|
|
"slice indices must be integers",
|
|
)
|
|
self._assert_tensor_to_object_error(
|
|
cpu_tensor,
|
|
npu_tensor,
|
|
torch.tensor([tensor_size, tensor_size]),
|
|
torch.tensor([tensor_size, tensor_size], device="npu"),
|
|
TypeError,
|
|
"only integer tensors of a single element can be converted to an index",
|
|
)
|
|
self._assert_tensor_to_object_error(
|
|
cpu_tensor.float(),
|
|
npu_tensor.float(),
|
|
tensor_size,
|
|
tensor_size,
|
|
pickle.UnpicklingError,
|
|
"invalid load key",
|
|
)
|
|
with self.assertRaisesRegex(AttributeError, "has no attribute 'cpu'"):
|
|
c10d._tensor_to_object(npu_tensor.tolist(), tensor_size, None)
|
|
|
|
if __name__ == "__main__":
|
|
run_tests()
|