Skip to content

fix(cache): StaticCache.get_seq_length() now returns int instead of Tensor#46010

Closed
saadkhaleeq610 wants to merge 1 commit into
huggingface:mainfrom
saadkhaleeq610:fix/static-cache-get-seq-length-returns-int
Closed

fix(cache): StaticCache.get_seq_length() now returns int instead of Tensor#46010
saadkhaleeq610 wants to merge 1 commit into
huggingface:mainfrom
saadkhaleeq610:fix/static-cache-get-seq-length-returns-int

Conversation

@saadkhaleeq610
Copy link
Copy Markdown

Problem

StaticLayer.get_seq_length() violates its -> int type contract by returning a shape-(1,) torch.Tensor instead of a Python int. This breaks interchangeability with DynamicCache and causes subtle type propagation bugs (e.g., int - tensor([N]) yields a Tensor, not an int).\n\nFixes #45987\n\n## Root Cause\n\nStaticLayer.__init__ initializes cumulative_length as a shape-(1,) tensor, and get_seq_length() returns it directly. A workaround existed in masking_utils.py to handle the tensor return.\n\n## Fix\n\n1. cache_utils.py: Initialize cumulative_length as a scalar torch.int64 tensor and call .item() in get_seq_length() to return a plain Python int. The tensor is preserved for in-place mutation and torch.compile graph stability.\n2. masking_utils.py: Remove the isinstance(torch.Tensor) workaround.\n3. tests/utils/test_cache_utils.py: Add regression test asserting get_seq_length() returns int and is consistent with DynamicCache.

…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
@github-actions
Copy link
Copy Markdown
Contributor

View the CircleCI Test Summary for this PR:

https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=46010&sha=0f9c12

@saadkhaleeq610
Copy link
Copy Markdown
Author

Hi, could a maintainer approve the workflow runs? Happy to address any review feedback too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants