This release is a pre-release and may not be stable for production use.
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.
Release files for safetensors-layer-grabber 0.1.0a3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| safetensors_layer_grabber-0.1.0a3.tar.gz | 4.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| safetensors_layer_grabber-0.1.0a3-py2.py3-none-any.whl | Python 2, Python 3 | none | any | Details |
Total release size: 8.9 kB
Release files / safetensors_layer_grabber-0.1.0a3.tar.gz
| Download URL | safetensors_layer_grabber-0.1.0a3.tar.gz |
|---|---|
| Size | 4.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6afe37a976c08bc10b74c923cedc68718778c382f6c8a06209bbe225c7cd3996
|
|
BLAKE2b-256 checksum How to use checksums |
6c563613941fbadeb0cd62b629227b513ed5f8aa5e0dedfe6708cd9f98aa825f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.12
|
Release files / safetensors_layer_grabber-0.1.0a3-py2.py3-none-any.whl
| Download URL | safetensors_layer_grabber-0.1.0a3-py2.py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 2 Python 3 |
|
SHA-256 checksum How to use checksums |
43d1de144c1a031bc395600fdc2daea198bc7ee27b5cc335d8f7744390e884d5
|
|
BLAKE2b-256 checksum How to use checksums |
f1b4b5d3c68fd71f8268b46542dcebcb28b29677f0c8783efe8b220ea148da13
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.13.12
|