aclnnDropout
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 : Supported
Function
Description: During training, randomly sets elements in the input to zero with probability p, and scales the output by a factor of 1/(1-p).
Formula:
Function Prototype
Each operator uses a two-phase API. Call the "aclnnDropoutGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor that contains the operator computation flow, and then call the "aclnnDropout" API to execute the computation.
aclnnStatus aclnnDropoutGetWorkspaceSize(
const aclTensor* input,
double p,
bool train,
int64_t seed,
int64_t offset,
aclTensor* out,
aclTensor* maskOut,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnDropout(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnDropoutGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor input (aclTensor*) Input Represents the tensor to be processed, which is input in the formula. Does not support empty tensors. BFLOAT16, FLOAT16, FLOAT32 ND 1-8 √ p (double) Input Probability of an element being zeroed, which is p in the formula. Value range: [0, 1]. - - - - train (bool) Input Whether it is a training use case. - - - - - seed(int64_t) Input Random number seed. Affects the generated random number sequence. - - - - - offset (int64_t) Input Random number offset, which affects the position in the generated random number sequence. - - - - - out (aclTensor*) Output Output tensor, out in the formula. - Does not support empty tensors.
- The data type must be a data type that input can be converted to, and the shape must be the same as that of input.
BFLOAT16, FLOAT16, FLOAT32 ND 1-8 √ maskOut (aclTensor*) Output Mask data of the bit type, stored using the UINT8 type. - Does not support empty tensors.
- When p=0, p=1, or train=false, the input maskOut is not processed; in other cases, a mask is generated using the given p as the zeroing probability.
- The shape must be (align(number of elements in input, 128)/8), indicating that the number of bits must be aligned to 128. align rounds up the number of elements in input to a multiple of 128. For example, align(1111, 128) returns 1152.
UINT8 ND 1 √ workspaceSize (uint64_t*) Output Returns the workspace size that needs to be applied on the side of the Device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, including the operator computation flow. - - - - - Atlas training products : The data type does not support BFLOAT16.
Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Error Code Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The input, out, and maskOut pointers are null. ACLNN_ERR_PARAM_INVALID 161002 The data types of input, out, and maskOut are not within the supported range. The value of p is not in the range of 0 to 1. The dimensions of input exceed 8. The shapes of input and out are inconsistent. The shape of maskOut does not meet the requirements.
aclnnDropout
Parameters:
Parameter Input/Output Description workspace Input Address of the workspace memory applied on the Device side. workspaceSize Input Size of the workspace memory applied on the Device side, obtained by calling the first-phase API aclnnDropoutGetWorkspaceSize. executor Input op executor that contains the computation flow of the operator. stream Input Stream for task execution. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- deterministic computation:
- aclnnDropout defaults to a deterministic implementation.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_dropout.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;
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate, resource 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 side.
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 the memory on the device side.
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 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;
}
int main() {
// 1. (Boilerplate) Initialize device/stream. For details, see the acl API manual.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output as required by the API definition.
std::vector<int64_t> selfShape = {4, 2};
std::vector<int64_t> outShape = {4, 2};
std::vector<int64_t> maskOutShape = {16};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
void* maskOutDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
aclTensor* maskOut = nullptr;
std::vector<float> selfHostData = {0, 1, 2, 3, 4, 5, 6, 7};
std::vector<float> outHostData(8, 0);
std::vector<uint8_t> maskOutHostData(16, 0);
double p = 0.5;
bool train = true;
int64_t seed = 1234;
int64_t offset = 0;
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the maskOut aclTensor.
ret = CreateAclTensor(maskOutHostData, maskOutShape, &maskOutDeviceAddr, aclDataType::ACL_UINT8, &maskOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API, and change it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnDropout.
ret = aclnnDropoutGetWorkspaceSize(self, p, train, seed, offset, out, maskOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDropoutGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
void* workspaceAddr = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
// Call the second-phase API of aclnnDropout.
ret = aclnnDropout(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnDropout failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronously wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value, and copy the result from device memory to the host side. Modify it according to the specific API definition.
auto size = GetShapeSize(outShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
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 ret);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
}
// 6. Release aclTensor and aclScalar. Modify them according to the specific API definition.
aclDestroyTensor(self);
aclDestroyTensor(out);
aclDestroyTensor(maskOut);
// 7. Release device resources. Modify them according to the specific API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(outDeviceAddr);
aclrtFree(maskOutDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}