Files
TorchNPU/pyproject.toml
T
zhenyu10 0a03ffb465 Optimize the patch for FSDP
Co-authored-by: zhenyu10<shizhenyu5@huawei.com>



# message auto-generated for no-merge-commit merge:
!34632 merge master into master

Optimize the patch for FSDP

Created-by: zhenyu10
Commit-by: zhenyu10
Merged-by: ascend-robot
Description: <!--
PR描述模板更新日期:20260203
-->

# 【合入来源】
> <font color="red">**如有社区issue,请关联issue链接**</font>\
> <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font>

- [x] 需求
- [ ] 问题单
- [ ]  issue/工单
- [ ] 重构优化
- [ ] 资料更新

社区 issue:https://gitcode.com/Ascend/pytorch/issues/1788

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

采用“上层复用原生,下层补齐后端语义”的设计:

1. torch-npu 删除 FSDP2 collectives patch:
   - 不再覆写 `_get_param_all_gather_inputs`
   - 不再覆写 `torch.ops.fsdp.all_gather_copy_in`
   - 不再覆写 `FSDPParamGroup.finalize_backward`
   - `fully_shard()` 入口只保留 NPU 侧增强 patch,例如内存缓存和 recompute/prefetch 状态管理

2. op-plugin 在 foreach copy 算子中对齐同设备 D2D fast path:
   - fast path 判定显式使用 PyTorch foreach 工具函数:
     - `at::native::can_use_fast_route(self, src)`
     - `check_tensor_dtype_support_base(src)`
   - `can_use_fast_route(self, src)` 本身会检查 `self/src` dtype 一致,因此 dtype 不同不会进入 NPU fast path,保持当前 NPU 不支持跨 dtype fast path 的既有语义。
   - 命中 fast path 时调用:

```cpp
split_and_exec_npu_cmd_copy(self, src, /*non_blocking=*/true);
```

3. fallback 路径保持调用方传入语义:
   - 未命中 fast path 时,`memcpyBatch(self, src, non_blocking)` 不变
   - slow path `foreach_tensor_copy_list_kernel_slow_(self, src, non_blocking)` 不变
   - `DO_COMPATIBILITY` 回退仍使用原始 `non_blocking`

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

不涉及。

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

不涉及。无新增客户可见接口;FSDP 对外使用方式不变。`_foreach_copy_` 的 NPU 后端实现由 op-plugin 承接,不新增 Python 侧公开接口。

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

测试场景:
1. FSDP collectives 覆写删除后,`fully_shard` 继续复用上游 PyTorch 原生 `finalize_backward`、`_get_param_all_gather_inputs`、`all_gather_copy_in` 主流程;
2. NPU 侧 `_foreach_copy_` 在 op-plugin 中优先走 D2D fast path,不满足条件时回退到已有 batch copy / slow path;
3. 跨 dtype NPU D2D copy 场景在异步排队后结果正确。

测试方法:
- op-plugin 用例:
  - `python test/test_v2r1_ops/test_foreach_copy.py`
  - `python test/test_v2r2_ops/test_foreach_copy.py`
- torch-npu 侧建议验证 FSDP fully-shard 训练主路径,功能科通过仓内测试用例。

执行结果:
- 已完成代码检查与 push hook 校验通过。
- NPU 环境 UT 结果请以当前 CI/转测执行结果为准。

UT看护:
- 已适配 op-plugin foreach copy 用例,在跨 dtype copy 断言前增加 `torch.npu.synchronize()`,覆盖新增异步 D2D fast path 行为。

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

