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

KonstantinConsistent cancellation API across the library: S_OK = continue, S_FALSE = stopad097a7

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