Skip to content

[Tracing] Allow torch.Sizes to be iterated #1308

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from

Conversation

kylesayrs
Copy link
Collaborator

@kylesayrs kylesayrs commented Apr 1, 2025

Purpose

  • Reduce user burden when tracing models by automatically handling the iteration of torch.Size types

The following code

input_shape = hidden_states.shape[:-1]
hidden_shape = (*input_shape, -1, self.head_dim) # iterate shape

gets graph compiled into

input_shape = getattr(hidden_states, "shape")
input_shape_2 = __getitem__(input_shape, slice(None, -1, None))
hidden_shape = (__getitem__(input_shape_2, 0), __getitem__(input_shape_2, 1), -1, head_dim)

Changes

  • Catch any attempts to iterate torch.Size proxies. Because lengths are assumed to be static during tracing, we can replace the iteration with an iteration of __getitem__ calls of each of the indices

Testing

  • Can now run sequential pipeline with llama3 with Linear granularity without custom code definition
MODEL_ID = "meta-llama/Meta-Llama-3-8B-Instruct"
recipe = GPTQModifier(targets="Linear", scheme="W4A16", ignore=["lm_head"], sequential_targets="Linear")

Signed-off-by: Kyle Sayers <[email protected]>
Copy link

github-actions bot commented Apr 1, 2025

👋 Hi! Thank you for contributing to llm-compressor. Please add the ready label when the PR is ready for review.

Note: This is required to complete the testing suite, please only add the label once the PR is code complete and local testing has been performed.

@kylesayrs kylesayrs changed the title [Tracing] Allow sizes to be iterated [Tracing] Allow torch.Sizes to be iterated Apr 1, 2025
Signed-off-by: Kyle Sayers <[email protected]>
Copy link
Collaborator

@brian-dellabetta brian-dellabetta left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

neat!


def iter(self, obj: Proxy) -> Iterator:
# special extension which allows torch.Sizes to be iterated, but keeps
# their values are dynamic
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe this?

Suggested change
# their values are dynamic
# their values as dynamic

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants