昇腾社区首页
中文
注册

aclnnBatchNormGatherStatsWithCounts

支持的产品型号

  • Atlas 训练系列产品。
  • Atlas A2训练系列产品/Atlas 800I A2推理产品。

接口原型

每个算子分为undefined,必须先调用“aclnnBatchNormGatherStatsWithCountsGetWorkspaceSize”接口获取入参并根据流程计算所需workspace大小,再调用“aclnnBatchNormGatherStatsWithCounts”接口执行计算。

  • 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)

功能描述

  • 算子功能: 收集所有device的均值和方差,更新全局的均值和方差。BatchNorm的性能和BatchSize相关,BatchSize越大,BatchNorm的统计量也会越准。然而像检测这样的任务,占用显存较高,一张显卡往往只拿较少的图片,比如两张来训练,这就导致BN的表现变差。一个解决方式就是SyncBatchNorm,所有卡共享同一个BatchNorm,得到全局的统计量。 batch_norm_gather_stats_with_counts计算时,依赖batch_norm_stats计算单卡数据的均值和标准差的倒数。

  • 计算公式:

y=(xE[x])Var(x)+εγ+βy = \frac{(x-E[x])}{\sqrt{Var(x)+ ε}} * γ + β

runningMean和runningVar更新公式如下:

runningMean=runningMean(1momentum)+E[x]runningMeanrunningMean=runningMean*(1-momentum) + E[x]*runningMean runningVar=runningVar(1momentum)+E[x]runningVarrunningVar=runningVar*(1-momentum) + E[x]*runningVar

aclnnBatchNormGatherStatsWithCountsGetWorkspaceSize

  • 参数说明:

    • input(aclTensor*, 计算输入): Device侧的aclTensor,shape支持2-8维,其中第2维固定为channel轴支持undefinedundefined支持ND。
      • Atlas 训练系列产品:数据类型支持FLOAT、FLOAT16。
      • Atlas A2训练系列产品/Atlas 800I A2推理产品:数据类型支持FLOAT、FLOAT16、BFLOAT16。
    • mean(aclTensor*, 计算输入): 输入数据均值,Device侧的aclTensor,shape支持2维,第一维的size需要与invstd和counts一致,第二维对应的size需要与input的Channel轴size一致,支持undefinedundefined支持ND。
      • Atlas 训练系列产品:数据类型支持FLOAT、FLOAT16。
      • Atlas A2训练系列产品/Atlas 800I A2推理产品:数据类型支持FLOAT、FLOAT16、BFLOAT16。
    • invstd(aclTensor*, 计算输入): 输入数据标准差的倒数,Device侧的aclTensor,shape支持2维,第一维的size需要与mean和counts一致,第二维对应的size需要与input的Channel轴size一致,支持undefinedundefined支持ND。支持元素值均大于0,小于等于0时,不做保证。
      • Atlas 训练系列产品:数据类型支持FLOAT、FLOAT16。
      • Atlas A2训练系列产品/Atlas 800I A2推理产品:数据类型支持FLOAT、FLOAT16、BFLOAT16。
    • runningMean(aclTensor*, 计算输入): 训练时数据的均值,Device侧的aclTensor,当runningMean非空指针时,shape支持1维,size需要与input的Channel轴size一致,支持undefinedundefined支持ND。支持元素值均大于0,小于等于0时,不做保证。
      • Atlas 训练系列产品:数据类型支持FLOAT、FLOAT16。
      • Atlas A2训练系列产品/Atlas 800I A2推理产品:数据类型支持FLOAT、FLOAT16、BFLOAT16。
    • runningVar(aclTensor*, 计算输入): 训练时数据的方差,Device侧的aclTensor,当runningMean非空指针时,shape支持1维,size需要与input的Channel轴size一致,支持undefinedundefined支持ND。支持元素值均大于0,小于等于0时,不做保证。
      • Atlas 训练系列产品:数据类型支持FLOAT、FLOAT16。
      • Atlas A2训练系列产品/Atlas 800I A2推理产品:数据类型支持FLOAT、FLOAT16、BFLOAT16。
    • momentum(double, 计算输入): runningMean和runningVar的指数平滑参数。
    • eps(double, 计算输入): 用于防止产生除0的偏移。
    • counts(aclTensor*, 计算输入): 输入数据元素的个数,Device侧的Tensor,shape支持一维,第一维的size需要与mean和invstd的第一维的size一致,支持undefinedundefined支持ND。支持元素值均为正整数,其余场景不做保证。
      • Atlas 训练系列产品:数据类型支持FLOAT、FLOAT16。
      • Atlas A2训练系列产品/Atlas 800I A2推理产品:数据类型支持FLOAT、FLOAT16、BFLOAT16。
    • meanAll(aclTensor*, 计算输出): 所有卡上数据的均值,Device侧的aclTensor,shape支持1维,shape需要与input的channel轴一致,支持undefinedundefined与输入一致。
      • Atlas 训练系列产品:数据类型支持FLOAT、FLOAT16。
      • Atlas A2训练系列产品/Atlas 800I A2推理产品:数据类型支持FLOAT、FLOAT16、BFLOAT16。
    • invstdAll(aclTensor*, 计算输出): 所有卡上数据的标准差的倒数,Device侧的aclTensor,shape支持1维,shape需要与input的channel轴一致,支持undefinedundefined与输入一致。
      • Atlas 训练系列产品:数据类型支持FLOAT、FLOAT16。
      • Atlas A2训练系列产品/Atlas 800I A2推理产品:数据类型支持FLOAT、FLOAT16、BFLOAT16。
    • workspaceSize(uint64_t*, 出参): 返回需要在Device侧申请的workspace大小。
    • executor(aclOpExecutor**, 出参): 返回op执行器,包含了算子计算流程。
  • 返回值:

    aclnnStatus: 返回状态码,具体参见undefined

    [object Object]

aclnnBatchNormGatherStatsWithCounts

  • 参数说明:

    • workspace(void *, 入参): 在Device侧申请的workspace内存地址。
    • workspaceSize(uint64_t, 入参): 在Device侧申请的workspace大小,由第一段接口aclnnBatchNormGatherStatsWithCountsGetWorkspaceSize获取。
    • executor(aclOpExecutor *, 入参): op执行器,包含了算子计算流程。
    • stream(const aclrtStream, 入参): 指定执行任务的 AscendCL Stream流。
  • 返回值:

    aclnnStatus: 返回状态码,具体参见undefined

约束与限制

调用示例

示例代码如下,仅供参考,具体编译和执行过程请参考undefined

[object Object]