← 返回列表
全部 Skill›DevOps›add-op-overload

add-op-overload

作者 @pytorch · 语言 en
Checklist for adding a new overload (e.g. `mm.dtype`, `addmm.compute_mode`) to an existing ATen op in native_functions.yaml. Use before the first rebuild when adding an overload or new kwarg variant of an existing operator, so every rebuild-triggering edit lands in one build and packet-level Python registrations (meta, decompositions, Inductor lowerings, DTensor) don't silently capture the new overload.
DevOpsGit 工作流通用执行命令待翻译
最近源活动
—
检测 SKILL.md 语言
en
星标 ★
105,722
分支 Fork
0
安装选项
提示
指令
使用 AI 助手安装
将下面的提示复制到你正在使用的 AI 助手中。
下载完整 Skill 目录(含全部配套文件)。 下载压缩包 ↓
SKILL.md 原文预览

Adding an overload to an existing ATen op

Editing native_functions.yaml regenerates headers that most of the tree includes, so it is

close to a full rebuild; so is editing ATen/Context.h (e.g. to add a global setting).

autocast_mode.h reaches only about a dozen translation units, but forgetting it still costs

another build, relink and test cycle. Each missed item below costs a rebuild or ships a

silent bug. Finish all of Part 1 before starting the first build.

Find the closest precedent first and copy its footprint. For matmul ops, that's the

out_dtype overloads (grep -n out_dtype aten/src/ATen/native/native_functions.yaml).

git log --oneline -S'<op>.<overload>' -- aten/src/ATen/native/native_functions.yaml finds

the commit that added it; git show --stat <sha> lists every file it touched.

Part 1: C++ and codegen (needs a rebuild)

  1. Schema (native_functions.yaml): add the functional overload and its _out

overload, each with a new, unique overload name. A thin wrapper that sets state and calls

the regular op should use CompositeExplicitAutograd, so it stays visible to autograd,

tracing and export instead of decomposing away.

  1. C++ overload resolution. The C++ API flattens kwarg-only arguments into positional

parameters, so the new argument lands where existing callers pass beta/alpha

positionally, e.g. at::addmm_out(result, t, sparse, dense, 0, 1) in

aten/src/ATen/native/sparse/SparseTensorMath.cpp.

  • str (std::string_view): the literal 0 converts to it as a null pointer, so the

call becomes ambiguous and the build fails late.

  • bool / int: 0 converts by a standard conversion, which beats Scalar's

constructor, so the call silently binds to the new overload.

Fix: put cpp_no_default_args: ['beta', 'alpha'] (the trailing defaulted args) on the

new overloads. Check every in-tree C++ call of the op against the new signature. If the

op previously had a single C++ overload, code that takes its address (at::mm passed

as a function pointer) also stops compiling; wrap it in a lambda.

  1. Autograd (tools/autograd/derivatives.yaml): a CompositeExplicitAutograd

overload needs its own entry; it does not inherit the base op's. If backward should

keep the new argument's semantics, call the same overload there and thread the argument

through the helpers in torch/csrc/autograd/FunctionsManual.{h,cpp}.

  1. Autocast lists are per-overload. If the base op is in AT_FORALL_LOWER_PRECISION_FP,

AT_FORALL_FP32, etc. in aten/src/ATen/autocast_mode.h, or in the KERNEL_CPU(...) /

KERNEL_MPS(...) lists in aten/src/ATen/autocast_mode.cpp, add _(op, overload) /

KERNEL_CPU(op, overload, policy) with the same policy. Without this, the wrapper's

inner call is autocast but its autograd node is not: forward runs in low precision and

backward fails with mixed dtypes (expected mat1 and mat2 to have the same dtype).

  1. Other per-overload C++ tables

(grep -rnE '"(aten::)?<op>[."]|VMAP_SUPPORT\(<op>,' aten/src/ATen --include=*.cpp):

  • vmap batch rules (VMAP_SUPPORT / m.impl in aten/src/ATen/functorch/BatchRules*.cpp).

Missing means the slow per-example fallback.

  • Conjugate fallthroughs in aten/src/ATen/ConjugateFallback.cpp. Missing means conj

inputs get materialized first.

Part 2: Python (no rebuild needed)

  1. Packet-level registrations capture the new overload. Registering on an

OpOverloadPacket (aten.op rather than aten.op.default) expands to *every* overload,

including yours, and then silently drops the new argument. Audit:

```bash

grep -rnE "aten\.<op>\b[^.]" torch --include=*.py | grep -E "register|lowering|decomp|fallback|flop"

```

Typical hits: register_meta(aten.op) in torch/_meta_registrations.py,

register_decomposition([aten.op]) in torch/_decomp/decompositions.py and

torch/_inductor/decomposition.py, and Inductor register_lowering(aten.op) in

torch/_inductor/kernel/. Narrow each one to explicit overloads

(aten.op.default, aten.op.out) unless it really handles the new argument. Adding your

own registration while a packet one exists fails with duplicate registrations for ....

torch/utils/flop_counter.py looks formulas up by packet, so it can't be narrowed;

check that the formula's *args, **kwargs accept the new overload's arguments.

  1. Inductor: to run the overload eagerly, add make_fallback(aten.op.overload) in

torch/_inductor/lowering.py. It must run before import_submodule(kernel) at the end

of that file, because get_overloads only expands a packet to overloads that don't

have a lowering yet. make_fallback asserts if a decomposition still covers the

overload, so narrow the decompositions first.

  1. DTensor: sharding strategies are per-overload (e.g.

register_single_dim_strategy(aten.mm.default) in

torch/distributed/tensor/_ops/_matrix_ops.py). Without one, DTensor uses

decomposition_table if it covers the overload (dropping the argument if a packet

decomposition captured it), and otherwise raises `does not have a sharding strategy

registered`.

  1. Expect files: EXPECTTEST_ACCEPT=1 python test/test_decomp.py -k HasDecomp. The diff

should show only the new overloads.

  1. Signatures and docs: update the get_testing_overrides() lambdas in

torch/overrides.py if the Python signature gains a parameter. In

torch/_torch_docs.py

评论
Ppytorch
pytorch
pytorch/pytorch
查看 GitHub 仓库 查看作者的其他 Skill
来源信息
作者@pytorch
所属大类DevOps / Git 工作流
国内可用性通用
最近源活动—
风险提示执行命令
提示:风险标签为程序静态扫描结果,仅作提示,不代表安全认证。安装前请阅读 SKILL.md 及其引用的脚本、权限,自行判断是否可信。