Simpo code reproduction

From XuShuwenn/RedAct · View interactive version

Simpo code reproduction is an AI agent skill from the GitHub repository XuShuwenn/RedAct. Implement SimPO loss function for language model alignment based on paper specifications and generate loss matrix.

How to use this skill

Skill file

SimPO Loss Implementation

When to Use

Input

Task

  1. Read SimPO paper to understand loss formula

1a. Before editing simpo_loss, inspect the surrounding SimPOTrainer code in /root/SimPO/scripts/simpo_trainer.py, relevant call sites/config such as /root/SimPO/scripts/simpo_config.py, and the full unit test in /root/SimPO/unit_test/unit_test_1.py so the implementation matches this repository's actual interface, tensor semantics, and expected outputs.

1a.i. Read the full current simpo_loss method body itself, not just search hits or truncated snippets, plus enough nearby helper logic to understand its inputs, returned values, class attributes, and any alternate branches before making any edit.

1a.ii. While inspecting, identify repository-specific fields, helpers, and branches that affect loss behavior (for example get_batch_logps, get_batch_loss_metrics, beta, gamma_beta_ratio, loss_type, label_smoothing, reward/logit conventions, return shapes, and detached metrics) and implement against those concrete expectations.

1a.iii. Determine from the repo/test whether inputs like policy_chosen_logps and policy_rejected_logps are raw sequence log-prob sums, already sequence-aggregated values, or length-normalized average log probabilities; if they are already averaged/normalized, do not divide by sequence length again when applying the SimPO formula.

1a.iv. If any file or PDF view is truncated, continue reading targeted ranges until you have the complete simpo_loss body, nearby helper/config logic, and the full unit test assertions before coding.

1b. Use the paper to confirm the loss formula, but validate assumptions against repository code; do not implement from partial snippets, summaries, or guessed normalization/scaling behavior.

1c. Explicitly map paper variables onto this repository's existing quantities before coding so you do not reapply normalization or mis-scale beta/gamma terms; if the paper uses a parameter not stored directly, derive it from existing config fields used by this codebase (for example, the effective gamma from beta * gamma_beta_ratio when applicable).

1d. Do not code from unreadable or partial paper extraction output. If the formula is not directly visible from a trustworthy paper excerpt, keep reading targeted paper/code/test sections until you can confirm the exact formula and map each term to local variables.

1e. Before editing simpo_loss, verify from the paper and local code/test together how beta, gamma_beta_ratio, loss_type, and label_smoothing are used, what tensors the function must return, and whether nearby reward/log-prob, reduction, or normalization branches affect the implementation.

  1. Implement simpo_loss function in SimPOTrainer class

2a. Reopen the exact simpo_loss region in /root/SimPO/scripts/simpo_trainer.py and patch only that real function body; do not use blind/global replacement or edits based on task text, summaries, or guessed snippets.

2b. Immediately after editing, re-read the modified function to confirm the change landed in the intended region, then run a lightweight file-level validation such as python -m py_compile /root/SimPO/scripts/simpo_trainer.py or an equivalent minimal import check before the full unit test.

  1. Run unit test to verify implementation
  2. Save loss to /root/loss.npz with key 'losses'
  3. Follow the critical path: inspect code/test/paper context -> implement only the minimal change needed in simpo_loss -> run the provided unit test -> save /root/loss.npz with key losses -> write /root/python_info.txt.
  4. Before finalizing, verify correctness against both the paper formula and the local trainer/test conventions, not just a passing test.
  1. Use this execution order unless a concrete traceback forces otherwise: inspect full trainer/config/test and readable paper context -> implement only the minimal simpo_loss change -> run python /root/SimPO/unit_test/unit_test_1.py -> save /root/loss.npz with key losses -> write /root/python_info.txt -> verify both files.
  2. If the unit test initially fails, capture the complete traceback before any install/upgrade, make only the smallest evidence-based environment or execution-context fix, then rerun the same real test immediately.
  3. Use a hard completion checklist before stopping: direct inspection completed, simpo_loss edited and read back, the provided unit test run successfully, /root/loss.npz saved with key losses, /root/python_info.txt updated after any package changes, and if the runtime requires a specific action schema or completion token, follow it exactly and finish with that exact completion signal.

Output

Workflow Guardrails

Tips

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.