Skip to main content

Reinforcement learning for text generation on MLX (Apple Silicon): GRPO/GSPO, environments, rollout, rewards, LoRA/QLoRA

Project description

TextPolicy

Reinforcement learning toolkit for text generation on MLX (Apple Silicon). TextPolicy provides algorithms (GRPO/GSPO), text-generation environments, a rollout runner, reward functions with a decorator registry, and LoRA/QLoRA utilities.

Install (uv)

uv add textpolicy

Optional model integration:

uv add mlx mlx-lm

Quickstart

Working example using a real model and tokenizer (mlx-lm required):

import mlx.core as mx
import textpolicy as tp
from textpolicy import load_model, create_policy
from textpolicy.environment.text_generation import TextGenerationEnv
from textpolicy.rollout import RolloutRunner, create_strategy

# 1) Load model and tokenizer (mlx-lm)
model, tokenizer = load_model("Qwen/Qwen3-0.6B")

# 2) Create a policy (controls generation)
generation_params = {"max_tokens": 25, "temperature": 0.7}
policy_fn = create_policy(model, tokenizer, generation_params)

# 3) Define a reward function (env uses this to score responses)
@tp.reward
def length_reward(prompt: str, completion: str, example: dict, **kwargs) -> float:
    return float(len(completion.split()))

# 4) Create an environment (requires a tokenizer)
env = TextGenerationEnv(["What is AI?"], length_reward, tokenizer=tokenizer)

# 5) Collect one rollout step
strategy = create_strategy('grpo')
runner = RolloutRunner(env, policy=policy_fn, strategy=strategy, max_steps=1)
buffer = runner.collect()
print(len(buffer.episodes))

Docs:

  • Quickstart: docs/QUICKSTART_UV.md
  • LoRA/QLoRA: docs/10_lora_qlora.md
  • Full index: docs/index.md

FAQ:

  • Do I need a model?
    • Yes for generation with create_policy. Use load_model() (mlx‑lm) to get (model, tokenizer). For reward‑only code (no generation), a model is not required.
  • Do I need a tokenizer?
    • Yes. Both TextGenerationEnv and TextGenerationEnvironment require a tokenizer. load_model() returns one for mlx‑lm models.
  • How do I control generation?
    • Pass generation_params to create_policy (for example, max_tokens, temperature, top_p, repetition_penalty).
  • What does step() return?
    • A dict with observation, reward, terminated, truncated, info. The runner enforces this.

Examples:

  • 01–06: reward functions, batch processing, minimal training
  • 08: GRPO training with rollout + buffer
  • 09–10: length reduction (GRPO/GSPO)
  • 11: LoRA/QLoRA configuration

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

textpolicy-0.1.4.tar.gz (197.5 kB view details)

Uploaded Source

Built Distribution

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

textpolicy-0.1.4-py3-none-any.whl (171.4 kB view details)

Uploaded Python 3

File details

Details for the file textpolicy-0.1.4.tar.gz.

File metadata

  • Download URL: textpolicy-0.1.4.tar.gz
  • Upload date:
  • Size: 197.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.16

File hashes

Hashes for textpolicy-0.1.4.tar.gz
Algorithm Hash digest
SHA256 0f046a298a3c2f6afbe926f6eab4fb71871d52a5c5897d8b201528145c46c9ba
MD5 3f5656b4164c1e2f3ce9d7226fbcb416
BLAKE2b-256 06d7c5b9aeae720bc43fc747bd2e6ab66730078ef6e67795c85e1f57e394b2ee

See more details on using hashes here.

File details

Details for the file textpolicy-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: textpolicy-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 171.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.7.16

File hashes

Hashes for textpolicy-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d0399e1a704296d0abdef0f57d2171294bcaee5e3c11c2b755392aa1254f3ae2
MD5 61a46aedca70b7d6ef35ea1fba54ad48
BLAKE2b-256 6a9f8443a01ea8e63cc2935bdf7302a14f6def9b456d5fb7d6d71ad3e209faa3

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