#10265·ms-swift

[Bug] `--reward_funcs math` crashes: MathORM still expects InferRequest objects while the GRPO reward dispatch passes completion strings

Author: LesereingrapeCreated Sep 29, 2026Updated Sep 29, 2026

Checklist / 检查清单

  • I have searched existing issues, and this is a new bug report. / 我已经搜索过现有的 issues,确认这是一个新的 bug report。

Bug Description / Bug 描述

--reward_funcs math crashes the first time rewards are computed. MathORM is the only built-in ORM that still assumes it is called with InferRequest objects (swift/rewards/orm.py:441-445 at dbc395ff):

python
def __call__(self, infer_requests: List[Union['InferRequest', Dict]], ground_truths: List[str],
             **kwargs) -> List[float]:
    rewards = []
    predictions = [request.messages[-1]['content'] for request in infer_requests]

but the single dispatch site used by both the HF and the Megatron GRPO trainers (swift/rl_core/grpo_algorithm.py::compute_rewards_per_func) calls every reward function with the completion texts and the dataset columns as keyword arguments:

python
completions = [s.messages[-1]['content'] for s in samples]
...
output = reward_func(completions, **reward_kwargs)

So request.messages is applied to a str, and the ground truth arrives under the solution keyword (the column the math datasets carry, as documented in Reward Function), not ground_truths.

Every sibling follows the dispatch contract instead: MathAccuracy.__call__(self, completions, solution, ...), Format.__call__(self, completions, ...), and ReactORM.__call__ even branches on if not isinstance(infer_requests[0], str) so that both shapes work. The example plugins do the same (examples/train/grpo/plugin/gsm8k/gsm8k_plugin.py).

How to Reproduce / 如何复现

No GPU, model or network access is needed — driving the real dispatch is enough:

python
import torch

from swift.rl_core.data import GRPOSample
from swift.rl_core.grpo_algorithm import compute_rewards_per_func
from swift.rewards import orms


def sample(content, solution):
    return GRPOSample(
        messages=[{'role': 'user', 'content': 'What is 1+2?'}, {'role': 'assistant', 'content': content}],
        extra={'solution': solution})


orm = orms['math'](args=None)
samples = [sample('The answer is \\boxed{3}', '\\boxed{3}'), sample('The answer is 4', '\\boxed{3}')]
print(compute_rewards_per_func(samples, [orm], [None], device=torch.device('cpu')))

Output on current main (dbc395ff):

AttributeError: 'str' object has no attribute 'messages'   # swift/rewards/orm.py:444

The failure mode depends on which ground-truth column the dataset carries. With the documented solution column, the call fails before any parsing:

TypeError: MathORM.__call__() missing 1 required positional argument: 'ground_truths'

With a ground_truths column added, the call reaches line 444 and raises the AttributeError above. toolbench, which does branch on the input shape, runs to completion in the same harness.

Expected Behavior / 期望行为

MathORM should accept what the GRPO dispatch hands it — completion texts plus the solution column — while keeping the InferRequest form working for programmatic callers, exactly like ReactORM.

Workaround / 临时方案

Use --reward_funcs accuracy (MathAccuracy), which implements the same contract; the math entry in orms is currently unusable from GRPO.