Skip to content

Fix SequenceEmpty dtype parameter ignored in eager mode#2949

Open
PratikWayase wants to merge 1 commit into
microsoft:mainfrom
PratikWayase:fix/sequence-empty-dtype
Open

Fix SequenceEmpty dtype parameter ignored in eager mode#2949
PratikWayase wants to merge 1 commit into
microsoft:mainfrom
PratikWayase:fix/sequence-empty-dtype

Conversation

@PratikWayase

Copy link
Copy Markdown

Summary

Fix SequenceEmpty dtype parameter being ignored in eager/trace mode by introducing a typed runtime representation for sequences. Fixes #1562.

Changes

As noted by @gramalingam in the issue, the root cause is that we reuse Python lists for runtime sequence values, and when the list is empty, we cannot infer a suitable ONNX type. This PR implements the first proposed fix: using a runtime representation that combines a type with the value.

  • Introduce _TypedSequence, a list subclass in onnxscript/_internal/typed_sequence.py that preserves ONNX dtype information for empty sequences
  • Modify BaseEvaluator.eval_op in onnxscript/_internal/evaluator.py to wrap SequenceEmpty results with _TypedSequence using the provided dtype attribute (defaults to FLOAT if None)
  • Modify _onnxscript_to_numpy_value in onnxscript/_internal/evaluator.py to preserve the _TypedSequence wrapper (instead of downcasting to a plain list) when preparing inputs for the ONNX runtime
  • Modify value_to_type_proto in onnxscript/_internal/utils.py to extract the dtype from _TypedSequence when inferring the ONNX TypeProto

Tests

Added OpInfo-style regression test test_sequence_empty_preserves_dtype covering:

  • FLOAT16 dtype
  • DOUBLE dtype
  • INT64 dtype

Additionally verified edge cases:

  • Missing dtype attribute (correctly defaults to FLOAT)
  • Multiple SequenceInsert operations on the same typed sequence
  • SequenceAt on a typed sequence

Files:

  • onnxscript/_internal/typed_sequence.py (new)
  • onnxscript/_internal/evaluator.py
  • onnxscript/_internal/utils.py
  • tests/eager_mode_test.py

Validation

python -m pytest tests/eager_mode_test.py -k "test_sequence_empty_preserves_dtype" -v

Passed:

tests/eager_mode_test.py::EagerModeTest_0_reference_runtime::test_sequence_empty_preserves_dtype PASSED
tests/eager_mode_test.py::EagerModeTest_1_onnxruntime::test_sequence_empty_preserves_dtype PASSED
2 passed

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

SequenceEmpty parameter dtype seems not work

1 participant