aclnnRReluWithNoise&aclnnInplaceRReluWithNoise
Applicable Products
Ascend 950PR&950DT products : 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: Implements the RReLU (Randomized Leaky Rectified Linear Unit) activation function with noise. When the input is less than or equal to 0, the slope is a; when the input is greater than 0, the slope is 1.
Formula:
where a is a random variable that follows a uniform distribution (lower, upper). In training mode (training == true), the noise is calculated as follows:
Function Prototype
- aclnnRReluWithNoise and aclnnInplaceRReluWithNoise implement the same functionality. The differences in usage are as follows. Select the appropriate API based on your actual scenario.
- aclnnRReluWithNoise: Requires creating a new output tensor object to store the computation result.
- aclnnInplaceRReluWithNoise: Does not require creating a new output tensor object and directly stores the computation result in the memory of the input tensor.
- Each operator uses a two-phase API. You must first call "aclnnRReluWithNoiseGetWorkspaceSize" or "aclnnInplaceRReluWithNoiseGetWorkspaceSize" to obtain input parameters and calculate the required workspace size based on the process, and then call "aclnnRReluWithNoise" or "aclnnInplaceRReluWithNoise" to perform the calculation.
aclnnStatus aclnnRReluWithNoiseGetWorkspaceSize(
const aclTensor *self,
const aclTensor *noise,
const aclScalar *lower,
const aclScalar *upper,
bool training,
int64_t seed,
int64_t offset,
aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnRReluWithNoise(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
const aclrtStream stream)aclnnStatus aclnnInplaceRReluWithNoiseGetWorkspaceSize(
const aclTensor* self,
const aclTensor* noise,
const aclScalar* lower,
const aclScalar* upper,
bool training,
int64_t seed,
int64_t offset,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnInplaceRReluWithNoise(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
const aclrtStream stream)aclnnRReluWithNoiseGetWorkspaceSize
Parameters:
Atlas training products : The supported data types are FLOAT16 and FLOAT.
Return value
aclnnStatus: return status code.For details, see aclnn return 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 self, noise, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of self or noise is not within the supported range. The size of self is greater than the size of noise. The data types or data formats of self, noise, and out are inconsistent. The shapes of self and out are inconsistent. The shape dimensions of self or noise exceed 25.
aclnnRReluWithNoise
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address applied on the Device side. workspaceSize Input Workspace size applied on the Device side, obtained by the first-phase API aclnnRReluWithNoiseGetWorkspaceSize. executor Input Op executor, includes the calculation process of the operator. stream Input Stream for task execution. Return value
aclnnStatus: return status code.For details, see aclnn return code.
aclnnInplaceRReluWithNoiseGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor self Input self in the formula. The number of supported dimensions of shape does not exceed 25. BFLOAT16, FLOAT16, FLOAT ND 0-25 √ noise Input noise_i in the formula. - Size needs to be no smaller than that of self (the shape is recommended to be the same as that of self).
- The data type needs to remain consistent with that of self.
- The data format needs to remain consistent with that of self.
- The number of dimensions supported by the shape does not exceed 25.
BFLOAT16, FLOAT16, FLOAT ND 0-25 √ lower Input The lower bound of the uniform distribution U. The data type needs to be consistent with that of self. BFLOAT16, FLOAT16, FLOAT - - - upper Input upper in uniform distribution U. Data type needs to be consistent with self. BFLOAT16, FLOAT16, FLOAT - - - training Input Distinguishes between training and inference. - BOOL - - - seed Input Seed of the random number generator, which affects the generated random number sequence. - INT64 - - - offset Input Offset of the random number generator, which affects the position of the generated random number sequence. After the offset is set, the generated random number sequence starts from the specified position. INT64 - - - workspaceSize Output Returns the workspace size that needs to be applied on the Device side. - - - - - executor Output Returns the op executor, which includes the calculation flow of the operator. - - - - - Atlas training products : The supported data types are FLOAT16 and FLOAT.
Return value
aclnnStatus: return status code.For details, see aclnn return code.
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 self or noise is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of self or noise is out of the supported range. The size of self is greater than the size of noise. The data type or data format of self is inconsistent with that of noise. The dimensions of the shape of self or noise exceed 25.
aclnnInplaceRReluWithNoise
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address applied on the Device side. workspaceSize Input Workspace size applied on the Device side, obtained by the first-phase API aclnnInplaceRReluWithNoiseGetWorkspaceSize. executor Input Op executor that includes the operator calculation process. stream Input Stream on which the task is executed. Return value
aclnnStatus: return status code. See specifically aclnn return codes.
Constraints
- Deterministic computation:
- aclnnRReluWithNoise&aclnnInplaceRReluWithNoise default to a deterministic implementation.
Example
The sample code is as follows for reference only. For the specific compilation and execution process, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_rrelu_with_noise.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 shape_size = 1;
for (auto i: shape) {
shape_size *= i;
}
return shape_size;
}
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 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);
// Calculate 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 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;
}
int main() {
// 1. (Boilerplate) Initialize the 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);
// Handle the check as needed.
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output. Perform custom construction according to the API.
std::vector<int64_t> selfShape = {2, 2};
std::vector<int64_t> outShape = {2, 2};
std::vector<int64_t> noiseShape = {2, 2};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
void* noiseDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
aclTensor* noise = nullptr;
aclScalar* lower = nullptr;
aclScalar* upper = nullptr;
std::vector<float> selfHostData = {1, 2, 3, 4};
std::vector<float> outHostData = {0, 0, 0, 0};
std::vector<float> noiseHostData = {4, 3, 2, 1};
float lowerValue = 0.1f;
float upperValue = 0.3f;
// 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 noise aclTensor.
ret = CreateAclTensor(noiseHostData, noiseShape, &noiseDeviceAddr, aclDataType::ACL_FLOAT, &noise);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the lower aclScalar.
lower = aclCreateScalar(&lowerValue, aclDataType::ACL_FLOAT);
CHECK_RET(lower != nullptr, return ret);
// Create the upper aclScalar.
upper = aclCreateScalar(&upperValue, aclDataType::ACL_FLOAT);
CHECK_RET(upper != nullptr, return ret);
bool training = false;
int64_t seed = 0;
int64_t offset = 0;
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Example of calling aclnnRReluWithNoise
// 3. Call the first-phase API of aclnnRReluWithNoise.
ret = aclnnRReluWithNoiseGetWorkspaceSize(self, noise, lower, upper, training, seed, offset,
out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRReluWithNoiseGetWorkspaceSize 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 aclnnRReluWithNoise.
ret = aclnnRReluWithNoise(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRReluWithNoise failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) 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 the device memory to the host memory. Modify it based on 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]);
}
// Example of calling aclnnInplaceRReluWithNoise
// step3. Call the first-phase API of aclnnInplaceRReluWithNoise
ret = aclnnInplaceRReluWithNoiseGetWorkspaceSize(self, noise, lower, upper, training, seed, offset,
&workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceRReluWithNoiseGetWorkspaceSize 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("allocate workspace failed. ERROR: %d\n", ret); return ret);
}
// Call the second-phase API of aclnnInplaceRReluWithNoise
ret = aclnnInplaceRReluWithNoise(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceRReluWithNoise failed. ERROR: %d\n", ret); return ret);
// step4. (boilerplate) Synchronize and wait for the task to complete
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// step5. Obtain the output value and copy the result from device memory to host memory. Modify the code according to the API definition.
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. Modify the code according to the API definition.
aclDestroyTensor(self);
aclDestroyTensor(out);
aclDestroyTensor(noise);
aclDestroyScalar(lower);
aclDestroyScalar(upper);
// 7. Release device resources. Modify the code according to the parameters of the API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(outDeviceAddr);
aclrtFree(noiseDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}