Skip to content
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

Remove pybind index error #40538

Merged
merged 3 commits into from
Mar 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 8 additions & 10 deletions paddle/fluid/pybind/slice_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
26 changes: 18 additions & 8 deletions paddle/fluid/pybind/tensor_py.h
Original file line number Diff line number Diff line change
Expand Up @@ -585,30 +585,40 @@ 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);
step = static_cast<int64_t>(lstep);
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."));
}
}

Expand Down