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

KonstantinSource codes8c4603c

master
1.7 KiB72 linesraw
1#include "stdafx.h"
2#include "downloadBuffer.h"
3#include "device.h"
4#include "MappedResource.h"
5
6namespace
7{
8	struct BufferInfo
9	{
10		D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc;
11		D3D11_BUFFER_DESC bufferDesc;
12		CComPtr<ID3D11Buffer> source;
13
14		HRESULT create( ID3D11ShaderResourceView* srv )
15		{
16			srv->GetDesc( &viewDesc );
17			if( viewDesc.ViewDimension != D3D_SRV_DIMENSION_BUFFER )
18				return E_INVALIDARG;
19
20			CComPtr<ID3D11Resource> res;
21			srv->GetResource( &res );
22			CHECK( res.QueryInterface( &source ) );
23
24			source->GetDesc( &bufferDesc );
25			return S_OK;
26		}
27
28		HRESULT download( void* rdi )
29		{
30			bufferDesc.BindFlags = 0;
31			bufferDesc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
32			bufferDesc.Usage = D3D11_USAGE_STAGING;
33			CComPtr<ID3D11Buffer> staging;
34			using namespace DirectCompute;
35			CHECK( device()->CreateBuffer( &bufferDesc, nullptr, &staging ) );
36
37			context()->CopyResource( staging, source );
38
39			MappedResource mapped;
40			mapped.map( staging, true );
41			memcpy( rdi, mapped.data(), bufferDesc.ByteWidth );
42			return S_OK;
43		}
44	};
45
46	size_t dxgiSizeof( DXGI_FORMAT fmt )
47	{
48		switch( fmt )
49		{
50		case DXGI_FORMAT_R16_FLOAT: return 2;
51		case DXGI_FORMAT_R32_FLOAT: return 4;
52		}
53		return 0;
54	}
55}
56
57template<class E>
58HRESULT DirectCompute::downloadBuffer( ID3D11ShaderResourceView* srv, std::vector<E>& vec )
59{
60	BufferInfo bi;
61	CHECK( bi.create( srv ) );
62
63	const size_t cb = dxgiSizeof( bi.viewDesc.Format );
64	if( cb != sizeof( E ) )
65		return E_INVALIDARG;
66
67	vec.resize( bi.bufferDesc.ByteWidth / cb );
68	return bi.download( vec.data() );
69}
70
71template HRESULT DirectCompute::downloadBuffer( ID3D11ShaderResourceView* srv, std::vector<uint16_t>& vec );
72template HRESULT DirectCompute::downloadBuffer( ID3D11ShaderResourceView* srv, std::vector<float>& vec );