- [x] 代码注释完备,正确记录错误日志
- [x] 代码实现进行了返回值、空指针等校验
- [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等
- [x] PR持续集成流水线(CI)执行通过,代码检查无异常

See merge request: Ascend/pytorch!34632
2026-05-25 14:25:16 +08:00

299 lines
7.9 KiB
TOML

# Package ######################################################################
[project]
name = "torch_npu"
dynamic = [
"classifiers",
"entry-points",
"dependencies",
"scripts",
"version",
"description",
"readme",
"license",
]
# Linter tools #################################################################
[tool.isort]
src_paths = ["torch_npu", "torchnpugen", "test"]
extra_standard_library = ["typing_extensions"]
skip_gitignore = true
skip_glob = ["third_party/*"]
atomic = true
profile = "black"
indent = 4
line_length = 88
lines_after_imports = 2
multi_line_output = 3
include_trailing_comma = true
combine_as_imports = true
[tool.usort]
preserve_inline_comments = true
collapse_blank_lines_in_category = false
[tool.usort.known]
first_party = ["torch_npu", "torchnpugen", "test"]
standard_library = ["typing_extensions"]
[tool.ruff]
line-length = 88
src = ["caffe2", "torch", "torchgen", "functorch", "test"]
extend-exclude = ["third_party", "test/dynamo/cpython", "test/npu/test_fault_mode.py"]
[tool.ruff.per-file-target-version]
"**/py312_intrinsics.py" = "py312"
[tool.ruff.format]
docstring-code-format = true
quote-style = "double"
[tool.ruff.lint]
# NOTE: Synchoronize the ignores with .flake8
external = [
"B001",
"B902",
"B950",
"E121",
"E122",
"E128",
"E131",
"E704",
"E723",
"F723",
"F812",
"P201",
"P204",
"T484",
"TOR901",
]
ignore = [
# these ignores are from flake8-bugbear; please fix!
"B007", "B008", "B017",
"B018", # Useless expression
"B023",
"B028", # No explicit `stacklevel` keyword argument found
"E402",
"C408", # C408 ignored because we like the dict keyword argument syntax
"E501", # E501 is not flexible enough, we're using B950 instead
"E741",
"EXE001",
"F405",
"FURB122", # writelines
# these ignores are from ruff NPY; please fix!
"NPY002",
# these ignores are from ruff PERF; please fix!
"PERF203",
"PERF401",
# these ignores are from PYI; please fix!
"PYI024",
"PYI036",
"PYI041",
"PYI056",
"SIM102", "SIM103", "SIM112", # flake8-simplify code styles
"SIM105", # these ignores are from flake8-simplify. please fix or ignore with commented reason
"SIM108", # SIM108 ignored because we prefer if-else-block instead of ternary expression
"SIM110", # Checks for for loops that can be replaced with a builtin function, like any or all.
"SIM114", # Combine `if` branches using logical `or` operator
"SIM116", # Disable Use a dictionary instead of consecutive `if` statements
"SIM117",
"SIM300", # Yoda condition detected
"TC006",
# TODO: Remove Python-3.10 specific suppressions
"B905",
]
select = [
"B",
"B904", # Re-raised error without specifying the cause via the from keyword
"C4",
"G",
"E",
"EXE",
"F",
"SIM",
"W",
# Not included in flake8
"FURB",
"LOG",
"NPY",
"PERF",
"PGH004",
"PIE",
"PLC0131", # type bivariance
"PLC0132", # type param mismatch
"PLC1802", # len({expression}) used as condition without comparison
"PLC0205", # string as __slots__
"PLC3002", # unnecessary-direct-lambda-call
"PLC0414", # Import alias does not rename original package
"PLE",
"PLR0133", # constant comparison
"PLR0206", # property with params
"PLR1722", # use sys exit
"PLR1736", # unnecessary list index
"PLW0127", # Self-assignment of variable
"PLW0129", # assert on string literal
"PLW0131", # named expr without context
"PLW0133", # useless exception statement
"PLW0245", # super without brackets
"PLW0406", # import self
"PLW0711", # binary op exception
"PLW1501", # bad open mode
"PLW1507", # shallow copy os.environ
"PLW1509", # preexec_fn not safe with threads
"PLW2101", # useless lock statement
"PLW3301", # nested min max
"PT006", # TODO: enable more PT rules
"PT014", # duplicate parameterize case
"PT022",
"PT023",
"PT024",
"PT025",
"PT026",
"PYI",
"Q003", # avoidable escaped quote
"Q004", # unnecessary escaped quote
"RSE",
"RUF007", # pairwise over zip
"RUF008", # mutable dataclass default
"RUF013", # ban implicit optional
"RUF015", # access first ele in constant time
"RUF016", # type error non-integer index
"RUF017",
"RUF018", # no assignment in assert
"RUF019", # unnecessary-key-check
"RUF020", # never union
"RUF024", # from keys mutable
"RUF026", # default factory kwarg
"RUF030", # No print statement in assert
"RUF033", # default values __post_init__ dataclass
"RUF041", # simplify nested Literal
"RUF048", # properly parse `__version__`
"RUF200", # validate pyproject.toml
"S324", # for hashlib FIPS compliance
"SLOT",
"TC",
"TRY002", # ban vanilla raise (todo fix NOQAs)
"TRY203",
"TRY401", # verbose-log-message
"UP",
"YTT",
"S101",
]
[tool.ruff.lint.per-file-ignores]
"torch_npu/distributed/fsdp/_add_fsdp_patch.py" = [
"UP007",
"UP045",
]
"__init__.py" = [
"F401",
]
"*.pyi" = [
"PYI011", # typed-argument-default-in-stub
"PYI021", # docstring-in-stub
"PYI053", # string-or-bytes-too-long
]
# upstream community path (not present in torch_npu)
# "functorch/docs/source/tutorials/**" = [
# "F401",
# ]
# upstream community path (not present in torch_npu)
# "test/export/**" = [
# "PGH004",
# ]
# upstream community path (not present in torch_npu)
# "test/typing/**" = [
# "PGH004"
# ]
# upstream community path (not present in torch_npu)
# "test/typing/reveal/**" = [
# "F821",
# ]
# upstream community path (not present in torch_npu)
# "test/torch_np/numpy_tests/**" = [
# "F821",
# "NPY201",
# ]
# upstream community path (not present in torch_npu)
# "test/dynamo/test_bytecode_utils.py" = [
# "F821",
# ]
"test/dynamo/test_debug_utils.py" = [
"UP037",
]
"test/dynamo/test_misc.py" = [
"PGH004",
]
"test/jit/**" = [
"PLR0133", # tests require this for JIT
"PYI",
"RUF015",
"UP", # We don't want to modify the jit test as they test specify syntax
]
"test/test_jit.py" = [
"PLR0133", # tests require this for JIT
"PYI",
"RUF015",
"UP", # We don't want to modify the jit test as they test specify syntax
]
"test/**" = ["S101"]
# upstream community path (not present in torch_npu)
# "test/inductor/s429861_repro.py" = [
# "PGH004",
# ]
# upstream community path (not present in torch_npu)
# "test/inductor/test_torchinductor.py" = [
# "UP037",
# ]
# autogenerated #TODO figure out why file level noqa is ignored
# upstream community paths (not present in torch_npu)
# "torch/_appdirs.py" = ["PGH004"]
# "torch/jit/_shape_functions.py" = ["PGH004"]
# "torch/_inductor/fx_passes/serialized_patterns/**" = ["F401", "F501"]
# "torch/_inductor/autoheuristic/artifacts/**" = ["F401", "F501"]
"torch_npu/_inductor/codegen/**" = [
"PGH004"
]
# upstream community path (not present in torch_npu)
# "torchgen/api/types/__init__.py" = [
# "F401",
# "F403",
# ]
"torch_npu/utils/collect_env.py" = [
"UP", # collect_env.py needs to work with older versions of Python
]
# upstream community path (not present in torch_npu)
# "torch/_vendor/**" = [
# "UP", # No need to mess with _vendor
# ]
"tools/linter/**" = [
"LOG015" # please fix
]
# torch_npu/ folders still needing S101 migration
# upstream community path (not present in torch_npu)
# "torch/_dynamo/**" = ["S101"]
"torch_npu/_inductor/**" = ["S101"]
[tool.codespell]
ignore-words = "tools/linter/dictionary.txt"
[tool.spin]
package = 'torch_npu'
[tool.spin.commands]
# upstream community paths (`.spin/cmds.py` not present in torch_npu)
"Build" = [
".spin/cmds.py:lint",
".spin/cmds.py:fixlint",
".spin/cmds.py:quicklint",
".spin/cmds.py:quickfix",
]
"Regenerate" = [
".spin/cmds.py:regenerate_version",
".spin/cmds.py:regenerate_type_stubs",
".spin/cmds.py:regenerate_clangtidy_files",
".spin/cmds.py:regenerate_github_workflows",
]