Skip to content

Fix ZeroDivisionError in metric evaluation across examples#384

Open
ARYANPATEL-BIT wants to merge 2 commits intokubeedge:mainfrom
ARYANPATEL-BIT:fix/acc-zero-division
Open

Fix ZeroDivisionError in metric evaluation across examples#384
ARYANPATEL-BIT wants to merge 2 commits intokubeedge:mainfrom
ARYANPATEL-BIT:fix/acc-zero-division

Conversation

@ARYANPATEL-BIT
Copy link
Copy Markdown

Status: Fixed ✅

🎯 Issue Fixed

Fixes #383

🚨 Problem

During metric evaluation, accuracy calculation divides by len(same_elements). Empty lists from prediction edge-cases cause ZeroDivisionError and crash the training/evaluation pipeline.

📁 Affected Files

examples/llm_simple_qa/testenv/acc.py
examples/government/singletask_learning_bench/objective/testenv/acc.py
examples/government_rag/singletask_learning_bench/testenv/acc.py
examples/cloud-edge-collaborative-inference-for-llm/testenv/accuracy.py

text

🔧 The Fix

Before (CRASH):

acc = sum(same_elements) / len(same_elements)

After (SAFE):

acc = sum(same_elements) / len(same_elements) if len(same_elements) > 0 else 0.0

💾 Complete Fixed Function

def calculate_accuracy(same_elements):
    """Safe accuracy calculation for empty prediction lists."""
    return sum(same_elements) / len(same_elements) if len(same_elements) > 0 else 0.0

✅ Test Cases

# All now work without crashing:
calculate_accuracy([])                          # 0.0
calculate_accuracy()                   # 0.666...[1]
calculate_accuracy([True, False, True])         # 0.666...

Signed-off-by: Aryan Patel <aryan.patel7291@gmail.com>
@kubeedge-bot
Copy link
Copy Markdown
Collaborator

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: ARYANPATEL-BIT
To complete the pull request process, please assign moorezheng after the PR has been reviewed.
You can assign the PR to them by writing /assign @moorezheng in a comment when ready.

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@kubeedge-bot kubeedge-bot added the size/S Denotes a PR that changes 10-29 lines, ignoring generated files. label Apr 11, 2026
@ARYANPATEL-BIT
Copy link
Copy Markdown
Author

/assign @MooreZheng

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request adds safety checks to prevent ZeroDivisionError in accuracy calculation functions across multiple test environments. The feedback suggests using more idiomatic Python by checking the truthiness of the list directly instead of its length.

Signed-off-by: Aryan Patel <aryan.patel7291@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

size/S Denotes a PR that changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ZeroDivisionError in acc.py Metric Calculation

3 participants