Model

Published 2026-08-24

Layer 4: How Are Models Saved and Published?

Understand from an engineering perspective how an AI model is saved from in-memory parameters and training state into files, and how weights, configuration, Tokenizer, Chat Template, Model Card, and other resources are organized and published as a model repository.

Core question: What files do training results ultimately become?

Key concepts: Checkpoint, Serialization, Weights, Safetensors, Shard, Index, Config, Tokenizer, Processor, Chat Template, Model Card, Model Repository, Revision

Training produces parameters and training state in memory; to pause training, distribute models, and run inference, these must be serialized into files and published together with configuration, Tokenizer, documentation, and other resources.

During training, model parameters exist as Tensors in memory or VRAM. A linear layer may contain: Weight Tensor of shape [4096, 4096], Bias Tensor of shape [4096].

The whole model is made of large numbers of such Tensors:

Model Parameters
β”œβ”€β”€ model.embed_tokens.weight
β”œβ”€β”€ model.layers.0.self_attn.q_proj.weight
β”œβ”€β”€ model.layers.0.self_attn.k_proj.weight
β”œβ”€β”€ model.layers.0.mlp.up_proj.weight
β”œβ”€β”€ model.layers.1...
└── lm_head.weight

The core work of saving a model is: serialize in-memory Tensorsβ€”their names, shapes, data types, and valuesβ€”into disk files.

Serialization

Convert in-memory data structures into a file format that can be stored or transmitted.

Loading a model does the reverse: disk file β†’ deserialization β†’ Tensor β†’ place into memory or VRAM.

A weight file is not the model's source code; it is the serialized result of the large set of numeric values obtained after training.

Checkpoint: Save Points During Training

A Checkpoint is the model state saved at a point in time during training, used to resume training, compare different stages, or choose the final model.

A complete training Checkpoint may include:

Training Checkpoint
β”‚
β”œβ”€β”€ Model Weights
β”œβ”€β”€ Optimizer State
β”œβ”€β”€ Learning Rate Scheduler State
β”œβ”€β”€ Training Step / Epoch
β”œβ”€β”€ Gradient Scaler State
└── Random Number Generator State
ContentRole
Model WeightsSave the parameters the model has currently learned
Optimizer StateSave the optimizer's internal state
Scheduler StateSave learning-rate schedule progress
Step / EpochRecord how far training has progressed
Random StateReproduce the training process as closely as possible

Checkpoint is also used loosely to mean a complete model file that can be loaded directly.

Weights: Data That Stores Model Capability

Weight files store the numeric values of each parameter after training. Conceptually they represent:

  • Tensor shape
  • Tensor data type
  • Tensor concrete values

Safetensors: A Format for Saving Weight Files

A container format specialized for holding large numbers of model-parameter Tensors. Early PyTorch models often used: pytorch_model.bin.

Such files are usually based on Python Pickle. Pickle can theoretically execute code carried in the payload during deserialization, so loading untrusted files has security risk.

One design focus of Safetensors is: save only Tensor data, without deserializing arbitrary Python objects via Pickle, reducing the risk of executing malicious code at the format level.

Shard: Why Does One Model Have Many Weight Files?

Weights of large models may be tens of GB, hundreds of GB, or even larger. Putting everything in one file causes:

  • Single file too large;
  • Awkward upload and download;
  • Possible filesystem or storage-service limits;
  • Harder on-demand loading;
  • Inconvenient distributed handling.

Therefore model weights are usually split into multiple files. This process is called Sharding. Each file is called a Shard.

model-00001-of-00018.safetensors
model-00002-of-00018.safetensors
model-00003-of-00018.safetensors
...
model-00018-of-00018.safetensors
 
model-00001-of-00018
      β”‚         β”‚
      β”‚         └── 18 shards in total
      └── This is shard 1

Weight Index: How Do You Know Which Shard a Parameter Is In?

After weights are sharded, the inference framework needs to know which file each parameter is stored in. Therefore repositories usually include: model.safetensors.index.json. Think of it as a directory or index table for weight shards. For example:

{
  "metadata": {
    "total_size": 55562855904.0
  },
  "weight_map": {
    "lm_head.weight": "model-00018-of-00018.safetensors",
    "model.language_model.embed_tokens.weight": "model-00003-of-00018.safetensors",
    "model.language_model.layers.0.input_layernorm.weight": "model-00001-of-00018.safetensors",
    "model.language_model.layers.0.linear_attn.A_log": "model-00001-of-00018.safetensors",
    "model.language_model.layers.0.linear_attn.conv1d.weight": "model-00001-of-00018.safetensors",
    ...
  }
}

