Source code for tripy.frontend.ops.transpose

# SPDX-FileCopyrightText: Copyright (c) 2024 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 dataclasses import dataclass

from tripy import constraints, export
from tripy.common.exception import raise_error


[docs] @export.public_api(document_under="operations/functions") @constraints.dtypes( constraints={"input": "T1", constraints.RETURN_VALUE: "T1"}, variables={"T1": ["float32", "float16", "bfloat16", "float8", "int4", "int8", "int32", "int64", "bool"]}, ) def transpose(input: "tripy.Tensor", dim0: int, dim1: int) -> "tripy.Tensor": """ Returns a new tensor that is a transposed version of the input tensor where ``dim0`` and ``dim1`` are swapped. Args: input: The input tensor. dim0: The first dimension to be transposed. dim1: The second dimension to be transposed. Returns: A new tensor. .. code-block:: python :linenos: :caption: Example input = tp.reshape(tp.arange(6, dtype=tp.float32), (2, 3)) output = tp.transpose(input, 0, 1) assert np.array_equal(cp.from_dlpack(output).get(), np.transpose(np.arange(6, dtype=np.float32).reshape(2, 3), (1, 0))) """ from tripy.frontend.trace.ops.permute import permute if input.rank < 2: raise_error("Transpose input must have at least 2 dimensions.", [f"Note: Input had {input.rank} dimensions."]) perm = list(range(input.rank)) perm[dim0], perm[dim1] = perm[dim1], perm[dim0] return permute(input, perm)