Skip to content

fix(cosmos): make AdaLayerNorm autocast device-agnostic - #1818

Open
li-lizhe wants to merge 1 commit into
hao-ai-lab:mainfrom
li-lizhe:fix/cosmos-autocast-device
Open

fix(cosmos): make AdaLayerNorm autocast device-agnostic#1818
li-lizhe wants to merge 1 commit into
hao-ai-lab:mainfrom
li-lizhe:fix/cosmos-autocast-device

Conversation

@li-lizhe

@li-lizhe li-lizhe commented Sep 5, 2026

Copy link
Copy Markdown

Problem

CosmosAdaLayerNorm and CosmosAdaLayerNormZero in fastvideo/models/dits/cosmos.py wrap their LayerNorm call in a hardcoded CUDA autocast context:

with torch.autocast(device_type="cuda", enabled=False):
    hidden_states = self.norm(hidden_states)

device_type="cuda" only disables autocast for CUDA tensors. On other accelerators (Ascend NPU, Apple MPS, Intel XPU, AMD ROCm) the autocast context does not apply to the tensor, so the norm may be computed in the wrong precision (e.g. bf16) instead of the intended fp32, which can cause numerical differences or dtype mismatches downstream.

Root cause

Hardcoded CUDA device-type assumption in torch.autocast().

Fix

Use the tensor's actual device type:

with torch.autocast(device_type=hidden_states.device.type, enabled=False):

This is device-agnostic: the autocast context follows the tensor's device (npu, mps, cuda, xpu, etc.).

Verification

Verified on Ascend 910B: torch.autocast(device_type="npu", enabled=False) is valid and correctly disables autocast for NPU tensors. On CUDA the behaviour is identical to the original code.

Fixes #1816

CosmosAdaLayerNorm and CosmosAdaLayerNormZero wrapped their LayerNorm
call in torch.autocast(device_type="cuda", enabled=False). That context
only disables autocast for CUDA tensors; on other accelerators (Ascend
NPU, MPS, XPU, ROCm) it does not apply, so the norm can be computed in
the wrong precision and downstream dtype mismatches may appear.

Use hidden_states.device.type so the autocast context follows the actual
device. Device-agnostic and verified on NPU.

Fixes hao-ai-lab#1816
@mergify mergify Bot added the scope: model Model architecture (DiTs, encoders, VAEs) label Sep 5, 2026
@mergify

mergify Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

⚠️ PR title format required

Your PR title must start with a type tag in brackets. Examples:

  • [feat] Add new model support
  • [bugfix] Fix VAE tiling corruption
  • [refactor] Restructure training pipeline
  • [perf] Optimize attention kernel
  • [ci] Update test infrastructure
  • [infra] Add activation trace hooks
  • [docs] Add inference guide
  • [misc] Clean up configs
  • [new-model] Port Flux2 to FastVideo
  • [skill] Add add-model agent skill

Valid tags: feat, feature, bugfix, fix, refactor, perf, ci, infra, doc, docs, misc, chore, kernel, new-model, skill, skills

Please update your PR title and the merge protection check will pass automatically.

@mergify

mergify Bot commented Sep 5, 2026

Copy link
Copy Markdown
Contributor

Merge Protections

🔴 1 of 1 protections blocking · waiting on 👀 reviews, 🤖 CI and 🙋 you

Protection Waiting on
🔴 PR merge requirements 👀 reviews, 🤖 CI and 🙋 you

🔴 PR merge requirements

Waiting for

  • #approved-reviews-by>=1
  • check-success=full-suite-passed
  • title~=(?i)^\[(feat|feature|bugfix|fix|refactor|perf|ci|doc|docs|misc|chore|kernel|new.?model|skill|skills|infra)\]
This rule is failing.
  • #approved-reviews-by>=1
  • check-success=full-suite-passed
  • title~=(?i)^\[(feat|feature|bugfix|fix|refactor|perf|ci|doc|docs|misc|chore|kernel|new.?model|skill|skills|infra)\]
  • check-success=fastcheck-passed
  • check-success~=pre-commit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

scope: model Model architecture (DiTs, encoders, VAEs)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Cosmos AdaLayerNorm autocast hardcodes device_type="cuda", breaking on non-CUDA accelerators

1 participant