未验证 提交 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( ...@@ -188,16 +188,14 @@ static void ParseIndexingSlice(
int start = static_cast<int>(PyLong_AsLong(slice_item)); int start = static_cast<int>(PyLong_AsLong(slice_item));
auto s_t = start; auto s_t = start;
start = start < 0 ? start + dim_len : start; start = start < 0 ? start + dim_len : start;
if (start >= dim_len || start < 0) {
std::string str_error_message = PADDLE_ENFORCE(
"The starting index " + std::to_string(s_t) + 0 <= start && start < dim_len,
" of slice is out of bounds in tensor " + std::to_string(dim) + platform::errors::OutOfRange("The starting index %d of slice is out "
"-th axis, it shound be in the range of [" + "of bounds in tensor %d-th axis, it "
std::to_string(-dim_len) + ", " + std::to_string(dim_len) + ")"; "shound be in the range of [%d, %d).",
// py::index_error is corresponding to IndexError in Python s_t, dim, -dim_len, dim_len));
// Used to indicate out of bounds access in __getitem__, __setitem__
throw py::index_error(str_error_message);
}
slice_axes->push_back(dim); slice_axes->push_back(dim);
slice_starts->push_back(start); slice_starts->push_back(start);
slice_ends->push_back(start + 1); slice_ends->push_back(start + 1);
......
...@@ -585,14 +585,20 @@ inline void _getSliceinfo(const framework::Tensor &self, py::object obj, ...@@ -585,14 +585,20 @@ inline void _getSliceinfo(const framework::Tensor &self, py::object obj,
auto &step = *pstep; auto &step = *pstep;
auto &slicelength = *pslicelength; auto &slicelength = *pslicelength;
const framework::DDim &srcDDim = self.dims(); const framework::DDim &srcDDim = self.dims();
if (dim < 0 || dim >= srcDDim.size()) { PADDLE_ENFORCE(
throw py::index_error(); 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)) { if (py::isinstance<py::slice>(obj)) {
size_t lstart, lstop, lstep, lslicelength; size_t lstart, lstop, lstep, lslicelength;
py::slice s = static_cast<py::slice>(obj); py::slice s = static_cast<py::slice>(obj);
if (!s.compute(srcDDim[dim], &lstart, &lstop, &lstep, &lslicelength)) { 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); start = static_cast<int64_t>(lstart);
stop = static_cast<int64_t>(lstop); stop = static_cast<int64_t>(lstop);
...@@ -600,15 +606,19 @@ inline void _getSliceinfo(const framework::Tensor &self, py::object obj, ...@@ -600,15 +606,19 @@ inline void _getSliceinfo(const framework::Tensor &self, py::object obj,
slicelength = static_cast<int64_t>(lslicelength); slicelength = static_cast<int64_t>(lslicelength);
} else if (py::isinstance<py::int_>(obj)) { } else if (py::isinstance<py::int_>(obj)) {
start = static_cast<int64_t>(static_cast<py::int_>(obj)); start = static_cast<int64_t>(static_cast<py::int_>(obj));
if (std::abs(start) >= srcDDim[dim]) { PADDLE_ENFORCE(
throw py::index_error(); 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; start = (start >= 0) ? start : srcDDim[dim] - start;
stop = start + 1; stop = start + 1;
step = 1; step = 1;
slicelength = 1; slicelength = 1;
} else { } 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.
先完成此消息的编辑!
想要评论请 注册