Skip to content

Register AutocastCPU for the attention and DISCO custom ops - #246

Merged
azrael417 merged 1 commit into
mainfrom
tkurth/attention-autocast-cpu
Aug 19, 2026
Merged

Register AutocastCPU for the attention and DISCO custom ops#246
azrael417 merged 1 commit into
mainfrom
tkurth/attention-autocast-cpu

Conversation

@azrael417

Copy link
Copy Markdown
Collaborator

Both op families registered an autocast kernel only at the AutocastCUDA dispatch key, so under torch.autocast("cpu", ...) nothing reconciled their inputs. The two symptoms differ:

  • Attention hard-fails. The kernels dispatch once on q's scalar type and then reinterpret every activation pointer as that type, so they require k, v and q to share a dtype and check it explicitly. Autocast casts some ops and not others, so a module mixing projections with normalization can hand the op an fp32 q next to an fp16 v, tripping the check: RuntimeError: v dtype (Half) must match q dtype (Float)

  • DISCO fails quietly. Its CPU kernel normalizes dtypes itself (disco_cpu_fwd.cpp upcasts reduced-precision inp and vals to fp32), so nothing errors -- but the op ran at whatever dtype it was handed rather than the autocast dtype, i.e. CPU autocast simply had no effect on DISCO layers.

Both now register AutocastCPU alongside AutocastCUDA, via one closure per op so the two keys cannot drift apart again.

Found on nvcr.io/nvidia/pytorch:24.12-py3 (torch ~2.6), where six CPU AMP rows of test_custom_implementation failed. It was invisible on newer PyTorch, on CI and on two other containers, because those happen to produce consistent dtypes for this graph -- the missing registration was latent there, not absent.

The new test is therefore at op level rather than module level: it constructs the mismatch directly (fp32 q, reduced-precision k/v) so the regression is detectable on any torch version. Without autocast active the same inputs raise, which makes the test discriminating rather than vacuous.

Suite: 596 passed, 15 skipped (attention + convolution).

Both op families registered an autocast kernel only at the AutocastCUDA
dispatch key, so under torch.autocast("cpu", ...) nothing reconciled their
inputs. The two symptoms differ:

- Attention hard-fails. The kernels dispatch once on q's scalar type and then
  reinterpret every activation pointer as that type, so they require k, v and q
  to share a dtype and check it explicitly. Autocast casts some ops and not
  others, so a module mixing projections with normalization can hand the op an
  fp32 q next to an fp16 v, tripping the check:
      RuntimeError: v dtype (Half) must match q dtype (Float)

- DISCO fails quietly. Its CPU kernel normalizes dtypes itself (disco_cpu_fwd.cpp
  upcasts reduced-precision inp and vals to fp32), so nothing errors -- but the
  op ran at whatever dtype it was handed rather than the autocast dtype, i.e.
  CPU autocast simply had no effect on DISCO layers.

Both now register AutocastCPU alongside AutocastCUDA, via one closure per op so
the two keys cannot drift apart again.

Found on nvcr.io/nvidia/pytorch:24.12-py3 (torch ~2.6), where six CPU AMP rows of
test_custom_implementation failed. It was invisible on newer PyTorch, on CI and
on two other containers, because those happen to produce consistent dtypes for
this graph -- the missing registration was latent there, not absent.

The new test is therefore at op level rather than module level: it constructs the
mismatch directly (fp32 q, reduced-precision k/v) so the regression is detectable
on any torch version. Without autocast active the same inputs raise, which makes
the test discriminating rather than vacuous.

Suite: 596 passed, 15 skipped (attention + convolution).

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@azrael417
azrael417 requested a review from bonevbs August 19, 2026 07:34
@azrael417 azrael417 self-assigned this Aug 19, 2026

@bonevbs bonevbs left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@azrael417
azrael417 merged commit 7bd2c98 into main Aug 19, 2026
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants