The embodiment of the invention provides an attention mechanism calculation optimization method and device, a storage medium and a program product, and relates to the technical field of
artificial intelligence, and the method comprises the steps: executing a forward re-calculation operator based on a query block and a key block, and obtaining 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 executing a point multiplication summation operator in parallel based on the forward output block and the output gradient block to obtain an intermediate
tensor; executing a first type of
transformation operator on the value intermediate result to obtain value gradient blocks; and meanwhile, a gradient back propagation operator is executed in parallel based on the output gradient block, the value block, the intermediate
tensor and the forward normalization result, and a reverse normalization result is obtained. And finally, on the basis of a reverse normalization result, the key blocks and the query blocks, obtaining query gradient blocks and key gradient blocks, so that effective pipeline
processing is performed on different operators, and the overall performance of attention mechanism calculation is improved.