FlashAttention Operator Performance Optimization

Case Study

The FlashAttentionScoreGrad operator in this case is used to compute the reverse output of attention in the training scenario, that is, backpropagation of the FlashAttentionScore operator.

The forward propagation formula for attention is as follows:

For convenience, the formula can be represented using variables S and P:

Then the attention backpropagation formula is as follows:

The following figure shows the computation process.

Figure 1 Operator computation process

Based on the implementation of FlashAttention backpropagation, the overall calculation process is as follows. It is provided as additional information for users who are interested in the algorithm of this operator.

  1. Recalculate the softmax result p of the FlashAttention process. The calculation result is stored in the UB.

  2. Calculate dp. This includes matmul calculation and dropout calculation. In matmul calculation, the left matrix is dy, and the right matrix is the transposed value.

  3. Calculate ds. In this calculation, the input parameter of FlashSoftmaxGrad calculation is dy, and the forward output is attention_in. The result is subtracted from dp, and then multiplied by p to obtain ds.

  4. Calculate dq. In this calculation, matmul computation is performed on the ds result and key, and the result is multiplied by scale to obtain dq.

  5. Calculate dk. In this calculation, matmul calculation is performed on the transposed ds result and query, and the result is multiplied by scale to obtain dk.

  6. Compute dv. In this computation, drop computation is performed on the result of p, and then matmul computation is performed on the transposed result and dy.

In this case, verification is conducted based on the Atlas A2 training product / Atlas A2 inference product , with two scenarios as an example. In the first causal scenario, the input dimension information is B=1, N1=12, N2=12, S1=6144, S2=6144, D=128. The causal scenario means the shape of atten_mask is lower triangle, as shown in Figure 2. In the second scenario, the input dimension information is B=24, N1=5, N2=5, S1=9216, S2=9216, D=64, without atten_mask and drop_mask input. The optimization methods include tiling size adjustment, inter-core load balancing, CV pipeline parallelism, MTE2 pipeline optimization, and FixPipe pipeline optimization.

Figure 2 atten_mask shape in the causal scenario

Obtaining Profile Data

The pipeline analysis tools include the CAModel and profiling tools. The first is to analyze the ratio of each pipeline from the profile data generated by the profiling tool. The second is to analyze the parallelism of each pipeline from the dotting chart generated by the CAModel tool.

Analyzing Main Bottlenecks

The performance bottlenecks can be determined by observing and analyzing the pipeline chart and profile data. Different bottlenecks may occur in different phases of the optimization process. Therefore, continuous optimization is required to achieve optimal performance.

Developing Optimization Solutions

Verifying Optimization Benefits

Summary

In the fusion operator scenario, you can refer to these optimizations.