Implementation:EvolvingLMMs Lab Lmms eval VisualPuzzles Utils
File: lmms_eval/tasks/VisualPuzzles/utils.py (116 lines)
Principle: Task Utility Functions
Overview
Utility functions for the VisualPuzzles benchmark evaluation task. Provides document-to-visual/text conversions, answer parsing with multiple regex patterns, and result processing for multiple-choice visual reasoning questions.
Key Functions
VisualPuzzles_doc_to_visual
def VisualPuzzles_doc_to_visual(doc)
Extracts the image from a document for visual input.
Parameters:
doc- Document instance containing image data
Returns: List containing the document's image
VisualPuzzles_doc_to_text
def VisualPuzzles_doc_to_text(doc, lmms_eval_specific_kwargs)
Formats question text with options and prompt instructions.
Parameters:
doc- Document with question and optionslmms_eval_specific_kwargs- Contains prompt type selection
Returns: Formatted question string with options (A-D) and instructions
Prompt Types:
MULTI_CHOICE_DIRECT_PROMPT- Direct answer instructionCOT_PROMPT- Chain-of-thought reasoning with answer format
parse_response
def parse_response(response, all_choices, index2ans)
Extracts answer choice from model response using multiple regex patterns.
Parameters:
response- Model's text responseall_choices- Valid answer choices (e.g., ["A", "B", "C", "D"])index2ans- Optional mapping from choice letters to answer text
Returns: Extracted answer letter or random choice if parsing fails
Parsing Strategy: Tries patterns in order:
- "Answer: (A)" format
- "Answer: A" format (excluding "Final Answer:")
- Parentheses: (A), (B), etc.
- Letter with parenthesis: A), B), etc.
- Braces: {A}, {B}, etc.
- Dollar signs: $A$, $B$, etc.
- Letter with period: A., B., etc.
- Standalone letters (for short responses)
- Searches for answer text from index2ans mapping
- Returns random choice if all patterns fail
VisualPuzzles_process_result
def VisualPuzzles_process_result(doc, results)
Processes model results and compares with ground truth.
Parameters:
doc- Document with answer key and optionsresults- Model's response list
Returns: Dictionary with "exact_match" score (1.0 or 0.0)
Constants
MULTI_CHOICE_DIRECT_PROMPT- "Answer the question with the option's letter from the given choices directly."COT_PROMPT- Chain-of-thought instruction with specific answer format requirementPROMPTS- Dictionary mapping prompt names to prompt strings
Usage Pattern
The module follows the standard task utility pattern:
- Convert document to visual input (image)
- Format question with options and prompt
- Get model response
- Parse response using robust regex patterns
- Calculate exact match accuracy
Dependencies
random- For fallback random choice selectionre- For regex-based answer extraction