diff --git a/src/operator/tensor/matrix_op-inl.h b/src/operator/tensor/matrix_op-inl.h index 69bc3e0e4dc9..5eecda622729 100644 --- a/src/operator/tensor/matrix_op-inl.h +++ b/src/operator/tensor/matrix_op-inl.h @@ -653,53 +653,43 @@ inline void GetIndexRange(const mxnet::TShape& dshape, } for (index_t i = 0; i < param_begin.ndim(); ++i) { - index_t b = 0, e = dshape[i], s = 1; + index_t s = param_step.ndim() != 0U && param_step[i].has_value() ? param_step[i].value() : 1; + CHECK_NE(s, 0) << "slice op step[" << i << "] cannot be 0"; + + index_t b = 0, e = 0; const index_t len = dshape[i]; - if (param_step.ndim() != 0U) { - const auto& opt_step_val = param_step[i]; - if (opt_step_val.has_value()) { - s = opt_step_val.value(); - CHECK_NE(s, 0) << "slice op step[" << i << "] cannot be 0"; + if (len > 0) { + b = param_begin[i].has_value() ? param_begin[i].value() : (s < 0 ? len - 1 : 0); + e = param_end[i].has_value() ? param_end[i].value() : (s < 0 ? -1 : len); + + // checking upper and lower bounds for begin + if (b < 0) { + b += len; + CHECK_GE(b, 0) << "slicing with begin[" << i << "]=" << b - len + << " exceeds limit of input dimension[" << i << "]=" << len; } - } - - if (len) { - if (param_begin[i].has_value()) { - b = param_begin[i].value(); - if (b < 0) { - b += len; - CHECK_GE(b, 0) << "slicing with begin[" << i << "]=" - << b - len << " exceeds limit of " << len; - } - } else if (s < 0) { - b = len - 1; + CHECK_LT(b, len) << "slicing with begin[" << i << "]=" << b + << " exceeds limit of input dimension[" << i << "]=" << len; + + // checking upper and lower bounds for end + if (e < 0 && param_end[i].has_value()) { + e += len; + CHECK_GE(e, 0) << "slicing with end[" << i << "]=" << e - len + << " exceeds limit of input dimension[" << i << "]=" << len; } - CHECK_LT(b, len) << "slicing with begin[" << i << "]=" - << b << " exceeds limit of " << len; - - if (param_end[i].has_value()) { - e = param_end[i].value(); - if (e < 0) { - e += len; - CHECK_GE(e, 0) << "slicing with end[" << i << "]=" - << e - len << " exceeds limit of " << len; - } - } else if (s < 0) { - e = -1; - } - CHECK_LE(e, len) << "slicing with end[" << i << "]=" - << e << " exceeds limit of " << len; + CHECK_LE(e, len) << "slicing with end[" << i << "]=" << e + << " exceeds limit of input dimension[" << i << "]=" << len; + // checking begin==end case which is not supported CHECK_NE(b, e) << "slicing with begin[" << i << "]=end[" << i << "]=" - << e << " results in an empty tensor"; - } else { - b = 0; - e = 0; + << e << " results in an empty tensor and is not supported"; } + (*begin)[i] = b; (*end)[i] = e; (*step)[i] = s; } + for (index_t i = param_begin.ndim(); i < dshape.ndim(); ++i) { (*begin)[i] = 0; (*end)[i] = dshape[i];