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 No Used Indexing Code #59281

Merged
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
420 changes: 0 additions & 420 deletions paddle/fluid/pybind/eager_method.cc

Large diffs are not rendered by default.

164 changes: 0 additions & 164 deletions paddle/fluid/pybind/slice_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -144,170 +144,6 @@ static int _PySlice_GetIndices(PySliceObject* r,
return 0;
}

static void ParseIndexingSlice(phi::DDim shape,
PyObject* _index,
std::vector<int64_t>* slice_axes,
std::vector<int64_t>* slice_starts,
std::vector<int64_t>* slice_ends,
std::vector<int64_t>* slice_strides,
std::vector<int64_t>* decrease_axis,
std::vector<int64_t>* none_axes,
std::vector<int64_t>* infer_flags,
std::vector<int64_t>* list_select_idxs,
bool* list_select_flag) {
// We allow indexing by Integers, Slices, Ellipsis, None, tuples of those
// types, and list of Bool and Integers.
// wrap to tuple

// NOTE(zhiqiu): PyTuple_Pack increases refcount.
PyObject* index = !PyTuple_Check(_index) ? PyTuple_Pack(1, _index) : _index;
DEFINE_PADDLE_SCOPE_GUARD([index, _index]() {
if (!PyTuple_Check(_index)) {
Py_DECREF(index);
VLOG(4) << "Call Py_DECREF";
}
});

const int rank = shape.size();
const int size = PyTuple_GET_SIZE(index);

// specified_dims is the number of dimensions which indexed by Interger,
// Slices.
int specified_dims = 0;
int ell_count = 0;
for (int dim = 0; dim < size; ++dim) {
PyObject* slice_item = PyTuple_GetItem(index, dim);
if (PyCheckInteger(slice_item) || PySlice_Check(slice_item)) {
specified_dims++;
} else if (slice_item == Py_Ellipsis) {
ell_count++;
}
}

PADDLE_ENFORCE_LE(ell_count,
1,
platform::errors::InvalidArgument(
"An index can only have a single ellipsis ('...')"));
int none_count = 0;
for (int i = 0, dim = 0; i < size; ++i) {
PyObject* slice_item = PyTuple_GetItem(index, i);

infer_flags->push_back(1);
int64_t dim_len = shape[dim];
if (PyCheckInteger(slice_item) || IsNumpyType(slice_item)) {
// integer, PyLong_AsLong supports both int and long
int64_t start = static_cast<int64_t>(PyLong_AsLong(slice_item));
auto s_t = start;
start = start < 0 ? start + dim_len : start;

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);
slice_strides->push_back(1);
decrease_axis->push_back(dim);
dim++;
} else if (PySlice_Check(slice_item)) {
// slice item
Py_ssize_t start, end, step;
PySliceObject* p = reinterpret_cast<PySliceObject*>(slice_item);
_PySlice_GetIndices(p, dim_len, &start, &end, &step);

// :: or : or 0:dim_len:1
if (start == 0 && end == dim_len && step == 1) {
dim++;
continue;
}
slice_axes->push_back(dim);
slice_starts->push_back(start);
slice_ends->push_back(end);
slice_strides->push_back(step);
dim++;
} else if (slice_item == Py_Ellipsis) {
dim += rank - specified_dims;
} else if (slice_item == Py_None) {
none_axes->push_back(dim + none_count);
none_count++;
} else if (PyList_Check(slice_item)) {
*list_select_flag = true;
PADDLE_ENFORCE_EQ(
size,
1,
platform::errors::InvalidArgument(
"When index contains a list, its length is excepted to 1, "
"but received %d",
size));
bool all_bool = true;
int list_size = PyList_GET_SIZE(slice_item);
for (int j = 0; j < list_size; ++j) {
PyObject* list_item = PyList_GetItem(slice_item, j);
if (PyCheckInteger(list_item)) {
all_bool = false;
} else if (!PyBool_Check(list_item)) {
PADDLE_THROW(platform::errors::InvalidArgument(
"Only support int or bool in index list."));
}
}
if (all_bool) {
PADDLE_ENFORCE_EQ(
list_size,
shape[0],
platform::errors::InvalidArgument(
"The dimension of bool index doesn't match indexed array along "
"dimension 0, the target dimension is %d, but received %d.",
shape[0],
list_size));

for (int j = 0; j < list_size; ++j) {
PyObject* list_item = PyList_GetItem(slice_item, j);
if (list_item == Py_True) {
list_select_idxs->push_back(j);
}
}
} else {
for (int j = 0; j < list_size; ++j) {
PyObject* list_item = PyList_GetItem(slice_item, j);
if (PyCheckInteger(list_item)) {
list_select_idxs->push_back(
static_cast<int>(PyLong_AsLong(list_item)));
} else if (list_item == Py_True) {
list_select_idxs->push_back(1);
} else {
list_select_idxs->push_back(0);
}
}
}

} else {
PADDLE_THROW(platform::errors::InvalidArgument(
"Currently, Tensor.__indices__() only allows indexing "
"by Integers, Slices, Ellipsis, None, tuples of these types "
"and list of Bool and Integers, but received "
"%s in %dth slice item",
std::string(Py_TYPE(slice_item)->tp_name),
i + 1));
}
}

// valid_index is the number of dimensions exclude None index
const int valid_indexs = size - none_axes->size() - ell_count;
PADDLE_ENFORCE_EQ(valid_indexs <= rank,
true,
platform::errors::InvalidArgument(
"Too many indices (%d) for tensor of dimension %d.",
valid_indexs,
rank));
}

static void ParseIndex(const paddle::Tensor& tensor,
PyObject* _index,
std::vector<int64_t>* slice_axes,
Expand Down
4 changes: 0 additions & 4 deletions python/paddle/base/dygraph/tensor_patch_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
EagerParamBase,
Parameter,
Variable,
_setitem_impl_,
convert_np_dtype_to_dtype_,
)
from .base import switch_to_static_graph
Expand Down Expand Up @@ -881,9 +880,6 @@ def __getitem__(self, item):
return self._getitem_dygraph(item)

def __setitem__(self, item, value):
if core.is_compiled_with_xpu():
# (NOTE): Currently, there is no index_put_xpu kernel.
return _setitem_impl_(self, item, value)
item, value = pre_deal_index_and_value(self, item, value)
return self._setitem_dygraph(item, value)

Expand Down
5 changes: 1 addition & 4 deletions python/paddle/base/framework.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
data_feed_pb2, # noqa: F401
framework_pb2,
)
from .variable_index import _getitem_static, _setitem_impl_, _setitem_static
from .variable_index import _getitem_static, _setitem_static
from .wrapped_decorator import signature_safe_contextmanager, wrap_decorator

if TYPE_CHECKING:
Expand Down Expand Up @@ -2472,9 +2472,6 @@ def __setitem__(self, item, value):
from .dygraph.base import in_to_static_mode

if in_to_static_mode():
if is_compiled_with_xpu():
# (NOTE): Currently, there is no index_put_xpu kernel.
return _setitem_impl_(self, item, value)
return _setitem_static(self, item, value)
else:
raise RuntimeError(
Expand Down
Loading