Skip to content

[onnx_importer] Disambiguate empty string: optional none vs tensor name#4551

Merged
IanWood1 merged 3 commits into
llvm:mainfrom
ziliangzl:fix/onnx-importer-empty-string-key-collision
May 12, 2026
Merged

[onnx_importer] Disambiguate empty string: optional none vs tensor name#4551
IanWood1 merged 3 commits into
llvm:mainfrom
ziliangzl:fix/onnx-importer-empty-string-key-collision

Conversation

@ziliangzl
Copy link
Copy Markdown
Contributor

Fixes #4550
The NodeImporter cached torch.constant.none under _nv_map[""], matching ONNX's convention that an empty string in Node.input denotes an omitted optional input. Some producers (e.g. Microsoft SkipSimplifiedLayerNormalization) also bind real intermediate results to outputs whose names are the empty string. Each such output overwrote _nv_map[""], so later nodes that use "" for omitted optionals (e.g. GroupQueryAttention's trailing inputs) incorrectly received those tensor SSA values instead of torch.constant.none.

Behavior changes:

  • Cache the shared none value under _OPTIONAL_NONE_CACHE_KEY instead of "".
  • When resolving node inputs, treat input_name == "" as omitted optional: append get_none() and an empty onnx.TypeProto without indexing _nv_map[""].
  • Register outputs named "" under unique keys _torch_mlir_onnx_importer_anon so multiple anonymous outputs do not overwrite each other.

Adds test/python/onnx_importer/test_empty_string_optional_inputs.py: minimal Identity -> custom op graph where optional inputs are "" and must import as %none operands, not tensor values stored under "".

Symptom fixed: GroupQueryAttention previously showed duplicated operands such as (%10#2, %10#2, %10#2) instead of (%none, %none, %none) for optional slots.

The NodeImporter cached torch.constant.none under _nv_map[""], matching ONNX's convention that an empty string in Node.input denotes an omitted optional input. Some producers (e.g. Microsoft SkipSimplifiedLayerNormalization) also bind real intermediate results to outputs whose names are the empty string. Each such output overwrote _nv_map[""], so later nodes that use "" for omitted optionals (e.g. GroupQueryAttention's trailing inputs) incorrectly received those tensor SSA values instead of torch.constant.none.

Behavior changes:
- Cache the shared none value under _OPTIONAL_NONE_CACHE_KEY instead of "".
- When resolving node inputs, treat input_name == "" as omitted optional: append get_none() and an empty onnx.TypeProto without indexing _nv_map[""].
- Register outputs named "" under unique keys __torch_mlir_onnx_importer_anon_<n> so multiple anonymous outputs do not overwrite each other.

Adds test/python/onnx_importer/test_empty_string_optional_inputs.py: minimal Identity -> custom op graph where optional inputs are "" and must import as %none operands, not tensor values stored under "".

Symptom fixed: GroupQueryAttention previously showed duplicated operands such as (%10#2, %10#2, %10#2) instead of (%none, %none, %none) for optional slots.
@ziliangzl
Copy link
Copy Markdown
Contributor Author

Hi @IanWood1 , sorry to bother you. It looks like this repository doesn’t automatically assign reviewers, and I don’t have permission to request reviews from others. Would you be able to review this PR when you get a chance?

@IanWood1 IanWood1 self-requested a review April 30, 2026 23:15
Copy link
Copy Markdown
Member

@IanWood1 IanWood1 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! The core idea looks right: empty string inputs should be imported as omitted optionals. I think there are still possible _nv_map collisions from storing importer private sentinel values in the map, so it would be good to address those before merging.

Comment thread python/torch_mlir/extras/onnx_importer.py Outdated
Comment thread python/torch_mlir/extras/onnx_importer.py Outdated
Comment thread test/python/onnx_importer/test_empty_string_optional_inputs.py Outdated
Comment thread test/python/onnx_importer/test_empty_string_optional_inputs.py Outdated
@ziliangzl ziliangzl requested a review from IanWood1 May 7, 2026 09:47
@ziliangzl
Copy link
Copy Markdown
Contributor Author

@IanWood1 Hi, I’ve addressed the comments and updated the PR. Could you please take another look when you have a chance? If everything looks good, could you help merge it? Thanks!

Copy link
Copy Markdown
Member

@IanWood1 IanWood1 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM!

@IanWood1 IanWood1 enabled auto-merge (squash) May 12, 2026 16:24
@IanWood1 IanWood1 merged commit 613c355 into llvm:main May 12, 2026
3 checks passed
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.

ONNX importer: _nv_map[""] collision between omitted optional inputs and tensors named ""

2 participants