feat: add --length_normalize to correct short-document bias#191
Open
darkness8i8 wants to merge 3 commits intoEleutherAI:mainfrom
Open
feat: add --length_normalize to correct short-document bias#191darkness8i8 wants to merge 3 commits intoEleutherAI:mainfrom
darkness8i8 wants to merge 3 commits intoEleutherAI:mainfrom
Conversation
Under mean loss reduction, shorter sequences produce larger-magnitude gradients, causing them to dominate attribution scores. The existing --unit_normalize flag overcorrects by removing magnitude entirely (cosine similarity). This adds a middle ground: --length_normalize scales each document's score by sqrt(num_tokens), dampening the short-doc advantage without erasing magnitude information. Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
for more information, see https://pre-commit.ci
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
--length_normalizeflag toPreprocessConfigthat scales each document's attribution score bysqrt(num_tokens)at score time--unit_normalizedoes)unit_normalize, preconditioning,attribute_tokens)Motivation
Under mean loss reduction, shorter sequences produce larger-magnitude gradients, causing them to consistently rank highest in attribution scores.
--unit_normalize(cosine similarity) overcorrects by removing magnitude entirely, which introduces its own bias toward short, focused documents.--length_normalizeprovides a middle ground:score * sqrt(num_tokens)partially compensates for the inverse-length dependence without erasing magnitude information.Changes
bergson/config.pylength_normalize: bool = FalsetoPreprocessConfigbergson/score/scorer.pylength_normalize+num_token_grads; applysqrt(n)scaling in__call__bergson/score/score.pycompute_num_token_grads()and pass toScorertests/test_score.pyraw_score * sqrt(n)) and validation (ValueErrorwithout token counts)Test plan
pytest tests/test_score.py::test_scorer_length_normalize— verifies scores equalraw_score * sqrt(num_tokens)pytest tests/test_score.py::test_scorer_length_normalize_requires_token_counts— verifiesValueErrorwhennum_token_gradsis missinglength_normalize=Falseby default)🤖 Generated with Claude Code