Fix IndexError in cast_inputs for ops with no formal inputs - #3045
Open
bodapatisaikrishna wants to merge 1 commit into
Open
Fix IndexError in cast_inputs for ops with no formal inputs#3045bodapatisaikrishna wants to merge 1 commit into
bodapatisaikrishna wants to merge 1 commit into
Conversation
autocast.cast_inputs() checks expected_inputs[-1].variadic to decide
whether extra positional arguments can be absorbed by a trailing
variadic parameter. When op_signature.inputs is empty (an op with no
formal input parameters, e.g. RandomNormal, which only has
attributes), this indexes into an empty list and raises an
unhandled IndexError instead of the intended ValueError with a clear
message.
Reproducible end-to-end via the generic Op.__call__ dispatch path
(values.Op), which does not pre-validate positional-argument count
before calling autocast.dynamic_cast_inputs -> cast_inputs:
from onnxscript._internal import values
from onnxscript import opset18
values.Op(opset18, 'RandomNormal')(1.0, 2.0, 3.0)
# IndexError: list index out of range
Guard the variadic check with 'expected_inputs and ...' so an empty
input list falls through to the existing else branch and raises the
intended ValueError('Number of actual parameters ... exceeds number
of formal parameters ...') instead.
Added onnxscript/_internal/autocast_test.py covering: the empty-input
error case, the empty-input no-args case, the existing
non-variadic-overflow case, and the homogeneous-variadic case (to
guard against regressing the condition just touched).
Author
|
@microsoft-github-policy-service agree |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
autocast.cast_inputs()is the shared implementation behind both eager-modeexecution (
dynamic_cast_inputs) and static script-translation(
static_cast_inputs). When it sees more positional arguments than the op'sformal input parameters, it's supposed to raise a clear error:
But the branch that decides whether to raise that error indexes into
expected_inputs[-1]unconditionally:For an op with zero formal input parameters (only attributes — e.g.
RandomNormal,RandomUniform),expected_inputsis[], soexpected_inputs[-1]raises an unhandledIndexError: list index out of rangeinstead of the intendedValueError.This is reachable end-to-end through
values.Op.__call__, which doesn'tpre-validate argument counts before dispatching to
evaluator.eval_op->autocast.dynamic_cast_inputs->cast_inputs. Thispath is used for any op accessed dynamically (e.g. custom-domain ops via
Opset.__getattr__, or anOpconstructed directly), as opposed to thecode-generated opset functions (e.g.
opset18.RandomNormal), which havefixed Python signatures that Python itself rejects extra arguments for.
Repro:
Fix
Guard the variadic check with
expected_inputs and ...so an empty inputlist falls through to the existing
elsebranch and raises the intended,descriptive
ValueErrorinstead of crashing with an unrelatedIndexError.One-line change; no other behavior affected — the non-empty variadic and
non-variadic paths are untouched.
Testing
Added
onnxscript/_internal/autocast_test.py(no test file existed for thismodule) covering:
IndexError, nowValueError)(), no regression)ValueError)just touched)
Confirmed the new failing-case test fails with the original
IndexErrorwhen the fix is reverted (
git stash), and passes with the fix applied.ruff check/ruff format --checkclean on both changed files.Found by reading
autocast.pyand testingcast_inputsagainst its ownerror-message contract, not from a reported issue.