aclnnCumprod&aclnnInplaceCumprod
Applicable Products
| Product | Supported |
|---|---|
| × | |
| √ | |
| √ | |
| × | |
| × | |
| × |
Function
Description: The
cumprodfunction computes the cumulative product of the input tensor along a specified dimension. For example, given a tensor representing a sequence of values,cumprodcan compute the product sequence from the starting position to the current position.Formula:
One-Dimensional Tensor (Vector) Case
For a one-dimensional tensor, the cumulative product is calculated as:
...
Expressed mathematically: , where .
High-dimensional tensor case (using a two-dimensional tensor as an example, dim=0 along the row direction)
For a two-dimensional tensor:
The resulting tensor after computation:
For the first column (j=1):
Therefore, for any column j, a similar pattern holds, namely:
High-dimensional tensor case (using a 2D tensor as an example, dim=1 along the column direction)
Therefore, for any column j, a similar pattern holds, namely:
Other parameters can be derived similarly based on the rules above.
Function Prototype
aclnnCumprod and aclnnInplaceCumprod implement the same function. The differences in usage are as follows. Select the appropriate operator based on your actual scenario.
aclnnCumprod: Create a new output tensor object to store the computation result.
aclnnInplaceCumprod: No need to create a new output tensor object. The computation result is stored directly in the memory of the input tensor.
Each operator uses the two-phase API. You must first call the "aclnnCumprodGetWorkspaceSize" or "aclnnInplaceCumprodGetWorkspaceSize" API to obtain the workspace size required for computation and the executor that encapsulates the operator computation process, and then call the "aclnnCumprod" or "aclnnInplaceCumprod" API to perform the computation.
aclnnStatus aclnnCumprodGetWorkspaceSize(
const aclTensor* input,
const aclScalar* dim,
const aclDataType dtype,
aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnCumprod(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnStatus aclnnInplaceCumprodGetWorkspaceSize(
aclTensor* input,
const aclScalar* dim,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnInplaceCumprod(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnCumprodGetWorkspaceSize
Parameters:
Name Input/Output Description Usage Data Type Data Format Dimension (Shape) Non-contiguous Tensor input (aclTensor*) Input Data for which the cumulative product is to be calculated. Empty tensors are supported. FLOAT, FLOAT16, BFLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, UINT16, UINT32, UINT64 ND The dimension cannot exceed 8. √ dim (aclScalar*) Input Dimension for calculating the cumulative product. For a two-dimensional tensor, dim=0 indicates that the calculation is performed along the row direction, and dim=1 indicates that the calculation is performed along the column direction. Value range: [–rank(input), rank(input)). INT32 - - - dtype (aclDataType) Input Data type of the input for the computation process. - If ACL_DT_UNDEFINED is used, the original type of the input is used for calculation.
- If a specific type is specified (within the range of data types supported by the input), the input is converted to this type before computation.
- - - - out (aclTensor*) Output Accumulated product result. - When dtype is ACL_DT_UNDEFINED, the data type must be the same as that of the input.
- When dtype is specified, the data type must be the same as that of dtype.
- The shape of out must be the same as that of the input.
FLOAT, FLOAT16, BFLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, UINT16, UINT32, UINT64 ND - √ workspaceSize (uint64_t*) Output Size of the workspace to be allocated on the device. - - - - - executor (aclOpExecutor**) Output Operator executor, containing the operator computation process. - - - - - Return Value:
aclnnStatus: status code. For details, see aclnn Return Codes.The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input or dim is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type and format of the input or dim are not within the supported range. The dim does not meet the shape constraint of the input. The shape of out is inconsistent with that of input.
aclnnCumprod
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address applied for on the Device side. workspaceSize Input Workspace size applied for on the Device side, obtained from the first-phase API aclnnCumprodGetWorkspaceSize. Executor Input Operator executor that contains the operator computation flow. stream Input Specifies the stream that executes the task. Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
aclnnInplaceCumprodGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor input (aclTensor*) Input/Output Data and result for which the cumulative product needs to be computed. Empty tensors are not supported. FLOAT, FLOAT16, BFLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, UINT16, UINT32, UINT64 ND No more than 8 dimensions. √ dim (aclScalar*) Input Specifies the dimension along which the cumulative product is computed. For a two-dimensional tensor, dim=0 indicates computation along the row direction, and dim=1 indicates computation along the column direction. Value range: [-rank(input), rank(input)]. INT32 - - - workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
| Return Value | Error Code | Description |
|---|---|---|
| ACLNN_ERR_PARAM_NULLPTR | 161001 | The input or dim is a null pointer. |
| ACLNN_ERR_PARAM_INVALID | 161002 | The data type or format of the input or dim passed in is not within the supported range. |
| The dim passed in does not meet the shape constraint of the input. |
aclnnInplaceCumprod
Parameters:
Parameter Input/Output Description workspace Input Specifies the workspace memory address applied for on the Device side. workspaceSize Input Specifies the workspace size applied for on the Device side, which is obtained by the first-phase API aclnnInplaceCumprodGetWorkspaceSize. Executor Input Operator executor, which contains the operator computation flow. Stream Input Specifies the stream for task execution. Return value
aclnnStatus: return code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnCumprod and aclnnInplaceCumprod default to a deterministic implementation.
Example
The following provides example code for reference only. For details about compilation and running, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_cumprod.h"
#define CHECK_RET(cond, return_expr) \
do \
{ \
if (!(cond)) \
{ \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do \
{ \
printf(message, ##__VA_ARGS__); \
} while (0)
int64_t GetShapeSize(const std::vector<int64_t> &shape)
{
int64_t shapeSize = 1;
for (auto i : shape)
{
shapeSize *= i;
}
return shapeSize;
}
void PrintOutResult(std::vector<int64_t> &shape, void **deviceAddr)
{
auto size = GetShapeSize(shape);
std::vector<int64_t> resultData(size, 0);
auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]),
*deviceAddr, size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return);
for (int64_t i = 0; i < size; i++)
{
LOG_PRINT("result[%ld] is: %d\n", i, resultData[i]);
}
}
template<typename T>
void PrintOutFloatResult(std::vector<T> &shape, void **deviceAddr, const char *name)
{
std::vector<float> resultData(shape.size(), 0);
auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]),
*deviceAddr, shape.size() * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return);
for (int64_t i = 0; i < shape.size(); i++)
{
LOG_PRINT("result var %s[%ld] is: %f\n", name, i, resultData[i]);
}
}
int Init(int32_t deviceId, aclrtStream *stream)
{
// Boilerplate: acl initialization.
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
return 0;
}
template <typename T>
int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr,
aclDataType dataType, aclTensor **tensor)
{
auto size = GetShapeSize(shape) * sizeof(T);
// Call aclrtMalloc to allocate device-side memory.
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
// Call aclrtMemcpy to copy host-side data to device-side memory.
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// Compute the strides of a contiguous tensor.
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--)
{
strides[i] = shape[i + 1] * strides[i + 1];
}
// Call the aclCreateTensor API to create an aclTensor.
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
template <typename T>
int CreateAclScalar(aclDataType dataType, T &hostData, aclScalar **scalar)
{
*scalar = aclCreateScalar(&hostData, dataType);
if (*scalar == nullptr)
{
return -1;
}
return 0;
}
int main()
{
// 1. (Boilerplate) Initialize the device/stream. Refer to the acl external API list and fill in deviceId based on your actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output. Custom construction is required based on the API interface.
void *xDeviceAddr = nullptr;
aclTensor *input = nullptr;
std::vector<int64_t> xShape = {3};
std::vector<int64_t> xHostData = {1,2,3};
// Create the original input x.
ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_INT64, &input);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the axis aclScalar.
int32_t axis_value = 0;
aclScalar *axis = nullptr;
ret = CreateAclScalar(aclDataType::ACL_INT32, axis_value, &axis);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the result aclTensor.
std::vector<int64_t> resultHostData(3, 0);
std::vector<int64_t> resultShape = {3};
void *resultDeviceAddr = nullptr;
aclTensor *result = nullptr;
ret = CreateAclTensor(resultHostData, resultShape, &resultDeviceAddr, aclDataType::ACL_INT64, &result);
CHECK_RET(ret == ACL_SUCCESS, return ret);
uint64_t workspaceSize = 0;
aclOpExecutor *executor;
aclDataType dtype = ACL_INT64;
void *workspaceAddr = nullptr;
// 3. Call the CANN operator library API.
// Call the first-phase API of aclnnCumprod.
ret = aclnnCumprodGetWorkspaceSize(input, axis, dtype, result, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCumprodGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for device memory based on the workspaceSize calculated by the first-phase API.
if (workspaceSize > 0)
{
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCumprod allocate workspace failed. ERROR: %d\n", ret); return ret);
}
// Call the aclnnCumprod second-phase API.
ret = aclnnCumprod(workspaceAddr, workspaceSize, executor, stream);
// 4. (Boilerplate) Wait for task execution to complete synchronously.
ret = aclrtSynchronizeStream(stream);
// 5. Obtain the output value and copy the result from the device memory to the host memory. Modify this based on the specific API definition.
PrintOutResult(resultShape, &resultDeviceAddr);
// 3. Call the CANN operator library API.
// Call the aclnnInplaceCumprod first-phase API.
ret = aclnnInplaceCumprodGetWorkspaceSize(input, axis, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceCumprodGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Apply for device memory based on the workspaceSize calculated by the first-phase API.
if (workspaceSize > 0)
{
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCumprod allocate workspace failed. ERROR: %d\n", ret); return ret);
}
// Call the second-phase API of aclnnInplaceCumprod.
ret = aclnnInplaceCumprod(workspaceAddr, workspaceSize, executor, stream);
// 4. (Boilerplate) Synchronize and wait for task execution to complete.
ret = aclrtSynchronizeStream(stream);
// 5. Obtain the output value and copy the result from device memory to host memory. Modify this based on the specific API definition.
PrintOutResult(resultShape, &xDeviceAddr);
// 6. Release aclTensor and aclScalar. Modify this based on the specific API definition.
aclDestroyTensor(input);
aclDestroyScalar(axis);
// // Release device resources. Modify based on the specific API definition.
aclrtFree(xDeviceAddr);
aclrtFree(resultDeviceAddr);
if (workspaceSize > 0)
{
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}