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#pragma once 2#include <immintrin.h> 3 4void addF16to32 (float * rdi ,const uint16_t * a ,const uint16_t * b ,size_t length ); 5void addF16to32 (float * rdi ,const uint16_t * a ,const float * b ,size_t length ); 6 7class AlignedSpan 8{ 9float * pointer ; 10 11public : 12AlignedSpan (void * data ) 13 { 14size_t i = (size_t )data ; 15 constexprsize_t mask32 = ~(size_t )31 ; 16i = (i + 31 )& mask32 ; 17pointer = (float * )i ; 18 } 19 20operator float * ( ) {return pointer ; } 21}; 22 23inline size_t tempBufferForFloats (size_t count ) 24{ 25// Round up by 8 to be able to use full-vector loads and stores 26 constexprsize_t mask8 = ~(size_t )7 ; 27count = (count + 7 )& mask8 ; 28 29// Add 32 more bytes to align the temporary buffer 30return (count * 4 )+ 32 ; 31} 32 33#define ALIGNED_SPAN (name ,countFloats ) AlignedSpan name{ _alloca( tempBufferForFloats( countFloats ) ) } 34 35void norm (float * rdi ,float * temp ,const float * rsi ,size_t length ); 36 37void fmaRepeatRow (float * rdi ,size_t len ,const float * w ,const float * b ,size_t lenPattern ); 38void __vectorcalladdRepeatScaleRow (float * rdi ,size_t len ,const float * b ,size_t lenPattern ,const __m256 scale ); 39void addRepeatRow (float * rdi ,size_t len ,const float * b ,size_t lenPattern ); 40void __vectorcallscaleRow (float * rdi ,size_t len ,const __m256 scale ); 41 42namespace DirectCompute 43{ 44struct LookupTablesData ; 45} 46const DirectCompute ::LookupTablesData & getLookupTables (); 47void addRepeatGeluRow (float * rdi ,size_t len ,const float * b ,size_t lenPattern ,const DirectCompute ::LookupTablesData & lookup ); 48 49void softMax (float * rdi ,size_t length ,const float inputScale ); 50 51// A cache line-aligned array where first 8 elements have all bits set, last 8 elements are zeros 52extern const std ::array < int ,16 > s_zeroTailMask ; 53 54// Load a tail mask as FP32 vector, for use with _mm256_and_ps or _mm256_blendv_ps instructions 55__forceinline__m256 loadTailMaskFloats (size_t remainder ) 56{ 57assert (remainder > 0 && remainder < 8 ); 58const float * rsi = (const float * )& s_zeroTailMask ; 59rsi += 8 ; 60return _mm256_loadu_ps (rsi - remainder ); 61} 62 63// Load a tail mask as int32 vector, for use with _mm256_maskstore_ps instruction 64template < bool assertIncomplete = true> 65__forceinline __m256i loadTailMaskInt (size_t remainder ) 66{ 67if constexpr(assertIncomplete ) 68assert (remainder > 0 && remainder < 8 ); 69else 70assert ( remainder >= 0 && remainder <= 8 ); 71 72const int * rsi = ( const int *)& s_zeroTailMask ; 73rsi += 8 ; 74return _mm256_loadu_si256 ( ( const __m256i * )( rsi - remainder ) ); 75} 76 77void floatsUpcast ( float * rdi , const uint16_t * rsi , size_t length ); 78 79void floatsDowncast ( uint16_t * rdi , const float * rsi , size_t length ); 80 81void addRowInPlace ( float * rdi , const float * rsi , size_t length ); 82void addRow ( float * rdi , const float * a , const float * b , size_t length );