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
ad097a7
master
1#pragma once 2#include <stdint.h> 3#include <assert.h> 4 5namespace Whisper 6{ 7// Available sampling strategies 8enum struct eSamplingStrategy :int 9 { 10// Always select the most probable token 11Greedy , 12// TODO: not implemented yet! 13BeamSearch , 14 }; 15 16using pfnNewSegment = HRESULT (__cdecl * )( iContext * ctx, uint32_t n_new, void * user_data ) noexcept; 17 18// Return S_OK to proceed, or S_FALSE to stop the process and return S_OK from runFull / runStreamed method 19using pfnEncoderBegin = HRESULT ( __cdecl * )( iContext * ctx, void * user_data ) noexcept; 20 21enum struct eFullParamsFlags : uint32_t 22{ 23Translate = 1 , 24NoContext = 2 , 25SingleSegment = 4 , 26PrintSpecial = 8 , 27PrintProgress = 0x10 , 28PrintRealtime = 0x20 , 29PrintTimestamps = 0x40 , 30 31// Experimental 32TokenTimestamps = 0x100 , 33SpeedupAudio = 0x200 , 34}; 35 36inline eFullParamsFlags operator | ( eFullParamsFlags a, eFullParamsFlags b ) 37{ 38return (eFullParamsFlags)( ( uint32_t )a | ( uint32_t )b ); 39} 40inline void operator |= ( eFullParamsFlags & a, eFullParamsFlags b ) 41{ 42a = a | b; 43} 44 45struct sFullParams 46{ 47eSamplingStrategy strategy ; 48// Count of CPU threads 49int cpuThreads ; 50int n_max_text_ctx ; 51int offset_ms ; // start offset in ms 52int duration_ms ; // audio duration to process in ms 53eFullParamsFlags flags ; 54uint32_t language ; 55 56// [EXPERIMENTAL] token-level timestamps 57float thold_pt ; // timestamp token probability threshold (~0.01) 58float thold_ptsum ; // timestamp token sum probability threshold (~0.01) 59int max_len ; // max segment length in characters 60int max_tokens ; // max tokens per segment (0 = no limit) 61 62struct 63{ 64int n_past ; 65} greedy ; 66 67struct 68{ 69int n_past ; 70int beam_width ; 71int n_best ; 72} beam_search ; 73 74// [EXPERIMENTAL] speed-up techniques 75int audio_ctx ; // overwrite the audio context size (0 = use default) 76 77// tokens to provide the whisper model as initial prompt 78// these are prepended to any existing text context from a previous call 79const whisper_token * prompt_tokens ; 80int prompt_n_tokens ; 81 82pfnNewSegment new_segment_callback ; 83void * new_segment_callback_user_data ; 84 85pfnEncoderBegin encoder_begin_callback ; 86void * encoder_begin_callback_user_data ; 87 88// Couple utility methods, they workaround the lack of bit fields in C++ 89inline bool flag ( eFullParamsFlags f ) const 90{ 91return 0 != ( ( uint32_t )flags & ( uint32_t )f ); 92} 93inline void resetFlag ( eFullParamsFlags bit ) 94{ 95uint32_t f = ( uint32_t )flags; 96f &= ~( uint32_t )bit; 97flags = ( eFullParamsFlags )f; 98} 99inline void setFlag ( eFullParamsFlags bit, bool set = true ) 100{ 101uint32_t f = ( uint32_t )flags; 102if ( set ) 103f |= ( uint32_t )bit; 104else 105f &= ~( uint32_t )bit; 106flags = ( eFullParamsFlags )f; 107} 108}; 109 110struct sSegmentTime 111{ 112int64_t begin , end ; 113}; 114 115inline uint32_t makeLanguageKey ( const char * code ) 116{ 117assert ( strlen ( code ) <= 4 ); 118uint32_t res = 0 ; 119uint32_t shift = 0 ; 120for ( size_t i = 0 ; i < 4 ; i ++ , code ++ , shift += 8 ) 121{ 122const char c = * code; 123if ( c == '\0' ) 124return res; 125uint32_t u32 = ( uint8_t )c; 126u32 = u32 << shift; 127res |= u32; 128} 129return res; 130} 131 132using pfnReportProgress = HRESULT ( __stdcall * )( double val, iContext * ctx, void * pv ) noexcept; 133struct sProgressSink 134{ 135pfnReportProgress pfn ; 136void * pv ; 137 }; 138}