Skip to main content

High-performance Rust core for StateSet Agents (GAE, group advantages, GSPO importance ratios, reward normalization). Optional accelerator for the stateset-agents Python framework.

Project description

stateset-rl-core

High-performance Rust implementations of reinforcement learning operations, with optional Python bindings via PyO3.

Features

  • GAE (Generalized Advantage Estimation) - Fast, parallel GAE computation
  • Advantage Computation - Group-relative advantages for GRPO/PPO training
  • Reward Normalization - Welford's algorithm for numerically stable online normalization
  • GSPO Support - Sequence-level importance ratios and clipping for GSPO
  • PPO Surrogate - Clipped surrogate objective computation
  • Parallel Processing - Automatic parallelization via Rayon

Installation

As a Python extension (recommended)

Pre-built abi3 wheels are published to PyPI on each rust-core-v* tag. Install directly or via the [rust] extra on stateset-agents:

# Standalone
pip install stateset-rl-core

# Bundled with the framework
pip install "stateset-agents[rust]"

One wheel per (OS, arch) covers Python 3.10–3.13 via the stable PyO3 ABI. Supported targets: Linux x86_64/aarch64 (manylinux2014), macOS x86_64/arm64, Windows x86_64.

As a Rust crate

[dependencies]
stateset-rl-core = "0.1"

From source (development)

cd rust_core
maturin develop --release

Usage

Rust

use stateset_rl_core::{compute_gae_internal, compute_advantages_for_group};

// Compute GAE
let rewards = vec![1.0, 0.0, 1.0, 0.0];
let values = vec![0.5, 0.5, 0.5, 0.5, 0.0]; // n+1 values for bootstrap
let advantages = compute_gae_internal(&rewards, &values, 0.99, 0.95);

// Compute group-relative advantages
let group_rewards = vec![1.0, 2.0, 3.0, 4.0];
let advantages = compute_advantages_for_group(&group_rewards, "mean", true);

Python

import numpy as np
import stateset_rl_core

# Compute GAE
rewards = np.array([1.0, 0.0, 1.0, 0.0])
values = np.array([0.5, 0.5, 0.5, 0.5, 0.0])
advantages = stateset_rl_core.compute_gae(rewards, values, gamma=0.99, gae_lambda=0.95)

# Batch GAE (parallel)
all_rewards = [np.random.randn(100) for _ in range(32)]
all_values = [np.random.randn(101) for _ in range(32)]
all_advantages = stateset_rl_core.batch_compute_gae(all_rewards, all_values)

# Group-relative advantages for GRPO
rewards_2d = np.random.randn(16, 4)  # 16 groups, 4 samples each
advantages = stateset_rl_core.compute_group_advantages(rewards_2d, "mean", normalize=True)

# Reward normalization with running stats
rewards = np.array([1.0, 2.0, 3.0])
normalized, mean, var, count = stateset_rl_core.normalize_rewards(rewards)

# GSPO importance ratios
log_probs_new = np.array([-10.0, -12.0, -11.0])
log_probs_old = np.array([-10.5, -11.5, -11.0])
seq_lengths = np.array([50, 60, 55])
ratios = stateset_rl_core.compute_gspo_importance_ratios(log_probs_new, log_probs_old, seq_lengths)

# PPO surrogate objective
ratios = np.array([1.1, 0.9, 1.05])
advantages = np.array([1.0, -1.0, 0.5])
objectives = stateset_rl_core.compute_ppo_surrogate(ratios, advantages, clip_epsilon=0.2)

API Reference

GAE Functions

  • compute_gae(rewards, values, gamma=0.99, gae_lambda=0.95) - Single trajectory GAE
  • batch_compute_gae(all_rewards, all_values, gamma=0.99, gae_lambda=0.95) - Parallel batch GAE

Advantage Functions

  • compute_group_advantages(rewards_2d, baseline_type, normalize) - GRPO-style group advantages
    • baseline_type: "mean", "median", or "min"

Reward Functions

  • normalize_rewards(rewards, running_mean=0, running_var=1, count=0, epsilon=1e-8) - Online normalization
  • clip_rewards(rewards, min_val, max_val) - Reward clipping
  • compute_reward_statistics(rewards) - Compute mean, std, min, max, median

Policy Gradient Functions

  • compute_gspo_importance_ratios(log_probs_new, log_probs_old, sequence_lengths) - GSPO ratios
  • apply_gspo_clipping(ratios, advantages, clip_left=3e-4, clip_right=4e-4) - GSPO clipping
  • compute_ppo_surrogate(ratios, advantages, clip_epsilon=0.2) - PPO clipped objective

Performance

