Skip to content

fix len() of unsized object #3395

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 1 commit into
base: develop
Choose a base branch
from

Conversation

XinyuWuu
Copy link

Changes

Added new logic when hasattr(tensor_collector.get_statistics().mean_values, "__len__") == False.

Reason for changes

if tensor_collector.get_statistics().mean_values is a nncf.tensor with shape () of dim 0, extend function call will fail.

Tests

tests/onnx/quantization/test_bias_correction.py is tested.
I am not sure how to test it as I encountered this bug during quantization of a network from one of my robotics projects. I guess the bug will only be triggled by certain networks and certain calibration datasets, therefor no new tests are added to varify the fix.

@XinyuWuu XinyuWuu requested a review from a team as a code owner March 31, 2025 13:09
@github-actions github-actions bot added the NNCF PTQ Pull requests that updates NNCF PTQ label Mar 31, 2025
@daniil-lyakhov daniil-lyakhov self-assigned this Apr 1, 2025
@daniil-lyakhov
Copy link
Collaborator

daniil-lyakhov commented Apr 1, 2025

Greetings, @XinyuWuu! Thank you for your interest in our project!

I afraid we need to find the root cause for this bug, looks like the effect you are describing is a consequence of the deeper error. MeanTensorStatistic, which unexpectedly returns a scalar, indicate a problems inside.
It is combined only with the MeanAggregator which should ALWAYS return a NNCFTensor (https://github.com/openvinotoolkit/nncf/blob/develop/nncf/torch/tensor_statistics/collectors.py#L293-L321). Combined with MeanPerChReducer and BatchMeanReducer, the aggregator should always receive NNCFTensors as an input. (For further details on reducers/aggregators, please see https://github.com/openvinotoolkit/nncf/blob/develop/nncf/experimental/common/tensor_statistics/collectors.py#L184-L191)

So please answer following question to clarify the problem:

  • Which NNCF backend did you use?
  • Could you please attach a minimal reproducer?

We indeed need to test it, I'll help you with that

@XinyuWuu
Copy link
Author

XinyuWuu commented Apr 1, 2025

Glad to spot something real.
I am using the OpenVino backend. I recorded some data and zipped them with the model for reproduce:
test.zip
test code:

import pickle

import nncf
import openvino as ov
import torch

ovcore = ov.Core()
ovmodel = ovcore.read_model("test.xml")
with open("test.pkl", "rb") as f:
    calibration_dataset = pickle.load(f)
calibration_dataset = nncf.Dataset(
    calibration_dataset,
    lambda x: {k: torch.tensor(v).to(torch.float32) for k, v in x.items()},
)
quantized_model = nncf.quantize(
    ovmodel,
    calibration_dataset,
    fast_bias_correction=False,
    target_device=nncf.TargetDevice.CPU,
    model_type=nncf.ModelType.TRANSFORMER,
)

The return type of MeanTensorStatistic is actully correct <class 'nncf.tensor.tensor.Tensor'> but the shape is ().

@MaximProshin
Copy link
Collaborator

@daniil-lyakhov , please clarify the next step here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
NNCF PTQ Pull requests that updates NNCF PTQ
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants