FilesLast commitLast update
14 days ago
3 months ago
2 months ago
22 days ago
22 days ago
1 month ago
22 days ago
1 month ago
15 days ago
README

ReduceStdV2Update

English | 简体中文

Product Support Status

Product Supported
Ascend 950PR&950DT products √
Atlas A3 products √
Atlas A2 products √
Atlas 200I/500 A2 inference products ×
Atlas inference products ×
Atlas training products ×

For the mapping between products and chips, refer to Ascend Product Form Description. Ascend 950PR&950DT products is the target platform of the newly added Ascend C Kernel implementation in this update. The other supported products are covered by the existing TBE binary.

Function Description

  • Operator function: receives the raw input x and the pre-computed mean mean (already broadcast to the shape of x), computes the sum of squared deviations along the specified dimension dim by reduction, and then divides the sum by N - correction to obtain the variance or standard deviation. Bessel correction (correction) and keepdim are supported. ReduceStdV2Update is an internal operator in CANN l0op. It is internally called by aclnnVar/aclnnVarCorrection/aclnnVarMean on the non-regbase path, so that aclnnVarMean can reuse the mean result to avoid computing the mean repeatedly.

  • Computation formula:

    diff=x−μ\text{diff} = x - \mu

    sq_sum=∑i∈Ddiffi2\text{sq\_sum} = \sum_{i \in D} \text{diff}_{i}^{2}

    var=sq_sumN−correction\text{var} = \frac{\text{sq\_sum}}{N - \text{correction}}

    output={varif if_std=truevarif if_std=false\text{output} = \begin{cases} \sqrt{\text{var}} & \text{if } \text{if\_std} = \text{true} \\ \text{var} & \text{if } \text{if\_std} = \text{false} \end{cases}

    Here, DD is the set of reduction dimensions, NN is the number of elements in the reduction dimensions, and μ\mu is the mean passed in externally. correction=0 indicates a biased estimate (denominator N), and correction=1 indicates an unbiased estimate (denominator N-1).

  • Differences between APIs: this operator provides two equivalent L0 APIs. ReduceStdV2Update uses the bool unbiased parameter (true=unbiased N-1, false=biased N) and is called by aclnnVar. ReduceStdV2UpdateCorrection uses the int64_t correction parameter (0=biased, 1=unbiased) and is called by aclnnVarCorrection. The two APIs are mathematically equivalent and differ only in the parameter expression. The L0 API always outputs the variance (if_std=false). In GE IR graph mode, the if_std attribute can be used to control whether to output the variance or the standard deviation.

Parameter Description

Parameter Name Input/Output/Attribute Description Data Type Data Format Non-contiguous Tensor
x (self) Input Raw input data, corresponding to x in the formula. 0-8 dimensional ND. FLOAT, FLOAT16, BFLOAT16 ND √
mean Input Pre-computed mean, corresponding to μ in the formula. It must have been broadcast to the shape of self through Expand. Its dtype is the same as that of self. FLOAT, FLOAT16, BFLOAT16 ND √
dim Attribute List of reduction dimensions, corresponding to D in the formula. Negative indexes are supported. If dim is empty, all dimensions are reduced. INT64 (ListInt) - -
if_std Attribute false=output variance, true=output standard deviation (square root). The default value is false. The value is fixed to false for the aclnn API. BOOL - -
unbiased Attribute Specifies whether to use Bessel correction. true=unbiased (N-1), false=biased (N). The default value is true. BOOL - -
keepdim Attribute Specifies whether to retain the reduction dimensions. true=set the dimension to 1, false=remove the dimension. The default value is false. BOOL - -
correction Attribute Bessel correction factor. 0=biased (N), 1=unbiased (N-1). The default value is 1. INT64 - -
output (output_var) Output Variance or standard deviation, corresponding to output in the formula. Its dtype is the same as that of x, and its shape is the shape of x after reduction along dim. FLOAT, FLOAT16, BFLOAT16 ND ×

Constraint Description

  • Input data type restriction: x and mean support only FLOAT, FLOAT16, and BFLOAT16, and their data types must be the same. DOUBLE, complex, and integer types are not supported.
  • shape restriction: mean must have been broadcast to the shape of x through Expand. The output shape is the shape of x after reduction along dim (the dimension is set to 1 if keepdim=true, and is removed if keepdim=false).
  • correction/unbiased restriction: only two semantics are supported, that is, correction=0 (biased) and correction=1 (unbiased). correction>1 is handled by the upper-layer aclnnVarCorrection.
  • Edge cases: scenarios with an empty tensor, or a single element and correction≥1, are usually handled in advance by the upper-layer aclnnVar/aclnnVarCorrection. The kernel still performs defensive processing. In the EMPTY_A path where the output is empty, no operation is performed. In the EMPTY_R path where the reduction axis is empty but the output is not empty, NaN is returned. In the normal path, when N <= correction, the same semantics are maintained through cof=NaN.
  • Precision restrictions (the 950 implementation is strictly aligned with the canndev prototype):
    • FP16/BF16 inputs are always promoted to FP32 for accumulation (fixed promotion, without GetPromoteType). FP32 inputs are computed directly. The results are converted back to the original dtype.
    • The conversion back to BF16 output uses the round mode (corresponding to AscendC Cast round), retaining the rounding semantics of BF16.
    • When if_std=true, high_precision vsqrt is used for the square root operation.
    • The output dtype is the same as that of the input x, and no type promotion is performed at the API layer.
  • Determinism: this operator is deterministic by default.

Invocation Description

Invocation Method Invocation Sample Description
GE graph-mode invocation test_geir_reduce_std_v2_update Invokes the ReduceStdV2Update operator by building a graph through the operator IR. The if_std attribute is supported to control whether to output the variance or the standard deviation.

Note: this operator is an internal sub-operator of l0op and does not expose an independent aclnn API externally. The L0 API always outputs the variance with if_std=false. In GE IR graph mode, the if_std attribute can be used to control whether to output the variance or the standard deviation. The upper-layer aclnnVar/aclnnVarCorrection/aclnnVarMean internally call this operator on the non-regbase path.