#
# SPDX-FileCopyrightText: Copyright (c) 2026 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
from nvtripy import export
from nvtripy.frontend.ops import utils as op_utils
from nvtripy.trace.ops.gather import Gather
from nvtripy.frontend import wrappers
from nvtripy.common import datatype as dt
from nvtripy.frontend.constraints import GetInput, GetReturn, OneOf
[docs]
@export.public_api(document_under="operations/functions")
@wrappers.interface(
input_requirements=OneOf(
GetInput("input").dtype,
[dt.float32, dt.float16, dt.bfloat16, dt.int4, dt.int8, dt.int32, dt.int64, dt.bool],
)
& OneOf(GetInput("index").dtype, [dt.int32, dt.int64]),
output_guarantees=GetReturn(0).dtype == GetInput("input").dtype,
)
def gather(input: "nvtripy.Tensor", dim: int, index: "nvtripy.Tensor") -> "nvtripy.Tensor":
"""
Gather values from the input tensor along the specified axis based on the specified indices.
This behaves similarly to `numpy.take() <https://numpy.org/doc/2.2/reference/generated/numpy.take.html>`_.
Args:
input: The input tensor.
dim: The dimension along which to gather.
index: A tensor of indices to gather.
Values along the provided dimension of the input are effectively replaced by the values
at the specified indices. If the index tensor is multi-dimensional, the values along
the dimension will be replaced by tensors instead of scalars.
Returns:
A new tensor of the same shape along every
dimension except ``dim``, which will have a size equal to ``index.shape[0]``.
.. code-block:: python
:linenos:
input = tp.iota((3, )) + 4
index = tp.Tensor([0, 2])
output = tp.gather(input, dim=0, index=index)
assert np.array_equal(cp.from_dlpack(output).get(), np.take(cp.from_dlpack(input).get(), np.from_dlpack(index), axis=0))
.. code-block:: python
:linenos:
:caption: Multi-dimensional Input
input = tp.iota((3, 3)) + 4
index = tp.Tensor([0, 2])
output = tp.gather(input, dim=0, index=index)
assert np.array_equal(cp.from_dlpack(output).get(), np.take(cp.from_dlpack(input).get(), np.from_dlpack(index), axis=0))
.. code-block:: python
:linenos:
:caption: Multi-dimensional Indices
input = tp.iota((3, )) + 4
index = tp.Tensor([[0], [2], [1], [2]])
output = tp.gather(input, dim=0, index=index)
assert np.array_equal(cp.from_dlpack(output).get(), np.take(cp.from_dlpack(input).get(), np.from_dlpack(index), axis=0))
.. code-block:: python
:linenos:
:caption: Multi-dimensional Input And Indices
input = tp.iota((3, 3)) + 4
index = tp.Tensor([[0, 2], [2, 1]])
output = tp.gather(input, dim=0, index=index)
assert np.array_equal(cp.from_dlpack(output).get(), np.take(cp.from_dlpack(input).get(), np.from_dlpack(index), axis=0))
"""
dim = op_utils.process_dim(dim, input.rank)
return op_utils.create_op(Gather, [input, index], dim)