huggingface
runtime_error
ai_generated
true
RuntimeError: expected all tensors to be on the same device, but model parameters have been offloaded to CPU
ID: huggingface/model-offloaded-to-cpu
80%Fix Rate
85%Confidence
1Evidence
2024-03-15First Seen
Version Compatibility
| Version | Status | Introduced | Deprecated | Notes |
|---|---|---|---|---|
| transformers>=4.36.0 | active | — | — | — |
| accelerate>=0.25.0 | active | — | — | — |
| PyTorch>=2.1.0 | active | — | — | — |
Root Cause
Model was loaded with device_map='auto' or similar offloading, but an operation (e.g., forward pass) is being performed on a different device without proper device placement.
generic中文
模型使用device_map='auto'或类似卸载策略加载,但前向传播等操作在没有正确设备放置的情况下在另一个设备上执行。
Official Documentation
https://huggingface.co/docs/accelerate/v0.28.0/en/usage_guides/big_modelingWorkarounds
-
85% success Ensure all operations use the same device context. Use accelerate's context manager: from accelerate import dispatch_model; dispatch_model(model, device_map='auto') and then run inference within a with torch.no_grad() block on the same device.
Ensure all operations use the same device context. Use accelerate's context manager: from accelerate import dispatch_model; dispatch_model(model, device_map='auto') and then run inference within a with torch.no_grad() block on the same device.
-
75% success Set device_map to a specific device (e.g., device_map='cuda:0') instead of 'auto' to force all layers onto GPU if memory permits.
Set device_map to a specific device (e.g., device_map='cuda:0') instead of 'auto' to force all layers onto GPU if memory permits.
中文步骤
Ensure all operations use the same device context. Use accelerate's context manager: from accelerate import dispatch_model; dispatch_model(model, device_map='auto') and then run inference within a with torch.no_grad() block on the same device.
Set device_map to a specific device (e.g., device_map='cuda:0') instead of 'auto' to force all layers onto GPU if memory permits.
Dead Ends
Common approaches that don't work:
-
Manually moving model to GPU with model.to('cuda') after loading with device_map
70% fail
device_map='auto' already handles placement; model.to() overrides it and may cause offloaded layers to be lost or cause memory issues on large models.
-
Setting device_map=None and manually placing model on GPU
85% fail
For very large models that require offloading, disabling device_map may cause OOM because the entire model won't fit on GPU.