# 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.fromtypingimportSequence,Unionfromnvtripyimportexport,utilsfromnvtripy.frontend.opsimportutilsasop_utilsfromnvtripy.utilsimportwrappers
[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"]},)defsqueeze(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))) """fromnvtripy.frontend.ops.reshapeimportreshapedims=utils.utils.make_tuple(dims)ifnotdims:returninputdims=tuple(op_utils.process_dim(dim,input.rank)fordimindims)shape=input.shapenew_shape=[dimforidx,diminenumerate(shape)ifidxnotindims]returnreshape(input,new_shape)