Skip to main content

Loading models

The Hub + from_pretrained pattern is the centre of Transformers: download (if needed), cache, and instantiate configuration, weights and preprocessors together.

Adapted for this playbook from the 🤗 Transformers documentation by Hugging Face. Official page: https://huggingface.co/docs/transformers/models. Images © Hugging Face (documentation-images) unless noted. This is not a substitute for the upstream docs — verify against the current version.

Covers Hugging Face pages: models, weightconverter, heterogeneous_configurations

Auto classes​

from transformers import AutoConfig, AutoModelForCausalLM, AutoTokenizer

model_id = "meta-llama/Llama-3.2-1B-Instruct"
config = AutoConfig.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
model = AutoModelForCausalLM.from_pretrained(
model_id,
torch_dtype="auto",
device_map="auto",
)

Pick the task head deliberately: AutoModelForSequenceClassification, AutoModelForTokenClassification, AutoModelForCausalLM, AutoModelForSeq2SeqLM, AutoModelForSpeechSeq2Seq, and so on.

Useful from_pretrained knobs​

ParameterPurpose
revisionPin a git commit / tag for reproducibility
torch_dtypefloat16, bfloat16, auto
device_map"auto" or explicit shard map
low_cpu_mem_usageStream weights to reduce RAM spikes
local_files_onlyFail if not already cached (air-gap)
token / loginAccess gated repos
trust_remote_codeAllow custom modelling code — review first

Dynamic weight loading​

Newer Transformers releases support more flexible weight conversion and loading paths (including converting between formats). Prefer official guides when migrating checkpoints: Dynamic weight loading.

Safety checklist​

  • Prefer revision pins in production.
  • Treat trust_remote_code=True as a security decision, not a convenience flag.
  • Log model id, revision and hash in your model card / change ticket.

Discussion

Comments​

Share feedback or questions about this page. No account required.

Loading comments…