Config: Telling the Program What Model Structure to Create

The most common configuration file is config.json, containing:

  • Model type;
  • Architecture class name;
  • Number of layers;
  • Hidden Size;
  • Number of Attention Heads;
  • Vocabulary Size;
  • Position Encoding configuration;
  • Maximum context length;
  • Data type hints;
  • Special Token IDs;
  • Multimodal structure configuration.

Generation Config: Controlling Default Generation Behavior

generation_config.json stores default generation parameters, for example:

  • Temperature;
  • Top-p;
  • Top-k;
  • Max Length;
  • Repetition Penalty;
  • EOS Token ID;
  • PAD Token ID.

Tokenizer Files: Saving Text Splitting Rules

A Tokenizer is not simply a vocabulary table. A complete Tokenizer may include:

Tokenizer
β”‚
β”œβ”€β”€ Vocabulary
β”œβ”€β”€ Tokenization Algorithm
β”œβ”€β”€ Merge Rules
β”œβ”€β”€ Normalization Rules
β”œβ”€β”€ Special Tokens
└── Tokenizer Config
FileRole
tokenizer.jsonComplete serialized Tokenizer definition
tokenizer_config.jsonTokenizer type and behavior configuration
vocab.jsonMapping between Tokens and Token IDs
merges.txtMerge rules used by algorithms such as BPE
special_tokens_map.jsonDefinitions of special Tokens
  • Model weights are trained on a specific Token ID system.
  • Tokenizer and model weights are usually paired.

Processor: Input Processing Rules for Multimodal Models

For image, audio, and video models, the model repository may also include:

preprocessor_config.json
processor_config.json
video_preprocessor_config.json

These may specify:

  • Image scaling method;
  • Image cropping method;
  • Pixel normalization rules;
  • Video sampling method;
  • Frame count or frame-rate handling;
  • Audio sample rate;
  • How different modalities are combined.

Multimodal model input resources can be summarized as:

  • Text β†’ Tokenizer
  • Image β†’ Image Processor
  • Video β†’ Video Processor
  • Audio β†’ Audio Processor

Processing rules must stay consistent with those used during model training.

Model Repository: The Complete Carrier After a Model Is Published

A model repository is a versioned store for saving, managing, and publishing the resources needed for a model version.

Model Repository
β”‚
β”œβ”€β”€ Model Identity
β”‚   β”œβ”€β”€ Repository Name
β”‚   β”œβ”€β”€ Model Family
β”‚   └── Model Variant
β”‚
β”œβ”€β”€ Model Structure
β”‚   └── config.json
β”‚
β”œβ”€β”€ Model Weights
β”‚   β”œβ”€β”€ *.safetensors
β”‚   └── *.index.json
β”‚
β”œβ”€β”€ Input Processing
β”‚   β”œβ”€β”€ tokenizer.json
β”‚   β”œβ”€β”€ tokenizer_config.json
β”‚   β”œβ”€β”€ vocab.json
β”‚   β”œβ”€β”€ merges.txt
β”‚   └── preprocessor_config.json
β”‚
β”œβ”€β”€ Inference Behavior
β”‚   β”œβ”€β”€ generation_config.json
β”‚   └── chat_template.jinja
β”‚
└── Documentation
    β”œβ”€β”€ README.md / Model Card
    └── LICENSE

LoRA and Quantized Model Repositories

Not every model repository contains a full set of weights.

A LoRA or Adapter repository usually contains only a small number of new or modified parameters relative to the base model.

A quantized model repository contains conversions from the original model such as:

  • INT8;
  • INT4;
  • FP8;
  • GGUF;
  • GPTQ;
  • AWQ.

Summary:

Parameters obtained from training are first saved as Checkpoints. For release, model weights are extracted and serialized into formats such as Safetensors; when too large they are split into multiple Shards, with an index file recording each parameter's location. Weights together with Config, Tokenizer, Processor, Chat Template, Model Card, License, and other resources form a model repository for others to download, load, and run.

  • Weight files store the large Tensor values obtained from training;
  • Config describes how to create the model structure; weights fill in the concrete parameters;
  • Shards are only file splits of the same set of weights, not multiple independent models;
  • In engineering, models are usually published as model repositories, not as a single weight file alone.