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 "../Whisper/sModelParams.h" 3#include "../Whisper/KeyValueBuffers.h" 4#include "../D3D/MappedResource.h" 5#include "../CPU/Tensor.h" 6 7class KeyValueDownloader 8{ 9CComPtr < ID3D11Buffer > keys ,values ; 10uint32_t length = 0 ; 11 12using E = uint16_t ; 13static constexprDirectCompute ::eDataType dataType = DirectCompute ::eDataType ::FP16 ; 14 15public : 16// Create the staging resources to download kvCross tensors produced by the GPGPU encoder 17HRESULT create (const Whisper ::sModelParams & mp ); 18 19// Download these two tensors from VRAM to the staging buffers in system RAM 20HRESULT download (const DirectCompute ::KeyValueBuffers & source ); 21 22class ReadMap 23 { 24const uint32_t length ; 25DirectCompute ::MappedResource mappedKeys ,mappedValues ; 26 27public : 28ReadMap (KeyValueDownloader & owner ); 29 ~ReadMap ()= default ; 30ReadMap (const ReadMap & )= delete ; 31 32// A slice of model.memory_k tensor 33CpuCompute ::Tensor keysView (uint32_t len ,uint32_t off )const 34 { 35if (len + off <=length ) 36 { 37E * rsi = (E * )mappedKeys .data (); 38rsi += off ; 39return CpuCompute ::Tensor ::fromData (rsi ,dataType ,len ); 40 } 41throw E_BOUNDS ; 42 } 43 44// A slice of model.memory_v tensor 45CpuCompute ::Tensor valuesView (uint32_t len ,uint32_t off )const 46 { 47if (len + off <=length ) 48 { 49E * rsi = (E * )mappedValues .data (); 50rsi += off ; 51return CpuCompute ::Tensor ::fromData (rsi ,dataType ,len ); 52 } 53throw E_BOUNDS ; 54 } 55 }; 56 57// Map both staging buffers, return RAII object which unmaps when destroyed, 58// which can supply the data in the shape of CpuCompute::Tensor vector 59decltype (auto )map () 60 { 61return ReadMap (* this ); 62 } 63};