yum-archive/TaSTT-Whisper
High-performance GPGPU inference of OpenAI's Whisper automatic speech recognition (ASR) model
git clone https://git.yummers.dev/yum-archive/TaSTT-Whisper
8c4603c
master
1// Special softMax shader for matrices with rows of 1500 elements. 2// Uses group shared buffer of that length to save global memory bandwidth, more than 2x faster than the original. 3// Dispatch [ nr, 1, 1 ] thread groups of this shader 4RWBuffer < float > result :register ( u0 ); 5 6cbuffer Constants :register ( b0 ) 7{ 8uint4 elements :packoffset ( c0 ); 9uint4 strides :packoffset ( c1 ); 10uint nr :packoffset ( c2 . x ); 11float inputScale :packoffset ( c2 . y ); 12} 13 14#include "miscUtils.hlsli" 15#include "groupReduce64.hlsli" 16 17static const uint THREADS = 64 ; 18static const uint ROW_LENGTH = 1500 ; 19groupsharedfloat rowBuffer [ ROW_LENGTH ]; 20 21static const float negativeInfinity = asfloat ( 0xff800000 ); 22 23[ numthreads ( THREADS , 1 , 1 ) ] 24void main ( uint3 group : SV_GroupID , uint thread : SV_GroupIndex ) 25{ 26const uint p = group . x * strides [ 1 ]; 27const uint nc = ROW_LENGTH ; 28uint i ; 29 30float m = negativeInfinity ; 31// First pass: compute maximum, and copy the row into the group shared buffer 32for ( i = thread ; i < nc ; i += THREADS ) 33{ 34float f = result [ p + i ]; 35m = max ( m , f ); 36rowBuffer [ i ] = f ; 37} 38horizontalMaxBroadcast ( thread , m ); 39 40// Second pass: apply initial scale, compute the exponent, and compute total sum over the row 41float sum = 0 ; 42for ( i = thread ; i < nc ; i += THREADS ) 43{ 44float f = rowBuffer [ i ]; 45 46[ branch ] 47if ( f != negativeInfinity ) 48{ 49f = ( f - m ) * inputScale ; 50#if 1 51// At least on Radeon Graphics GPU inside Ryzen 7 5700G, computing exponent instead of loading from the buffer improves the performance 52f = exp ( f ); 53#else 54uint s = fp16Rounded ( f ); 55s = lookupTable [ s ]; 56f = f16tof32 ( s ); 57#endif 58sum += f ; 59} 60else 61f = 0 ; 62 63rowBuffer [ i ] = f ; 64} 65 66horizontalSum ( thread , sum ); 67if ( 0 == thread ) 68sharedAccumulators [ 0 ] = 1.0 / sum ; 69GroupMemoryBarrierWithGroupSync (); 70const float scale = sharedAccumulators [ 0 ]; 71 72// Final pass: apply the final scale, and copy the row from the group shared buffer back into the global memory 73for ( i = thread ; i < nc ; i += THREADS ) 74{ 75float f = rowBuffer [ i ]; 76f *= scale ; 77result [ p + i ] = f ; 78} 79}