已合并
【API一致性任务】test: add Partial placement test for compute_local_shape_and_global_offset #45657
【API一致性任务】test: add Partial placement test for compute_local_shape_and_global_offset #45657
已合并
m0_53076416创建于 9月1日
m0_53076416
m0_53076416
9月1日

【合入来源】

https://gitcode.com/Ascend/pytorch-ecosystem/issues/326
请勿携带内部流程信息(需求链接、问题单、内部issue等)

【修改方案】

请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列
如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容)

  1. API 功能说明:torch.distributed.tensor._utils.compute_local_shape_and_global_offset 是 DTensor 模块下一个纯 Python 实现的内部元数据工具函数。其功能为:给定全局张量的形状、设备网格(DeviceMesh)以及放置策略(placements),计算并推导出当前 rank 对应的局部张量形状(local_shape)以及该数据块在全局张量中的绝对偏移量(global_offset)。
  2. 新增测试用例的原因:经排查 PyTorch 原生测试代码,官方原有的 1D 和 2D 测试用例中仅覆盖了 Shard(切分)和 Replicate(复制)策略,客观上遗漏了对另一合法核心策略 Partial(部分状态)的入参验证。为了保证该 API 在 NPU 环境下的参数完备性与逻辑全覆盖,必须进行用例补齐。
  3. 实现细节:新增 test_compute_local_shape_and_global_offset_partial 测试用例,显式导入 Partial 策略类,并断言验证其推导结果:在 Partial 策略下,局部张量尺寸未发生物理切分(保持等于全局尺寸),且全局偏移坐标全为 0。
  4. 修改test_utils.py存在的bug:

具体的bug:

8558551ab9c0a8c7d41a2985eb87d50a.png

经过检查原第5行中的 torch.distributed._tensor._utils.compute_local_shape 已不存在,其已被替换成 torch.distributed._tensor._utils.compute_local_shape.compute_local_shape_and_global_offset 。

检查结果如下, compute_local_shape 已不存在:

11f8b84b10562fbd42f2525fb789071a.png

torch.distributed._tensor._utils.compute_local_shape.compute_local_shape_and_global_offset函数返回一个元组 (local_shape, global_offset),其中第一个元素 local_shape 与原 torch.distributed._tensor._utils.compute_local_shape 的返回值完全一致。因此通过元组解包 local_shape, _ = compute_local_shape_and_global_offset(...) 丢弃第二个返回值即可。

原来代码:

 local_size = compute_local_shape(size, mesh, placements)

修改为:

local_size, _ = compute_local_shape_and_global_offset(size, mesh, placements)

【资料变更】

请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及”

不涉及。

原因说明:本次测试的目标接口 torch.distributed.tensor._utils.compute_local_shape_and_global_offset 属于带有 _utils 前缀的内部私有 API。上游 PyTorch 官方未对其开放公开的原生独立文档。且该 API 逻辑为纯 Python 的坐标数学推导,不涉及 NPU 底层内存操作或算子调用,无特殊硬件约束,故不涉及对外公开资料的编写与变更。

【接口变更】

请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及”

不涉及。本次提交仅为纯测试层面的用例补充。

【功能验证】

说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤
新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图

测试场景与方法:在分布式环境下(世界大小为 8),测试传入 Partial 策略时的尺寸和偏移量推导是否正确。
执行步骤:

python test_utils.py -v -k test_compute_local_shape_and_global_offset_partial

测试结果:8 个通信进程均正常计算完毕并匹配正确断言,结果为 OK。

910B3的4卡测试自验证截图,无A5多卡环境,无法验证A5支持情况,不再进行A5多卡验证测试:
863f8bf185099906b1026019f599ca1f.png

修改bug之后的函数测试:
执行步骤:

python test_utils.py -v -k test_compute_local_shape_2d_uneven

验证截图:
11f8b84b10562fbd42f2525fb789071a.png

【CheckList】

PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x]

likedislike
Pull Request已成功合入, 合并人@ascend-robot
(感谢 m0_53076416 的贡献)
m0_53076416m0_53076416
9月1日 创建了 pull request,commit 86fd23da
m0_53076416m0_53076416
9月1日 关联了issue:【API一致性任务】8月社区任务第三期-Ascend for PyTorch API 一致性开发(136)
atomgit-bot
atomgit-bot
9月1日 评论:

变更摘要

本 PR 属于 API 一致性任务下的纯测试补充,针对 DTensor 内部元数据工具函数 torch.distributed._tensor._utils.compute_local_shape_and_global_offset 补齐 Partial 放置策略的测试覆盖。官方原有 1D/2D 测试仅覆盖 Shard 与 Replicate 策略,遗漏了合法核心策略 Partial;本 PR 在 test/distributed/_tensor/test_utils.py 中新增测试用例,验证在 Partial 策略下局部张量未发生物理切分(local_size 等于全局形状),且全局偏移坐标全为 0。

主要改动

  • 扩展 Partial 策略导入: 在 test/distributed/_tensor/test_utils.py 中,将导入语句由 from torch.distributed._tensor.placement_types import Replicate, Shard 扩展为同时导入 Partial,为新增用例提供策略类支持。
  • 新增 test_compute_local_shape_and_global_offset_partial 测试用例: 使用 @skipIfUnsupportMultiNPU(4) 与 @with_comms 装饰,在分布式环境下构造 Partial() 放置策略及 DeviceMesh,调用 compute_local_shape_and_global_offset(global_shape, device_mesh, placements)。
  • 断言验证 Partial 策略推导结果: 断言 local_size 与全局形状 global_shape 相等(未切分),并断言 global_offset 为长度等于全局形状维度的全 0 元组,验证偏移量推导逻辑。
