Fix StaticLayer.get_seq_length return type annotation (#45987)#46173
Open
Sanjays2402 wants to merge 1 commit into
Open
Fix StaticLayer.get_seq_length return type annotation (#45987)#46173Sanjays2402 wants to merge 1 commit into
Sanjays2402 wants to merge 1 commit into
Conversation
) `StaticLayer.cumulative_length` is a shape-(1,) `torch.Tensor`, so `StaticLayer.get_seq_length()` actually returns a `Tensor`, not the `int` its annotation promised. Per maintainer guidance on huggingface#45987, calling `.item()` to coerce would force a CUDA sync and isn't worth it when the tensor broadcasts identically to an int at every call site. The right fix is just to relax the type annotation. - Update `CacheLayerMixin.get_seq_length` abstract signature to `int | torch.Tensor`. - Update `StaticLayer.get_seq_length` to match, with a docstring note explaining why a tensor is returned.
Contributor
|
View the CircleCI Test Summary for this PR: https://huggingface.co/spaces/transformers-community/circle-ci-viz?pr=46173&sha=ece191 |
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.
Fixes #45987.
StaticLayer.cumulative_lengthis initialized as a shape-(1,)torch.Tensor(so that in-place.add_()updates staytorch.compile-friendly), andStaticLayer.get_seq_length()returns it directly. The current-> intannotation lies about that.Per @Rocketknight1's comment on the issue:
This PR does exactly that — minimal annotation-only change:
CacheLayerMixin.get_seq_lengthabstract:-> int→-> int | torch.TensorStaticLayer.get_seq_length:-> int→-> int | torch.Tensor, plus a docstring note explaining why a tensor is returned.No runtime behavior change. No
.item()calls added. Other concreteget_seq_lengthoverrides (DynamicLayer,StaticSlidingWindowLayer,QuantizedLayer, ...) already return ints and don't need touching.The four earlier PRs for this issue (#46005, #46010, #46081, #45997) all attempted heavier rewrites that added
.item()or restructuredcumulative_length, which is the opposite of the requested fix. This PR sticks to the maintainer-specified shape.Before submitting
Who can review?
@Rocketknight1 (you commented on #45987 with the requested approach)