aclnnCumprod&aclnnInplaceCumprod
Applicable Products
Ascend 950PR&950DT products : Not supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not 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 in mathematical formula .
High-dimensional tensor case (taking a two-dimensional tensor as an example, with 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 also applies:
High-dimensional tensor (taking a two-dimensional tensor as an example, with dim=1 along the column direction) Therefore, for any column j, a similar pattern also applies:
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 a two-phase API. Call the "aclnnCumprodGetWorkspaceSize" or "aclnnInplaceCumprodGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor containing the operator computation process, and then call the "aclnnCumprod" or "aclnnInplaceCumprod" API to execute 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:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor input (aclTensor*) Input Data for computing the cumulative product. Supports empty tensor. FLOAT, FLOAT16, BFLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, UINT16, UINT32, UINT64 ND Up to 8 dimensions √ dim (aclScalar*) Input Specifies the dimension along which the cumulative product is computed. For a 2D 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 - - - dtype (aclDataType) Input Data type of input for the specified computation. - If ACL_DT_UNDEFINED, the original data type of input is used for computation.
- If a specific type is specified (it must be within the data types supported by input), input is converted to this type before computation.
- - - - out (aclTensor*) Output Cumulative product result. - When dtype=ACL_DT_UNDEFINED, the data type must be consistent with that of input.
- When dtype is specified, the data type must be consistent with that of dtype.
- The shape of out must be consistent with that of input.
FLOAT, FLOAT16, BFLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, UINT16, UINT32, UINT64 ND - √ workspaceSize (uint64_t*) Output Returns the size of the workspace to be allocated on the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - Return value
aclnnStatus: return status code, 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 input or dim is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type and format of input or dim are not within the supported range. The shape constraint between dim and input is not met. The shape of out is inconsistent with that of input.
aclnnCumprod
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device. workspaceSize Input Workspace size allocated on the Device, obtained by calling the first-phase API aclnnCumprodGetWorkspaceSize. executor Input Op executor that contains the operator computation process. stream Input Stream for executing the task. Return value
aclnnStatus: Return status code, see aclnn return code.
aclnnInplaceCumprodGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor input (aclTensor*) Input/Output Data and result for the cumulative product computation. Empty tensors are not supported. FLOAT, FLOAT16, BFLOAT16, DOUBLE, INT8, INT16, INT32, INT64, UINT8, UINT16, UINT32, UINT64 ND Up to 8 dimensions √ dim (aclScalar*) Input 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 allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor containing the operator computation flow. - - - - - Return value
aclnnStatus: return status code, 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 types and formats of the input and dim parameters are not within the supported range. The shape constraint between dim and input is not met.
aclnnInplaceCumprod
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address requested on the device. workspaceSize Input Size of the workspace requested on the device, obtained by the first-phase API aclnnInplaceCumprodGetWorkspaceSize. executor Input Op executor, which contains the computation flow of the operator. stream Input Stream for executing tasks. Return value
aclnnStatus: return status code, see aclnn return code.
Constraints
- Deterministic computation:
- aclnnCumprod and aclnnInplaceCumprod default to a deterministic implementation.
Example
The following sample code is for reference only. For the compilation and execution procedure, 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 for 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 memory on the device
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 data to the device 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);
// Calculate the strides of the 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 aclCreateTensor 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. For details, see the list of external acl APIs, and set deviceId based on the 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
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);
// Allocate 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 aclnnCumprod.
ret = aclnnCumprod(workspaceAddr, workspaceSize, executor, stream);
// 4.(boilerplate) Synchronize and wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
// 5. Obtain the output value and copy the result from device memory to host memory. Modify the code according to the specific API definition.
PrintOutResult(resultShape, &resultDeviceAddr);
// 3. Call the CANN operator library API.
// Call the first-phase API of aclnnInplaceCumprod.
ret = aclnnInplaceCumprodGetWorkspaceSize(input, axis, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceCumprodGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate 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 the task execution to complete.
ret = aclrtSynchronizeStream(stream);
// 5. Obtain the output value and copy the result from device memory to host memory. Modify the code based on the actual API definition.
PrintOutResult(resultShape, &xDeviceAddr);
// 6. Release aclTensor and aclScalar. Modify the code based on the actual API definition.
aclDestroyTensor(input);
aclDestroyScalar(axis);
// // 7. Release device resources. Modify the code based on the actual API definition.
aclrtFree(xDeviceAddr);
aclrtFree(resultDeviceAddr);
if (workspaceSize > 0)
{
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}