aclCreateTensor

Description

Creates an aclTensor object based on the tensor data type, data layout format, dimension, stride, offset, and device storage address. This object is used as the input parameter for single-operator API calls.

aclTensor is a built-in structure for managing and storing tensor data. You can use it without knowing how it works inside.

Prototype

aclTensor *aclCreateTensor(const int64_t *viewDims, uint64_t viewDimsNum, aclDataType dataType, const int64_t *stride, int64_t offset, aclFormat format, const int64_t *storageDims, uint64_t storageDimsNum, void *tensorData)

Parameter Description

  • aclDataType is a built-in enumeration class of data types. For details, see aclDataType.
  • aclFormat is a built-in enumeration class of data formats. For details, see aclFormat.
  • StorageShape and ViewShape of aclTensor:
    • ViewShape indicates the logical shape of the tensor, which is the size of the tensor required in actual use.
    • StorageShape indicates the actual physical layout shape of the tensor, which is the actual size of the tensor in the memory.

    Examples:

    • If StorageShape is [10, 20], the tensor is arranged in the memory based on [10, 20].
    • If ViewShape is [2, 5, 20], the tensor can be considered as a data block [2, 5, 20] for operator use.

Parameter

Input/Output

Description

viewDims

Input

ViewShape dimension value of a tensor, which is a non-negative integer.

viewDimsNum

Input

ViewShape dimension number of a tensor.

dataType

Input

Data type of a tensor.

stride

Input

Stride of elements in each dimension of the tensor, which is a non-negative integer.

offset

Input

Offset of the first element of the tensor relative to storage, which is a non-negative integer.

format

Input

Tensor format.

storageDims

Input

StorageShape dimension value of the tensor, which is a non-negative integer.

storageDimsNum

Input

StorageShape dimension number of a tensor.

tensorData

Input

Storage address of the tensor on the device. The address must be 32-byte aligned. Otherwise, an undefined error may occur.

Return Value

Created aclTensor on success; otherwise, nullptr.

Constraints

Examples

The definition of aclTensor is similar to that of torch.Tensor. aclTensor consists of a contiguous or discontiguous memory address and a series of description information (such as stride and offset). Based on the shape, stride, and offset information, the tensor can fetch data from the memory and obtain discontiguous memory (for example, y in Figure 1).

Figure 1 Logical structure of a tensor
  • Figure 1 shows how to create an x tensor.
    1
    2
    3
    4
    5
    6
    aclTensor *CreateXTensor() {
        std::vector<int64_t> viewDims = {2, 4};
        std::vector<int64_t> stride = {4, 1}; // The stride of dimension 1 is 4, and the stride of dimension 2 is 1.
        std::vector<int64_t> storageDims = {2, 4};       
        return aclCreateTensor(viewDims.data(), 2, ACL_FLOAT16, stride.data(), 0, ACL_FORMAT_ND, storageDims.data(), 2, nullptr);
    }
    
  • Figure 1 shows how to create a transposed x^T tensor corresponding to x:
    1
    2
    3
    4
    5
    6
    aclTensor *CreateXTransposedTensor() {
        std::vector<int64_t> viewDims = {4, 2};       
        std::vector<int64_t> stride = {1, 4};         // Transpose stride
        std::vector<int64_t> storageDims = {2, 4};    
        return aclCreateTensor(viewDims.data(), 2, ACL_FLOAT16, stride.data(), 0, ACL_FORMAT_ND, storageDims.data(), 2, nullptr);
    }
    

According to the preceding examples, the code that uses aclTensor as the input parameter for single-operator APIs is as follows. The following code examples are for reference only. Do not copy and run it.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
// Create an aclTensor.
aclTensor *xTensor = CreateXTensor();
aclTensor *xTransposedTensor = CreateXTransposedTensor();
// Use the aclTensor as the input parameter for single-operator API calls.
auto ret = aclxxXxxGetWorkspaceSize(xTensor, xTransposedTensor, ..., outTensor, ..., &workspaceSize, &executor);
ret = aclxxXxx(...);
...
// Destroy the aclTensor.
ret = aclDestroyTensor(xTensor);
ret = aclDestroyTensor(xTransposedTensor);