Skip to content

support pytorch for openpi - #1

Open
yaolug wants to merge 16 commits into
kevin/pi05-supportfrom
pi05-pytorch
Open

yaolug wants to merge 16 commits into
kevin/pi05-supportfrom
pi05-pytorch

Conversation

@yaolug

@yaolug yaolug commented Aug 12, 2025

Copy link
Copy Markdown
Owner

This PR implements pytorch model under openpi. Currently it supports inference only for pi0 and pi05 models. Finetuning WIP.

I have done the following tests.

  1. inference test to use same input and noise to check output for jax and pytorch. They will have < 5% relative diff.
  2. pi05_libero, converted to pytorch ckpt and run eval, can get 93%
  3. pi05_droid, converted to pytorch ckpt and run offline eval, MSE < 0.02

TODOs:

  1. run pytest . to make sure no tests are broken

PRECISIONS = {"bfloat16": torch.bfloat16, "float32": torch.float32, "float16": torch.float16}


def flatten_for_inspection(tree, parent_key="", separator="/"):

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: you could use flax.nnx.traversals.flatten_mapping here

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done.

Comment thread src/openpi/models/model.py Outdated
import jax.numpy as jnp
import numpy as np
import orbax.checkpoint as ocp
from safetensors.torch import load_model

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: import by module, rather than by function

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

Comment thread src/openpi/models/model.py Outdated
return nnx.merge(graphdef, state)

def load_pytorch(self, train_config, weight_path: str):
print(f"train_config: {train_config}")

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

generally shouldn't print from production code, can use logger.info if you want

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

changed to logging

Comment thread src/openpi/policies/policy.py Outdated
output_transforms: Sequence[_transforms.DataTransformFn] = (),
sample_kwargs: dict[str, Any] | None = None,
metadata: dict[str, Any] | None = None,
device: str = "cpu",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe call this torch_device to make it clear that it only affects PyTorch?

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. Also added doc.

Comment thread src/openpi/policies/policy_config.py Outdated
sample_kwargs=sample_kwargs,
metadata=train_config.policy_metadata,
is_pytorch=is_pytorch,
device="cuda" if is_pytorch else None,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

actually, seems like maybe the device isn't needed since you hardcode to "cuda"

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Changed.

Comment thread src/openpi/shared/array_typing.py Outdated
T = TypeVar("T")


# Custom type checker that handles both JAX arrays and torch tensors

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused why this is necessary -- it doesn't do anything special

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Removed.

Comment thread src/openpi/shared/array_typing.py Outdated

jaxtyping._decorator._check_dataclass_annotations = _check_dataclass_annotations # noqa: SLF001

TorchTensor = torch.Tensor

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that, instead of all this, you can just redefine Array = jax.Array | torch.Tensor and it should work anywhere. And then you don't need to change the usage either, you can just do at.Float[Array, "b ..."]. jaxtyping is supposed to have built-in PyTorch support.

Copy link
Copy Markdown
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. Fixed.

@yaolug
yaolug force-pushed the pi05-pytorch branch 2 times, most recently from 698ed43 to f83909e Compare August 26, 2025 06:35
Comment thread README.md
1. Upgrade the transformers library to 4.53.2
- The required version is already specified in pyproject.toml
- If you set up your environment previously, reinstall it to ensure you have transformers 4.53.2
- You can verify the version with `pip show transformers`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- You can verify the version with `pip show transformers`
- You can verify the version with `uv pip show transformers`

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants