Skip to content

fix(ifeval): align C++ implementation with Python reference (char counting & whitespace stripping) - #47

Open
AhmedTElthakeb wants to merge 2 commits into
mainfrom
fix-ifeval-strip-whitespace
Open

AhmedTElthakeb wants to merge 2 commits into
mainfrom
fix-ifeval-strip-whitespace

Conversation

@AhmedTElthakeb

Copy link
Copy Markdown

Summary

This PR brings the C++ IFEval implementation in line with the Python reference by fixing two discrepancies that caused different scoring behavior.

Changes

1. Count characters, not bytes, in tolerance checks

Problem: The ends_with, starts_with, and ConstrainedResponse checks used std::string::size() which counts bytes, while the Python reference uses len() which counts characters. Any non-ASCII character in the tolerance window caused the C++ check to fail where Python would pass.

Example: "My answer is no. ⁇ " is 19 characters (passes Python limit of 16+3) but 21 bytes (fails in C++).

Fix:

  • Added utf8_len() to count UTF-8 characters (lead bytes only)
  • Added utf8_offset() to get byte offset of the n-th character
  • Updated ends_with(), starts_with(), and ConstrainedResponse::verify_() to use character counting

2. Strip whitespace to match Python reference behavior

Problem: The C++ implementation didn't strip whitespace in places where the Python reference does, causing responses with leading/trailing whitespace or blank lines to score differently.

Fix:

  • Reordered operations in transform_response() to trim after removing first/last lines (matching Python order)
  • Added trim checks in IsFollowed() to prevent blank text from counting as following (strict and loose modes)
  • Updated Quotation::verify_() to trim the response before checking for quotes

Files Modified

  • common.h: Added UTF-8 helper functions, updated ends_with(), starts_with(), and transform_response()
  • types.h: Updated Instruction::IsFollowed(), ConstrainedResponse::verify_(), and Quotation::verify_()

@AhmedTElthakeb
AhmedTElthakeb requested review from a team and anhappdev as code owners November 10, 2026 18:08
@github-actions

Copy link
Copy Markdown

MLCommons CLA bot All contributors have signed the MLCommons CLA ✍️ ✅

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant