Skip to content

Conversation

ronantakizawa
Copy link

@ronantakizawa ronantakizawa commented Oct 14, 2025

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-tensors
during 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:

  1. Model-level (helpers.py): Materialize meta tensors in model parameters/buffers
    before subgraph execution
  2. Input-level (pipeline.py): Materialize meta tensors in inputs before passing
    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:
    • Added _materialize_model_meta_tensors() method to Subgraph class
    • Added _materialized flag to prevent duplicate materialization
    • Handles integer dtype tensors by converting to buffers instead of parameters
  • src/llmcompressor/pipelines/sequential/pipeline.py:
    • Added input materialization before both calibration and propagation passes

TEST PLAN:
Implemented unit test file tests/llmcompressor/pipelines/test_sequential_vlm.py
Successfully quantized HuggingFaceTB/SmolVLM-Instruct (idefics3 architecture):

  • GPTQ W4A16: 168 layers quantized, 4.4 GB → 1.97 GB (55.1% reduction)
  • AWQ W4A16: 168 layers quantized, 4.4 GB → 1.97 GB (55.1% reduction)

Copy link

👋 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.

Copy link
Contributor

Summary of Changes

Hello @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 RuntimeError that occurs when PyTorch FX tracing generates meta tensors during sequential quantization of vision-language models. The core issue is that operations like .item() fail on these meta tensors. The fix employs a two-pronged approach: materializing meta tensors within the model's parameters and buffers, and also materializing meta tensors found in the input data before execution. This ensures that all necessary tensors are concrete, allowing quantization pipelines to proceed without error and enabling efficient model compression.

Highlights

  • Problem Resolution: Fixes RuntimeError: Tensor.item() cannot be called on meta tensors encountered during sequential quantization (GPTQ/AWQ) of vision-language models, caused by PyTorch FX tracing creating meta tensors.
  • Model-Level Materialization: Implemented a new method _materialize_model_meta_tensors in Subgraph to materialize meta tensors in model parameters and buffers before subgraph execution, preventing issues with offloaded modules.
  • Input-Level Materialization: Added logic to materialize meta tensors within input data structures (tensors, dicts, lists, tuples) before passing them to the traced code during both calibration and and propagation passes.
  • Integer Dtype Handling: Specifically addresses integer dtype parameters by converting them to buffers during materialization, as they cannot require gradients.
  • Performance Verified: The fix has been successfully tested with GPTQ and AWQ quantization on a complex vision-language model, demonstrating correct functionality and significant memory reduction.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a 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.

@kylesayrs
Copy link
Collaborator

Hi @ronantakizawa,

Do you have a model example which uses Tensor.item?

These kind of Tensor.item calls typically shouldn't be present in model definitions anyways. I think in this scenario, it might make more sense to add a patching layer to patch the model definition to avoid the item call.

@ronantakizawa ronantakizawa reopened this Oct 14, 2025
@ronantakizawa
Copy link
Author

ronantakizawa commented Oct 14, 2025

@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 SequentialPipeline partitions the model using PyTorch FX tracing, device-offloaded modules get replaced with meta tensors during symbolic execution. The traced code then executes rotary_emb(), which internally calls .item() on tensor dimensions/position indices. When those are meta tensors, it crashes.

@ronantakizawa ronantakizawa force-pushed the ronantakizawa/sequentialpipeline branch 2 times, most recently from 2ee7493 to 4e69a2e Compare October 15, 2025 05:13
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]>
- 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]>
@brian-dellabetta
Copy link
Collaborator

@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)

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.

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)):
Copy link
Collaborator

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():
Copy link
Collaborator

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):
Copy link
Collaborator

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?

@dsikka
Copy link
Collaborator

dsikka commented Oct 17, 2025

Are you using latest transformers? @ronantakizawa

@brian-dellabetta
Copy link
Collaborator

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?

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.

4 participants