fix: fail fast when sharing segmenter weights without a compressor - #77
Open
ClaireXi99 wants to merge 1 commit into
Open
fix: fail fast when sharing segmenter weights without a compressor#77ClaireXi99 wants to merge 1 commit into
ClaireXi99 wants to merge 1 commit into
Conversation
LlmLingua2Segmenter.__init__ loads its own model when shared is False and adopts the compressor's weights when a compressor is given, but when shared is True and no compressor is supplied neither branch runs. The instance is then built without model, tokenizer or buffer_len, and the first access fails with a confusing AttributeError far from the real cause. Raise a ValueError naming both ways to resolve the configuration instead, and cover the sharing paths with tests.
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.
What breaks
LlmLingua2Segmenter.__init__has two initialization paths:When
shared is Trueand no compressor is supplied, neither branch runs. The object is constructed successfully but withoutmodel,tokenizerorbuffer_len, so the failure surfaces later and far from its cause —LightMemory.__init__immediately readsself.segmenter.buffer_len, giving:That message points at the segmenter's internals rather than at the actual problem, which is a configuration one:
precomp_topic_shared=Trueasks the segmenter to share a pre-compressor's weights whilepre_compress=Falsemeans there is no pre-compressor to share.Reproduced directly against the class:
Fix
Add the missing
elsebranch so an unsatisfiable configuration fails immediately with an actionable message naming both ways out:This is intentionally narrow — it only converts a silent half-initialization into a clear error. Neither existing branch changes behavior.
Relationship to PR #75
PR #75 fixes a related crash one layer up, where
LightMemorypassed an unsetself.compressorto the segmenter factory. The two changes are complementary rather than overlapping: #75 touches onlylightmem.py, this one onlyllmlingua_2.py, and I have deliberately left #75's one-line fix out of this branch to avoid duplicating it.Worth noting that #75 makes this bug more reachable. Once
self.compressorcorrectly defaults toNone, theprecomp_topic_shared=Truepluspre_compress=Falsecombination stops failing on the missing attribute and starts flowing into the segmenter ascompressor=Nonewithshared=True— landing exactly on the branchless path above. That is the same cascade described in PR #63, where PR #59's fix removed an early return and exposed an uninitialized variable underneath.With
precomp_topic_sharedat its default ofFalsenothing changes:shared is Falsetakes the first branch and the segmenter loads its own model, so #75's scenario and its assertion of(topic_segmenter, False, None)are unaffected.Tests
Added
tests/test_topic_segmenter_shared_weights.pycovering both sharing paths:shared=Truewith no compressor raises the newValueErrorshared=Truewith a compressor still adopts its model, tokenizer andmax_position_embeddings-derivedbuffer_lenThe
shared=Falsepath is unchanged and untested here because it downloads a real model. The module importstorch, so the file usespytest.importorskip("torch").Verified on Python 3.11 with the pinned
transformers==4.57.0; the pre-existing sensory-memory tests still pass.