The embodiment of the present application provides an optimization method, device, storage medium and program product for attention mechanism calculation, which relates to the field of
artificial intelligence technology. The method includes: executing a forward recalculation operator based on the query block and the key block to obtain a forward normalization result. Executing a first
matrix multiplication operator based on the output gradient block and the forward normalization result to obtain a value intermediate result, and simultaneously executing a dot product summation operator based on the forward output block and the output gradient block in parallel to obtain an intermediate
tensor. Executing a first type of
transformation operator on the value intermediate result to obtain a value gradient block; and simultaneously executing a gradient
backpropagation operator based on the output gradient block, the value block, the intermediate
tensor and the forward normalization result in parallel to obtain a reverse normalization result. Finally, based on the reverse normalization result, the key block and the query block, the query gradient block and the key gradient block are obtained, thereby achieving effective pipeline
processing of different operators, thereby improving the overall performance of the attention mechanism calculation.