onnxruntime
onnxruntime copied to clipboard
[WIP] ORT aborts with the `linspace` implementation when input is empty
Fix ORT abort with linspace implementation when input is empty
Analysis
- [x] Explore repository structure and understand the issue
- [x] Reproduce the issue: confirmed it's a floating point exception (abort) during division by zero
- [x] Identify the root cause: The issue occurs in the Div operation when dividing by zero with integer types
- [x] Locate relevant source files:
onnxruntime/core/providers/cpu/math/element_wise_ops.cc - [x] Understand the current implementation uses Eigen arithmetic which doesn't check for division by zero
Implementation
- [x] Added
#include <type_traits>for compile-time type checking - [x] Created
CheckDivisionByZero<T>()helper function that:- Uses
if constexpr (std::is_integral_v<T>)to only check integer types - Iterates through denominator tensor to detect any zero values
- Returns proper error status instead of allowing SIGFPE
- Uses
- [x] Modified
Div<T>::Compute()to call validation before performing division - [x] Added comprehensive test cases for both integer and floating point division by zero
- [x] Verified code compiles successfully
- [x] Validated logic with standalone tests
Changes Summary
-
25 lines added to
onnxruntime/core/providers/cpu/math/element_wise_ops.cc -
44 lines added to
onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc - Total: 69 lines added, 0 deleted (minimal surgical changes)
Root Cause
The linspace implementation creates a model that performs division by zero:
- Range(0, 1, 1) produces [0]
- Sub(1, 1) produces 0
- Div(1, 0) attempts to divide by zero, causing floating point exception instead of proper error message
Solution
For integer types (int32_t, int64_t, etc.), division by zero now returns:
INVALID_ARGUMENT: "Division by zero error in Div operator"
For floating point types (float, double), behavior remains unchanged (produces infinity per IEEE 754).
Testing & Validation
- [x] Standalone validation confirms logic works correctly
- [x] Integer division by zero detected and returns proper error
- [x] Float division by zero allowed (produces infinity as expected)
- [x] Code compiles without errors
- [x] Original reproduction case still fails as expected (fix needs full build to test)
Next steps: Full build and integration testing will validate that the fix resolves the original crashing issue.
Fixes #16998.
💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.