Implementation:LLMBook zh LLMBook zh github io Trainer Save Model Pretraining
| Knowledge Sources | |
|---|---|
| Domains | Deep_Learning, Training |
| Last Updated | 2026-02-08 00:00 GMT |
Overview
Concrete tool for saving trained model checkpoints using HuggingFace Trainer provided by the Transformers library.
Description
Trainer.save_model and Trainer.save_state are used at the end of pre-training to persist the trained model. In this repository, the model is saved to a /checkpoint-final subdirectory. When save_only_model=True (the default in this codebase), only model weights and config are saved, skipping optimizer state.
This is a Wrapper Doc — it documents how the LLMBook repository uses the HuggingFace Trainer API.
Usage
Call these methods after trainer.train() completes to persist the trained model and training state.
Code Reference
Source Location
- Repository: LLMBook-zh
- File: code/6.2 预训练实践.py
- Lines: 66-67
Signature
trainer.save_model(output_dir: str)
# Saves model weights and config to output_dir
trainer.save_state()
# Saves full trainer state (optimizer, scheduler, RNG)
Import
from transformers import Trainer
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| output_dir | str | Yes | Directory path for saving the checkpoint |
Outputs
| Name | Type | Description |
|---|---|---|
| checkpoint directory | Files | Model weights, config.json, tokenizer files |
| trainer state | Files | optimizer.pt, scheduler.pt, rng_state (via save_state) |
Usage Examples
# After training completes
trainer.train()
# Save final model checkpoint
trainer.save_model(args.output_dir + "/checkpoint-final")
# Save training state for potential resumption
trainer.save_state()