Skip to content
Merged
Changes from 3 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
23 changes: 19 additions & 4 deletions nemo_rl/environments/math_environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@

import ray
import torch
from math_verify import parse, verify
from math_verify.metric import math_metric
from math_verify.parser import ExprExtractionConfig, LatexExtractionConfig

from nemo_rl.distributed.batched_data_dict import BatchedDataDict
from nemo_rl.distributed.virtual_cluster import PY_EXECUTABLES
Expand Down Expand Up @@ -53,9 +54,23 @@ def verify(
results = []
for response, ground_truth in zip(pred_responses, ground_truths):
try:
gold = parse(ground_truth)
pred = parse(response[-100:]) # avoid looking at the whole string
results.append(float(verify(gold, pred)))
# Use Latex and plain math extraction from predictions
# https://github.com/huggingface/Math-Verify?tab=readme-ov-file#extraction-targets
verify_func = math_metric(
gold_extraction_target=(LatexExtractionConfig(),),
pred_extraction_target=(
ExprExtractionConfig(),
LatexExtractionConfig(),
),
)

ground_truth_parsable = "\\boxed{" + ground_truth + "}"
try:
ret_score, _ = verify_func([ground_truth_parsable], [response])
except Exception:
ret_score = 0.0

results.append(float(ret_score))
except Exception:
results.append(0)
return results
Expand Down