feat(c_shim): version AOTInductor NPU C shim header per torch version
Co-authored-by: chz34<chenhaozhe1@huawei.com> # message auto-generated for no-merge-commit merge: !40590 merge compat/c-shim into master feat(c_shim): version AOTInductor NPU C shim header per torch version Created-by: c_34 Commit-by: chz34 Merged-by: ascend-robot Description: <!-- PR描述模板更新日期:20260203 --> # 【合入来源】 > <font color="red">**如有社区issue,请关联issue链接**</font>\ > <font color="red">**请勿携带内部流程信息(需求链接、问题单、内部issue等)**</font> - [ ] 需求 - [ ] 问题单 - [x] issue/工单 https://gitcode.com/Ascend/pytorch/issues/2568 - [x] 重构优化 - [ ] 资料更新 # 【修改方案】 > 请描述修改内容的具体实现,涉及哪些组件之间进行交互,可以用1、2、3、...进行罗列\ > 如果是需求或者重构类的PR,需要补充详细设计文档(说明上下游组件关系、时序图、类图、DFX能力等内容) 根据多个版本分别在不同的目录存储`c_shim_npu.h`,构建过程中根据构建命令(实际参考version.txt)选择不同的版本进行校验。 构建中固定生成一份新的`c_shim_npu.h`文件,存放在原有的位置。 对于开发中的上游最新版本,对应版本目录缺失或者文件缺失,进行软报错,打印提示但不中断构建,用于放行上游的主动改变。 对于已经稳定的上游版本,根据对应目录进行强校验,不一致则拦截,主要控制PTA与op-plugin的内部修改。 详情参考issue内容:https://gitcode.com/Ascend/pytorch/issues/2568 # 【资料变更】 > 请确认是否涉及资料变更。如涉及,需要在PR中体现,并简要说明修改内容。如不涉及,需填写“不涉及” 不涉及 # 【接口变更】 > 请确认是否涉及跨代码仓或者客户面可见的接口变更。如涉及,需要详细说明接口以及对应的变更内容,同时需要在资料中体现。如不涉及,需填写“不涉及” 不涉及 # 【功能验证】 > 说明测试场景,测试方法。如果本次测试方式与常规单元测试不同,请详细说明您的测试步骤\ > 新增/变更内容是否已新增/适配UT测试用例看护,并补充测试自验证截图 针对现有功能的多版本兼容重构,基于现有用例进行看护 # 【CheckList】 > PR提交人对以下CheckList自检项进行全量自检,自检通过或不涉及,均修改 [ ] 为 [x] - [x] 代码注释完备,正确记录错误日志 - [x] 代码实现进行了返回值、空指针等校验 - [x] PR标题正确使用类型标签,如:feat、fix、refactor、docs、test等 - [x] PR持续集成流水线(CI)执行通过,代码检查无异常 See merge request: Ascend/pytorch!40590
This commit is contained in:
@@ -160,6 +160,12 @@ cmake-build-debug
|
||||
#
|
||||
# Below files are not deleted by "setup.py clean".
|
||||
|
||||
# AOTInductor NPU C shim: canonical header is a build-time copy of the
|
||||
# versioned snapshot in aoti_torch/generated/<v2rXX>/. The versioned snapshots
|
||||
# are git-tracked; this canonical copy is regenerated per build. See
|
||||
# docs/proposal_c_shim_multi_version.md (scheme 2).
|
||||
torch_npu/csrc/inductor/aoti_torch/generated/c_shim_npu.h
|
||||
|
||||
# Visual Studio Code files
|
||||
.vscode
|
||||
.vs
|
||||
|
||||
+2
-1
@@ -62,7 +62,8 @@ ${python_execute} -m torchnpugen.gen_backend_stubs \
|
||||
--source_yaml="$source_yaml" \
|
||||
--impl_path="$CDIR/torch_npu/csrc/aten" \
|
||||
--op_plugin_impl_path="$CDIR/third_party/op-plugin/op_plugin/ops" \
|
||||
--op_plugin_yaml_path="$op_plugin_config_path/op_plugin_functions.yaml"
|
||||
--op_plugin_yaml_path="$op_plugin_config_path/op_plugin_functions.yaml" \
|
||||
--pytorch_version_dir="$pytorch_dir"
|
||||
|
||||
${python_execute} -m torchnpugen.autograd.gen_autograd \
|
||||
--out_dir="$CDIR/torch_npu/csrc/aten" \
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
# v2r14 (PyTorch 2.14) AOTInductor C shim snapshot — under development.
|
||||
#
|
||||
# The c_shim_npu.h ABI snapshot for this track has not been seeded yet.
|
||||
# Until it is, building against torch 2.14 soft-skips the ABI watchdog
|
||||
# (see torchnpugen/gen_npu_c_shim.py). Seed it by running codegen in a
|
||||
# torch 2.14 environment with --update-aoti-c-shim, then commit the
|
||||
# generated v2r14/c_shim_npu.h and delete this placeholder.
|
||||
@@ -704,6 +704,15 @@ def main() -> None:
|
||||
help="Update AOTInductor C shim after adding an entry to inductor_fallback_ops in torchgen/aoti/fallback_ops.py. "
|
||||
"WARNING: Do not use this unless you are sure what you are doing!!!",
|
||||
)
|
||||
parser.add_argument(
|
||||
"--pytorch_version_dir",
|
||||
type=str,
|
||||
default=None,
|
||||
help="PyTorch version directory tag (e.g. v2r13) used to select/store the "
|
||||
"versioned AOTInductor C shim header under aoti_torch/generated/<tag>/. "
|
||||
"When omitted, the legacy single-header layout "
|
||||
"(aoti_torch/generated/c_shim_npu.h) is used.",
|
||||
)
|
||||
options = parser.parse_args()
|
||||
|
||||
run(
|
||||
@@ -714,6 +723,7 @@ def main() -> None:
|
||||
options.op_plugin_impl_path,
|
||||
options.op_plugin_yaml_path,
|
||||
options.update_aoti_c_shim,
|
||||
options.pytorch_version_dir,
|
||||
)
|
||||
|
||||
|
||||
@@ -1600,6 +1610,7 @@ def run(
|
||||
op_plugin_impl_path: str | None,
|
||||
op_plugin_yaml_path: str | None,
|
||||
update_aoti_c_shim: bool,
|
||||
pytorch_version_dir: str | None,
|
||||
) -> None:
|
||||
rename_privateuse1_dispatch_key()
|
||||
torchgen_path = get_torchgen_dir()
|
||||
@@ -1806,6 +1817,7 @@ def run(
|
||||
[backend_dispatch_key],
|
||||
structured_native_functions,
|
||||
update_aoti_c_shim,
|
||||
pytorch_version_dir,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -220,6 +220,60 @@ def gen_npu_c_shim(
|
||||
)
|
||||
|
||||
|
||||
def _check_c_shim_header_unchanged(
|
||||
expected_path: str,
|
||||
header_content: str,
|
||||
update_hint: str,
|
||||
) -> None:
|
||||
"""Watchdog: compare the committed header against the freshly generated one.
|
||||
|
||||
On mismatch this raises a RuntimeError describing the ABI breakage; on a
|
||||
missing expected file it prints a notice and returns (so a brand new track
|
||||
can be bootstrapped with --update-aoti-c-shim). See
|
||||
docs/proposal_c_shim_multi_version.md (scheme 2) for the versioned layout.
|
||||
"""
|
||||
try:
|
||||
with open(expected_path) as old_file:
|
||||
old_header = old_file.read()
|
||||
except FileNotFoundError:
|
||||
print(
|
||||
f"[torchnpugen] {expected_path} not found; skipping ABI watchdog. "
|
||||
f"Run codegen with --update-aoti-c-shim to create the tracked snapshot."
|
||||
)
|
||||
return
|
||||
if old_header == header_content:
|
||||
return
|
||||
diff = "\n".join(
|
||||
difflib.unified_diff(
|
||||
old_header.splitlines(),
|
||||
header_content.splitlines(),
|
||||
fromfile=f"expected ({expected_path})",
|
||||
tofile="actual",
|
||||
lineterm="",
|
||||
)
|
||||
)
|
||||
raise RuntimeError(f"""
|
||||
The generated AOTInductor C shim header has unexpectedly changed. This
|
||||
indicates an AOTInductor fallback operator ABI backward compatibility breakage!!!
|
||||
|
||||
Expected (committed): {expected_path}
|
||||
|
||||
1. You added a fallback op to the inductor_fallback_ops list in
|
||||
torchgen/aoti/fallback_ops.py. If that's the case, run codegen with
|
||||
--update-aoti-c-shim to add a new entry to the C shim header file.
|
||||
|
||||
2. You added a new default argument to an existing fallback op. This is clearly
|
||||
a BC breaking change in the AOTInductor land. You need to annotate the new
|
||||
default argument in torchgen/aoti/fallback_ops.py, and then run codegen with
|
||||
--update-aoti-c-shim to update the C shim header file by creating different
|
||||
versions of the fallback op.
|
||||
|
||||
{update_hint}
|
||||
|
||||
{diff}
|
||||
""")
|
||||
|
||||
|
||||
def gen_npu_c_shim_files(
|
||||
aoti_fm: FileManager,
|
||||
native_functions: Sequence[NativeFunction],
|
||||
@@ -227,6 +281,7 @@ def gen_npu_c_shim_files(
|
||||
dispatch_keys: Sequence[DispatchKey],
|
||||
structured_native_functions: Sequence[NativeFunctionsGroup],
|
||||
update_aoti_c_shim: bool,
|
||||
pytorch_version_dir: str | None = None,
|
||||
) -> None:
|
||||
global NPU_DISPATCH_KEYS
|
||||
NPU_DISPATCH_KEYS = dispatch_keys
|
||||
@@ -286,39 +341,37 @@ def gen_npu_c_shim_files(
|
||||
header_filename = f"c_shim_{NPU_DEVICE}.h"
|
||||
cpp_filename = f"c_shim_{NPU_DEVICE}.cpp"
|
||||
|
||||
if update_aoti_c_shim:
|
||||
aoti_fm.write(header_filename, lambda: header_content)
|
||||
if pytorch_version_dir:
|
||||
# Scheme 2: per-torch-version tracked snapshots live under
|
||||
# generated/<pytorch_version_dir>/. The versioned header is the ABI
|
||||
# source of truth (watchdog-validated, only updated via
|
||||
# --update-aoti-c-shim); the canonical generated/c_shim_npu.h is a
|
||||
# build-time copy matching the torch version being compiled, so the
|
||||
# shipped wheel keeps a single stable include path regardless of
|
||||
# which version was built.
|
||||
versioned_filename = f"{pytorch_version_dir}/{header_filename}"
|
||||
versioned_path = os.path.join(aoti_fm.install_dir, versioned_filename)
|
||||
if update_aoti_c_shim:
|
||||
aoti_fm.write(versioned_filename, lambda: header_content)
|
||||
print(f"[torchnpugen] Updated {versioned_filename}")
|
||||
else:
|
||||
_check_c_shim_header_unchanged(
|
||||
versioned_path,
|
||||
header_content,
|
||||
f"Update the {versioned_filename} snapshot for the "
|
||||
f"{pytorch_version_dir} track.",
|
||||
)
|
||||
else:
|
||||
try:
|
||||
with open(os.path.join(aoti_fm.install_dir, header_filename)) as old_file:
|
||||
old_header = old_file.read()
|
||||
if old_header != header_content:
|
||||
diff = "\n".join(
|
||||
difflib.unified_diff(
|
||||
old_header.splitlines(),
|
||||
header_content.splitlines(),
|
||||
fromfile="expected",
|
||||
tofile="actual",
|
||||
lineterm="",
|
||||
)
|
||||
)
|
||||
raise RuntimeError(f"""
|
||||
The generated AOTInductor C shim header files have unexpectedly changed. This
|
||||
indicates an AOTInductor fallback operator ABI backward compatibility breakage!!!
|
||||
# Legacy path (no --pytorch_version_dir): a single canonical header
|
||||
# is the tracked source of truth and is validated in place.
|
||||
canonical_path = os.path.join(aoti_fm.install_dir, header_filename)
|
||||
if not update_aoti_c_shim:
|
||||
_check_c_shim_header_unchanged(canonical_path, header_content, "")
|
||||
|
||||
1. You added a fallback op to the inductor_fallback_ops list in torchgen/aoti/fallback_ops.py.
|
||||
If that's the case, run codegen with --update-aoti-c-shim to add a new entry to
|
||||
existing C shim header files.
|
||||
|
||||
2. You added a new default argument to an existing fallback op. This is clearly a BC breaking
|
||||
change in the AOTInductor land. You need to annotate the new default argument in
|
||||
torchgen/aoti/fallback_ops.py, and then run codegen with --update-aoti-c-shim to
|
||||
update the C shim header files by creating different versions of the fallback op.
|
||||
|
||||
{diff}
|
||||
""")
|
||||
except FileNotFoundError:
|
||||
print(f"{os.path.join(aoti_fm.install_dir, header_filename)} not found")
|
||||
# Always materialize the canonical build header for this torch version.
|
||||
# In versioned mode this is the per-build copy; in legacy mode this is a
|
||||
# no-op refresh when the watchdog already validated an identical file.
|
||||
aoti_fm.write(header_filename, lambda: header_content)
|
||||
|
||||
aoti_fm.write(cpp_filename, lambda: cpp_content)
|
||||
|
||||
|
||||
Reference in New Issue
Block a user