Repository navigation
feat: raise proper Python exceptions instead of panicking - #4
Merged
Merged
Conversation
- Ops return Result; ExecutionContext::require replaces .expect() on inputs. - Each node runs under catch_unwind so backend panics become RuntimeError with node name and op type. Default panic hook is silenced unless RUST_BACKTRACE is set, since the message is carried in the exception. - Validate inputs up front: TypeError for non-ndarray / non-float32, ValueError for wrong count, rank or static dims. - Fix Fortran-ordered inputs (e.g. x.T) being read in memory order; only C-contiguous arrays take the fast path. - flex_to_numpy returns PyResult instead of panicking. - Extend tests/test_ops.py with error-path and non-contiguous cases. Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
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.
Summary
Every failure path used to be an
.expect()/panic!, which reaches Python aspyo3_runtime.PanicException(aBaseException, soexcept Exceptiondoesn't catch it). Now:TypeError: input 'x': expected a float32 array, got float64 (use .astype(np.float32))ValueError: input 'x': expected shape [?, 3], got shape [2, 4]PanicException+ Rust stderr dumpRuntimeError: node 'gemm1' (Gemm): matmul: inner dimensions must match ...RuntimeError: model output 'y' was never computedHow:
OpResult;ExecutionContext::requirereplaces.expect()on inputs.catch_unwind, so panics inside Burn are converted too, with node name and op type attached.RUST_BACKTRACEis set.Bug fix found along the way
Fortran-ordered inputs such as
x.Twere read in memory order, silently scrambling the data — rust-numpy'sas_slice()succeeds for F-contiguous arrays too. Only C-contiguous arrays now take the fast path; everything else is gathered in logical order.Testing
tests/test_ops.pygains error-path cases (dtype, non-array, rank, static dim, input count, backend panic →RuntimeError) and non-contiguous inputs (transposed, strided, negative stride). All 24 checks pass, plustest_bridge.py,compare_ort.py,cargo fmt --check, andclippy -D warnings.Note
Node names in errors are onnx-ir's generated names (
gemm1), not the original names in the ONNX file — onnx-ir renames nodes during parsing.🤖 Generated with Claude Code