Convert PyTorch AT_DISPATCH macros to AT_DISPATCH_V2 format in ATen C++ code. Use when porting AT_DISPATCH_ALL_TYPES_AND*, AT_DISPATCH_FLOATING_TYPES*, or other dispatch macros to the new v2 API. For ATen kernel files, CUDA kernels, and native operator implementations.
we've indexed the metadata for this skill but the body is fetched on demand. click "view source" above to read the canonical SKILL.md on smithery, or "run inline in claude" to apply it without leaving your session.
read on smitherydon't have the plugin yet? install it then click "run inline in claude" again.
added explicit inputs (source file, dispatch_v2.h reference, scalar_type expression, lambda body, type list), numbered procedure steps with input/output pairs, decision points for macro arity, edge cases (no arity, compound macros, multi-line lambdas), and outcome signal describing user-facing verification steps; preserved all original transformations and examples.
convert legacy pytorch at_dispatch macros to the new at_dispatch_v2 format defined in aten/src/ATen/Dispatch_v2.h. use this skill when porting aten kernels, cuda kernels, or native operator implementations that rely on at_dispatch_all_types_and*, at_dispatch_floating_types*, or similar dispatch macros. the v2 api removes arity from macro names (no more and2, and3, and4 variants) and allows composable type group expansion via at_expand().
aten/src/ATen/native/, aten/src/ATen/native/cuda/, or similar)aten/src/ATen/Dispatch_v2.h containing the new macro definitions and type group references (at_all_types, at_floating_types, at_complex_types, at_integral_types, etc.)iter.dtype(), tensor.scalar_type(), dtype) passed to the old macroscan the file for patterns matching at_dispatch_* (case-insensitive in practice). note the exact macro name (e.g., at_dispatch_all_types_and2, at_dispatch_floating_types_and3, at_dispatch_all_types_and_complex_and2).
output of step 1: a list of line numbers and macro names to convert.
if not already present, add #include <ATen/Dispatch_v2.h> immediately after or near the existing #include <ATen/Dispatch.h> line. keep the old include in place; other code may depend on it.
input: file location and existing includes section. output: file with the new include directive inserted.
extract:
all_types, floating_types, integral_types, all_types_and_complexscalar_type (e.g., khalf, kbfloat16, kbool)input: a single at_dispatch_* call (multi-line or single-line). output: structured breakdown of the five components above.
use the reference table below to determine which type group(s) to use:
| old macro base | at_dispatch_v2 type group |
|---|---|
| all_types | at_expand(at_all_types) |
| floating_types | at_expand(at_floating_types) |
| integral_types | at_expand(at_integral_types) |
| complex_types | at_expand(at_complex_types) |
| all_types_and_complex | at_expand(at_all_types), at_expand(at_complex_types) |
| floating_and_complex_types | at_expand(at_floating_types), at_expand(at_complex_types) |
for compound macros (e.g., at_dispatch_all_types_and_complex_and2), include both the at_all_types and at_complex_types groups.
input: the base macro name from step 3. output: one or more at_expand(group) tokens.
assemble the new call with this argument order:
AT_DISPATCH_V2(
scalar_type,
"name",
AT_WRAP(lambda),
type_groups_with_at_expand,
individual_types_without_at_expand
)
input: components from steps 3 and 4. output: a single at_dispatch_v2(...) call formatted across multiple lines for readability.
enclose the entire lambda (from [...]() { to the final }) in at_wrap(...). this prevents internal commas or complex expressions from breaking macro parsing.
input: the lambda body from step 3. output: at_wrap(...{ ... }).
replace the old at_dispatch_* call with the new at_dispatch_v2 call using text substitution. preserve all whitespace, indentation, and surrounding code.
input: file path, old call text, new call text. output: file with the macro call replaced.
iterate through the list from step 1 until all calls are converted.
input: remaining at_dispatch_* calls. output: fully converted file.
check:
input: converted file. output: confirmation of correctness, or a list of issues to fix.
if the old macro has an arity suffix (and2, and3, etc.): extract all the individual types from before the scalar_type argument and add them as trailing arguments to at_dispatch_v2 after the type group(s). each type becomes a separate argument; do not wrap individual types in at_expand().
if the old macro has no arity suffix (e.g., at_dispatch_all_types): there are no individual types to extract. proceed directly to constructing at_dispatch_v2 with only type groups. example:
// before
AT_DISPATCH_ALL_TYPES(dtype, "op", [&]() { kernel<scalar_t>(); });
// after
AT_DISPATCH_V2(dtype, "op", AT_WRAP([&]() { kernel<scalar_t>(); }), AT_EXPAND(AT_ALL_TYPES));
if the lambda contains commas (function calls, nested structures, etc.): mandatory use of at_wrap(). without it, the preprocessor will misparse the commas as macro argument separators.
if the base macro name includes both "and_complex" and an arity suffix (e.g., at_dispatch_all_types_and_complex_and2): include multiple at_expand() calls for both at_all_types and at_complex_types, then list the individual types. example:
// before
AT_DISPATCH_ALL_TYPES_AND_COMPLEX_AND2(kComplexHalf, kHalf, dtype, "op", [&]() {});
// after
AT_DISPATCH_V2(dtype, "op", AT_WRAP([&]() {}), AT_EXPAND(AT_ALL_TYPES), AT_EXPAND(AT_COMPLEX_TYPES), kComplexHalf, kHalf);
if the target file does not already include dispatch_v2.h: add it near the existing dispatch.h include. do not remove dispatch.h, as other code in the file may depend on it.
if a dispatch call spans multiple lines with inconsistent indentation: preserve the indentation pattern of the surrounding file. reformat the new at_dispatch_v2 call to match the file's style.
the converted file must have the following properties:
#include <ATen/Dispatch_v2.h> present; #include <ATen/Dispatch.h> retainedthe user will know the skill worked if:
cd <pytorch_root> && python setup.py build or the local build command, though this skill does not perform compilation)at_dispatch_all_types|at_dispatch_floating_types|at_dispatch_integral_types returns no matches in the converted file (except in comments)aten/src/ATen/Dispatch_v2.h and confirm that all type groups and individual types are valid