Skip to main content

PPO for stateful/recurrent networks in JAX and flax.nnx.

Project description

nnx-ppo

Experimental implementation of Proximal Policy Optimization in JAX, with first-class support for recurrent/stateful networks. Networks are built with flax.nnx; environments follow the MuJoCo Playground API.

Status: experimental — the API may change without notice.

Highlights

  • Stateful modules. Recurrent layers, delayed connections, and noisy / variational populations are all first-class citizens. Carry state is threaded through rollout collection and multi-epoch loss replay, and is reset correctly when the environment resets — something other JAX RL libraries (e.g. Brax) do not natively support.
  • PyTree observations. Observations can be arbitrary nested dicts, which makes it easy to route different streams (proprioception, vision, imitation targets, …) to different parts of a network.
  • PyTree actions and rewards. Actions and rewards are also allowed to be PyTrees, which simplifies multi-actuator and multi-agent setups.

Installation

pip install nnx_ppo

nnx-ppo installs the CPU build of JAX by default. For a CUDA 12 GPU build:

pip install nnx_ppo "jax[cuda12]"

Optional extras:

  • nnx_ppo[examples]brax, wandb, and playground (import name mujoco_playground) for the scripts in examples/.
  • nnx_ppo[dev] — test-suite dependencies (pytest, pyright, beartype, absl-py, plus playground for the env-driven tests).

Quick example

from flax import nnx
import mujoco_playground

from nnx_ppo.algorithms import ppo
from nnx_ppo.algorithms.config import TrainConfig, PPOConfig, EvalConfig
from nnx_ppo.networks.factories import make_mlp_actor_critic
from nnx_ppo.wrappers import episode_wrapper

env = mujoco_playground.registry.load("CartpoleBalance")
train_env = episode_wrapper.EpisodeWrapper(env, 1000)

rngs = nnx.Rngs(0)
nets = make_mlp_actor_critic(
    env.observation_size,
    env.action_size,
    actor_hidden_sizes=[64] * 4,
    critic_hidden_sizes=[256] * 2,
    rngs=rngs,
    normalize_obs=True,
)

result = ppo.train_ppo(
    train_env,
    nets,
    TrainConfig(
        ppo=PPOConfig(n_envs=1024, rollout_length=30, total_steps=10_000_000),
        eval=EvalConfig(enabled=True, every_steps=500_000, n_envs=64,
                        max_episode_length=1000),
    ),
    eval_env=env,
)
print(f"Final eval reward: {result.eval_history[-1]['episode_reward_mean']}")

See examples/wandb_logging.py for a complete training script with W&B logging and video recording.

Documentation

Full documentation, tutorials, and API reference are at https://nnx-ppo.readthedocs.io.

License

BSD 3-Clause — see 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

nnx_ppo-0.2.1.tar.gz (74.0 kB view details)

Uploaded Source

Built Distribution

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

nnx_ppo-0.2.1-py3-none-any.whl (96.7 kB view details)

Uploaded Python 3

File details

Details for the file nnx_ppo-0.2.1.tar.gz.

File metadata

  • Download URL: nnx_ppo-0.2.1.tar.gz
  • Upload date:
  • Size: 74.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nnx_ppo-0.2.1.tar.gz
Algorithm Hash digest
SHA256 7cd21efbd2165ff4e6dbe7afcfa678e22fb3fc9ce4d28fbb568e577f8547ba33
MD5 84550384bc3c25bb4e9a782bf99060d7
BLAKE2b-256 8473743753417ed282851b0ec47894b550573729cd1326f09cd4d5d327c5e339

See more details on using hashes here.

Provenance

The following attestation bundles were made for nnx_ppo-0.2.1.tar.gz:

Publisher: release.yml on emiwar/nnx-ppo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file nnx_ppo-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: nnx_ppo-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 96.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for nnx_ppo-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 24ce4cfcd836df3b4cd690a86312f73a2da5b4d51a82433c5c3752f2d27b089d
MD5 98bb026add2b4325116b6a7064077a21
BLAKE2b-256 1d40e15d368d9cf40f1a48bafc0f828a9e3a69b0308197b978f6a89a413f1b1c

See more details on using hashes here.

Provenance

The following attestation bundles were made for nnx_ppo-0.2.1-py3-none-any.whl:

Publisher: release.yml on emiwar/nnx-ppo

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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