aclnnBatchNormGatherStatsWithCounts

📄 View Source Code

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:

    Collects the mean and variance from all devices, and updates the global mean and the inverse of the standard deviation. The performance of BatchNorm is related to BatchSize. A larger BatchSize yields more accurate BatchNorm statistics. However, for tasks such as detection, which consume a large amount of video memory, a single GPU often uses only a small number of images, for example two, for training. This degrades the performance of BatchNorm. One solution is SyncBatchNorm, where all devices share the same BatchNorm to obtain global statistics.

    The computation of aclnnBatchNormGatherStatsWithCounts depends on aclnnBatchNormStats to calculate the mean and the inverse of the standard deviation for single-device data.

  • Formula:

    y=(x−E[x])Var(x)+eps∗γ+βy = \frac{(x-E[x])}{\sqrt{Var(x)+ eps}} * γ + β

    where the update formulas for runningMean and runningVar are as follows:

    runningMean=runningMean∗(1−momentum)+E[x]∗momentumrunningMean=runningMean*(1-momentum) + E[x]*momentum runningVar=runningVar∗(1−momentum)+Var(x)∗momentumrunningVar=runningVar*(1-momentum) + Var(x)*momentum

Function Prototype

Each operator uses a two-phase API. First, call "aclnnBatchNormGatherStatsWithCountsGetWorkspaceSize" to obtain the input parameters and calculate the required workspace size according to the process, and then call "aclnnBatchNormGatherStatsWithCounts" to perform the computation.

aclnnStatus aclnnBatchNormGatherStatsWithCountsGetWorkspaceSize(
  const aclTensor* input,
  const aclTensor* mean,
  const aclTensor* invstd,
  aclTensor*       runningMean,
  aclTensor*       runningVar,
  double           momentum,
  double           eps,
  const aclTensor* counts,
  aclTensor*       meanAll,
  aclTensor*       invstdAll,
  uint64_t*        workspaceSize,
  aclOpExecutor**  executor)
aclnnStatus aclnnBatchNormGatherStatsWithCounts(
  void                *workspace,
  uint64_t             workspaceSize,
  aclOpExecutor       *executor,
  const aclrtStream    stream)

