cuda-index-width

Diagnose and fix 32-bit vs 64-bit index math overflows in PyTorch CUDA kernels.

103k|29.1k|Updated Aug 13, 2016
One-click install
npx skills add https://github.com/pytorch/pytorch --skill cuda-index-width
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: cuda-index-width
Source: https://github.com/pytorch/pytorch/tree/main/.claude/skills/cuda-index-width
Command: npx skills add https://github.com/pytorch/pytorch --skill cuda-index-width

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

PyTorch CUDA kernels can silently overflow when tensor sizes exceed 2^31 elements, producing wrong results or memory errors. This Skill classifies the overflow site and applies the correct fix pattern without unnecessarily inflating binary size or slowing hot loops.

Core Features & Use Cases

  • Overflow Site Classification: Distinguishes one-time setup offsets, hot per-element indexing, grid-dimension overflow, and 32-bit-only algorithms.
  • Canonical Utilities: Uses canUse32BitIndexMath, AT_DISPATCH_INDEX_TYPES, and CUDA_KERNEL_LOOP_TYPE instead of ad hoc checks.
  • Fix Patterns: Provides localized 64-bit casts, templated grid-stride kernels, strided TensorInfo dispatch, and early TORCH_CHECK failure for size-limited ops.
  • Binary-Size and Performance Tradeoffs: Guides when to template on index_t versus a single local cast, with commands to measure object/library size and symbol multiplication.
  • Regression Tests: Adds boundary-crossing tests guarded by @largeTensorTest and validates with compute-sanitizer.

Quick Start

Review the failing CUDA kernel, identify the exact expression that overflows 32-bit indexing, and apply the matching fix pattern from this skill.

Frequently Asked Questions about cuda-index-width

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I fix int32 overflow in PyTorch CUDA kernels?

Identify the exact expression that exceeds 2^31, then apply one of three patterns: a local int64_t cast for setup offsets, AT_DISPATCH_INDEX_TYPES templating for hot per-element loops, or TORCH_CHECK with canUse32BitIndexMath for size-limited operators.

When should I use int64_t vs templated index_t in CUDA kernels?

Use a local int64_t cast when only a base-offset multiply overflows outside the hot loop. Use templated index_t with AT_DISPATCH_INDEX_TYPES when per-element linear indexing can exceed 32 bits, since 64-bit division in hot loops is measurable.

Does canUse32BitIndexMath check both numel and storage offsets?

Yes. at::native::canUse32BitIndexMath(tensor, INT_MAX) verifies both numel and the maximum storage offset, so a small numel tensor with large strided views can still require 64-bit indexing.

Why does my CUDA kernel fail near 2^31 elements?

The kernel uses int arithmetic for an offset or loop index that overflows at INT_MAX. Either template the kernel on index_t, cast the specific overflowing expression to int64_t, or add a TORCH_CHECK guard if the operator has a documented size limit.

What are the binary-size costs of templating CUDA kernels on index_t?

Each index_t dispatch duplicates the kernel for every scalar dtype and memory-format specialization. Measure impact with stat on the .cu.o file and nm -S --size-sort on libtorch_cuda.so before templating many kernels.