onnx/onnx

[Feature request] Allow Type Inference Without Shape Inference in ONNX

Open

#7.100 geöffnet am 2. Juli 2025

Auf GitHub ansehen
 (10 Kommentare) (1 Reaktion) (0 zugewiesene Personen)Python (3.585 Forks)batch import
contributions welcomegood first issuemodule: shape inferencetopic: enhancement

Repository-Metriken

Stars
 (16.592 Stars)
PR-Merge-Metriken
 (Durchschn. Merge 1T 23h) (80 gemergte PRs in 30 T)

Beschreibung

System information

Top of tree.

What is the problem that this feature solves?

Currently, ONNX’s shape_inference.infer_shapes API performs both type and shape inference simultaneously. Type inference is guaranteed in a valid model, but shape inference is on "best effort" basis. For some operators (e.g., GridSample, Gather, Reshape, Where, etc), shape inference may fail due to incomplete or complex shape logic, even when type inference could succeed. This tight coupling prevents users from recovering type information when shape inference is unsupported or fails. For example, when users attempt to inject cast nodes or quantize nodes and propagate the type information downstream.

Request: Please consider adding an option to ONNX’s shape inference API to:

  • Run type inference independently of shape inference, so that type information can be propagated even when shape inference fails or is not implemented for an operator.
  • Alternatively, relax the requirement that shape inference must succeed for type inference to run.

Benefits:

  • Enables users and downstream tools to recover and validate type information in more models, improving robustness and usability.
  • Reduces manual workarounds (such as manually setting types for affected nodes).
  • Makes ONNX models more portable and easier to analyze, even when shape logic is incomplete.

Alternatives considered

  1. Create a separate function to support only type inference infer_types (preferable)
  2. Add boolean argument check_shape=True to the existing infer_shapes API (renaming the function is advisable in this case, infer_shapes_and_types or similar).

Describe the feature

Decouple type inference from shape inference.

Will this influence the current api (Y/N)?

Depends on the alternative selected. Can maintain backwards compatiblity.

Feature Area

shape_inference

Are you willing to contribute it (Y/N)

None

Notes

Related issues https://github.com/onnx/onnx/issues/4934

Contributor Guide