Extracts all weights corresponding to a specific layer or block from large models saved in safetensors format, and exports them as a PyTorch-compatible state dict.
Project description
safetensors-layer-grabber
Extracts all weights corresponding to a specific layer or block from large models saved in safetensors format, and exports them as a PyTorch-compatible state dict.
Example
safetensors_layer_grabber \
--model-dir ~/models/my_model \
--layer model.layers.12.mlp \
--output layer12_mlp.ckpt
Features
- Targeted Extraction: Extract weights corresponding to any layer or nested component by name prefix (e.g.
model.layers.8.self_attn) - Flexible Output: Choose your output filename, or default to
<layer>.ckpt - Efficient: Works directly with multipart safetensors files; no need to load the entire model
Installation
pip install safetensors-layer-grabber
After installation, run it as safetensors_layer_grabber.
Arguments
| Argument | Required | Description |
|---|---|---|
| --model-dir | Yes | Directory containing .safetensors files of the model |
| --layer | Yes | Layer/component prefix to extract (e.g. model.layers.8.self_attn) |
| --output | No | Output file name (defaults to <layer>.ckpt) |
Understanding Parameter Names
In safetensors files
Safetensors model shards store parameters as key–tensor pairs, with keys like:
model.layers.8.self_attn.q_proj.weight
model.layers.8.self_attn.k_proj.weight
model.layers.8.self_attn.v_proj.bias
In the extracted .ckpt file
When you use a prefix with --layer, that prefix is stripped from the keys in the saved state dict.
- Example:
- Input safetensors key:
model.layers.8.self_attn.q_proj.weight - Layer argument:
model.layers.8.self_attn - Output key in
.ckpt:q_proj.weight
- Input safetensors key:
- Another example:
- Input safetensors key:
model.layers.10.mlp.fc1.weight - Layer argument:
model.layers.10.mlp - Output key in
.ckpt:fc1.weight
- Input safetensors key:
Key Mapping Table
| Safetensors key | Layer argument | Saved .ckpt key |
|---|---|---|
| model.layers.8.self_attn.q_proj.weight | model.layers.8.self_attn | q_proj.weight |
| model.layers.8.self_attn.k_proj.bias | model.layers.8.self_attn | k_proj.bias |
| model.layers.8.self_attn.inner.linear | model.layers.8.self_attn | inner.linear |
This makes the exported layer weights easy to load with torch.load or directly into submodules.
FAQ & Notes
- If the specified
--layerprefix doesn't match any weights exactly, the script will raise an exception and not write a file. - If no
.safetensorsfiles are found in the directory, an error will be raised.
Contributing
Contributions are welcome! Please submit pull requests or open issues on the GitHub repository.
License
This project is licensed under the MIT License.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file safetensors_layer_grabber-0.1.0a3.tar.gz.
File metadata
- Download URL: safetensors_layer_grabber-0.1.0a3.tar.gz
- Upload date:
- Size: 4.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6afe37a976c08bc10b74c923cedc68718778c382f6c8a06209bbe225c7cd3996
|
|
| MD5 |
6696d44af4d5c991a736e2ef1db81466
|
|
| BLAKE2b-256 |
6c563613941fbadeb0cd62b629227b513ed5f8aa5e0dedfe6708cd9f98aa825f
|
File details
Details for the file safetensors_layer_grabber-0.1.0a3-py2.py3-none-any.whl.
File metadata
- Download URL: safetensors_layer_grabber-0.1.0a3-py2.py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.12
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
43d1de144c1a031bc395600fdc2daea198bc7ee27b5cc335d8f7744390e884d5
|
|
| MD5 |
0688029155a3f8166f685500ea7860fe
|
|
| BLAKE2b-256 |
f1b4b5d3c68fd71f8268b46542dcebcb28b29677f0c8783efe8b220ea148da13
|