Skip to main content

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
  • Another example:
    • Input safetensors key: model.layers.10.mlp.fc1.weight
    • Layer argument: model.layers.10.mlp
    • Output key in .ckpt: fc1.weight

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 --layer prefix doesn't match any weights exactly, the script will raise an exception and not write a file.
  • If no .safetensors files 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

safetensors_layer_grabber-0.1.0a3.tar.gz (4.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

safetensors_layer_grabber-0.1.0a3-py2.py3-none-any.whl (4.7 kB view details)

Uploaded Python 2Python 3

File details

Details for the file safetensors_layer_grabber-0.1.0a3.tar.gz.

File metadata

File hashes

Hashes for safetensors_layer_grabber-0.1.0a3.tar.gz
Algorithm Hash digest
SHA256 6afe37a976c08bc10b74c923cedc68718778c382f6c8a06209bbe225c7cd3996
MD5 6696d44af4d5c991a736e2ef1db81466
BLAKE2b-256 6c563613941fbadeb0cd62b629227b513ed5f8aa5e0dedfe6708cd9f98aa825f

See more details on using hashes here.

File details

Details for the file safetensors_layer_grabber-0.1.0a3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for safetensors_layer_grabber-0.1.0a3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 43d1de144c1a031bc395600fdc2daea198bc7ee27b5cc335d8f7744390e884d5
MD5 0688029155a3f8166f685500ea7860fe
BLAKE2b-256 f1b4b5d3c68fd71f8268b46542dcebcb28b29677f0c8783efe8b220ea148da13

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page