aclnnBatchNormGatherStatsWithCounts

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    input (aclTensor*) Input Indicates the sample values used for statistics computation, which is `x` in the corresponding formula.
    • Does not support null Tensor.
    • The second dimension is fixed to the channel axis.
    FLOAT32, FLOAT16, BFLOAT16 ND 2-8 √
    mean (aclTensor*) Input Indicates the input data mean, which is `E(x)` in the corresponding formula.
    • Does not support null Tensor.
    • The size of the first dimension needs to be consistent with `invstd` and `counts`, and the size of the second dimension needs to be consistent with the Channel axis size of `input`.
    FLOAT32, FLOAT16, BFLOAT16 ND 2 √
    invstd (aclTensor*) Input Indicates the reciprocal of the standard deviation of the input data, corresponding to the reciprocal of the square root of `Var(x)+ eps` in the corresponding formula.
    • Does not support null Tensor.
    • All element values must be greater than 0. If an element value is less than or equal to 0, precision is not ensured.
    • The size of the first dimension needs to be consistent with `mean` and `counts`, and the size of the second dimension needs to be consistent with the Channel axis size of `input`.
    FLOAT32, FLOAT16, BFLOAT16 ND 2 √
    runningMean (aclTensor*) Input Optional parameter. Indicates the mean used for tracking the entire training process, corresponding to `runningMean` in the formula.
    • Does not support null tensors.
    • Element values must be greater than 0. If values are less than or equal to 0, precision is not guaranteed.
    • When `runningMean` is not a null pointer, a 1-dimensional shape is supported, and the size needs to be consistent with the size of the Channel axis in `input`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1 √
    runningVar (aclTensor*) Input Optional parameter, indicates the variance for tracking the entire training process, corresponding to `runningVar` in the formula.
    • Does not support null tensors.
    • Element values must all be greater than 0. Precision is not guaranteed when they are less than or equal to 0.
    • When `runningVar` is a non-null pointer, 1-D shapes are supported and the size needs to be consistent with the Channel axis size of `input`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1 √
    momentum (double) Input Indicates the exponential smoothing parameter for runningMean and runningVar, corresponding to `momentum` in the formula. - - - - -
    eps (double) Input Indicates the value added to the variance to avoid division by zero. Corresponds to `eps` in the formula. - - - - -
    counts (aclTensor*) Input Indicates the number of elements in the input data.
    • Does not support null tensors.
    • Supports only positive integer element values. Other use cases are not guaranteed.
    • The first dimension size needs to be consistent with the first dimension size of mean and invstd.
    FLOAT32, FLOAT16, BFLOAT16 ND 1 √
    meanAll (aclTensor*) Output Indicates the mean of data on all devices, corresponding to `E(x)` in the formula.
    • Does not support null tensors.
    • The shape needs to be consistent with the channel axis in input parameter `input`.
    • The data format needs to be consistent with the data format in input parameter `input`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1 √
    invstdAll (aclTensor*) Output Indicates the reciprocal of the standard deviation of data on all devices, that is, the reciprocal of the square root of `Var(x)+ eps` in the corresponding formula.
    • Does not support null tensors.
    • The shape must be consistent with the channel axis of input parameter `input`.
    • The data format must be consistent with the data format of input parameter `input`.
    FLOAT32, FLOAT16, BFLOAT16 ND 1 √
    workspaceSize (uint64_t*) Output Returns the workspace size that users need to allocate on the Device side. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor containing the operator computation process. - - - - -
    • Atlas training products, Atlas inference products: The data types of parameters input, mean, invstd, runningMean, runningVar, counts, meanAll, and invstdAll do 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 input, mean, invstd, counts, meanAll, or invstdAll is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of input, meanAll, invstdAll, mean, invstd, runningMean, runningVar, or counts is out of the supported range.
    The dimension of input is greater than 8.
    The dimension of input is less than 2.
    The dimension of mean or invstd is not 2.
    The size of counts, mean, and invstd in the first dimension is not consistent.
    The size of mean or invstd in the second dimension mismatches the channel axis of input.
    When runningMean is not null, the size of runningMean mismatches the channel axis of input.
    When runningVar is not null, the size of runningVar mismatches the channel axis of input.
    The dimension of counts is not 1-D.
    The shape of meanAll or invstdAll mismatches the channel axis of input.

aclnnBatchNormGatherStatsWithCounts

  • Parameters

    Parameter Input/Output Description
    workspace Input Workspace memory address requested on the Device side.
    workspaceSize Input Workspace size requested on the Device side, obtained by calling the first-phase API aclnnBatchNormGatherStatsWithCountsGetWorkspaceSize.
    executor Input Executor of the op, which contains the operator computation flow.
    stream Input Stream for executing the task.
  • Return Value

    aclnnStatus: return status code. For details, see aclnn Error Codes.

