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.5 KiB63 linesraw
1#include "stdafx.h"
2#include "ConstantBuffer.h"
3#include "../D3D/MappedResource.h"
4using namespace DirectCompute;
5
6HRESULT ConstantBuffer::create()
7{
8	if( nullptr == buffer )
9	{
10		CD3D11_BUFFER_DESC desc{ 16 * 3 * 2, D3D11_BIND_CONSTANT_BUFFER, D3D11_USAGE_DYNAMIC, D3D11_CPU_ACCESS_WRITE };
11		return device()->CreateBuffer( &desc, nullptr, &buffer );
12	}
13	return HRESULT_FROM_WIN32( ERROR_ALREADY_INITIALIZED );
14}
15
16namespace
17{
18	__forceinline void copy32( __m128i* rdi, const TensorShape& ts )
19	{
20		_mm_storeu_si128( rdi, ts.sizeVec() );
21		_mm_storeu_si128( rdi + 1, ts.stridesVec() );
22	}
23}
24
25HRESULT ConstantBuffer::update( const TensorShape& t0 )
26{
27	MappedResource mapped;
28	CHECK( mapped.map( buffer, false ) );
29
30	__m128i* const rdi = ( __m128i* )mapped.data();
31	copy32( rdi, t0 );
32	return S_OK;
33}
34
35HRESULT ConstantBuffer::update( const TensorShape& t0, const TensorShape& t1 )
36{
37	MappedResource mapped;
38	CHECK( mapped.map( buffer, false ) );
39
40	__m128i* const rdi = ( __m128i* )mapped.data();
41	copy32( rdi, t0 );
42	copy32( rdi + 2, t1 );
43	return S_OK;
44}
45
46HRESULT ConstantBuffer::update( const TensorShape& t0, const TensorShape& t1, const TensorShape& t2 )
47{
48	MappedResource mapped;
49	CHECK( mapped.map( buffer, false ) );
50
51	__m128i* const rdi = ( __m128i* )mapped.data();
52	copy32( rdi, t0 );
53	copy32( rdi + 2, t1 );
54	copy32( rdi + 4, t2 );
55	return S_OK;
56}
57
58void ConstantBuffer::bind() const
59{
60	ID3D11Buffer* p = buffer;
61	assert( nullptr != p );
62	context()->CSSetConstantBuffers( 0, 1, &p );
63}