Skip to content

Commit e1236ca

Browse files
authored
[CPU] Fix arithmetic overflow and legacy TODO in Det operator (#27070)
### Description This PR fixes the legacy `TODO: fix the warnings` in the `Det` operator. The arithmetic overflow warning (C26451) is addressed by using `int64_t` for tensor dimension and batch size calculations, ensuring safe pointer arithmetic. ### Motivation and Context - Removes unused warning suppression pragma. - Prevents potential overflow when handling large batches of matrices.
1 parent 0432e71 commit e1236ca

File tree

1 file changed

+8
-11
lines changed
  • onnxruntime/core/providers/cpu/math

1 file changed

+8
-11
lines changed

onnxruntime/core/providers/cpu/math/det.cc

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,7 @@
33

44
#include "core/providers/cpu/math/det.h"
55
#include "core/util/math_cpuonly.h"
6-
// TODO: fix the warnings
7-
#if defined(_MSC_VER) && !defined(__clang__)
8-
// Chance of arithmetic overflow could be reduced
9-
#pragma warning(disable : 26451)
10-
#endif
6+
#include "core/common/narrow.h"
117

128
using namespace onnxruntime::common;
139

@@ -33,7 +29,7 @@ Status Det<T>::Compute(OpKernelContext* context) const {
3329
ORT_ENFORCE(X != nullptr);
3430

3531
const auto& X_shape = X->Shape();
36-
int X_num_dims = static_cast<int>(X_shape.NumDimensions());
32+
size_t X_num_dims = X_shape.NumDimensions();
3733

3834
// input validation
3935
if (X_num_dims < 2) { // this is getting capture by shape inference code as well
@@ -44,10 +40,11 @@ Status Det<T>::Compute(OpKernelContext* context) const {
4440
}
4541

4642
const auto* X_data = X->Data<T>();
47-
int matrix_dim = static_cast<int>(X_shape[X_num_dims - 1]);
43+
int64_t matrix_dim = X_shape[X_num_dims - 1];
4844

4945
auto get_determinant = [matrix_dim](const T* matrix_ptr) -> T {
50-
auto one_eigen_mat = ConstEigenMatrixMapRowMajor<T>(matrix_ptr, matrix_dim, matrix_dim);
46+
auto one_eigen_mat = ConstEigenMatrixMapRowMajor<T>(
47+
matrix_ptr, onnxruntime::narrow<Eigen::Index>(matrix_dim), onnxruntime::narrow<Eigen::Index>(matrix_dim));
5148
return one_eigen_mat.determinant();
5249
};
5350

@@ -60,15 +57,15 @@ Status Det<T>::Compute(OpKernelContext* context) const {
6057
std::vector<int64_t> output_shape;
6158
output_shape.reserve(X_num_dims - 2);
6259
int64_t batch_size = 1;
63-
for (int i = 0; i < X_num_dims - 2; ++i) {
60+
for (size_t i = 0; i < X_num_dims - 2; ++i) {
6461
batch_size *= X_shape[i];
6562
output_shape.push_back(X_shape[i]);
6663
}
6764

68-
int num_matrix_elems = matrix_dim * matrix_dim;
65+
int64_t num_matrix_elems = matrix_dim * matrix_dim;
6966
auto* Y = context->Output(0, output_shape);
7067
auto* Y_data = Y->MutableData<T>();
71-
for (int b = 0; b < static_cast<int>(batch_size); ++b) { // can be parallelized if need to
68+
for (int64_t b = 0; b < batch_size; ++b) { // can be parallelized if need to
7269
const T* one_matrix = X_data + (b * num_matrix_elems);
7370
*Y_data++ = get_determinant(one_matrix);
7471
}

0 commit comments

Comments
 (0)