← All notes

Loading big models: device_map, meta device, shards

Answer: Set device_map="auto" in from_pretrained to enable Accelerate's Big Model Inference, which avoids holding two copies of the weights and dispatches them across devices.

Why big models run out of memory

Loading creates a model with random weights, loads the pretrained weights, then places them on the model. You need enough memory to hold two copies of the model weights.

What Big Model Inference does

Sharded checkpoints

save_pretrained automatically shards checkpoints larger than 50GB; max_shard_size sets the maximum size before sharding.

Artifacts

See also

Claims

Sources

License: this note is CC BY 4.0 (https://creativecommons.org/licenses/by/4.0/). Summary by iinventedlight (AI-assisted). Sources: Hugging Face docs (huggingface/transformers, huggingface/huggingface_hub, huggingface/diffusers on GitHub), Apache License 2.0 (https://www.apache.org/licenses/LICENSE-2.0). Text was condensed and changed from the originals.

License: CC-BY-4.0 · Summary by iinventedlight (AI-assisted). Sources: Hugging Face docs, Loading models, Apache-2.0 (huggingface docs repos); Hugging Face docs, Text generation, Apache-2.0 (huggingface docs repos).

Cite this

Permanent ID: iil:note:bj5q4j84

iinventedlight. 2026. "Loading big models: device_map, meta device, shards." iinventedlight Library, iil:note:bj5q4j84, version of 2026-11-09. https://iinventedlight.com/library/notes/hf-transformers-big-model-loading/. License: CC-BY-4.0.

@misc{iil_note_bj5q4j84,
  author = {{iinventedlight}},
  title = {{Loading big models: device\_map, meta device, shards}},
  howpublished = {iinventedlight Library},
  year = {2026},
  date = {2026-11-09},
  url = {https://iinventedlight.com/library/notes/hf-transformers-big-model-loading/},
  note = {Permanent ID iil:note:bj5q4j84. License: CC-BY-4.0.},
}

Markdown

Spotted a mistake? Use the MCP feedback tool or email hello@iinventedlight.com.

For language models and agents: start at /llms.txt | /agents.md | /.well-known/agent-library.json. This page in Markdown: /library/notes/hf-transformers-big-model-loading.md.