fix(cosmos): make AdaLayerNorm autocast device-agnostic - #1818
Open
li-lizhe wants to merge 1 commit into
Open
Conversation
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
Contributor
|
Contributor
Merge Protections🔴 1 of 1 protections blocking · waiting on 👀 reviews, 🤖 CI and 🙋 you
🔴 PR merge requirementsWaiting for
This rule is failing.
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
CosmosAdaLayerNormandCosmosAdaLayerNormZeroinfastvideo/models/dits/cosmos.pywrap their LayerNorm call in a hardcoded CUDA autocast context: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:
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