Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions docs/algorithms.md
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ kwargs = { eps = 1e-8 }

`AdvantageInputs.rollouts` is a list of `verifiers.RolloutOutput`, so you have access to the full rollout (turns, tool calls, custom metadata) — not just the reward. Use this for anything reward-shaping-like that needs trajectory context.

### Per-Env Advantage

`advantage` can be set per training environment. Each env inherits the top-level `[orchestrator.advantage]` when it doesn't set its own, so mixed-env runs can give each env its own advantage computation:

```toml
[orchestrator.advantage]
type = "default" # the default every env inherits unless it overrides

[[orchestrator.train.env]]
id = "math-env" # inherits the default above

[[orchestrator.train.env]]
id = "agent-env"
advantage = { type = "custom", import_path = "my_module.normalized_advantage" }
```

## Filters

Filters drop rollouts between scoring and training. Built-ins (composable):
Expand Down
96 changes: 53 additions & 43 deletions packages/prime-rl-configs/src/prime_rl/configs/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,49 @@ def _deprecate_max_tokens(cls, data: Any) -> Any:
return data


class TokensLengthPenaltyConfig(BaseConfig):
type: Literal["tokens"] = "tokens"

completion_weight: float = Field(1.0, ge=0, allow_inf_nan=False)
"""Weight on model completion tokens. Finite and non-negative."""

tool_response_weight: float = Field(1.0, ge=0, allow_inf_nan=False)
"""Weight on tool-response tokens (read from the rollout's ``*_total_tool_response_tokens`` harness metric; 0 if absent). Finite and non-negative."""


class TurnsLengthPenaltyConfig(BaseConfig):
type: Literal["turns"] = "turns"


LengthPenaltyConfig: TypeAlias = Annotated[
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

wondering if default should not just be plain reward - mean(reward). and the length penalty addition is a custom function

TokensLengthPenaltyConfig | TurnsLengthPenaltyConfig,
Field(discriminator="type"),
]


class DefaultAdvantageConfig(BaseConfig):
type: Literal["default"] = "default"

length_penalty: LengthPenaltyConfig | None = None
"""Correctness-gated length penalty. ``tokens`` shapes by weighted token cost; ``turns`` shapes by trajectory turn count; None disables shaping. In mixed groups, lower-cost correct rollouts get amplified advantage (up to 2x), higher-cost correct rollouts are unchanged, incorrect untouched. In all-correct groups, below-average-cost rollouts get advantage in [0, 1], others get 0."""


class CustomAdvantageConfig(BaseConfig):
type: Literal["custom"] = "custom"

import_path: str
"""Import path to the advantage function (e.g. ``my_module.my_advantage``)."""

kwargs: dict[str, Any] = Field(default_factory=dict)
"""Kwargs forwarded to the advantage function."""


AdvantageConfig: TypeAlias = Annotated[
DefaultAdvantageConfig | CustomAdvantageConfig,
Field(discriminator="type"),
]


class EnvConfig(BaseConfig):
id: str = "reverse-text"
"""Registered verifiers environment ID (e.g. ``math-env``, ``primeintellect/math-env``). May include an ``@version`` suffix for installation."""
Expand Down Expand Up @@ -214,6 +257,11 @@ class TrainEnvConfig(EnvConfig):
"""Rollouts generated per example for GRPO group-relative advantages.
Inherits from ``orchestrator.group_size`` when unset."""

advantage: AdvantageConfig | None = None
"""Advantage strategy for this env's GRPO groups. Inherits from the top-level
``orchestrator.advantage`` when unset; set a different ``default``/``custom``
config to give this env its own advantage computation."""


class EvalEnvConfig(EnvConfig):
sampling: EvalSamplingConfig = EvalSamplingConfig()
Expand Down Expand Up @@ -374,49 +422,6 @@ class CheckpointConfig(BaseConfig):
"""Skip loading the progress from checkpoint."""


class TokensLengthPenaltyConfig(BaseConfig):
type: Literal["tokens"] = "tokens"

completion_weight: float = Field(1.0, ge=0, allow_inf_nan=False)
"""Weight on model completion tokens. Finite and non-negative."""

tool_response_weight: float = Field(1.0, ge=0, allow_inf_nan=False)
"""Weight on tool-response tokens (read from the rollout's ``*_total_tool_response_tokens`` harness metric; 0 if absent). Finite and non-negative."""


class TurnsLengthPenaltyConfig(BaseConfig):
type: Literal["turns"] = "turns"


LengthPenaltyConfig: TypeAlias = Annotated[
TokensLengthPenaltyConfig | TurnsLengthPenaltyConfig,
Field(discriminator="type"),
]


