Skip to main content

Rehydrax: A utility library to rehydrate stablehlo into JAX.

Project description

Rehydrax

Rehydrate stablehlo into jax.

[!WARNING] This code is still experimental, all ops are not handled yet.

Install

Jax only

pip install rehydrax

with torch

pip install rehydrax[torch] torch torch-xla-cuda-plugin@https://storage.googleapis.com/pytorch-xla-releases/wheels/cuda/12.1/torch_xla_cuda_plugin-2.5.0-py3-none-any.whl

Examples

Loading pytorch model into jax

model = MyModel()
inputs_torch = (torch.randn((16,), dtype=torch.float32),)

# Convert out model to jax
model_state, model_f = rehydrax.rehydrate_torch_module(model, inputs_torch)
inputs_jax = (jax.numpy.array(inputs_torch[0].detach().numpy()),)

# Using our model as any other jax functions
model_grad = jax.grad(lambda state, x: jnp.sum(model_f(state, x)))
model_grad_jitted = jax.jit(model_grad)
grad = model_grad_jitted(model_state, *inputs_jax)

Loading model from other project

You may wan't to just try to use model from another project but don't wan't to depend on that project for any reason. You can do that by just exporting the model from project A and just rehydrate the model on project B.

Project A

@jax.jit
def init(rng):
    model = Model(rngs=nnx.Rngs(rng))
    return nnx.split(model)[1]

@jax.jit
def forward(state, x):
    model = Model(nnx.Rngs(0))
    model_graph = nnx.split(model)[0]
    model = nnx.merge(model_graph, state)
    return model(x)

key = jax.random.PRNGKey(0)

init_lowered = init.lower(key)
init_stablehlo = init_lowered.as_text()
state_abstract = jax.eval_shape(init, key)

sample = jax.random.uniform(key, (1, 2), jnp.float32)
forward_lowered = forward.lower(state_abstract, sample)
forward_stablehlo = forward_lowered.as_text()

Project B

init_rehydrated = rehydrax.rehydrate_stablehlo(init_stablehlo)
state = init_rehydrated(key)
forward_rehydrated = rehydrax.rehydrate_stablehlo(forward_stablehlo)
y2 = forward_rehydrated(*state, sample)

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

rehydrax-0.1.0.tar.gz (11.4 kB view details)

Uploaded Source

Built Distribution

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

rehydrax-0.1.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

File details

Details for the file rehydrax-0.1.0.tar.gz.

File metadata

  • Download URL: rehydrax-0.1.0.tar.gz
  • Upload date:
  • Size: 11.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.24

File hashes

Hashes for rehydrax-0.1.0.tar.gz
Algorithm Hash digest
SHA256 0cc45a68350aa3f4f989db64ba1db879c6d8278df5ff5c673df85a36b643f103
MD5 4ac77e24b3043d7b62ac5ae3777719ea
BLAKE2b-256 261072598c52052b12af5733529513fe61c8ee8cece31ff5ac56ccca70ccb7dd

See more details on using hashes here.

File details

Details for the file rehydrax-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: rehydrax-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 16.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.4.24

File hashes

Hashes for rehydrax-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e6331f70a627d8bb04549803114087a48e6557e9c97f25ee8553b296532d0808
MD5 967f8e15f64bb1e1acb8c15b0f496fa9
BLAKE2b-256 b917bc70184d25e3c21a6d8a363f4ece61cbeff71cff26d167cda3f3627b2d6a

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