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// GGML_TASK_COMPUTE step for matrix*matrix product, where nb01 < nb00 2Buffer < float > arg0 :register ( t0 ); 3Buffer < float > arg1 :register ( t1 ); 4RWBuffer < float > resultTensor :register ( u0 ); 5RWBuffer < float > tempBuffer :register ( u1 ); 6 7cbuffer Constants :register ( b0 ) 8{ 9uint4 aSize :packoffset ( c0 ); 10uint4 aStride :packoffset ( c1 ); 11uint4 bSize :packoffset ( c2 ); 12uint4 bStride :packoffset ( c3 ); 13uint4 resSize :packoffset ( c4 ); 14bool resultFp16 :packoffset ( c5 . x ); 15uint ne :packoffset ( c5 . y ); 16} 17 18#include "miscUtils.hlsli" 19 20// tempBuffer[ rdi .. ] = 0.0 21inline void writeTempZeros ( uint rdi , const uint len , const uint thread ) 22{ 23const uint rdiEnd = rdi + len ; 24for ( rdi += thread ; rdi < rdiEnd ; rdi += 32 ) 25tempBuffer [ rdi ] = 0.0 ; 26} 27 28// tempBuffer[ rdi .. ] += mul * arg0[ rsi .. ] 29inline void vectorMad ( uint rsi , uint rdi , const uint len , const float mul , const uint thread ) 30{ 31const uint rsiEnd = rsi + len ; 32rsi += thread ; 33rdi += thread ; 34for ( ; rsi < rsiEnd ; rsi += 32 , rdi += 32 ) 35{ 36float f = tempBuffer [ rdi ]; 37f = mad ( mul , arg0 [ rsi ], f ); 38[ branch ] 39if ( resultFp16 ) 40f = adjustFp16 ( f ); 41tempBuffer [ rdi ] = f ; 42} 43} 44 45// resultTensor[ rdi .. ] = tempBuffer[ rsi .. ] 46inline void copyRow ( uint rsi , uint rdi , const uint len , const uint thread ) 47{ 48const uint rsiEnd = rsi + len ; 49rsi += thread ; 50rdi += thread ; 51for ( ; rsi < rsiEnd ; rsi += 32 , rdi += 32 ) 52{ 53float f = tempBuffer [ rsi ]; 54resultTensor [ rdi ] = f ; 55} 56} 57 58// resultTensor[ rdi .. ] += tempBuffer[ rsi .. ] 59inline void addRow ( uint rsi , uint rdi , const uint len , const uint thread ) 60{ 61const uint rsiEnd = rsi + len ; 62rsi += thread ; 63rdi += thread ; 64for ( ; rsi < rsiEnd ; rsi += 32 , rdi += 32 ) 65{ 66float f = resultTensor [ rdi ]; 67f += tempBuffer [ rsi ]; 68resultTensor [ rdi ] = f ; 69} 70} 71 72[ numthreads ( 32 , 1 , 1 )] 73void main ( const uint3 group : SV_GroupID , const uint thread : SV_GroupIndex ) 74{ 75const uint i1 = group [ 0 ]; 76const uint i2 = group [ 1 ]; 77const uint i3 = group [ 2 ]; 78 79const uint ne00 = aSize [ 0 ]; 80const uint ne01 = aSize [ 1 ]; 81const uint ne02 = aSize [ 2 ]; 82const uint ne03 = aSize [ 3 ]; 83 84const uint ne10 = bSize [ 0 ]; 85const uint ne11 = bSize [ 1 ]; 86const uint ne12 = bSize [ 2 ]; 87const uint ne13 = bSize [ 3 ]; 88 89const uint ne0 = resSize [ 0 ]; 90const uint ne1 = resSize [ 1 ]; 91const uint ne2 = resSize [ 2 ]; 92const uint ne3 = resSize [ 3 ]; 93 94const uint nb00 = aStride [ 0 ]; 95const uint nb01 = aStride [ 1 ]; 96const uint nb02 = aStride [ 2 ]; 97const uint nb03 = aStride [ 3 ]; 98 99const uint nb10 = bStride [ 0 ]; 100const uint nb11 = bStride [ 1 ]; 101const uint nb12 = bStride [ 2 ]; 102const uint nb13 = bStride [ 3 ]; 103 104// dst_row = wdata + wo + i3*ne2*ne1*ne0 + i2*ne1*ne0 + i1*ne0; 105const uint tempRowThread0 = i3 * ne2 * ne1 * ne0 + i2 * ne1 * ne0 + i1 * ne0 ; 106 107// Faking 4 CPU threads trying to achieve bitwise compatibility with the CPU version 108const uint nth = 4 ; 109 110// GGML_TASK_COMPUTE 111{ 112// src0_col = src0->data + ( i00 * nb00 + i02 * nb02 + i03 * nb03 ); 113const uint aBase = i2 * nb02 + i3 * nb03 ; 114// src1_val = * (float *) ((char *) src1->data + (i10*nb10 + i11*nb11 + i12*nb12 + i13*nb13)); 115const uint bBase = i1 * nb11 + i2 * nb12 + i3 * nb13 ; 116 117// total columns in src1 118const uint nc = ne10 ; 119// columns per thread 120const uint dc = ( nc + nth - 1 ) / nth ; 121 122uint tempRow = tempRowThread0 ; 123for ( uint ith = 0 ; ith < nth ; ith ++ , tempRow += ne ) 124{ 125writeTempZeros ( tempRow , ne01 , thread ); 126 127// column range for this thread 128const uint ic0 = dc * ith ; 129const uint ic1 = min ( ic0 + dc , nc ); 130 131for ( uint ic = ic0 ; ic < ic1 ; ic ++ ) 132{ 133const uint idxA = aBase + ic * aStride [ 0 ]; 134const uint idxB = bBase + ic * bStride [ 0 ]; 135const float bValue = arg1 [ idxB ]; 136vectorMad ( idxA , tempRow , ne01 , bValue , thread ); 137} 138} 139} 140 141// GGML_TASK_FINALIZE 142{ 143const uint rdi = tempRowThread0 ; 144// const uint rdi = i1 * resSize[ 0 ] + i2 * resSize[ 0 ] * resSize[ 1 ] + i3 * resSize[ 0 ] * resSize[ 1 ] * resSize[ 2 ]; 145// const uint rdi = ( ( i3 * resSize[ 2 ] + i2 ) * resSize[ 1 ] + i1 ) * resSize[ 0 ]; 146 147uint tempRow = tempRowThread0 ; 148copyRow ( tempRow , rdi , ne01 , thread ); 149 150tempRow += ne ; 151for ( uint ith = 1 ; ith < nth ; ith ++ , tempRow += ne ) 152addRow ( tempRow , rdi , ne01 , thread ); 153} 154}