Skip to content

fix: clamp KL divergence to non-negative values#231

Closed
JasonOA888 wants to merge 3 commits intop-e-w:masterfrom
JasonOA888:fix/kl-divergence
Closed

fix: clamp KL divergence to non-negative values#231
JasonOA888 wants to merge 3 commits intop-e-w:masterfrom
JasonOA888:fix/kl-divergence

Conversation

@JasonOA888
Copy link
Copy Markdown

Fixes #141

- KL divergence is nonnegative by definition
- Floating point arithmetic can produce -0.0000
- Clamp to 0.0 to avoid displaying negative zero

Fixes p-e-w#141
@gemini-code-assist
Copy link
Copy Markdown
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves an issue where the Kullback-Leibler (KL) divergence calculation could yield negative values, which is mathematically incorrect. By introducing a clamping operation, the system now guarantees that the KL divergence metric remains non-negative, thereby enhancing the accuracy and reliability of the evaluation process.

Highlights

  • KL Divergence Correction: Implemented a fix to ensure that the calculated Kullback-Leibler (KL) divergence is always non-negative, aligning with its mathematical definition.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/heretic/evaluator.py
    • Clamped the kl_divergence value to a minimum of 0.0 to prevent negative results.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Copy Markdown
Contributor

@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 correctly clamps the KL divergence value to be non-negative, which is a valid fix since KL divergence cannot be negative by definition. I have one minor suggestion to align a new comment with the repository's style guide.

reduction="batchmean",
log_target=True,
).item()
# Clamp to non-negative (KL divergence cannot be negative by definition)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

medium

This comment should end with a period to adhere to the repository's style guide.

Suggested change
# Clamp to non-negative (KL divergence cannot be negative by definition)
# Clamp to non-negative (KL divergence cannot be negative by definition).
References
  1. Comments should start with a capital letter and end with a period. They should use correct grammar and spelling. (link)

@p-e-w
Copy link
Copy Markdown
Owner

p-e-w commented Mar 16, 2026

This should no longer be an issue with #194. Are you still seeing negative KLDs for some models?

@JasonOA888
Copy link
Copy Markdown
Author

Thanks for the pointer! I checked PR #194 - it fixes KL divergence measurement issues caused by prefix detection, but Issue #141 is specifically about floating-point -0.0 display.

Even with #194 merged, the computation can still produce negative zero due to floating-point arithmetic (not a logical bug). The fix here simply clamps the value to non-negative before formatting.

I don't have a specific model to reproduce this right now - I created the fix based on the issue description. If this isn't occurring in practice anymore, feel free to close both the issue and this PR.

@p-e-w
Copy link
Copy Markdown
Owner

p-e-w commented Mar 16, 2026

Yeah, I think we should actually keep the current behavior, since this should never occur in practice, and if it does (as it has in the past), it's indicative of a bug, and it's best if the value sticks out like a sore thumb in that case.

Note also that the KLD logic is currently undergoing a major revision in #53.

Thanks anyway for the PR!

@p-e-w p-e-w closed this Mar 16, 2026
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.

Don't display negative KL divergences

2 participants