Tokenizers and Models
After Pipeline, split the Preprocessor from the PreTrainedModel. Almost every checkpoint loads through AutoClass + from_pretrained.
Which AutoClass?
model_type in config.json picks the concrete class (e.g. DistilBertForSequenceClassification). You do not memorize class names.
Encode and decode
input_ids: vocabulary indices—the integers the model consumes.attention_mask:1for real tokens,0for pad, so pad does not attend.return_tensors="pt": PyTorch tensors (see the PyTorch tutorial).- DistilBERT adds
[CLS]/[SEP]; GPT-style models may only have BOS/EOS—or neither.
Batching needs padding, and a defined pad token:
Forward vs generate
A classification head consumes tokenizer output and exposes logits:
Causal LMs use generate() (next chapter). A single forward is not “the finished article.”
Instruct models and chat templates
Models such as Qwen/Qwen2.5-0.5B-Instruct must go through apply_chat_template. Do not treat the user sentence as raw continuation:
The template lives in the tokenizer config. Do not paste Qwen markers onto a DeepSeek model.
Saving and the three artifacts
That is the official trio: PreTrainedConfig, PreTrainedModel, Preprocessor. Later, from_pretrained(save_dir) loads offline.