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// Ported from ggml_compute_forward_flash_attn_f16 2// Dispatch with [ neq1*neq2*neq3, 1, 1 ] thread groups 3Buffer<float> q: register( t0 ); 4Buffer<float> k: register( t1 ); 5Buffer<float> v: register( t2 ); 6 7RWBuffer<float> result: register( u0 ); 8// This temporary buffer should fit tempBufferStride * neq1 * neq2 * neq3 elements, FP32 precision 9RWBuffer<float> temp: register( u1 ); 10 11cbuffer Constants: register( b0 ) 12{ 13 uint4 q_elements: packoffset( c0 ); 14 uint4 q_strides: packoffset( c1 ); 15 uint4 k_elements: packoffset( c2 ); 16 uint4 k_strides: packoffset( c3 ); 17 uint4 v_elements: packoffset( c4 ); 18 uint4 v_strides: packoffset( c5 ); 19 uint4 res_elements: packoffset( c6 ); 20 uint4 res_strides: packoffset( c7 ); 21 22 bool masked : packoffset( c8.x ); 23 // 1.0 / sqrt( (double) D ) 24 float scale : packoffset( c8.y ); 25 // This number is required to be >= nek1, and ideally rounded up to either 32 (L2 line) or 128 (L1 line) bytes 26 uint tempBufferStride: packoffset( c8.z ); 27} 28 29static const float negativeInfinity = asfloat( 0xff800000 ); 30 31// Convert FP32 number to FP16 using rounding to nearest, then upcast back to FP32 32inline float roundToFp16( const float src ) 33{ 34 const uint trunc16 = f32tof16( src ); 35 const float trunc32 = f16tof32( trunc16 ); 36 37 const uint truncExp = ( trunc16 >> 10 ) & 0x1F; 38 if( truncExp != 0x1F ) 39 { 40 const uint next16 = trunc16 + 1; 41 const float next32 = f16tof32( next16 ); 42 43 const float errTrunc = abs( src - trunc32 ); 44 const float errNext = abs( src - next32 ); 45 46 if( errTrunc < errNext ) 47 { 48 // Truncated was closer to the source 49 return trunc32; 50 } 51 else if( errTrunc > errNext ) 52 { 53 // Truncated + 1 was closer to the source 54 return next32; 55 } 56 else 57 { 58 // Exactly half, doing banker's rounding to nearest even 59 return ( 0 == ( trunc16 & 1 ) ) ? trunc32 : next32; 60 } 61 } 62 else 63 { 64 // INF or NAN 65 return trunc32; 66 } 67}