This crate is optimized for performance:

  • LTO enabled - Link-time optimization for maximum speed
  • Single codegen unit - Better optimization opportunities
  • Rayon parallelization - Automatic multi-threading for batch operations
  • Zero-copy Python interop - Minimal overhead when called from Python

Typical speedups over pure Python/NumPy: 10-100x for batch operations.

License

BUSL-1.1

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

stateset_rl_core-0.1.1.tar.gz (17.9 kB view details)

Uploaded Source

Built Distributions

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

stateset_rl_core-0.1.1-cp310-abi3-win_amd64.whl (206.8 kB view details)

Uploaded CPython 3.10+Windows x86-64

stateset_rl_core-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (297.3 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ x86-64

stateset_rl_core-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (282.4 kB view details)

Uploaded CPython 3.10+manylinux: glibc 2.17+ ARM64

stateset_rl_core-0.1.1-cp310-abi3-macosx_11_0_arm64.whl (261.8 kB view details)

Uploaded CPython 3.10+macOS 11.0+ ARM64

stateset_rl_core-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl (275.7 kB view details)

Uploaded CPython 3.10+macOS 10.12+ x86-64

File details

Details for the file stateset_rl_core-0.1.1.tar.gz.

File metadata

  • Download URL: stateset_rl_core-0.1.1.tar.gz
  • Upload date:
  • Size: 17.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.16

File hashes

Hashes for stateset_rl_core-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1a22d3751f8f981ee1fbbf66a45d8974dbe97ac5c7c8a0696057961c5380dfc7
MD5 07fa2135d540b03d066c9818ea99837d
BLAKE2b-256 d8331db1cd26ce295f791d893294c03c453255ff9da3316a1cb42e710fd05e1f

See more details on using hashes here.

File details

Details for the file stateset_rl_core-0.1.1-cp310-abi3-win_amd64.whl.

File metadata

File hashes

Hashes for stateset_rl_core-0.1.1-cp310-abi3-win_amd64.whl
Algorithm Hash digest
SHA256 e0f603ef349d413e466f91d1761a4305ccd0e2e4b825b29d619ce3c2650f4b40
MD5 42fd1864d670ef99552c5c150819a782
BLAKE2b-256 135f45ed3ee64073ee9c20f7c629d185afbaa2c9c882af12261dd62f3ae6c1e6

See more details on using hashes here.

File details

Details for the file stateset_rl_core-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.

File metadata

File hashes

Hashes for stateset_rl_core-0.1.1-cp310-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
Algorithm Hash digest
SHA256 7f4528422f64dcdcd9959429296f50e79d656d0d1b1fc54bc767b529ebd9a074
MD5 44ae57fbc2bc6c4190024684ea7e90a7
BLAKE2b-256 227aaa7c1d1904d1c167e5dcbcf1935ac85e29e3dfcb2d6a515f2f70637fd9e1

See more details on using hashes here.

File details

Details for the file stateset_rl_core-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl.

File metadata

File hashes

Hashes for stateset_rl_core-0.1.1-cp310-abi3-manylinux_2_17_aarch64.manylinux2014_aarch64.whl
Algorithm Hash digest
SHA256 2f910474d97cf1752320906122212221db5d211aeb062464a448c5f9f132934d
MD5 1ae3975a144b131e62d41bdef6a4d06e
BLAKE2b-256 9312970386e99d148753f69f17859085d56690ac8794cee6391b60415e1f92f3

See more details on using hashes here.

File details

Details for the file stateset_rl_core-0.1.1-cp310-abi3-macosx_11_0_arm64.whl.

File metadata

File hashes

Hashes for stateset_rl_core-0.1.1-cp310-abi3-macosx_11_0_arm64.whl
Algorithm Hash digest
SHA256 8b9de36ecebbda123051c5a3bcf6fcfb9a95923b9d350f5aff3dcb2872db1db4
MD5 4fa5ce0598a859b64734db907f430bc8
BLAKE2b-256 07d9d02859a50788962b992869827d11cb902ea60b0b5dc86a93acf90144fb38

See more details on using hashes here.

File details

Details for the file stateset_rl_core-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl.

File metadata

File hashes

Hashes for stateset_rl_core-0.1.1-cp310-abi3-macosx_10_12_x86_64.whl
Algorithm Hash digest
SHA256 701de8400fa712ef76c00eefbffb83ba808d4b3b8322eb8f1912482d2f192ae4
MD5 3a760cac1a20a980e0fbbf5107259e0d
BLAKE2b-256 448f7190ccc4eebf8e8a835330064e2601864d0cc4c8655f8dfd2c68ce1e3e8c

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