Source code for nvtripy.frontend.ops.squeeze

# SPDX-FileCopyrightText: Copyright (c) 2025 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 typing import Sequence, Union

from nvtripy import export, utils
from nvtripy.frontend.ops import utils as op_utils
from nvtripy.utils import wrappers


[docs] @export.public_api(document_under="operations/functions") @wrappers.interface( dtype_constraints={"input": "T1", wrappers.RETURN_VALUE: "T1"}, dtype_variables={"T1": ["float32", "float16", "bfloat16", "int8", "int32", "int64", "bool"]}, ) def squeeze(input: "nvtripy.Tensor", dims: Union[Sequence[int], int]) -> "nvtripy.Tensor": """ Returns a new tensor with the specified singleton dimensions of the input tensor removed. Args: input: The input tensor. dims: The dimension(s) to remove. These must have a length of 1. Returns: A new tensor. .. code-block:: python :linenos: :caption: Squeeze All Dimensions input = tp.iota((1, 2, 1), dtype=tp.float32) output = tp.squeeze(input, dims=(0, 2)) assert np.array_equal(cp.from_dlpack(output).get(), np.squeeze(cp.from_dlpack(input).get())) .. code-block:: python :linenos: :caption: Squeeze First Dimension input = tp.iota((1, 2, 1), dtype=tp.float32) output = tp.squeeze(input, 0) assert np.array_equal(cp.from_dlpack(output).get(), np.squeeze(cp.from_dlpack(input).get(), 0)) .. code-block:: python :linenos: :caption: Squeeze First And Third Dimension input = tp.iota((1, 2, 1), dtype=tp.float32) output = tp.squeeze(input, (0, 2)) assert np.array_equal(cp.from_dlpack(output).get(), np.squeeze(cp.from_dlpack(input).get(), (0, 2))) """ from nvtripy.frontend.ops.reshape import reshape dims = utils.utils.make_tuple(dims) if not dims: return input dims = tuple(op_utils.process_dim(dim, input.rank) for dim in dims) shape = input.shape new_shape = [dim for idx, dim in enumerate(shape) if idx not in dims] return reshape(input, new_shape)