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 "LargeBuffer.h" 3 4namespace CpuCompute 5{ 6// Callback interface for the parallel `for` 7__interface iComputeRange 8 { 9// The implementation calls this method on multiple thread pool threads in parallel, and aggregates status codes. 10HRESULT __stdcallcompute (size_t begin ,size_t end )const ; 11 }; 12 13// Similar to ThreadPoolWork in parallelFor.h, optimized to be used as a direct replacement of OpenMP pool. 14class alignas(64 )ParallelForRunner 15 { 16public : 17ParallelForRunner (int threads ); 18~ ParallelForRunner (); 19 20HRESULT setThreadsCount ( int threads ); 21 22HRESULT parallelFor ( iComputeRange & compute, size_t length, size_t minBatch = 1 ); 23 24// Allocate a temporary buffer for the calling thread. 25// The pointer is guaranteed to be aligned by page size = 4kb 26void * threadLocalBuffer ( size_t cb ); 27 28private : 29 30int maxThreads; 31PTP_WORK work = nullptr ; 32iComputeRange * computeRange = nullptr ; 33size_t countItems = 0 ; 34size_t countThreads = 0 ; 35 36// Aligning by cache lines. 37// Avoiding cache line sharing between CPU cores improves performance, despite wasting a few bytes of memory. 38struct alignas ( 64 ) ThreadBuffer 39{ 40LargeBuffer memory; 41size_t cb = 0 ; 42}; 43std ::vector < ThreadBuffer > threadBuffers; 44 45alignas( 64 ) volatile long threadIndex = 0 ; 46volatile HRESULT status = S_OK ; 47 48void runBatch ( size_t ith ) noexcept; 49 50static void __stdcall workCallbackStatic ( PTP_CALLBACK_INSTANCE Instance, void * pv, PTP_WORK Work ) noexcept ; 51 }; 52}