aclInitTensor

Description

Initializes the parameters of a specified aclTensor created by calling aclCreateTensor.

If you want to reuse an existing aclTensor, you can call this API to reset the aclTensor attributes.

Prototype

aclnnStatus aclInitTensor(aclTensor *tensor, 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 *tensorDataAddr)

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

tensor

Input

aclTensor whose parameters are to be initialized.

viewDims

Input

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

viewDimsNum

Input

Number of ViewShape dimensions 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

Number of StorageShape dimensions of a tensor.

tensorDataAddr

Input

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

Return Value

0 on success; otherwise, failure. For details about the return codes, see Common API Return Codes.

Constraints

None.

Example

The following sample code is for reference only. Do not copy and run it.

1
2
3
4
5
6
std::vector<int64_t> viewDims = {2, 4};
std::vector<int64_t> stride = {4, 1};
std::vector<int64_t> storageDims = {2, 4};  
// The created aclTensor is reused as a tensor.
// deviceAddr indicates the storage address of the tensor on the device.
auto ret = aclInitTensor(tensor, viewDims.data(), viewDims.size(), ACL_FLOAT16, stride.data(), 0, aclFormat::ACL_FORMAT_ND, storageDims.data(), storageDims.size(), deviceAddr);