aclGetViewStrides

Description

Obtains the ViewStrides of the aclTensor, that is, the stride relative to ViewShape. The aclTensor is created by calling aclCreateTensor.

Prototype

aclnnStatus aclGetViewStrides(const aclTensor *tensor, int64_t **stridesValue, uint64_t *stridesNum)

Parameter Description

Parameter

Input/Output

Description

tensor

Input

Input aclTensor. The aclTensor must be created by calling the aclCreateTensor API in advance.

stridesValue

Output

Returned ViewStrides value.

stridesNum

Output

Number of stride values of ViewStrides.

Return Value

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

Possible causes:

  • Error code 161001: tensor, stridesValue, or stridesNum is a null pointer.

Constraints

The memory for stridesValue is internally allocated by this API. After use, you need to delete the memory to manually release it.

Example

Assume that there is an aclTensor object (xTensor). Obtain its attributes such as the data type, data layout format, dimension, stride, and offset, and create an aclTensor object (yTensor) based on these attributes.

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

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
// 1. Create an xTensor.
int64_t xViewDims = {2, 4};       
int64_t xStridesValue = {4, 1};  // The stride of dimension 1 is 4, and the stride of dimension 2 is 1.
int64_t xStorageDims = {2, 4};    
xTensor = aclCreateTensor(xViewDims, 2, ACL_FLOAT16, xStridesValue, 0, ACL_FORMAT_ND, xStorageDims, 2, nullptr);

// 2. Obtain the attribute values of xTensor.
// Obtain the logical shape of xTensor. viewDims is {2, 4}, and viewDimsNum is 2.
int64_t *viewDims = nullptr;
uint64_t viewDimsNum = 0;
auto ret = aclGetViewShape(xTensor, &viewDims, &viewDimsNum);
// Obtain the data type (ACL_FLOAT16) of xTensor.
aclDataType dataType = aclDataType::ACL_DT_UNDEFINED;
ret = aclGetDataType(xTensor, &dataType);
// Obtain the stride of xTensor. stridesValue is {4, 1}, and stridesNum is 2.
int64_t *stridesValue = nullptr;
uint64_t stridesNum = 0;
ret = aclGetViewStrides(xTensor, &stridesValue, &stridesNum);
// Obtain the offset of the first element of xTensor relative to storage. The offset is 0.
int64_t offset = 0;
ret = aclGetViewOffset(xTensor, &offset);
// Obtain the data layout format (ACL_FORMAT_ND) of xTensor.
aclFormat format = aclFormat::ACL_FORMAT_UNDEFINED;
ret = aclGetFormat(xTensor, &format);
// Obtain the actual physical shape of xTensor. storageDims is {2, 4}, and storageDimsNum is 2.
int64_t *storageDims = nullptr;
uint64_t storageDimsNum = 0;
ret = aclGetStorageShape(xTensor, &storageDims, &storageDimsNum);
// Device address
void *deviceAddr;

// 3. Create a tensor based on the xTensor attributes.
aclTensor *yTensor = aclCreateTensor(viewDims, viewDimsNum, dataType, stridesValue, offset, format, storageDims, storageDimsNum, deviceAddr);

// 4. Manually free the memory.
delete[] viewDims;
delete[] stridesValue;
delete[] storageDims;