Customising models
Adapt pretrained architectures by changing configuration, swapping heads, composing modular blocks, or carefully patching forward passes.
Adapted for this playbook from the π€ Transformers documentation by Hugging Face. Official page: https://huggingface.co/docs/transformers/custom_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: custom_models, monkey_patching, fusion_mapping, how_to_hack_models, modular_transformers
Common customisation pathsβ
- Config overrides β change
num_labels, dropout, max positions beforefrom_pretrained. - Task heads β load a backbone then attach
AutoModelFor*heads for classification, QA, etc. - Modular shards β declare reuse in
modular_*.py(contributor workflow). - Component hacks β attention backends, fusion maps, intermediate tracing (advanced).
from transformers import AutoConfig, AutoModelForSequenceClassification
config = AutoConfig.from_pretrained("bert-base-uncased", num_labels=4)
model = AutoModelForSequenceClassification.from_pretrained(
"bert-base-uncased",
config=config,
)
Guidanceβ
| Approach | When |
|---|---|
| Config + head | Fine-tuning for a new label set |
| PEFT (LoRA) | Efficient adaptation without full weight updates β see PEFT |
| Modular contribution | Upstreaming a new architecture |
| Monkey patch | Spike / debug only |
Official deep dives: Custom models, Monkey patching, Modular transformers.
Discussion
Commentsβ
Share feedback or questions about this page. No account required.
Loading commentsβ¦