Skip to content
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

quant_pre_process SymbolicShapeInference causes AttributeError: 'NoneType' object has no attribute 'HasField' when the model has a Constant node. #22422

Open
AyoubMDL opened this issue Oct 12, 2024 · 2 comments
Labels
quantization issues related to quantization

Comments

@AyoubMDL
Copy link

Describe the issue

When attempting to run SymbolicShapeInference on a model containing a Constant node, an error occurs: AttributeError: 'NoneType' object has no attribute 'HasField'.

The issue arises from this part of the code in the symbolic_shape_infer.py file:

def _infer_Constant(self, node):  # noqa: N802
     t = get_attribute(node, "value")
     self.sympy_data_[node.output[0]] = numpy_helper.to_array(t)

There are two key problems in this section:

  • The Constant node can have attributes other than just "value". For instance, Constant<value_float=1.0>() would cause get_attribute to return None, which leads to the error.
  • Since get_attribute itself returns a value, we should be able to pass it directly to self.sympy_data_ without using numpy_helper.to_array(t), which triggers the AttributeError.

Suggested Solution:

The _infer_Constant sould handle different Constant attributes ["sparse_value", "value", "value_float", "value_floats", "value_int", "value_ints", "value_string","value_strings"]. Additionally, adjusting the code to bypass numpy_helper.to_array(t) when get_attribute returns a directly usable value could resolve the error.
I can submit a pull request to address the issue. Please let me know if this is acceptable or if there are any specific guidelines I should follow.

To reproduce

You can reproduce it with the following code:

import tempfile

import onnx
import onnx.checker
from onnx.parser import parse_model
from onnxruntime.quantization.shape_inference import quant_pre_process


def test_constant_symbolic_shape_infer():
    model_to_parser = """
    < ir_version: 3, opset_import: ["" : 15] >
    test_model (float[N, 10] X) => (float [N, ?] Y) {
        min = Constant<value_float=1.0>()
        Y = Clip(X, min)
    }
    """
    model = parse_model(model_to_parser)
    onnx.checker.check_model(model)

    with tempfile.TemporaryDirectory() as tmp_dir:
        model_path = f"{tmp_dir}/constant_shape_infer.onnx"
        onnx.save(model, model_path)

        quant_pre_process(model_path, model_path)

Urgency

No response

Platform

Linux

OS Version

Ubuntu 22.04.3 LTS

ONNX Runtime Installation

Released Package

ONNX Runtime Version or Commit ID

1.18.0

ONNX Runtime API

Python

Architecture

X64

Execution Provider

Default CPU

Execution Provider Library Version

No response

@github-actions github-actions bot added the quantization issues related to quantization label Oct 12, 2024
@Johansmm
Copy link
Contributor

Any news about this topic ?

@IggShaman
Copy link

I am looking at the exact same problem, onnxruntime v1.20.0.
A constant node has a field called "value_float", the code only looks for a field named "value", and it is not present in one of the nodes.

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

No branches or pull requests

3 participants