Skip to content

Commit d47819d

Browse files
authored
Create model-repo-layout.md
1 parent c31665e commit d47819d

File tree

1 file changed

+60
-0
lines changed

1 file changed

+60
-0
lines changed

docs/hub/model-repo-layout.md

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
# Model repository files
2+
3+
A model repository holds all the files required to initialize a pretrained model for inference or training. The repository directory structure and files may vary depending on the library integration, but this guide covers what to expect in a Transformers or Diffusers model repository.
4+
5+
## Transformers
6+
7+
A [Transformers](https://hf.co/docs/transformers/index) model repository generally contains model files and preprocessor files.
8+
9+
<div class="flex justify-center">
10+
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/model-files-repo.png"/>
11+
</div>
12+
13+
### Model
14+
15+
- The **`config.json`** file stores details about the model architecture such as the number of hidden layers, vocabulary size, number of attention heads, the dimensions of each head, and more. This metadata is the model blueprint.
16+
- The **`model.safetensors`** file stores the models pretrained layers and weights. For large models, the safetensors file is sharded to limit the amount of memory required to load it. Browse the **`model.safetensors.index.json`** file to see which safetensors file the model weights are being loaded from.
17+
18+
```json
19+
{
20+
"metadata": {
21+
"total_size": 16060522496
22+
},
23+
"weight_map": {
24+
"lm_head.weight": "model-00004-of-00004.safetensors",
25+
"model.embed_tokens.weight": "model-00001-of-00004.safetensors",
26+
...
27+
}
28+
}
29+
```
30+
31+
You can also visualize this mapping by clicking on the ↗ button on the model card.
32+
33+
<div class="flex justify-center">
34+
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/model-files-safetensors-button.png"/>
35+
</div>
36+
37+
[Safetensors](https://hf.co/docs/safetensors/index) is a safer and faster serialization format - compared to [pickle](./security-pickle#use-your-own-serialization-format) - for storing model weights. You may encounter weights pickled in formats such as **`bin`**, **`pth`**, or **`ckpt`**, but **`safetensors`** is increasingly adopted in the model ecosystem as a better alternative.
38+
39+
- A model may also have a **`generation_config.json`** file which stores details about how to generate text, such as whether to sample, the top tokens to sample from, the temperature, and the special tokens for starting and stopping generation.
40+
41+
### Preprocessor
42+
43+
- The **`tokenizer_config.json`** file stores the special tokens added by a model. These special tokens signal many things to a model such as the beginning of a sentence, specific formatting for chat templates, or indicating an image. This file also shows the maximum input sequence length the model can accept, the preprocessor class, and the outputs it returns.
44+
- The **`tokenizer.json`** file stores the model's learned vocabulary.
45+
- The **`special_tokens_map.json`** is a mapping of the special tokens. For example, in [Llama 3.1-8B-Instruct](https://huggingface.co/meta-llama/Llama-3.1-8B-Instruct/blob/main/special_tokens_map.json), the beginning of string token is `"<|begin_of_text|>"`.
46+
47+
> [!TIP]
48+
> For other modalities, the `tokenizer_config.json` file is replaced by `preprocessor_config.json`.
49+
50+
## Diffusers
51+
52+
A [Diffusers](https://hf.co/docs/diffusers/index) model repository contains all the required model sub-components such as the variational autoencoder for encoding images and decoding latents, text encoder, transformer model, and more. These sub-components are organized into a multi-folder layout.
53+
54+
<div class="flex justify-center">
55+
<img src="https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/hub/diffusers-model-files-repo.png"/>
56+
</div>
57+
58+
Each subfolder contains the weights and configuration - where applicable - for each component similar to a Transformers model.
59+
60+
Weights are usually stored as safetensors files and the configuration is usually a json file with information about the model architecture.

0 commit comments

Comments
 (0)