invoke-ai/InvokeAI · warning

I've had issues with optimizer in recent versions of PyTorch

Error message

I've had issues with optimizer in recent versions of PyTorch / ONNX.Try onnxruntime optimization if this doesn't work.

What it means

This is a warning emitted by the vendored ONNX optimization script before running the onnx optimizer over the serialized model. The upstream tooling had known issues in recent PyTorch/ONNX versions causing 'duplicate definition of name' errors (onnx/onnx#2401), so the script warns that if optimization fails, onnxruntime-based optimization (see onnx_validate.py) is the recommended alternative.

Source

Thrown at invokeai/backend/image_util/normal_bae/nets/submodules/efficientnet_repo/onnx_optimize.py:71

        'eliminate_unused_initializer',
        'extract_constant_to_initializer',
        'fuse_add_bias_into_conv',
        'fuse_bn_into_conv',
        'fuse_consecutive_concats',
        'fuse_consecutive_reduce_unsqueeze',
        'fuse_consecutive_squeezes',
        'fuse_consecutive_transposes',
        #'fuse_matmul_add_bias_into_gemm',
        'fuse_pad_into_conv',
        #'fuse_transpose_into_gemm',
        #'lift_lexical_references',
    ]

    # Apply the optimization on the original serialized model
    # WARNING I've had issues with optimizer in recent versions of PyTorch / ONNX causing
    # 'duplicate definition of name' errors, see: https://github.com/onnx/onnx/issues/2401
    # It may be better to rely on onnxruntime optimizations, see onnx_validate.py script.
    warnings.warn("I've had issues with optimizer in recent versions of PyTorch / ONNX."
                  "Try onnxruntime optimization if this doesn't work.")
    optimized_model = optimizer.optimize(onnx_model, passes)

    num_optimized_nodes, optimzied_graph_str = traverse_graph(optimized_model.graph)
    print('==> The model after optimization:\n{}\n'.format(optimzied_graph_str))
    print('==> The optimized model has {} nodes, the original had {}.'.format(num_optimized_nodes, num_original_nodes))

    # Save the ONNX model
    onnx.save(optimized_model, args.output)


if __name__ == "__main__":
    main()

View on GitHub (pinned to 0b6a024f2f)

Solutions

  1. Use the onnxruntime-based optimization path instead (onnx_validate.py script)
  2. Pin onnx to an older version without the duplicate-definition bug (pre-regression versions referenced in onnx/onnx#2401)
  3. If optimize() succeeds, ignore the warning; it is informational
  4. Apply `onnxruntime.InferenceSession` graph optimization as a post-step instead of the onnx optimizer passes

Example fix

# before
optimizer.optimize(onnx_model, passes)  # may raise duplicate definition errors
# after
import onnxruntime as ort
sess_options = ort.SessionOptions()
sess_options.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
session = ort.InferenceSession("model.onnx", sess_options)
Defensive patterns

Strategy: fallback

Validate before calling

# Prefer onnxruntime optimization when available
import importlib.util
use_ort = importlib.util.find_spec("onnxruntime") is not None
if not use_ort:
    warnings.warn("onnx optimizer may hit duplicate-definition errors on recent ONNX versions")

Try / catch

try:
    optimized_model = optimizer.optimize(onnx_model, passes)
except Exception as e:
    warnings.warn(f"onnx optimizer failed ({e}); falling back to onnxruntime optimization")
    import onnxruntime as ort
    so = ort.SessionOptions()
    so.graph_optimization_level = ort.GraphOptimizationLevel.ORT_ENABLE_ALL
    ort.InferenceSession(model_path, so)  # optimized graph via onnxruntime

Prevention

When it happens

Trigger: Running `python onnx_optimize.py` (the `main` entry point) on an ONNX model with newer ONNX/PyTorch versions where the onnx optimizer hits the duplicate-definition bug; the warning appears unconditionally before `optimizer.optimize(...)` runs.

Common situations: Normal execution of the normal_bae ONNX export/optimize workflow on modern ONNX versions; hitting 'duplicate definition of name' errors during optimize; maintaining the vendored EfficientNet repo tooling in a newer environment than it was written for.

Related errors


AI-assisted analysis of invoke-ai/InvokeAI@0b6a024f2f (2026-08-29). Data as JSON: /api/errors/0ab7bc4e33599bcc. Report an issue: GitHub.