at-dispatch-v2
Official PyTorch guidance for converting AT_DISPATCH macros to AT_DISPATCH_V2.
- Skill Road
- at-dispatch-v2
Categories
at-dispatch-v2 is an official skill from the PyTorch source repository for converting legacy AT_DISPATCH macros to the newer AT_DISPATCH_V2 interface. It targets developers working on ATen C++ files, CUDA kernels, and native operator implementations in the PyTorch source tree. According to the provider, the skill is especially useful when macros such as AT_DISPATCH_ALL_TYPES_AND, AT_DISPATCH_FLOATING_TYPES, or related variants need to move to the new explicitly composable format. This makes the entry a focused source-maintenance guide rather than a standalone package, compiler, or general introduction to C++ macros.
Purpose and classification
The guide points to aten/src/ATen/Dispatch_v2.h as the definition of the new API. The important change is argument ordering. The scalar type and debug name come first, followed by the lambda and then explicit type groups. A conversion therefore is not just a spelling change; the original semantics must be understood and represented in the new form. The skill helps make the actual supported type set visible during that work. This matters when a kernel accepts several integral, floating-point, complex, or quantized types.
Practical conversion
According to the provider, the first step is adding the Dispatch_v2.h include near the existing Dispatch include. The older include remains because other code in the file may still need its definitions. The macro usage is then classified. A base macro for all types can become an AT_ALL_TYPES group, while floating-point, integral, or complex groups use their corresponding AT_EXPAND expressions. Extra types such as Half, BFloat16, or individual Float8 variants are listed separately at the end. The lambda is wrapped with AT_WRAP. That wrapper is important when the lambda contains commas in template arguments or function calls, because it keeps macro parsing from splitting the body into unintended arguments.
Review and quality
A safe migration covers every affected macro invocation, operator name, and extra type. Mechanical replacement can accidentally change the supported type set, reorder arguments incorrectly, or omit an include dependency. Review the result against Dispatch_v2.h and the concrete kernel implementation. Confirm that the new form covers the same data types as the old path and that diagnostics still retain a useful operator name. According to the provider, the skill is scoped to ATen code and native operator work; it does not claim that an arbitrary third-party macro definition is compatible.
Boundaries and safety
The guide does not modify an installed PyTorch version or execute changes automatically. It assumes a controlled local checkout, C++ knowledge, PyTorch build familiarity, and awareness of the relevant backend files. Before editing, inspect the branch, commit, and local working tree. Unknown scripts, generated files, and commands with external side effects should not enter the workflow without review. The conversion guarantees neither identical runtime nor identical numerical results; those properties require the relevant PyTorch tests and a review of the affected kernel.
Source and compatibility
The primary source is https://github.com/pytorch/pytorch/tree/main/.claude/skills/at-dispatch-v2. The complementary official PyTorch documentation at https://docs.pytorch.org/docs/stable/notes/extending.html explains how extensions and native operator work fit into the project. The PyTorch source tree is licensed under BSD-3-Clause. This description was reviewed on September 9, 2026. GitHub stars are not stored because the Skill model has no github_stars field. Compatibility with Claude Code follows from publication as a resource under .claude/skills; technical review must still be performed against the concrete PyTorch revision in use.
Installation instructions
What at-dispatch-v2 is and why it is needed
The at-dispatch-v2 skill comes directly from the official pytorch/pytorch repository, where it lives under .claude/skills as part of the project's own tooling for coding agents. According to the provider, the PyTorch project itself, the skill helps convert legacy AT_DISPATCH macros in the ATen C++ core into the newer AT_DISPATCH_V2 format, as defined in the file aten/src/ATen/Dispatch_v2.h. ATen is the tensor library that PyTorch is built on, and the AT_DISPATCH macros control which data types a given compute kernel is compiled for and selected at runtime. The old macro system needed a separate macro variant, such as AND2, AND3, or AND4, for every additional combination of types, which made the code hard to follow. For developers contributing to PyTorch's internal CUDA or CPU kernels, this skill is relevant because it automates the mechanical, error-prone rewriting work while correctly preserving argument order.
Step 1: Prerequisites
To make meaningful use of the skill, you need access to the PyTorch source code, typically via a local checkout of the repository, plus a coding agent such as Claude Code that automatically reads the .claude/skills directory. Basic C++ knowledge and an understanding of the ATen dispatch mechanism are helpful for reviewing the changes the agent proposes. Since the skill explicitly does not run a compile or test step, the toolchain used by PyTorch's build system, such as CMake and a suitable C++ compiler, must be available separately to verify the conversion afterward.
Setting it up step by step
Because the skill is part of the official PyTorch repository, it does not need to be installed separately once the repository is checked out locally and Claude Code is started inside the project directory. The agent automatically recognizes when a file contains AT_DISPATCH_ALL_TYPES_AND-style macros or when the user explicitly asks for a dispatch-v2 conversion. In its workflow, the agent first reads the affected file, adds the Dispatch_v2.h include if it is missing, identifies the macro pattern in use, maps the type group to the matching AT_EXPAND entry, extracts any individual extra types, and assembles the new AT_DISPATCH_V2 call from those pieces. Finally, the agent shows the complete modified file and explains the changes made so a developer can review them before committing.
Security and best practices
Because this is a source-code transformation in a security-sensitive, widely used framework, the skill explicitly states that the agent should not compile or test the code but should focus strictly on an accurate conversion. That reduces the risk of automated test runs triggering unintended side effects, but it shifts responsibility for verification to the human reviewer. Before merging a conversion, the regular PyTorch build and test process should therefore always be run, including the project's own CI checks. It is also important not to remove the old Dispatch.h include, since other parts of the code may still depend on it, something the skill correctly accounts for.
A practical example and its limits
A typical example is converting AT_DISPATCH_ALL_TYPES_AND3(kBFloat16, kHalf, kBool, iter.dtype(), "min_values_cuda", [&]() { ... }) into the new form using AT_DISPATCH_V2, where the lambda is wrapped in AT_WRAP and the type group is given explicitly as AT_EXPAND(AT_ALL_TYPES). The limits of the skill are that it is designed exclusively for this one, very specific refactoring task within the PyTorch codebase and has no application outside that context, for instance in other C++ projects with their own dispatch mechanisms. For PyTorch contributors, it is nonetheless a useful, narrowly scoped tool that speeds up repetitive migration work.
- Provider
- PyTorch
- License
- BSD-3-Clause
- Last reviewed
- 09.09.2026
Repository and documentation
Categories
Compatible with
Related guides
Guides and background related to this entry.