aclCreateTensor

Function Usage

Creates an aclTensor.

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)

Parameters

  • The definitions of aclDataType and aclFormat are provided by AscendCL. For details, see aclDataType and aclFormat in Application Development APIs.
  • StorageShape and ViewShape of aclTensor:
    • ViewShape indicates the logical shape of the aclTensor, which is the size of the tensor required in actual use.
    • StorageShape indicates the actual physical layout shape of the aclTensor, which is the actual size of the tensor in the memory.

    An example is as follows:

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

Parameter

Input/Output

Description

viewDims

Input

ViewShape dimension value of a tensor. The value cannot be a negative number.

viewDimsNum

Input

ViewShape dimension number of a tensor.

dataType

Input

Data type of a tensor.

stride

Input

Stride of the next value of each dimension of a tensor. The value cannot be a negative number.

offset

Input

Offset of the first element of the tensor relative to storage.

format

Input

Tensor format.

storageDims

Input

StorageShape dimension value of a tensor. The value cannot be a negative number.

storageDimsNum

Input

StorageShape dimension number of a tensor.

tensorData

Input

Storage address of a tensor on the device.

Returns

Created aclTensor on success; else, nullptr.

Reference

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 or obtain discontiguous memory (for example, y in Figure 1).

Figure 1 Logical structure of a tensor

Examples

  • The following uses Figure 1 as an example to describe how to create an x tensor:
    aclTensor *CteateXTensor()
    {
        int64_t viewDims = {2, 4};       
        int64_t stride = {4, 1};          // Stride of dimension 1 is 4, and stride of dimension 2 is 1.
        int64_t storageDims = {2, 4};    
        return aclCreateTensor(viewDims, 2, ACL_FLOAT16, stride, 0, ACL_FORMAT_ND, storageDims, 2, nullptr);
    }
  • The following uses Figure 1 as an example to describe how to create a transposed x^T tensor corresponding to x:
    aclTensor *CreateXTransposedTensor()
    {
        int64_t viewDims = {4, 2};       
        int64_t stride = {1, 4};         // Transpose stride
        int64_t storageDims = {2, 4};    
        return aclCreateTensor(viewDims, 2, ACL_FLOAT16, stride, 0, ACL_FORMAT_ND, storageDims, 2, nullptr);
    }