transformers icon indicating copy to clipboard operation
transformers copied to clipboard

CPU forward of NemotronH fails with `Expected x.is_cuda()` when causal-conv1d is installed (5.15+)

Open kamran-nvidia opened this issue 2 weeks ago • 4 comments

System Info

  • transformers 5.20.2 and 5.23.0 (reproduced on both); main at 4289d2c still has no device check in the wrapper
  • causal-conv1d 1.7.0, mamba-ssm 2.3.1; kernels not installed
  • torch 2.15.0a0, Python 3.12, Linux x86_64 (H100) and aarch64 (GB200)

Who can help?

@vasqu @drbh (kernels)

Reproduction

With causal-conv1d installed, run a NemotronH model on CPU:

import torch
import transformers

torch.manual_seed(0)
cfg = transformers.NemotronHConfig(
    hidden_size=128, intermediate_size=256, num_hidden_layers=4, num_attention_heads=4, num_key_value_heads=2,
    head_dim=32, vocab_size=256, max_position_embeddings=128, tie_word_embeddings=False,
    layers_block_type=["mamba", "attention", "moe", "mamba"], mamba_num_heads=8, mamba_head_dim=32,
    ssm_state_size=16, n_groups=2, n_routed_experts=4, num_experts_per_tok=2, moe_intermediate_size=64,
    moe_shared_expert_intermediate_size=128, num_nextn_predict_layers=0,
)
model = transformers.NemotronHForCausalLM(cfg).to(torch.bfloat16).eval()  # on CPU
ids = torch.randint(1, 256, (1, 22))
with torch.no_grad():
    model(input_ids=ids, attention_mask=torch.ones_like(ids, dtype=torch.bool))

It fails in the first Mamba layer:

Traceback (transformers 5.20.2, trimmed)
  File ".../transformers/models/nemotron_h/modeling_nemotron_h.py", line 534, in forward
    hidden_states_B_C = causal_conv1d_fn(
  File ".../transformers/integrations/hub_kernels.py", line 1051, in wrapped
    return implementation(*args, **kwargs)
  File ".../causal_conv1d/causal_conv1d_interface.py", line 117, in causal_conv1d_fn
    return CausalConv1dFn.apply(
  File ".../causal_conv1d/causal_conv1d_interface.py", line 53, in forward
    out = causal_conv1d_fwd_function(
  File ".../causal_conv1d/cpp_functions.py", line 104, in causal_conv1d_fwd_function
    _causal_conv1d_fwd_cpp(
  ...
  File ".../causal_conv1d/cpp_functions.py", line 22, in _causal_conv1d_fwd_cpp
    causal_conv1d_cuda.causal_conv1d_fwd(
RuntimeError: Expected x.is_cuda() to be true, but got false.

The helper fails on its own too, and the same call with .cuda() tensors works:

import torch
from transformers.models.nemotron_h import modeling_nemotron_h as m

m.causal_conv1d_fn(torch.randn(1, 8, 6), torch.randn(8, 4), torch.randn(8), activation="silu")
# RuntimeError: Expected x.is_cuda() to be true, but got false.

use_kernel_func_from_hub_with_fallback picks the installed package's function once, at import (hub_kernels.py L1010-L1020), and wrapped calls it whatever device the inputs are on. Once a package is installed, the only path to the PyTorch function is the torch.export check (L1027-L1031). Up to 5.14, NemotronHMamba2Mixer.forward used the kernels only when "cuda" in self.in_proj.weight.device.type and called torch_forward otherwise (v5.14.0). 5.15 moved the mixer onto these helpers without that check.

Mamba, Mamba2, Bamba, Jamba, Zamba2, FalconH1, GraniteMoeHybrid, LFM2, Qwen3-Next, Qwen3.5 and Kimi Linear use the same wrapper and have no device check in their forward either, so their CPU forwards should fail the same way when causal-conv1d, mamba-ssm or fla is installed. We only ran NemotronH.

We hit this when running a Hugging Face model on CPU as the numerical reference for a GPU implementation of the same model.

Expected behavior

CPU inputs should use the PyTorch implementation, as they did before 5.15, so the forward above succeeds. One option: have wrapped call torch_function when its tensor inputs are on CPU, as it already does under torch.export, and raise there for packed sequences (cu_seqlens / seq_idx) the way the no-package path does. Downstream, we currently patch hub_kernels.is_torchdynamo_exporting to return True around CPU forwards (NVIDIA-NeMo/Megatron-Bridge#6971), which depends on an internal detail.

kamran-nvidia avatar Nov 17 '26 15:11 kamran-nvidia

Related: #50819 reports the same Expected x.is_cuda() failure in transformers' own test_training_overfit for qwen3_5 and qwen3_next on main (GPU machine with fla 0.6.0 and causal-conv1d 1.7.0; the test runs on CPU), so this isn't specific to NemotronH.

kamran-nvidia avatar Nov 17 '26 15:11 kamran-nvidia

I'd like to work on this. Plan: in use_kernel_func_from_hub_with_fallback, use the torch fallback when inputs are not on CUDA, plus a CPU test that mocks the kernel as available. Happy to take it if no one else is.

shupandee avatar Nov 18 '26 05:11 shupandee

Fair enough, the priority for us to use the kernels via hf kernels use_kernels=True but we can add a supported hw list to the original fallbacks. Now it's too eager exchanging in any case.

Do you want to open a PR @kamran-nvidia? I.e. reject e.g. cpu directly

vasqu avatar Nov 18 '26 14:11 vasqu

Opened #50930 for this. Each original package now has a list of the devices its kernels run on (CUDA for causal-conv1d and mamba-ssm; CUDA, XPU, MLU and NPU for fla), and inputs on anything else, like CPU, go straight to the torch path. @shupandee thanks for offering to take it, a review there is welcome.

kamran-nvidia avatar Nov 18 '26 19:11 kamran-nvidia