[feature] support get_buffer for symm mem
Co-authored-by: ccyyccyyccyycc<changqiaowei@huawei.com> # message auto-generated for no-merge-commit merge: !46842 merge feat/symm-mem-getbuffer into master [feature] support get_buffer for symm mem Created-by: ccyyccyyccyycc Commit-by: ccyyccyyccyycc Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 https://gitcode.com/Ascend/pytorch/issues/5031 - [ ] 需求 - [ ] 问题单 - [x] issue/工单 - [ ] 重构优化 - [ ] 资料更新 # 【修改方案】 - `NPUSHMEMSymmetricMemory` 构造函数:填 `buffers_`(每个 group rank 的 `Aclshmem_ptr` 结果),`Aclshmem_ptr` 返回 nullptr 时 `TORCH_CHECK`。无需新增。 - 社区`SymmetricMemory`通过`get_buffer`,调用`NPUSHMEMSymmetricMemory`后端`get_buffer_ptrs()`、`get_buffer_size()`等接口,直接返回 `buffers_`、`buffer_size_`等。 # 【资料变更】 不涉及 # 【接口变更】 不涉及 # 【功能验证】 | 测试用例 | 验证点 | 类型 | | --- | --- | --- | | `test_get_buffer` | `buffer_size` 正确;本地 `data_ptr()`==本地 tensor;对端 `data_ptr()`==`buffer_ptrs[peer]`;本地与对端数据值正确;`storage_offset=4` 子切片地址与数据正确;越界抛 "exceeds the allocated size" | 集成 | | `test_get_buffer_alias` | 本地 `get_buffer(rank)[3:4].fill_` 对本地 tensor 可见;rank0 经 `get_buffer(peer)[7:8].fill_` 跨卡写入,synchronize+barrier 后 rank1 本地 tensor[7]==写入值 | 集成 | | `test_get_buffer_allocation_lifetime` | `del tensor`+barrier 后仍可 `get_buffer`,地址与数据保持正确,验证 handle 引用计数保活 allocation | 集成 | 测试执行test_shmem.py   # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!46842
This commit is contained in:
@@ -1,11 +1,11 @@
|
||||
import os
|
||||
from unittest import skip
|
||||
import ctypes
|
||||
from unittest import skipIf
|
||||
|
||||
import torch
|
||||
import torch.distributed as dist
|
||||
import torch.distributed._symmetric_memory as symm_mem
|
||||
from torch.testing._internal.common_distributed import MultiProcContinuousTest
|
||||
from torch.testing._internal.common_utils import instantiate_parametrized_tests, run_tests
|
||||
import torch_npu
|
||||
from torch_npu.testing.common_distributed import skipIfUnsupportMultiNPU
|
||||
|
||||
|
||||
@@ -14,8 +14,19 @@ device_type = "npu"
|
||||
device_module = torch.get_device_module(device_type)
|
||||
|
||||
|
||||
def _shmem_available() -> bool:
|
||||
"""Return whether the NPU and CANN SHMEM runtime are available."""
|
||||
if not torch.npu.is_available():
|
||||
return False
|
||||
try:
|
||||
ctypes.CDLL("libshmem.so", mode=ctypes.RTLD_GLOBAL)
|
||||
return True
|
||||
except OSError:
|
||||
return False
|
||||
|
||||
|
||||
@instantiate_parametrized_tests
|
||||
@skip("request shmem")
|
||||
@skipIf(not _shmem_available(), "CANN SHMEM is not available")
|
||||
class NPUSHMEMSymmetricMemoryTest(MultiProcContinuousTest):
|
||||
world_size = 2
|
||||
|
||||
@@ -42,6 +53,20 @@ class NPUSHMEMSymmetricMemoryTest(MultiProcContinuousTest):
|
||||
def device(self) -> torch.device:
|
||||
return torch.device(device_type, self.rank)
|
||||
|
||||
def _make_get_buffer_fixture(self):
|
||||
"""Create one two-rank symmetric allocation with distinguishable values."""
|
||||
rank = self.rank
|
||||
peer = 1 - rank
|
||||
group_name = dist.group.WORLD.group_name
|
||||
symm_mem.enable_symm_mem_for_group(group_name)
|
||||
|
||||
# rank 0 保存 [0..15],rank 1 保存 [100..115]。
|
||||
tensor = symm_mem.empty(16, dtype=torch.int32, device=self.device)
|
||||
tensor.copy_(torch.arange(16, dtype=torch.int32, device=self.device) + rank * 100)
|
||||
handle = symm_mem.rendezvous(tensor, group=group_name)
|
||||
dist.barrier(device_ids=[rank])
|
||||
return rank, peer, tensor, handle
|
||||
|
||||
@skipIfUnsupportMultiNPU(2)
|
||||
def test_alloc(self) -> None:
|
||||
self._init_device()
|
||||
@@ -179,5 +204,76 @@ class NPUSHMEMSymmetricMemoryTest(MultiProcContinuousTest):
|
||||
elif self.rank == 1:
|
||||
dist.barrier(device_ids=[self.rank])
|
||||
|
||||
@skipIfUnsupportMultiNPU(2)
|
||||
def test_get_buffer(self) -> None:
|
||||
"""Validate mapped buffers, explicit offsets and bounds checking."""
|
||||
self._init_device()
|
||||
rank, peer, tensor, handle = self._make_get_buffer_fixture()
|
||||
|
||||
self.assertEqual(handle.buffer_size, tensor.numel() * tensor.element_size())
|
||||
|
||||
local = handle.get_buffer(rank, (16,), torch.int32)
|
||||
remote = handle.get_buffer(peer, (16,), torch.int32)
|
||||
self.assertEqual(local.data_ptr(), tensor.data_ptr())
|
||||
self.assertEqual(remote.data_ptr(), handle.buffer_ptrs[peer])
|
||||
self.assertEqual(local.untyped_storage().size(), handle.buffer_size)
|
||||
torch.testing.assert_close(local, tensor)
|
||||
torch.testing.assert_close(
|
||||
remote, torch.arange(16, dtype=torch.int32, device=self.device) + peer * 100
|
||||
)
|
||||
|
||||
# storage_offset=4 表示跳过 4 个 int32,即 16 字节。
|
||||
remote_slice = handle.get_buffer(peer, (4,), torch.int32, 4)
|
||||
self.assertEqual(
|
||||
remote_slice.data_ptr(), handle.buffer_ptrs[peer] + 4 * tensor.element_size()
|
||||
)
|
||||
torch.testing.assert_close(
|
||||
remote_slice, torch.arange(4, 8, dtype=torch.int32, device=self.device) + peer * 100
|
||||
)
|
||||
with self.assertRaisesRegex(RuntimeError, "exceeds the allocated size"):
|
||||
handle.get_buffer(peer, (4,), torch.int32, 14)
|
||||
dist.barrier(device_ids=[rank])
|
||||
|
||||
@skipIfUnsupportMultiNPU(2)
|
||||
def test_get_buffer_alias(self) -> None:
|
||||
"""Validate local aliasing and writes through a peer mapping."""
|
||||
self._init_device()
|
||||
rank, peer, tensor, handle = self._make_get_buffer_fixture()
|
||||
local = handle.get_buffer(rank, (16,), torch.int32)
|
||||
remote = handle.get_buffer(peer, (16,), torch.int32)
|
||||
|
||||
local[3:4].fill_(1000 + rank)
|
||||
torch.npu.synchronize()
|
||||
self.assertEqual(tensor[3].item(), 1000 + rank)
|
||||
dist.barrier(device_ids=[rank])
|
||||
|
||||
if rank == 0:
|
||||
remote[7:8].fill_(12345)
|
||||
torch.npu.synchronize()
|
||||
dist.barrier(device_ids=[rank])
|
||||
if rank == 1:
|
||||
self.assertEqual(tensor[7].item(), 12345)
|
||||
dist.barrier(device_ids=[rank])
|
||||
|
||||
@skipIfUnsupportMultiNPU(2)
|
||||
def test_get_buffer_allocation_lifetime(self) -> None:
|
||||
"""Validate that a live handle keeps the SHMEM allocation alive."""
|
||||
self._init_device()
|
||||
rank, peer, tensor, handle = self._make_get_buffer_fixture()
|
||||
local_ptr = handle.buffer_ptrs[rank]
|
||||
del tensor
|
||||
dist.barrier(device_ids=[rank])
|
||||
|
||||
local = handle.get_buffer(rank, (16,), torch.int32)
|
||||
remote = handle.get_buffer(peer, (16,), torch.int32)
|
||||
self.assertEqual(local.data_ptr(), local_ptr)
|
||||
torch.testing.assert_close(
|
||||
local, torch.arange(16, dtype=torch.int32, device=self.device) + rank * 100
|
||||
)
|
||||
torch.testing.assert_close(
|
||||
remote, torch.arange(16, dtype=torch.int32, device=self.device) + peer * 100
|
||||
)
|
||||
dist.barrier(device_ids=[rank])
|
||||
|
||||
if __name__ == "__main__":
|
||||
run_tests()
|
||||
|
||||
Reference in New Issue
Block a user