-
Notifications
You must be signed in to change notification settings - Fork 3.3k
[WIP] ORT aborts with the linspace
implementation when input is empty
#25065
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can commit the suggested changes from lintrunner.
const size_t size = static_cast<size_t>(denominator_tensor.Shape().Size()); | ||
|
||
for (size_t i = 0; i < size; ++i) { | ||
if (data[i] == T{0}) { | ||
return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, | ||
"Division by zero error in Div operator"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const size_t size = static_cast<size_t>(denominator_tensor.Shape().Size()); | |
for (size_t i = 0; i < size; ++i) { | |
if (data[i] == T{0}) { | |
return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, | |
"Division by zero error in Div operator"); | |
const size_t size = static_cast<size_t>(denominator_tensor.Shape().Size()); | |
for (size_t i = 0; i < size; ++i) { | |
if (data[i] == T{0}) { | |
return ORT_MAKE_STATUS(ONNXRUNTIME, INVALID_ARGUMENT, | |
"Division by zero error in Div operator"); |
const auto& input_tensor_1 = context->RequiredInput<Tensor>(1); | ||
|
||
// Check for division by zero (only for integer types) | ||
ORT_RETURN_IF_ERROR(CheckDivisionByZero<T>(input_tensor_1)); | ||
|
||
ProcessBroadcastSpanFuncs funcs{ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
const auto& input_tensor_1 = context->RequiredInput<Tensor>(1); | |
// Check for division by zero (only for integer types) | |
ORT_RETURN_IF_ERROR(CheckDivisionByZero<T>(input_tensor_1)); | |
ProcessBroadcastSpanFuncs funcs{ | |
const auto& input_tensor_1 = context->RequiredInput<Tensor>(1); | |
// Check for division by zero (only for integer types) | |
ORT_RETURN_IF_ERROR(CheckDivisionByZero<T>(input_tensor_1)); | |
ProcessBroadcastSpanFuncs funcs{ |
test.AddInput<int32_t>("B", {1}, {0}); | ||
test.AddOutput<int32_t>("C", {1}, {0}); // Output doesn't matter since we expect failure | ||
|
||
// Should return an error instead of crashing | ||
test.Run(OpTester::ExpectResult::kExpectFailure, | ||
"Division by zero error in Div operator"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test.AddInput<int32_t>("B", {1}, {0}); | |
test.AddOutput<int32_t>("C", {1}, {0}); // Output doesn't matter since we expect failure | |
// Should return an error instead of crashing | |
test.Run(OpTester::ExpectResult::kExpectFailure, | |
"Division by zero error in Div operator"); | |
test.AddInput<int32_t>("B", {1}, {0}); | |
test.AddOutput<int32_t>("C", {1}, {0}); // Output doesn't matter since we expect failure | |
// Should return an error instead of crashing | |
test.Run(OpTester::ExpectResult::kExpectFailure, | |
"Division by zero error in Div operator"); |
test.AddInput<int64_t>("B", {1}, {0}); | ||
test.AddOutput<int64_t>("C", {1}, {0}); // Output doesn't matter since we expect failure | ||
|
||
// Should return an error instead of crashing | ||
test.Run(OpTester::ExpectResult::kExpectFailure, | ||
"Division by zero error in Div operator"); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test.AddInput<int64_t>("B", {1}, {0}); | |
test.AddOutput<int64_t>("C", {1}, {0}); // Output doesn't matter since we expect failure | |
// Should return an error instead of crashing | |
test.Run(OpTester::ExpectResult::kExpectFailure, | |
"Division by zero error in Div operator"); | |
test.AddInput<int64_t>("B", {1}, {0}); | |
test.AddOutput<int64_t>("C", {1}, {0}); // Output doesn't matter since we expect failure | |
// Should return an error instead of crashing | |
test.Run(OpTester::ExpectResult::kExpectFailure, | |
"Division by zero error in Div operator"); |
test.AddOutput<float>("C", {1}, {std::numeric_limits<float>::infinity()}); | ||
|
||
// Should succeed and produce infinity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test.AddOutput<float>("C", {1}, {std::numeric_limits<float>::infinity()}); | |
// Should succeed and produce infinity | |
test.AddOutput<float>("C", {1}, {std::numeric_limits<float>::infinity()}); | |
// Should succeed and produce infinity |
test.AddOutput<double>("C", {1}, {std::numeric_limits<double>::infinity()}); | ||
|
||
// Should succeed and produce infinity |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
test.AddOutput<double>("C", {1}, {std::numeric_limits<double>::infinity()}); | |
// Should succeed and produce infinity | |
test.AddOutput<double>("C", {1}, {std::numeric_limits<double>::infinity()}); | |
// Should succeed and produce infinity |
Fix ORT abort with
linspace
implementation when input is emptyAnalysis
onnxruntime/core/providers/cpu/math/element_wise_ops.cc
Implementation
#include <type_traits>
for compile-time type checkingCheckDivisionByZero<T>()
helper function that:if constexpr (std::is_integral_v<T>)
to only check integer typesDiv<T>::Compute()
to call validation before performing divisionChanges Summary
onnxruntime/core/providers/cpu/math/element_wise_ops.cc
onnxruntime/test/providers/cpu/math/element_wise_ops_test.cc
Root Cause
The
linspace
implementation creates a model that performs division by zero: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
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.