MCPcopy Create free account
hub / github.com/PaddlePaddle/Paddle / parse_index

Function parse_index

python/paddle/base/variable_index.py:260–427  ·  view source on GitHub ↗
(x, indices)

Source from the content-addressed store, hash-verified

258
259
260def parse_index(x, indices):
261 is_tensor_array = is_tensor_array_type(x)
262
263 advanced_index = (
264 [] if is_tensor_array else [None] * 2 * len(x.shape)
265 ) # content is (dim, index)
266 # for set_value / slice / strided_slice OP
267 decrease_axes = []
268 axes = []
269 starts = []
270 ends = []
271 steps = []
272 use_strided_slice = False
273 has_advanced_index = False
274
275 if not isinstance(indices, tuple):
276 indices = (indices,)
277
278 indices = replace_ndarray_and_range(indices)
279 indices = replace_ellipsis(x, indices)
280 indices, none_axes = replace_none(indices)
281
282 estimated_dim = 0
283 dim = 0
284 for i, slice_item in enumerate(indices):
285 start, end, step = None, None, None
286 if type(slice_item) is int:
287 if (
288 not is_tensor_array
289 and x.shape[dim] is not None
290 and x.shape[dim] >= 0
291 and slice_item >= x.shape[dim]
292 ):
293 # For python, if users write a, b = var, the __getitem__
294 # method will iterate through 0, 1, 2 ... until __getitem__
295 # throws an IndexError, then stop. The var[0], var[1] will
296 # be given to a, b respectively. If more values are given,
297 # the unpack size would cause error.
298 # We raises IndexError here to support grammar like `a, b = var`
299 raise IndexError(
300 f"slice_item {slice_item} at dim {dim} should be >= 0 and < x.shape[{dim}]: {x.shape[dim]}"
301 )
302 # not calculate result to reduce call times for slice OP.
303 decrease_axes.append(dim)
304 start = slice_item
305 step = 1
306 end = slice_item + 1 if slice_item != -1 else MAX_INTEGER
307 dim += 1
308 elif is_scalar_tensor(slice_item):
309 # not calculate result to reduce call times for slice OP.
310 decrease_axes.append(dim)
311 start = slice_item
312 step = 1
313 end = slice_item + 1
314 dim += 1
315 elif isinstance(slice_item, bool):
316 # single bool is advanced-indexing
317 none_axes.append(dim)

Callers 2

_setitem_staticFunction · 0.85
_getitem_staticFunction · 0.85

Calls 10

is_tensor_array_typeFunction · 0.85
replace_noneFunction · 0.85
IndexErrorClass · 0.85
is_scalar_tensorFunction · 0.85
to_tensorMethod · 0.80
replace_ellipsisFunction · 0.70
typeFunction · 0.50
appendMethod · 0.45

Tested by

no test coverage detected