Converts torch models into PyTrees for Equinox
Project description
statedict2pytree
The goal of this package is to simplify the conversion from PyTorch models into JAX PyTrees (which can be used e.g. in Equinox). The way this works is by putting both models side my side and aligning the weights in the right order. Then, all statedict2pytree is doing, is iterating over both lists and matching the weight matrices.
Usually, if you declared the fields in the same order as in the PyTorch model, you don't have to rearrange anything -- but the option is there if you need it.
(Theoretically, you can rearrange the model in any way you like - e.g. last layer as the first layer - as long as the shapes match!)
Get Started
Installation
Run
pip install statedict2pytree
Basic Example
import equinox as eqx
import jax
import torch
import statedict2pytree as s2p
def test_mlp():
in_size = 784
out_size = 10
width_size = 64
depth = 2
key = jax.random.PRNGKey(22)
class EqxMLP(eqx.Module):
mlp: eqx.nn.MLP
batch_norm: eqx.nn.BatchNorm
def __init__(self, in_size, out_size, width_size, depth, key):
self.mlp = eqx.nn.MLP(in_size, out_size, width_size, depth, key=key)
self.batch_norm = eqx.nn.BatchNorm(out_size, axis_name="batch")
def __call__(self, x, state):
return self.batch_norm(self.mlp(x), state)
jax_model = EqxMLP(in_size, out_size, width_size, depth, key)
class TorchMLP(torch.nn.Module):
def __init__(self, in_size, out_size, width_size, depth):
super(TorchMLP, self).__init__()
self.layers = torch.nn.ModuleList()
self.layers.append(torch.nn.Linear(in_size, width_size))
for _ in range(depth - 1):
self.layers.append(torch.nn.Linear(width_size, width_size))
self.layers.append(torch.nn.Linear(width_size, out_size))
self.batch_norm = torch.nn.BatchNorm1d(out_size)
def forward(self, x):
for layer in self.layers[:-1]:
x = torch.relu(layer(x))
x = self.batch_norm(self.layers[-1](x))
return x
torch_model = TorchMLP(in_size, out_size, width_size, depth)
state_dict = torch_model.state_dict()
s2p.start_conversion(jax_model, state_dict)
if __name__ == "__main__":
test_mlp()
There exists also a function called s2p.convert
which does the actual conversion:
class Field(BaseModel):
path: str
shape: tuple[int, ...]
class TorchField(Field):
pass
class JaxField(Field):
type: str
def convert(
jax_fields: list[JaxField],
torch_fields: list[TorchField],
pytree: PyTree,
state_dict: dict,
):
...
If your models already have the right "order", then you might as well use this function directly. Note that the lists jax_fields
and torch_fields
must have the same length and each matching entry must have the same shape!
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
Built Distribution
Hashes for statedict2pytree-0.2.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 82201c96bbf23d395bc61d86e73476e7bd854324ec60a7aed8bc4aea9611b112 |
|
MD5 | 48d42364866f9d77fe70a0ddb6bfdee3 |
|
BLAKE2b-256 | f6b9e14033d347f6f28fb8e146921f1a8c808775bba2b86ba497418f40fb760d |