MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / _get_idx

Function _get_idx

imperative/python/megengine/functional/tensor.py:659–679  ·  view source on GitHub ↗
(index, axis)

Source from the content-addressed store, hash-verified

657
658
659def _get_idx(index, axis):
660 index_dims = len(index.shape)
661 idx = []
662 if axis < 0:
663 axis += index_dims
664 for i in range(index_dims):
665 if i != axis:
666 shape = [1] * index_dims
667 shape[i] = index.shape[i]
668 arange = linspace(
669 0, index.shape[i] - 1, index.shape[i], device=index.device,
670 )
671 arange = (
672 broadcast_to(arange.reshape(*shape), index.shape)
673 .reshape(-1)
674 .astype(np.int32)
675 )
676 idx.append(arange)
677 else:
678 idx.append(index.reshape(-1))
679 return tuple(idx)
680
681
682def gather(inp: Tensor, axis: int, index: Tensor) -> Tensor:

Callers 2

gatherFunction · 0.85
scatterFunction · 0.85

Calls 5

linspaceFunction · 0.70
broadcast_toFunction · 0.70
astypeMethod · 0.45
reshapeMethod · 0.45
appendMethod · 0.45

Tested by

no test coverage detected