likedislike
不准确?
atomgit-bot
atomgit-bot
9月1日 评论:

代码审查

✅ 未发现问题

likedislike
不准确?
ascend-robotascend-robot成员
9月1日 添加了label:ascend-cla/yes
此处折叠了143条消息 查看更多
AtlasAccount
AtlasAccount成员
26 天前 评论:
流水线 PR-pipeline_pytorch#67338 [ commitID:79011655 ] 已完成
>>>代码风格自动修复执行成功(无修复内容)
阶段 任务名 状态 详情
编译构建 Build_X86 ✅ COMPLETED >>>
Build_ARM ✅ COMPLETED >>>
Build_X86_torchair ⚪ IGNORED >>>
Build_ARM_torchair ⚪ IGNORED >>>
patch_test ⚪ IGNORED >>>
Build_X86_213 ⚪ IGNORED >>>
Build_ARM_213 ⚪ IGNORED >>>
恶意代码检查 Antipoison ✅ COMPLETED >>>
编码安全与规范检查 codecheck_pre-commit ✅ COMPLETED >>>
check_error ✅ COMPLETED >>>
lintrunner ✅ COMPLETED >>>
开源片段检查 SCA ✅ COMPLETED >>>
开发者测试 UT_ARM_A3_Part_01 ✅ COMPLETED >>>
UT_ARM_A3_Part_02 ✅ COMPLETED >>>
UT_ARM_A2_Part_01 ✅ COMPLETED >>>
UT_ARM_A2_Part_02 ✅ COMPLETED >>>
UT_ARM_A2_Part_03 ✅ COMPLETED >>>
UT_inductor_Part_01 ⚪ IGNORED >>>
UT_inductor_Part_02 ⚪ IGNORED >>>
UT_inductor_Part_03 ⚪ IGNORED >>>
UT_inductor_Part_04 ⚪ IGNORED >>>
UT_DIST_ARM_Part_01 ✅ COMPLETED >>>
UT_DIST_ARM_Part_02 ✅ COMPLETED >>>
UT_DIST_ARM_Part_03 ✅ COMPLETED >>>
UT_DIST_ARM_Part_04 ✅ COMPLETED >>>
UT_ARM_A2_Select_Part_01 ⚪ IGNORED >>>
UT_ARM_A2_Select_Part_02 ⚪ IGNORED >>>
UT_inductor_Part_213 ⚪ IGNORED >>>
流水线 PR-pipeline_pytorch ✅ COMPLETED >>>
此流水线已支持下列评论快捷指令,仅PR创建者和白名单成员[wujinyuan1, huangjingwei, liangsongwei, yashi999, culechan, Dring, wuyouqi1, L1919_snow, qq_52711437, WhiteNight12, nomiz, xiu_21, ffmh, wanglijun55, hss-shuai, husichao, smallsilly, lanshaozuishuai, jimmyisme1, lzy0920232, alpha-junh, Sunshine_Youngster, wei_zhuoyi, zhangyihuiben, zyw-hw, zzzkeke, rmch, yangch0324, LucciC, AACAES, renyujin, wjlflyer, senzhen-town, pengjingyou, qsc97, limuan, yule100, xiaoqi-zhou, kuhn7, chenxingying, hanye02, zichun_ye, anyrenwei, kkjocker, wangzili121, Lu_G, yvjc, puddingfjz, HandsoemLemon, bigprestigee1, huawuyi, zhenyu10, dairenjie, du-jin-hang, zou-jieyu, adelaideliu, TrHan, wanlinan, Windwindzzz, pengqihw, kisnwang, yuheng_wang, honghao_wang, jizewei, zhangguoguang, sunyu-xuan, chenrayray, hbhu_bin, liujunzhu, c_34, LiNuoh, maoyuanpeng1, zzhongmin, zhaoyu65, bellatan, jiabaolin, zhuofanshen, wencaiwen, lu_zhuge, caoshuyang, molly12, lyx324521, LQ1206, gitcode-chenjiao, cai-weiwei1989, CHDong, ogqin, yuanlipingGit, xuqinglin1, lqz2, zouwei1, chaoluoa, paradox325, jackzhang1116, yaoyao, akh, yujiacheng, dengjie0116, Hubert11111, Shine_Ws, wslhj555, longqiand, OYtao666, JiaqingQiang, luyyyy, Kingbelial, zhanghaiyu0101, wenxp1018, yanliu-luoluo, ksun_sekiro, liyong328, wgzheng, tangky, vivi_is_coding, aoiaoisola, weixin_44494597, wangmengmengwang65667, hid57809721, qq_35468730, comeonup, C547032, gcw_m5OQChA4, yao_yao_ling_xian, cnnbwcy, szqfes_12, cora_19, cann_lilin, can, shawnylee233, fanglanyue0916, hhz0, LiNuohang, taohuoquan, Jesse, WSs_321, SCh_zx]评论有效
  • compile、compile_inductor、compile_torchair : 运行流水线
  • retry : 重试流水线所有失败子任务
  • retry <任务名> : 仅重试指定失败子任务
  • stop : 停止流水线
likedislike
chengpeng25成员
24 天前 评论:

/approve

likedislike
ascend-robotascend-robot成员
24 天前 添加了label:approvedlgtm
ascend-robotascend-robot成员
24 天前 合入了pull request
AtlasAccount
AtlasAccount成员
24 天前 评论:
流水线 pytorch_gitcode_PR_multiVersion#15249 [ commitID:79011655 ] 已完成
likedislike