class DefaultAdvantageConfig(BaseConfig):
type: Literal["default"] = "default"

length_penalty: LengthPenaltyConfig | None = None
"""Correctness-gated length penalty. ``tokens`` shapes by weighted token cost; ``turns`` shapes by trajectory turn count; None disables shaping. In mixed groups, lower-cost correct rollouts get amplified advantage (up to 2x), higher-cost correct rollouts are unchanged, incorrect untouched. In all-correct groups, below-average-cost rollouts get advantage in [0, 1], others get 0."""


class CustomAdvantageConfig(BaseConfig):
type: Literal["custom"] = "custom"

import_path: str
"""Import path to the advantage function (e.g. ``my_module.my_advantage``)."""

kwargs: dict[str, Any] = Field(default_factory=dict)
"""Kwargs forwarded to the advantage function."""


AdvantageConfig: TypeAlias = Annotated[
DefaultAdvantageConfig | CustomAdvantageConfig,
Field(discriminator="type"),
]


# Flags rare tokens generated at high entropy (Section 5.2, https://arxiv.org/abs/2510.02387).
class GibberishFilterConfig(BaseConfig):
type: Literal["gibberish"] = "gibberish"
Expand Down Expand Up @@ -876,6 +881,11 @@ def resolve_batching(self):
if "group_size" not in env_cfg.model_fields_set:
env_cfg.group_size = self.group_size

# Propagate the top-level ``advantage`` into each train env that didn't set its own.
for env_cfg in self.train.env:
if "advantage" not in env_cfg.model_fields_set:
env_cfg.advantage = self.advantage

# Resolve train env num_workers from max_inflight_rollouts
for env_cfg in self.train.env:
if env_cfg.num_workers == "auto":
Expand Down
1 change: 0 additions & 1 deletion src/prime_rl/orchestrator/orchestrator.py
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,6 @@ async def setup(self) -> None:
mm_token_type_ids_mapping=self.mm_token_type_ids_mapping,
batch_size=config.batch_size,
token_batch_size=config.token_batch_size,
advantage_config=config.advantage,
pre_filters=pre_filters,
post_filters=post_filters,
)
Expand Down
17 changes: 10 additions & 7 deletions src/prime_rl/orchestrator/train_sink.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
import uuid
from collections import defaultdict

from prime_rl.configs.orchestrator import AdvantageConfig, OrchestratorConfig
from prime_rl.orchestrator.advantage import assign_advantages, setup_advantage_fn
from prime_rl.configs.orchestrator import OrchestratorConfig
from prime_rl.orchestrator.advantage import AdvantageFn, assign_advantages, setup_advantage_fn
from prime_rl.orchestrator.envs import TrainEnvs
from prime_rl.orchestrator.filters import RolloutFilter, apply_filters
from prime_rl.orchestrator.trajectories import (
Expand All @@ -44,7 +44,6 @@ def __init__(
mm_token_type_ids_mapping: dict[int, int] | None,
batch_size: int | None,
token_batch_size: int | None,
advantage_config: AdvantageConfig | None,
pre_filters: list[RolloutFilter],
post_filters: list[RolloutFilter],
) -> None:
Expand All @@ -58,9 +57,13 @@ def __init__(
self.mm_token_type_ids_mapping = mm_token_type_ids_mapping
self.batch_size = batch_size
self.token_batch_size = token_batch_size
# Built once — custom advantage funcs do an ``import_object`` and
# we don't want to pay that per group. ``None`` = reward-only path
self.advantage_fn = setup_advantage_fn(advantage_config) if advantage_config is not None else None
# Built once per env — custom advantage funcs do an ``import_object`` and
# we don't want to pay that per group. Each env carries its own advantage
# config (inheriting the top-level default when unset). ``None`` = reward-only path.
self.advantage_fns: dict[str, AdvantageFn | None] = {
env.name: setup_advantage_fn(env.config.advantage) if env.config.advantage is not None else None
for env in train_envs
}
Comment on lines +60 to +66
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

should we init them in the training envs themselves, might make sense if they are tighlty coupled no?

self.pre_filters = pre_filters
self.post_filters = post_filters

Expand Down Expand Up @@ -200,7 +203,7 @@ def process_group(self, group_id: uuid.UUID) -> None:
)
return

assign_advantages(survivors, self.advantage_fn)
assign_advantages(survivors, self.advantage_fns[env_name])

# Propagate to the pre-tokenized samples so the orchestrator can
# collect samples at ship time without re-walking rollouts. The env
Expand Down
Loading