未验证 提交 47d764a3 编写于 作者: Z zyfncg 提交者: GitHub

Remove pybind index error (#40538)

* change the exception of getitem from pybind type to PADDLE_ENFORCE

* fix bug

* remove pybind::index_error exception
上级 f181d47f
......@@ -188,16 +188,14 @@ static void ParseIndexingSlice(
int start = static_cast<int>(PyLong_AsLong(slice_item));
auto s_t = start;
start = start < 0 ? start + dim_len : start;
if (start >= dim_len || start < 0) {
std::string str_error_message =
"The starting index " + std::to_string(s_t) +
" of slice is out of bounds in tensor " + std::to_string(dim) +
"-th axis, it shound be in the range of [" +
std::to_string(-dim_len) + ", " + std::to_string(dim_len) + ")";
// py::index_error is corresponding to IndexError in Python
// Used to indicate out of bounds access in __getitem__, __setitem__
throw py::index_error(str_error_message);
}
PADDLE_ENFORCE(
0 <= start && start < dim_len,
platform::errors::OutOfRange("The starting index %d of slice is out "
"of bounds in tensor %d-th axis, it "
"shound be in the range of [%d, %d).",
s_t, dim, -dim_len, dim_len));
slice_axes->push_back(dim);
slice_starts->push_back(start);
slice_ends->push_back(start + 1);
......
......@@ -585,14 +585,20 @@ inline void _getSliceinfo(const framework::Tensor &self, py::object obj,
auto &step = *pstep;
auto &slicelength = *pslicelength;
const framework::DDim &srcDDim = self.dims();
if (dim < 0 || dim >= srcDDim.size()) {
throw py::index_error();
}
PADDLE_ENFORCE(
0 <= dim && dim < srcDDim.size(),
platform::errors::OutOfRange("The dim %d of slice is out of bounds, it "
"shound be in the range of [0, %d).",
dim, srcDDim.size()));
if (py::isinstance<py::slice>(obj)) {
size_t lstart, lstop, lstep, lslicelength;
py::slice s = static_cast<py::slice>(obj);
if (!s.compute(srcDDim[dim], &lstart, &lstop, &lstep, &lslicelength)) {
throw py::index_error();
PADDLE_THROW(platform::errors::OutOfRange(
"Slice on dim: %d is error, please check the validity of tensor "
"dims or slice item.",
dim));
}
start = static_cast<int64_t>(lstart);
stop = static_cast<int64_t>(lstop);
......@@ -600,15 +606,19 @@ inline void _getSliceinfo(const framework::Tensor &self, py::object obj,
slicelength = static_cast<int64_t>(lslicelength);
} else if (py::isinstance<py::int_>(obj)) {
start = static_cast<int64_t>(static_cast<py::int_>(obj));
if (std::abs(start) >= srcDDim[dim]) {
throw py::index_error();
}
PADDLE_ENFORCE(
std::abs(start) < srcDDim[dim],
platform::errors::OutOfRange("The start %d of slice is out of bounds, "
"it shound be in the range of (%d, %d).",
start, -srcDDim[dim], srcDDim[dim]));
start = (start >= 0) ? start : srcDDim[dim] - start;
stop = start + 1;
step = 1;
slicelength = 1;
} else {
throw py::index_error();
PADDLE_THROW(
platform::errors::OutOfRange("Index object error, the index object for "
"slice only supports slice(::) and int."));
}
}
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册