-
Notifications
You must be signed in to change notification settings - Fork 261
Fix meta tensor materialization in SequentialPipeline #1930
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
base: main
Are you sure you want to change the base?
Fix meta tensor materialization in SequentialPipeline #1930
Conversation
👋 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. |
Summary of ChangesHello @ronantakizawa, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a robust solution to a Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Code Review
This pull request addresses a RuntimeError
with meta tensors during sequential quantization by materializing them at both the model and input levels. The overall approach is sound and provides a robust fix. My review includes suggestions to improve the implementation by removing hardcoded device assignments in favor of dynamically determined ones, and refactoring duplicated code to enhance maintainability. These changes will make the solution more robust and easier to manage.
Hi @ronantakizawa, Do you have a model example which uses These kind of |
@kylesayrs here is the project I was working on that caused the meta tensor error. My patch fixed it. https://colab.research.google.com/drive/15L5F-p01M5mBv4UDueyw8C57KxkFaqjS?usp=sharing The .item() call doesn't occur in the model definition itself - it happens inside the traced subgraph code that PyTorch FX generates during sequential pipeline partitioning. When |
2ee7493
to
4e69a2e
Compare
Signed-off-by: ronantakizawa <[email protected]>
- Materialize meta tensors in model parameters/buffers before subgraph execution - Materialize meta tensors in inputs before passing to traced code - Fixes 'Tensor.item() on meta tensors' error from offloaded modules - Enables GPTQ quantization for idefics3-based models like SmolVLM Signed-off-by: ronantakizawa <[email protected]>
- Handle integer dtype parameters that can't require gradients - Convert integer parameters to buffers instead of Parameters - Prevents 'Only floating point tensors can require gradients' error Signed-off-by: ronantakizawa <[email protected]>
- Use _parameters and _buffers dicts directly to avoid conflicts - Properly remove parameter before converting to buffer - Prevents 'attribute already exists' KeyError Signed-off-by: ronantakizawa <[email protected]>
- Only materialize meta tensors once per subgraph - Add existence checks before modifying _parameters and _buffers - Add try-catch blocks with warnings for materialization failures - Prevents KeyError from duplicate buffer registration Signed-off-by: ronantakizawa <[email protected]>
Signed-off-by: ronantakizawa <[email protected]>
- Use model_device instead of hardcoded cuda:0 for multi-GPU compatibility - Define _materialize_meta_tensors once before loop to avoid duplication - Improves maintainability and correctness in multi-device environments Signed-off-by: ronantakizawa <[email protected]>
Run make style to format code according to project standards Signed-off-by: ronantakizawa <[email protected]>
4e69a2e
to
ee7c416
Compare
@kylesayrs does this look good to you? A user reported it resolved their issue with Qwen 3 VL and AWQ -- #1939 (comment) I plan to validate Qwen 3 VL + AWQ use case tomorrow (along with updating the e2e tests from qwen 2.5 vl to qwen 3 vl) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for the very impressive contribution! I tried this and it does in fact resolve the issue using Qwen 3 inside the sequential pipeline.
The code looks really good, but I have a question and a couple change requests for the tests
|
||
for module in model.modules(): | ||
# Materialize parameters | ||
for name, param in list(module.named_parameters(recurse=False)): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why don't we need to recurse here?
|
||
|
||
@pytest.mark.unit | ||
def test_meta_tensor_materialization(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this test looks like it might have been vibe coded 😄
|
||
@pytest.mark.integration | ||
@requires_gpu | ||
def test_sequential_pipeline_with_meta_tensors(tmp_path): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
rather than include calls to oneshot, a better unit test here would just be to make sure the model, after materializing, has parameters than aren't meta type?
Are you using latest transformers? @ronantakizawa |
Hi @ronantakizawa , it seems like materialized tensors might be a useful thing in general when tracing subgraphs, but I tried running a sequential pipeline after upgrading my torch/transformers versions, and it works without your PR. Can you try and see if it works on your side without your changes? |
SUMMARY:
Problem
Sequential quantization (GPTQ/AWQ) fails on vision-language models with:
RuntimeError: Tensor.item() cannot be called on meta tensors
.This occurs in
SequentialPipeline
because PyTorch FX tracing creates meta-tensorsduring graph partitioning for offloaded modules. When the traced code executes
operations like
.item()
, it fails on these meta tensors.Solution
Added meta tensor materialization at two levels:
before subgraph execution
to traced code
This defense-in-depth approach ensures meta-tensors are materialized regardless of
where they originate.
Changes
src/llmcompressor/pipelines/sequential/helpers.py
:_materialize_model_meta_tensors()
method to Subgraph class_materialized
flag to prevent duplicate materializationsrc/llmcompressor/pipelines/sequential/pipeline.py
:TEST PLAN:
Implemented unit test file
tests/llmcompressor/pipelines/test_sequential_vlm.py
Successfully quantized HuggingFaceTB/SmolVLM-Instruct (idefics3 architecture):