Skip to content

Add Unsigned Integer Support

Introduction

The Add Unsigned Integer Support skill is a specialized utility designed for PyTorch contributors and core developers. It streamlines the process of expanding operator type coverage to include unsigned integer types (uint16, uint32, and uint64).

In the PyTorch ecosystem, adding support for new dtypes often involves complex updates to C++ dispatch macros. This skill automates the heavy lifting by analyzing existing kernel implementations and applying the correct AT_DISPATCH_V2 transformations.

Key Features:

  • Automatic V2 Conversion: Detects legacy AT_DISPATCH macros and upgrades them to the modern V2 format before adding new types.
  • Intelligent Type Grouping: Automatically decides whether to use AT_BAREBONES_UNSIGNED_TYPES or upgrade existing groups to AT_INTEGRAL_TYPES_V2.
  • Multi-Site Consistency: Identifies and updates all relevant dispatch sites (CPU, CUDA, etc.) within a file to ensure uniform type support.
  • Semantic Validation: Ensures that unsigned types are only added to operators where they are mathematically and logically applicable.

Installation

To use this skill within your Claude Code environment, ensure you are working within the pytorch/pytorch repository.

  1. Configure the skill: Add the skill definition to your local .claude/skills directory or include it in your project instructions.
  2. Verify environment: Ensure you have the necessary PyTorch development headers available, as the skill performs static analysis on C++ source files.
Terminal window
# Example: Adding the skill to your project
cp add-uint-support.md .claude/skills/

Usage

This skill is triggered when you need to expand the functionality of PyTorch kernels. You can invoke it by describing the operator you want to update or by referencing specific “unsigned types” or “barebones” support.

Example Prompts:

  • “Add unsigned integer support to the min_values CUDA kernel.”
  • “Enable uint16 and uint32 for the element-wise addition operator.”
  • “Check if native_batch_norm.cpp supports barebones unsigned types and update if necessary.”

The Decision Workflow

The skill follows a structured logic to ensure code integrity:

  1. Check for V2: If a file uses the old AT_DISPATCH macro, it first runs the at-dispatch-v2 skill.
  2. Analyze Groups: It looks for AT_INTEGRAL_TYPES or AT_ALL_TYPES.
  3. Transform:
    • If AT_INTEGRAL_TYPES is present, it replaces it with AT_INTEGRAL_TYPES_V2.
    • Otherwise, it explicitly appends AT_BAREBONES_UNSIGNED_TYPES to the dispatch list.

Code Reference

Before Transformation:

AT_DISPATCH_V2(dtype, "op", AT_WRAP([&]() {
kernel<scalar_t>();
}), AT_EXPAND(AT_ALL_TYPES));

After Transformation:

AT_DISPATCH_V2(dtype, "op", AT_WRAP([&]() {
kernel<scalar_t>();
}), AT_EXPAND(AT_ALL_TYPES), AT_EXPAND(AT_BAREBONES_UNSIGNED_TYPES));

Note: This skill specifically targets uint16, uint32, and uint64. Note that kByte (uint8) is handled separately in PyTorch’s legacy type groups.

Constraints & Edge Cases

  • Floating Point Only: The skill will skip operators that are semantically restricted to floating-point types (e.g., certain transcendental functions).
  • Complex Types: Unsigned types are added alongside complex types without conflict.
  • Manual Verification: While the skill automates the macro updates, developers are encouraged to run functional tests to ensure the underlying kernel logic handles unsigned overflows correctly.