Hugging Face Transformers Tutorial: Load Your First Pretrained Model
Use a pretrained model, understand tokenisation and inspect a bounded generation.
Executive summary
How do Hugging Face Transformers load and execute models in Python?
Transformers provides a standardized API (`AutoTokenizer` and `AutoModelForCausalLM`) to download weights from the Hugging Face Hub, tokenize strings into tensor inputs, and execute autoregressive text generation.
- Core pipeline
- Text → Tokenizer → Tensors → Model
- Framework support
- PyTorch, TensorFlow, JAX
Use `device_map="auto"` and `bitsandbytes` for seamless 4-bit GPU loading.
Who This Guide Is For
- Research, prototyping custom model architectures, and fine-tuning experiments.
- Direct programmatic control over generation logits, temperature, and top-p sampling.
Important Considerations
- Out of Memory (OOM) errors from loading unquantized FP32/FP16 models directly.
- Mismatch between tokenizer padding tokens and model attention masks.
Hugging Face Transformers lets you load supported pretrained models through a common Python interface. For a first experiment, use a small text-generation checkpoint and the high-level pipeline API. The essential lesson is how model files, tokenisation and generation settings fit together, not how many lines you can copy without understanding them.
Prepare an isolated environment
Read the official Transformers repository and installation guidance for a compatible Python and machine-learning framework combination. Create a separate virtual environment so the experiment does not disrupt another application's dependencies.
A minimal environment setup is:
python -m venv .venv
Activate it using your operating system's virtual-environment instructions, then install the framework-compatible Transformers distribution. The repository documents the PyTorch extra:
python -m pip install 'transformers[torch]'
These commands are instructional and were not executed for this guide. Accelerator packages can have platform-specific requirements; check their official installation instructions instead of assuming the same wheel supports every GPU.
Allow disk space for downloaded weights and the package cache. An initial model request may require internet access and can take longer than subsequent requests. Use a separate environment lock or dependency record after the example works.
Load a small explicit checkpoint
The checked repository demonstrates the pipeline API with Qwen/Qwen2.5-1.5B. This is a text-generation example, not a claim that this checkpoint is the newest model or best choice for a chatbot.
from transformers import pipeline
generate = pipeline(
task="text-generation",
model="Qwen/Qwen2.5-1.5B",
)
result = generate(
"A useful study timetable should",
max_new_tokens=60,
do_sample=False,
)
print(result[0]["generated_text"])
The model identifier matters: it determines which configuration, weights and tokenizer are loaded. Before accepting any download, inspect the publisher's model card, licence and intended use. Pin a revision for a repeatable application after the initial experiment.
The output is a continuation of the supplied text. A base completion model is not identical to an instruction-tuned assistant. If you need a chat model, choose a documented instruction checkpoint and use its expected message format rather than inserting role labels arbitrarily.
Understand what the pipeline hides
A tokenizer converts text into token identifiers, which the model uses to generate further tokens. Decoding converts those identifiers back to readable text. Tokens do not map uniformly to words, particularly across languages and code.
The pipeline conveniently handles much of this preparation. That is useful for learning, but inspect inputs and outputs before building a larger application. Generated text may include the original prompt, depending on the pipeline's settings and return format.
An output limit controls how much new text can be produced; it does not guarantee completeness or correctness. A very small limit may cut an explanation midway. A much larger one can consume time and memory without improving the answer.
For conversations, follow the chat-template documentation. Models expect particular control tokens and role arrangements. Supplying a familiar-looking string is not sufficient evidence that the model received the format it was trained on.
Check failure cases deliberately. Try an unsupported question, an ambiguous request and an input near your intended length. Keep the examples free of personal information. A working import proves little about reliability on documents or programming tasks.
When memory is insufficient, reduce checkpoint size before adding complicated device mapping. Advanced quantisation and GPU placement can help, but they introduce compatibility questions that are easier to resolve after the basic workflow is understood.
The next useful milestone is a saved script, fixed dependency set, explicit model revision and a few expected outputs or evaluation criteria. That turns a one-off demonstration into an experiment another developer can reproduce.
Quick questions, clear answers
Do I need to train a model first?
No. A pretrained checkpoint already contains learned weights and can perform inference once loaded by a compatible runtime. Fine-tuning is a separate decision. Begin by checking whether an existing instruction model and a better prompt meet your requirement before collecting training data.
Why does a chat model need a template?
Its training used a specific structure for roles and message boundaries. The tokenizer's chat template applies that structure. Incorrect formatting can reduce quality even when the weights and hardware are otherwise correct, making an integration problem look like a weak model.
Should I enable remote custom code?
Only after reviewing why the checkpoint requires it and inspecting the relevant repository code. Loading executable custom code has different implications from reading weight files. Prefer supported architectures for an initial tutorial and isolate any experiment requiring additional executable components.
About this guide
Aniket Thakur prepared this guide from secondary research checked on 22 October 2026. No first-hand software testing or performance benchmarking was conducted. Examples are instructional and require validation in your environment.