aclGetViewShape

Function Usage

Obtains the ViewShape of the aclTensor created by calling aclCreateTensor.

ViewShape indicates the logical shape of the aclTensor, which is the size of the tensor required in actual use. If ViewShape is [2, 5, 20], when the operator is used, the aclTensor can be considered as a data block [2, 5, 20].

Prototype

aclnnStatus aclGetViewShape(const aclTensor *tensor, int64_t **viewDims, uint64_t *viewDimsNum)

Parameters

Parameter

Input/Output

Description

tensor

Input

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

viewDims

Output

Dimension value of the viewShape.

viewDimsNum

Output

Number of dimensions of the viewShape.

Returns

0 on success; else, failure. For details about the return codes, see Common APIs and Return Codes.

Possible causes:

  • If error code 161001 is returned, tensor, viewDims, or viewDimsNum is a null pointer.

Constraints

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

Examples

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 code examples are for reference only and are not intended for direct copying and execution:

 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 information about 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-side 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;