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 "TensorShape.h" 3#include "TensorGpuViews.h" 4#include "../D3D/enums.h" 5 6namespace DirectCompute 7{ 8// A minimal tensor object sufficient to compute things on GPU, with compute shaders 9// This class only takes 48 bytes in system memory, and is very cheap to make copies 'coz GPU objects are reference counted. 10class Tensor :public TensorShape ,public TensorGpuViews 11 { 12CComPtr < ID3D11Buffer > getBuffer ()const ; 13 14struct TensorType 15 { 16eDataType type ; 17eBufferUse usage ; 18bool hasInitialData ; 19 }; 20#ifdef _DEBUG 21// In debug builds, we include a few pieces of data to this class. 22TensorType dbgType ; 23#endif 24protected : 25HRESULT create (eDataType type ,std ::initializer_list < uint32_t > sizeElements ,eBufferUse usage ,CComPtr < ID3D11Buffer >& buffer ,const void * rsi ,ID3D11Buffer ** ppStagingBuffer ); 26 27static uint32_t dxgiSizeof (DXGI_FORMAT format ); 28 29void downloadImpl (const D3D11_SHADER_RESOURCE_VIEW_DESC & viewDesc ,uint32_t countElements ,size_t cbElement ,void * rdi )const ; 30 31public : 32Tensor ()= default ; 33 34// These copy operators don't copy any data, they merely increment ref.counter of the GPU resources 35Tensor (const Tensor & ); 36Tensor (Tensor && that )noexcept ; 37Tensor & operator = (const Tensor & that ); 38Tensor & operator = (Tensor && that )noexcept ; 39 40// Move the provided buffer views into this newly created tensor, and assign the shape 41// This destroys old values in the smart pointers 42Tensor (const TensorShape & shape ,CComPtr < ID3D11ShaderResourceView >& srv ,CComPtr < ID3D11UnorderedAccessView >& uav )noexcept ; 43 44Tensor (const TensorShape & shape ,const TensorGpuViews & views ); 45 46// Create a tensor from the GGML's one 47HRESULT create (const ggml_tensor & ggml ,eBufferUse usage ,bool uploadData ); 48 49// Create a new dense tensor of the specified size in elements, without initial data 50HRESULT create (eDataType type ,std ::initializer_list < uint32_t > sizeElements ); 51HRESULT create (eDataType type ,const std ::array < uint32_t ,4 >& sizeElements ); 52HRESULT createImmutable (eDataType type ,const std ::array < int ,4 >& size ,const void * rsi ); 53 54eDataType getType ()const ; 55 56// This method should probably only be used to test things 57// TensorEx is better for production usage, because it creates staging buffer in advance. 58void download (std ::vector < float >& vec )const ; 59void download (std ::vector < uint16_t >& vec )const ; 60 61// ggml_reshape_3d 62Tensor reshape3d (uint32_t ne0 ,uint32_t ne1 ,uint32_t ne2 )const ; 63 64inline void dbgSetType (eDataType dt ,bool hasData = false,eBufferUse use = eBufferUse ::ReadWrite ) 65 { 66#ifdef _DEBUG 67dbgType .type = dt ; 68dbgType .hasInitialData = hasData ; 69dbgType .usage = use ; 70#endif 71 } 72 73__m128i getMemoryUse ()const 74 { 75return resourceMemoryUsage (srv ); 76 } 77 }; 78}