Description
🐛 Describe the bug
Hi, I am trying to convert the Mamba model from Hugging Face to ONNX. However, I encountered the following error:
<class 'RuntimeError'>: Found <class 'transformers.cache_utils.MambaCache'> in output, which is not a known type.
Here is the code:
from transformers import MambaForCausalLM
import torch
model = MambaForCausalLM.from_pretrained("state-spaces/mamba-130m-hf")
dummy_inputs = torch.tensor([[33]], dtype=torch.int32)
with torch.no_grad():
onnx_program = torch.onnx.export(model,
dummy_inputs,
"mamba_hf.onnx",
input_names=["input"],
opset_version=15,
external_data=False,
dynamo=True)
onnx_program.optimize()
onnx_program.save("mamba_hf.onnx")
When I set use_cache=False
, the export works without issues. However, the operations differ between the cached (use_cache=True
) and non-cached (use_cache=False
) cases—specifically, not caching involves different operations (conv1D).
This code works:
from transformers import MambaForCausalLM
import torch
# Disable caching
model = MambaForCausalLM.from_pretrained("state-spaces/mamba-130m-hf", use_cache=False)
dummy_inputs = torch.tensor([[33]], dtype=torch.int32)
with torch.no_grad():
onnx_program = torch.onnx.export(model,
dummy_inputs,
"mamba_hf.onnx",
input_names=["input"],
opset_version=15,
external_data=False,
dynamo=True)
onnx_program.optimize()
onnx_program.save("mamba_hf.onnx")
I am particularly interested in exporting the model with the operations that occur when the cache is available.
Question:
How can I modify the export to ignore the MambaCache
object but still retain the logic from this part of the code (with caching):
https://github.com/huggingface/transformers/blob/924f1c717a72261a4b9286a31f199d9512448dd0/src/transformers/models/mamba/modeling_mamba.py#L248
and not this part (without caching):
https://github.com/huggingface/transformers/blob/924f1c717a72261a4b9286a31f199d9512448dd0/src/transformers/models/mamba/modeling_mamba.py#L268
Any guidance on how to handle this would be greatly appreciated!
Versions
PyTorch version: 2.6.0+cu124
Is debug build: False
CUDA used to build PyTorch: 12.4
ROCM used to build PyTorch: N/A
OS: Ubuntu 20.04.6 LTS (x86_64)
GCC version: (Ubuntu 9.4.0-1ubuntu1~20.04.2) 9.4.0
Clang version: Could not collect
CMake version: version 3.31.3
Libc version: glibc-2.31
Python version: 3.11.11 (main, Dec 4 2024, 08:55:08) [GCC 9.4.0] (64-bit runtime)
Python platform: Linux-5.15.0-130-generic-x86_64-with-glibc2.31
Is CUDA available: True
CUDA runtime version: Could not collect
CUDA_MODULE_LOADING set to: LAZY
GPU models and configuration: GPU 0: NVIDIA GeForce RTX 2080 Ti
Nvidia driver version: 550.107.02
cuDNN version: Could not collect
HIP runtime version: N/A
MIOpen runtime version: N/A
Is XNNPACK available: True
CPU:
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
Address sizes: 39 bits physical, 48 bits virtual
CPU(s): 16
On-line CPU(s) list: 0-15
Thread(s) per core: 2
Core(s) per socket: 8
Socket(s): 1
NUMA node(s): 1
Vendor ID: GenuineIntel
CPU family: 6
Model: 165
Model name: Intel(R) Core(TM) i7-10700 CPU @ 2.90GHz
Stepping: 5
CPU MHz: 4410.645
CPU max MHz: 4800,0000
CPU min MHz: 800,0000
BogoMIPS: 5799.77
Virtualization: VT-x
L1d cache: 256 KiB
L1i cache: 256 KiB
L2 cache: 2 MiB
L3 cache: 16 MiB
NUMA node0 CPU(s): 0-15
Vulnerability Gather data sampling: Mitigation; Microcode
Vulnerability Itlb multihit: KVM: Mitigation: VMX disabled
Vulnerability L1tf: Not affected
Vulnerability Mds: Not affected
Vulnerability Meltdown: Not affected
Vulnerability Mmio stale data: Mitigation; Clear CPU buffers; SMT vulnerable
Vulnerability Reg file data sampling: Not affected
Vulnerability Retbleed: Mitigation; Enhanced IBRS
Vulnerability Spec rstack overflow: Not affected
Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl and seccomp
Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization
Vulnerability Spectre v2: Mitigation; Enhanced / Automatic IBRS; IBPB conditional; RSB filling; PBRSB-eIBRS SW sequence; BHI SW loop, KVM SW loop
Vulnerability Srbds: Mitigation; Microcode
Vulnerability Tsx async abort: Not affected
Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl vmx smx est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch cpuid_fault invpcid_single ssbd ibrs ibpb stibp ibrs_enhanced tpr_shadow vnmi flexpriority ept vpid ept_ad fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt intel_pt xsaveopt xsavec xgetbv1 xsaves dtherm ida arat pln pts hwp hwp_notify hwp_act_window hwp_epp pku ospke md_clear flush_l1d arch_capabilities
Versions of relevant libraries:
[pip3] numpy==1.26.4
[pip3] nvidia-cublas-cu12==12.4.5.8
[pip3] nvidia-cuda-cupti-cu12==12.4.127
[pip3] nvidia-cuda-nvrtc-cu12==12.4.127
[pip3] nvidia-cuda-runtime-cu12==12.4.127
[pip3] nvidia-cudnn-cu12==9.1.0.70
[pip3] nvidia-cufft-cu12==11.2.1.3
[pip3] nvidia-curand-cu12==10.3.5.147
[pip3] nvidia-cusolver-cu12==11.6.1.9
[pip3] nvidia-cusparse-cu12==12.3.1.170
[pip3] nvidia-cusparselt-cu12==0.6.2
[pip3] nvidia-nccl-cu12==2.21.5
[pip3] nvidia-nvjitlink-cu12==12.4.127
[pip3] nvidia-nvtx-cu12==12.4.127
[pip3] onnx==1.16.1
[pip3] onnxconverter-common==1.16.0
[pip3] onnxmltools==1.13.0
[pip3] onnxruntime==1.19.0
[pip3] onnxruntime_extensions==0.13.0
[pip3] onnxscript==0.1.0.dev20250114
[pip3] pytorch-lightning==1.9.5
[pip3] torch==2.6.0
[pip3] torchao==0.8.0
[pip3] torchaudio==2.5.1
[pip3] torchmetrics==1.6.1
[pip3] torchtune==0.5.0
[pip3] torchvision==0.21.0
[pip3] triton==3.2.0
[conda] Could not collect