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

KonstantinMinor, micro-optimization01325d7

master
1.5 KiB57 linesraw
1#include "stdafx.h"
2#include "miscUtils.h"
3#include <cmath>
4
5void setCurrentThreadName( const char* threadName )
6{
7	const DWORD dwThreadID = GetCurrentThreadId();
8
9	// https://stackoverflow.com/a/10364541/126995
10#pragma pack(push,8)
11	typedef struct tagTHREADNAME_INFO
12	{
13		DWORD dwType;      // Must be 0x1000.
14		LPCSTR szName;     // Pointer to name (in user addr space).
15		DWORD dwThreadID;  // Thread ID (-1=caller thread).
16		DWORD dwFlags;     // Reserved for future use, must be zero.
17	} THREADNAME_INFO;
18#pragma pack(pop)
19
20	THREADNAME_INFO info;
21	info.dwType = 0x1000;
22	info.szName = threadName;
23	info.dwThreadID = dwThreadID;
24	info.dwFlags = 0;
25
26	constexpr DWORD MS_VC_EXCEPTION = 0x406D1388;
27	__try
28	{
29		RaiseException( MS_VC_EXCEPTION, 0, sizeof( info ) / sizeof( ULONG_PTR ), (ULONG_PTR*)&info );
30	}
31	__except( EXCEPTION_EXECUTE_HANDLER )
32	{
33	}
34}
35
36float computeScaling( int mul, int div )
37{
38#ifdef _DEBUG
39	const float ref = (float)std::pow( (double)mul / (double)div, -0.25 );
40#endif
41	// Make int32 vector with both numbers
42	__m128i iv = _mm_cvtsi32_si128( mul );
43	iv = _mm_insert_epi32( iv, div, 1 );
44	// Convert both numbers to FP64
45	__m128d v = _mm_cvtepi32_pd( iv );
46	// Compute mul / div
47	v = _mm_div_sd( v, _mm_unpackhi_pd( v, v ) );
48	// Square root
49	v = _mm_sqrt_sd( v, v );
50	// 4-th root
51	v = _mm_sqrt_sd( v, v );
52	// Invert the value
53	v = _mm_div_sd( _mm_set_sd( 1.0 ), v );
54	// Downcast to FP32, and return the result
55	__m128 f32 = _mm_cvtsd_ss( _mm_setzero_ps(), v );
56	return _mm_cvtss_f32( f32 );
57}