Transpose

Availability

Atlas Training Series Product

Function Usage

Transposes the tensor without changing its value.

The transpose operator can be used to rearrange the tensor dimensions.

Specifically, it transposes the shape of input tensor x according to the permutation of dimensions (perm) and outputs the result.

Prototype

  • If the input and output addresses are different:

    const aclTensor *Transpose(const aclTensor *x, const aclTensor *y, const aclTensor *perm, aclOpExecutor *executor)

  • If the input and output addresses are the same:

    const aclTensor *Transpose(const aclTensor *x, const aclIntArray *perm, aclOpExecutor *executor)

Parameters

Parameter

Input/Output

Description

x

Input/Output

Input tensor. The input tensor x must be contiguous memory data. The supported data types are FLOAT16, FLOAT, INT8, INT16, INT32, INT64, UINT8, UINT16, UINT32, UINT64, BOOL. The data format can be ND.

y

Output

Output tensor after transposing. It has the same data type and format as x.

perm

Input

Permutation of dimensions of input tensor x, which is an integer array. It can be of the aclIntArray* or aclTensor* type. A maximum of 8D is supported. The value range is [0, Dimension count of x – 1]. The data type can be INT32 or INT64. The data format can be ND.

executor

Input

Operator executor, containing the operator computation process.

Returns

Success: The transposed tensor is returned. Failure: nullptr is returned.

Constraints

  • A maximum of 8D is supported. That is, the maximum dim value of input x and perm is 8.
  • The input x must have the same dimensions (dim) as perm.

Example

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
// (Fixed writing) Create an OpExecutor and check parameters.
auto uniqueExecutor = CREATE_EXECUTOR();
CHECK_RET(uniqueExecutor.get() != nullptr, ACLNN_ERR_INNER_CREATE_EXECUTOR);

// (Fixed writing) Convert the input self into a contiguous tensor.
auto selfContiguous = l0op::Contiguous(self, uniqueExecutor.get());
CHECK_RET(selfContiguous != nullptr, ACLNN_ERR_INNER_NULLPTR);

int64_t dims = selfContiguous->GetViewShape().GetDimNum();
int64_t valuePerm[dims] = {0, 2, 1, 3}; // The middle two dimensions of the original four dimensions are transposed, that is, the first axis and the second axis are exchanged.

auto perm = executor->AllocIntArray(valuePerm, dims);
selfContiguous = l0op::Transpose(selfContiguous, perm, uniqueExecutor.get());