algorithm-expert

A specialist guide for implementing reinforcement-learning algorithms that train models from reward signals. It covers methods such as PPO and GRPO, where the model is adjusted according to how well its actions perform.

In plain words
What is it for?
Use it when working on GRPO, PPO, DAPO, GSPO, SAPO, REINFORCE++, advantage calculations, reward functions, loss computation, or training loops.
Why use it?
It helps keep algorithm choices, reward calculations, loss formulas, and training-loop changes aligned with the project’s supported configurations.

Agent

Install

Getting it into your agent

One page per mod, every tool's command on it. A separate URL per tool would split the same page into five that compete with each other.

agentmods
npx agentmods add agents/redai-infra/relax/algorithm-expert
Clone the repo
git clone --depth 1 https://github.com/redai-infra/Relax
Per session 37 Only the description is in the session, so the agent can decide to use it. The body loads when it is invoked.
When invoked 1,314 The whole file, excluding the scripts and references it only reads on demand.
Security scan A 0 findings. Scan, not verified.
Origin original No closer match found in the catalogue.
Token cost

What it costs to keep this loaded

Counted locally with the o200k_base tokenizer, which is exact for GPT models; Claude uses its own tokenizer and its counts differ. Treat this as one consistent yardstick across the catalogue rather than a bill. Prices are per million input tokens.

ModelPer sessionOnce invoked
Fable 5 $0.00037 $0.01314
Opus 5 $0.00018 $0.00657
Sonnet 5 $0.00007 $0.00263
Haiku 4.5 $0.00004 $0.00131

Measured 2d ago against content hash e207f665125f, method: parsed. Prices are Anthropic first-party input rates as of 2026-08-30, from the pricing page.

Security

Grade A, and why

algorithm-expert scanned grade A with 0 findings against 26 rules in 11 categories — prompt injection, anti-refusal, data exfiltration, privilege escalation, supply chain, agent snooping, system-prompt leakage, SSRF and excessive agency — measured 2d ago.

A static scan of the body, not an audit. Every finding is printed with the line that produced it so you can judge whether it matters here. A mod is markdown that instructs an agent; that is exactly why what it instructs is worth reading.

Nothing flagged

None of the 26 patterns this scan looks for appear in this file: no shell pipes, no recursive deletes, no credential paths, no hidden text, no instruction-override or anti-refusal phrasing, no agent-config snooping. That is not a guarantee, it is the absence of the things that are checkable.

.opencode/agents/algorithm-expert.md · 118 lines

How it starts

The opening of the file, as written. The whole thing — 118 lines — stays where its author put it; the contents beside it link to each section on GitHub.

Algorithm Expert

Relax 中 RL 算法族的配置、损失计算和奖励函数。For project-level rules see AGENTS.md.

算法族

通过 --advantage-estimator 选择:

算法 特点 配置
GRPO (默认) Critic-free, 组归一化 --advantage-estimator grpo
PPO Critic-based, GAE --advantage-estimator gae, --kl-coef > 0
GSPO Sequence-level 重要性采样 --advantage-estimator gspo
SAPO Soft gating 替代 hard clipping compute_sapo_loss()
DAPO Dynamic batch size --use-dynamic-batch-size
REINFORCE++ Discounted REINFORCE --advantage-estimator reinforce_plus_plus
REINFORCE++ BL + leave-one-out baseline --advantage-estimator reinforce_plus_plus_baseline
OPD Teacher-student KL penalty --on-policy-distillation

核心参数

位置: relax/utils/arguments.pyget_slime_extra_args_provider()

参数 默认 说明
--eps-clip 0.2 PPO clipping range
--eps-clip-high None 非对称 clipping 上界
--eps-clip-c None Dual-clip 下界
--kl-coef 0.0 KL penalty(0 = critic-free)
--kl-loss-type k1 k1 / k2 / k3 / low_var_kl
--entropy-coef 0.0 熵正则化
--gamma 1.0 GAE 折扣因子
--lambd 1.0 GAE lambda
--n-samples-per-prompt 1 GRPO 组大小
--normalize-advantages False 跨 DP 组白化

KL Loss 类型

位置: relax/utils/training/ppo_utils.pycompute_approx_kl()

类型 公式 场景
k1 log_ratio 简单快速(默认)
k2 (log_ratio)^2 / 2 平方近似
k3 / low_var_kl 非负无偏低方差 Schulman's KL

损失计算

位置: relax/backends/megatron/loss.py

  • policy_loss_function() — PPO clipped loss + 可选 dual-clip / 非对称 clipping
  • compute_sapo_loss() — SAPO soft gating
  • value_loss_function() — 价值函数 clipping
  • 高级特性: TIS(截断重要性采样)、OPSM(序列级掩码)、OPD

Advantage 计算

  • compute_advantages_and_returns() — GAE / 组归一化
  • distributed_masked_whiten() — 跨 DP 组归一化
  • 支持 Context Parallel (CP) 掩码

奖励函数

位置: relax/engine/rewards/

文件 领域
math_utils.py 数学题验证
deepscaler.py DeepScaler
gpqa.py GPQA 评估
f1.py F1 分数
multiple_choice.py 选择题
ifbench.py IFBench
dapo_genrm.py DAPO GenRM
openr1mm.py OpenR1MM

Read the full file on GitHub · 118 lines

Changes

What this file has done since we first saw it

Hashed on every crawl. A supply-chain change to an agent config is a question of when, not whether, so the history is kept rather than the latest state alone.

  1. 2d ago First seen · 118 lines · 37 tokens per session scan A e207f665125f

Subscribe to this mod's changes

algorithm-expert is an agent published in the GitHub repository redai-infra/Relax (580 stars, last pushed 4d ago), licensed Apache-2.0. It adds 37 tokens to every session and 1,314 once invoked, about $0.0002 per session on Opus 5. A static security scan graded it A with 0 findings. No closer match exists in the catalogue, so it is treated as the original; first seen 2026-08-30.