fix(cache): StaticCache.get_seq_length() now returns int instead of Tensor#46010
Closed
saadkhaleeq610 wants to merge 1 commit into
Closed
Conversation
…ensor StaticLayer.cumulative_length is initialized as a scalar torch.int64 tensor (instead of shape-(1,) tensor) and get_seq_length() calls .item() to satisfy the -> int type contract and ensure interchangeability with DynamicCache. Removes the isinstance(torch.Tensor) workaround in masking_utils.py that existed solely to paper over this inconsistency. Fixes huggingface#45987
Contributor
|
View the CircleCI Test Summary for this PR: https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=46010&sha=0f9c12 |
Author
|
Hi, could a maintainer approve the workflow runs? Happy to address any review feedback too. |
3 tasks
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.
Problem
StaticLayer.get_seq_length()violates its-> inttype contract by returning a shape-(1,)torch.Tensorinstead of a Pythonint. This breaks interchangeability withDynamicCacheand causes subtle type propagation bugs (e.g.,int - tensor([N])yields aTensor, not anint).\n\nFixes #45987\n\n## Root Cause\n\nStaticLayer.__init__initializescumulative_lengthas a shape-(1,) tensor, andget_seq_length()returns it directly. A workaround existed inmasking_utils.pyto handle the tensor return.\n\n## Fix\n\n1.cache_utils.py: Initializecumulative_lengthas a scalartorch.int64tensor and call.item()inget_seq_length()to return a plain Pythonint. The tensor is preserved for in-place mutation andtorch.compilegraph stability.\n2.masking_utils.py: Remove theisinstance(torch.Tensor)workaround.\n3.tests/utils/test_cache_utils.py: Add regression test assertingget_seq_length()returnsintand is consistent withDynamicCache.