Constraints

  • Deterministic computation:
    • aclnnBatchNormGatherStatsWithCounts 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_batch_norm_gather_stats_with_counts.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 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 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);
    // Handle check as required.
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

    // 2. Construct input and output. Customize the construction according to the API.
    std::vector<int64_t> inputShape = {2, 4, 2};
    std::vector<int64_t> meanShape = {4, 4};
    std::vector<int64_t> rMeanShape = {4};
    std::vector<int64_t> rVarShape = {4};
    std::vector<int64_t> countsShape = {4};
    std::vector<int64_t> invstdShape = {4, 4};
    std::vector<int64_t> meanAllShape = {4};
    std::vector<int64_t> invstdAllShape = {4};
    void* inputDeviceAddr = nullptr;
    void* meanDeviceAddr = nullptr;
    void* rMeanDeviceAddr = nullptr;
    void* rVarDeviceAddr = nullptr;
    void* countsDeviceAddr = nullptr;
    void* invstdDeviceAddr = nullptr;
    void* meanAllDeviceAddr = nullptr;
    void* invstdAllDeviceAddr = nullptr;
    aclTensor* input = nullptr;
    aclTensor* mean = nullptr;
    aclTensor* rMean = nullptr;
    aclTensor* rVar = nullptr;
    aclTensor* counts = nullptr;
    aclTensor* invstd = nullptr;
    aclTensor* meanAll = nullptr;
    aclTensor* invstdAll = nullptr;
    std::vector<float> inputHostData = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
    std::vector<float> meanHostData = {0, 1, 2, 3, 4, 5, 6, 7};
    std::vector<float> rMeanHostData = {1, 2, 3, 4};
    std::vector<float> rVarHostData = {5, 6, 7, 8};
    std::vector<float> countsHostData = {1, 2, 3, 4};
    std::vector<float> invstdHostData = {1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4};
    std::vector<float> meanAllHostData = {4, 0};
    std::vector<float> invstdAllHostData = {4, 0};
    double momentum = 1e-2;
    double eps = 1e-4;

    // Create the input aclTensor.
    ret = CreateAclTensor(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_FLOAT, &input);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the mean aclTensor.
    ret = CreateAclTensor(meanHostData, meanShape, &meanDeviceAddr, aclDataType::ACL_FLOAT, &mean);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create rMean aclTensor.
    ret = CreateAclTensor(rMeanHostData, rMeanShape, &rMeanDeviceAddr, aclDataType::ACL_FLOAT, &rMean);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create rVar aclTensor.
    ret = CreateAclTensor(rVarHostData, rVarShape, &rVarDeviceAddr, aclDataType::ACL_FLOAT, &rVar);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create counts aclTensor.
    ret = CreateAclTensor(countsHostData, countsShape, &countsDeviceAddr, aclDataType::ACL_FLOAT, &counts);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create invstd aclTensor.
    ret = CreateAclTensor(invstdHostData, invstdShape, &invstdDeviceAddr, aclDataType::ACL_FLOAT, &invstd);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create meanAll aclTensor.
    ret = CreateAclTensor(meanAllHostData, meanAllShape, &meanAllDeviceAddr, aclDataType::ACL_FLOAT, &meanAll);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create invstdAll aclTensor.
    ret = CreateAclTensor(invstdAllHostData, invstdAllShape, &invstdAllDeviceAddr, aclDataType::ACL_FLOAT, &invstdAll);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;

    // Example of calling the aclnnBatchNormGatherStatsWithCounts API.
    // 3. Call the CANN operator library API, and change it to the specific API name.
    // Call the first-phase API of aclnnBatchNormGatherStatsWithCounts.
    ret = aclnnBatchNormGatherStatsWithCountsGetWorkspaceSize(
        input, mean, invstd, rMean, rVar, momentum, eps, counts, meanAll, invstdAll, &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS,
              LOG_PRINT("aclnnBatchNormGatherStatsWithCountsGetWorkspaceSize 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 aclnnBatchNormGatherStatsWithCounts.
    ret = aclnnBatchNormGatherStatsWithCounts(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnBatchNormGatherStatsWithCounts failed. ERROR: %d\n", ret);
              return ret);

    // 4. (Boilerplate) Synchronize and wait for the task execution to finish.
    ret = aclrtSynchronizeStream(stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

    // 5. Obtain the output values and copy the results from Device memory to the Host side. Modify based on the specific API definition.
    auto size = GetShapeSize(meanAllShape);
    std::vector<float> resultData(size, 0);
    ret = aclrtMemcpy(
        resultData.data(), resultData.size() * sizeof(resultData[0]), meanAllDeviceAddr, 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 based on the specific API definition.
    aclDestroyTensor(input);
    aclDestroyTensor(mean);
    aclDestroyTensor(rMean);
    aclDestroyTensor(rVar);
    aclDestroyTensor(counts);
    aclDestroyTensor(invstd);
    aclDestroyTensor(meanAll);
    aclDestroyTensor(invstdAll);

    // 7. Release Device resources. Modify based on the specific API definition.
    aclrtFree(inputDeviceAddr);
    aclrtFree(meanDeviceAddr);
    aclrtFree(rMeanDeviceAddr);
    aclrtFree(rVarDeviceAddr);
    aclrtFree(countsDeviceAddr);
    aclrtFree(invstdDeviceAddr);
    aclrtFree(meanAllDeviceAddr);
    aclrtFree(invstdAllDeviceAddr);
    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}