weightbridge 🌉
What?
A library to map (deep learning) model weights between different model implementations in Python.
Why?
Model weights trained using one implementation of an architecture typically cannot directly be loaded into a different implementation of the same architecture, due to:
- Different parameter and layer names.
- Different nesting of modules.
- Different parameter shapes (e.g.
(8, 8)vs(64)vs(1, 8, 8)). - Different order of dimensions (e.g.
(64, 48)vs(48, 64)). - Different deep learning frameworks (e.g. PyTorch, Tensorflow, Flax).
Adapting the weights manually is a tedious and error-prone process:
k = k.replace('downsample_layers.0.', 'stem.')
k = re.sub(r'stages.([0-9]+).([0-9]+)', r'stages.\1.blocks.\2', k)
k = re.sub(r'downsample_layers.([0-9]+).([0-9]+)', r'stages.\1.downsample.\2', k)
k = k.replace('pwconv', 'mlp.fc')
if 'grn' in k:
k = k.replace('grn.beta', 'mlp.grn.bias')
v = v.reshape(v.shape[-1])
k = k.replace('head.', 'head.fc.')
if v.ndim == 2 and 'head' not in k:
model_shape = model.state_dict()[k].shape
v = v.reshape(model_shape)
weightbridge does most of this work for you.
How?
import weightbridge
new_my_weights = weightbridge.adapt(their_weights, my_weights)
my_weightscontains the (random) untrained weights created at model initialization (e.g. as the result ofmodel.state_dict()in PyTorch, or usingmodel.initin Flax and Haiku).their_weightscontains the pretrained weights (e.g. as the result oftorch.load,tf.train.load_checkpointornp.load).
The output has the same structure and weight shapes as my_weights, but with the weight values from their_weights. It can be used as drop-in for my_weights, and for example be stored back into the model using model.load_state_dict in PyTorch, or be used in model.apply in Flax and Haiku.
Installation:
pip install weightbridge
Full examples:
- examples/mamba2flax.py: Download weights for Mamba and load into custom Flax implementation. Print example text.
- examples/gpt2haiku.py: Download weights for OpenAI GPT-2 and load into custom Haiku implementation. Print example text.
- examples/convnext2timm.py: Download original weights for ConvNext and ConvNextV2 and load into timm implementation. Test on an example image of ImageNet.
- examples/vit2flax.py: Download weights for a vision transformer from torchvision and load into a custom Flax implementation. Test on an example image of ImageNet.
Additional parameters:
{in_format|out_format}="{pytorch|tensorflow|flax|haiku|...}"when weights are adapted between different deep learning frameworks (to permute weight axes as required).hints=[...]to provide additional hints when ambiguous matches cannot be resolved. weightbridge prints an error when this happens, for example:
We can passFailed to pair the following nodes OUT load_prefix/encode/stage3/block6/reduce/linear/w ((262144,),) OUT load_prefix/encode/stage3/block6/expand/linear/w ((262144,),) IN backbone.0.body.layer3.5.conv1.weight ((262144,),) IN backbone.0.body.layer3.5.conv3.weight ((262144,),)hints=[("reduce", "conv1")](consisting of some uniquely identifying substrings) to resolve the matching failure.cache="some-file"to store the mapping in a file and reuse it in subsequent calls. If it is not an absolute path, the file is created in the directory of the module from whichweightbridge.adaptis called.verbose=Trueto print the matching steps and the final mapping between weights.
weightbridge internally uses a set of heuristics based on the weights' names and shapes to iteratively find mappings between subsets of my_weights and their_weights, until a unique pairing between all weights is found.
What does weightbridge not do?
- Model implementation: weightbridge does not implement the model, but adapts the weights once the model is implemented (athough it provides a partial sanity-check for the implementation by ensuring that a mapping between the two sets of weights is possible).
When the architecture is implemented using different operations, the weights have to be adapted manually. E.g. in Transformer attention, queries, keys and values can be inferred using different operations:
# Option 1 x = nn.Linear(features=3 * c)(x) q, k, v = jnp.split(x, 3, axis=-1) # Option 2 q = nn.Linear(features=c)(x) k = nn.Linear(features=c)(x) v = nn.Linear(features=c)(x)
The corresponding weights have to be split/ concatenated manually and will not be matched by weightbridge otherwise, since it relies on a one-to-one mapping between weights. - Hyperparameters: weightbridge does not ensure that hyperparameters like
nn.LayerNorm(epsilon=1e-6)ornn.Conv(padding="SAME")are set correctly (although some hyperparameters likeuse_bias={True|False}will raise an exception if not set correctly).
Metadata
Release files for weightbridge 0.0.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| weightbridge-0.0.4.tar.gz | 18.9 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| weightbridge-0.0.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 41.0 kB
Release files / weightbridge-0.0.4.tar.gz
| Download URL | weightbridge-0.0.4.tar.gz |
|---|---|
| Size | 18.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
350ad9b03cf695ec8c12d367e82de2e9cb36c33229a87ac302ba531e4c1a2c43
|
|
BLAKE2b-256 checksum How to use checksums |
b946e003e57b91d68860aec31424f3de985d925c0f771c39c42aed54b799dac9
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|
Release files / weightbridge-0.0.4-py3-none-any.whl
| Download URL | weightbridge-0.0.4-py3-none-any.whl |
|---|---|
| Size | 22.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7cbf3a032eea4522e4dbbd5896cfa4ab1a4d650223b320f4716a436a10bdcedc
|
|
BLAKE2b-256 checksum How to use checksums |
e406adb7bd5c800193d13c791f9f1fd4caeda4b47490560c595ae461709eca69
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/6.2.0 CPython/3.10.12
|
Security for weightbridge 0.0.4
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.