summaryrefslogtreecommitdiffstats
path: root/Whisper/ML/LookupTables.cpp
blob: 2fc1cc88ee304e19e2c81484775d2dd71402fb38 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
#include "stdafx.h"
#include "LookupTables.h"
#include "LookupTablesData.h"
#include <memory>
using namespace DirectCompute;

namespace
{
	HRESULT uploadLookupTable( const std::array<uint16_t, 0x10000>& rsi, CComPtr<ID3D11ShaderResourceView>& rdi )
	{
		rdi = nullptr;
		CComPtr<ID3D11Buffer> buffer;

		CD3D11_BUFFER_DESC desc{ 0x10000 * 2, D3D11_BIND_SHADER_RESOURCE, D3D11_USAGE_IMMUTABLE };
		D3D11_SUBRESOURCE_DATA srd{ rsi.data(), 0, 0 };
		CHECK( device()->CreateBuffer( &desc, &srd, &buffer ) );

		CD3D11_SHADER_RESOURCE_VIEW_DESC viewDesc{ D3D11_SRV_DIMENSION_BUFFER, DXGI_FORMAT_R16_UINT, 0, 0x10000 };
		CHECK( device()->CreateShaderResourceView( buffer, &viewDesc, &rdi ) );

		return S_OK;
	}
}

HRESULT LookupTables::create()
{
	std::unique_ptr<LookupTablesData> data;
	try
	{
		data = std::make_unique<LookupTablesData>();
	}
	catch( const std::bad_alloc& )
	{
		return E_OUTOFMEMORY;
	}

	CHECK( uploadLookupTable( data->gelu, m_gelu ) );
	CHECK( uploadLookupTable( data->exponent, m_exponent ) );

	return S_OK;
}

void LookupTables::clear()
{
	m_gelu = nullptr;
	m_exponent = nullptr;
}

__m128i LookupTables::getMemoryUsage() const
{
	__m128i v = resourceMemoryUsage( m_gelu );
	v = _mm_add_epi64( v, resourceMemoryUsage( m_exponent ) );
	return v;
}