test: verify community 2.14.0 features and fixes master
Co-authored-by: Chansinging<1183520536@qq.com> # message auto-generated for no-merge-commit merge: !46469 merge verify_2.14_version into master test: verify community 2.14.0 features and fixes master Created-by: chansinging Commit-by: Chansinging Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 适配2.14版本更新 https://github.com/pytorch/pytorch/pull/187388,PyTorch 分布式后端发现机制重构为 Python entrypoints 动态注册路由。做下代码适配,与上游做法一致。 1. 一个 entry point 只注册对应后端。 PTA 导入时仍会主动注册 HCCL/LCCL,entry point 随后可能再次调用注册函数,所以需要重复调用保护 2. 通过 entry_points 注册 hccl/lccl(setup.py),注册幂等化。两个 entry point 都指向同一函数,而该函数一次注册 hccl 和 lccl 两个后端——第二次调用会因重复注册抛错。将其先检查 Backend.backend_list 再注册;同时去掉多余的 lambda 包装,直接传` _new_process_group_hccl_helper`。保留了"hccl 必须先注册以保证 default_device_backend_map 正确"的既有顺序。 https://github.com/pytorch/pytorch/pull/188611 将 setSequenceNumberForGroup 降级为 Backend 级别,该接口废弃(带一次性警告)。 上游 2.14 已从 _new_process_group_helper 中移除 _set_sequence_number_for_group() 调用,pta做同步的删除操作 https://github.com/pytorch/pytorch/pull/188489 修复_new_group_with_tag 向自定义 PG 委托创建子组时遗漏了 backend 参数。跟随社区,新增兼容性测试。低版本选择跳过,自定义 DelegatingProcessGroup 注册为 cpu 后端,断言 new_group(ranks=[0]) 收到默认后端名 pta_new_group_backend_test、new_group(ranks=[0],backend="cpu:pta_new_group_backend_test") 收到显式 cpu: 前缀形式。 !189359,转需求 # 【资料变更】 不涉及 # 【接口变更】 不涉及 # 【功能验证】 !187388 验证,通过脚本,由 PyTorch 根据后端名称 hccl,找到 PTA 安装包中的 entrypoint,查看是否自动加载并完成注册  !187388 验证  !188489  setsequence 2.13版本测试  # 【CheckList】 - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!46469
This commit is contained in:
@@ -831,6 +831,10 @@ setup(
|
||||
'torch.backends': [
|
||||
'torch_npu = torch_npu:_autoload',
|
||||
],
|
||||
'torch.distributed.backends': [
|
||||
'hccl = torch_npu._init.registry.distributed:register_distributed_backend_for_npu',
|
||||
'lccl = torch_npu._init.registry.distributed:register_distributed_backend_for_npu',
|
||||
],
|
||||
'torch_dynamo_backends': [
|
||||
'npu = torch_npu.dynamo:_npu_backend_entrypoint',
|
||||
'npugraph_ex = torch_npu.dynamo:_npugraph_ex_backend_entrypoint',
|
||||
|
||||
@@ -14,7 +14,7 @@
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
|
||||
"""Validate ``ProcessGroup._get_sequence_number_for_group`` on NPU.
|
||||
"""Validate the sequence number getter and setter on NPU.
|
||||
|
||||
On torch < 2.14 the upstream implementation only accepts backends in the
|
||||
backendSupportsSequenceNumbers whitelist and rejects CUSTOM (HCCL), so
|
||||
@@ -24,6 +24,11 @@ upstream implementation for other backends. On torch >= 2.14
|
||||
(pytorch#190138) the upstream implementation covers HCCL natively and
|
||||
the shim is not installed. See the upstream test
|
||||
``pytorch/test/distributed/test_c10d_nccl.py::ProcessGroupNCCLTest::test_sequence_number_initialized``.
|
||||
|
||||
The ``Backend`` level setter is kept for compatibility: on torch < 2.14
|
||||
``Backend::setSequenceNumberForGroup()`` throws for backends without an
|
||||
override, so ``ProcessGroupHCCL`` keeps an empty override until the minimum
|
||||
supported torch version reaches 2.14 (pytorch#188611).
|
||||
"""
|
||||
|
||||
import os
|
||||
@@ -104,6 +109,25 @@ class TestGetSequenceNumber(TestCase):
|
||||
finally:
|
||||
dist.destroy_process_group()
|
||||
|
||||
@skipIfUnsupportMultiNPU(1)
|
||||
def test_set_sequence_number_for_group_hccl(self):
|
||||
"""The hccl backend setter must not raise on any supported torch.
|
||||
|
||||
On torch < 2.14 ``Backend::setSequenceNumberForGroup()`` throws for
|
||||
backends without an override, so ``ProcessGroupHCCL`` keeps an empty
|
||||
override; without it this call raises "Backend hccl does not yet
|
||||
support sequence numbers". On torch >= 2.14 the base implementation
|
||||
is a warning no-op.
|
||||
"""
|
||||
torch.npu.set_device(0)
|
||||
store = dist.FileStore(os.path.join(tempfile.mkdtemp(), "store"), 1)
|
||||
dist.init_process_group("hccl", rank=0, world_size=1, store=store)
|
||||
try:
|
||||
backend = c10d._get_default_group()._get_backend(torch.device("npu"))
|
||||
backend._set_sequence_number_for_group()
|
||||
finally:
|
||||
dist.destroy_process_group()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
if _WORKER_ARG in sys.argv:
|
||||
|
||||
@@ -0,0 +1,77 @@
|
||||
# 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
|
||||
|
||||
"""Compatibility check for PyTorch's custom process-group delegation (#188489)."""
|
||||
|
||||
import tempfile
|
||||
import unittest
|
||||
from pathlib import Path
|
||||
|
||||
import torch
|
||||
import torch.distributed as dist
|
||||
|
||||
|
||||
class TestNewGroupBackend(unittest.TestCase):
|
||||
@unittest.skipIf(
|
||||
tuple(int(part) for part in torch.__version__.split("+")[0].split(".")[:2]) < (2, 14),
|
||||
"Custom PG subgroup delegation requires PyTorch 2.14 or newer",
|
||||
)
|
||||
def test_custom_pg_receives_default_and_explicit_backend(self):
|
||||
backend_name = "pta_new_group_backend_test"
|
||||
received_backends = []
|
||||
|
||||
class DelegatingProcessGroup(dist.ProcessGroup):
|
||||
def __init__(self, rank, size):
|
||||
super().__init__(rank, size)
|
||||
|
||||
def getBackendName(self):
|
||||
return backend_name
|
||||
|
||||
def _get_backend(self, device):
|
||||
return self
|
||||
|
||||
def new_group(
|
||||
self,
|
||||
ranks,
|
||||
timeout=None,
|
||||
backend=None,
|
||||
pg_options=None,
|
||||
group_name=None,
|
||||
group_desc=None,
|
||||
):
|
||||
received_backends.append(str(backend))
|
||||
return DelegatingProcessGroup(ranks.index(0), len(ranks))
|
||||
|
||||
dist.Backend.register_backend(
|
||||
backend_name,
|
||||
lambda opts, pg_options: DelegatingProcessGroup(
|
||||
opts.group_rank, opts.group_size
|
||||
),
|
||||
extended_api=True,
|
||||
devices=["cpu"],
|
||||
)
|
||||
|
||||
with tempfile.TemporaryDirectory() as temp_dir:
|
||||
store = dist.FileStore(str(Path(temp_dir) / "store"), 1)
|
||||
dist.init_process_group(
|
||||
backend_name, store=store, rank=0, world_size=1
|
||||
)
|
||||
try:
|
||||
dist.new_group(ranks=[0])
|
||||
dist.new_group(ranks=[0], backend=f"cpu:{backend_name}")
|
||||
self.assertEqual(
|
||||
received_backends,
|
||||
[backend_name, f"cpu:{backend_name}"],
|
||||
)
|
||||
finally:
|
||||
dist.destroy_process_group()
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
unittest.main()
|
||||
@@ -440,20 +440,6 @@ if CURRENT_VERSION < (2, 14) or not _upstream_supports_npu_default_backend():
|
||||
|
||||
backend_class = creator_fn(dist_backend_opts, backend_options)
|
||||
|
||||
# Set sequence numbers for gloo and nccl backends.
|
||||
if backend_str == Backend.GLOO:
|
||||
if not isinstance(backend_class, ProcessGroupGloo):
|
||||
raise AssertionError(
|
||||
f"Expected ProcessGroupGloo, got {type(backend_class)}"
|
||||
)
|
||||
backend_class._set_sequence_number_for_group()
|
||||
elif backend_str == Backend.NCCL:
|
||||
if not isinstance(backend_class, ProcessGroupNCCL):
|
||||
raise AssertionError(
|
||||
f"Expected ProcessGroupNCCL, got {type(backend_class)}"
|
||||
)
|
||||
backend_class._set_sequence_number_for_group()
|
||||
|
||||
# If the type is a subclass of ProcessGroup then return this process group immediately
|
||||
# TODO: This defaults to the old behavior for PythonProcessGroups which overwrites the
|
||||
# ProcessGroup instance
|
||||
|
||||
@@ -34,21 +34,18 @@ def _new_process_group_lccl_helper(dist_backend_opts, pg_options):
|
||||
def register_distributed_backend_for_npu():
|
||||
# init and register hccl backend
|
||||
# Note: Since torch 2.8, the hccl backend must be registered at first to keep a right default_device_backend_map
|
||||
torch.distributed.Backend.register_backend(
|
||||
"hccl",
|
||||
lambda dist_backend_opts, pg_options: _new_process_group_hccl_helper(
|
||||
dist_backend_opts, pg_options
|
||||
),
|
||||
extended_api=True,
|
||||
devices=["npu"],
|
||||
)
|
||||
if "hccl" not in torch.distributed.Backend.backend_list:
|
||||
torch.distributed.Backend.register_backend(
|
||||
"hccl",
|
||||
_new_process_group_hccl_helper,
|
||||
extended_api=True,
|
||||
devices=["npu"],
|
||||
)
|
||||
|
||||
# init and register lccl backend
|
||||
torch.distributed.Backend.register_backend(
|
||||
"lccl",
|
||||
lambda dist_backend_opts, pg_options: _new_process_group_lccl_helper(
|
||||
dist_backend_opts, pg_options
|
||||
),
|
||||
extended_api=True,
|
||||
devices=["npu"],
|
||||
)
|
||||
if "lccl" not in torch.distributed.Backend.backend_list:
|
||||
torch.distributed.Backend.register_backend(
|
||||
"lccl",
|
||||
_new_process_group_lccl_helper,
|
||||
extended_api=True,
|
||||
devices=["npu"],
|
||||
)
|
||||
|
||||
@@ -703,8 +703,11 @@ class C10_NPU_API ProcessGroupHCCL : public c10d::Backend {
|
||||
|
||||
static const int64_t kProcessGroupHCCLOpTimeoutMillis;
|
||||
|
||||
// Agrees on an initial sequence number for the whole group by having rank 0
|
||||
// create it and broadcast it to other ranks using the store.
|
||||
// COMPAT(< 2.14): torch < 2.14's Backend::setSequenceNumberForGroup() throws
|
||||
// unless the backend overrides it (2.14, pytorch#188611, turns it into a
|
||||
// warning no-op and removes the backend overrides), so HCCL keeps this
|
||||
// empty override.
|
||||
// CAN REMOVE when MIN_SUPPORTED >= (2, 14)
|
||||
void setSequenceNumberForGroup() override;
|
||||
|
||||
// Retrieves the current sequence number for the whole group, which should be
|
||||
|
||||
Reference in New Issue
Block a user