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
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
|
#include "stdafx.h"
#include "Tensor.h"
#include "../D3D/MappedResource.h"
#include "../D3D/createBuffer.h"
#include "../source/ggml.h"
using namespace DirectCompute;
Tensor::Tensor( const Tensor& that )
{
ne = that.ne;
nb = that.nb;
srv = that.srv;
uav = that.uav;
#ifdef _DEBUG
dbgType = that.dbgType;
#endif
}
Tensor::Tensor( Tensor&& that ) noexcept
{
ne = that.ne;
nb = that.nb;
srv.Attach( that.srv.Detach() );
uav.Attach( that.uav.Detach() );
#ifdef _DEBUG
dbgType = that.dbgType;
#endif
}
Tensor& Tensor::operator=( const Tensor& that )
{
ne = that.ne;
nb = that.nb;
srv = that.srv;
uav = that.uav;
#ifdef _DEBUG
dbgType = that.dbgType;
#endif
return *this;
}
Tensor& Tensor::operator=( Tensor&& that ) noexcept
{
ne = that.ne;
nb = that.nb;
srv.Attach( that.srv.Detach() );
uav.Attach( that.uav.Detach() );
#ifdef _DEBUG
dbgType = that.dbgType;
#endif
return *this;
}
Tensor::Tensor( const TensorShape& shape, CComPtr<ID3D11ShaderResourceView>& srv, CComPtr<ID3D11UnorderedAccessView>& uav ) noexcept :
TensorShape( shape )
{
TensorGpuViews::srv.Attach( srv.Detach() );
TensorGpuViews::uav.Attach( uav.Detach() );
}
Tensor::Tensor( const TensorShape& shape, const TensorGpuViews& views ) :
TensorShape( shape )
{
srv = views;
uav = views;
}
HRESULT Tensor::create( const ggml_tensor& ggml, eBufferUse usage, bool uploadData )
{
TensorGpuViews::clear();
switch( usage )
{
case eBufferUse::Immutable:
case eBufferUse::ReadWriteDownload:
break;
default:
return E_INVALIDARG;
}
CComPtr<ID3D11Buffer> buffer;
CHECK( TensorShape::create( ggml ) );
const ggml_type dataType = ggml.type;
const uint32_t cbElement = (uint32_t)ggml_type_size( dataType );
const size_t totalBytes = ggml_nbytes( &ggml );
if( totalBytes > INT_MAX )
return DISP_E_OVERFLOW;
const uint32_t countElements = (uint32_t)( totalBytes / cbElement );
{
const void* const rsi = uploadData ? ggml.data : nullptr;
CHECK( createBuffer( usage, totalBytes, &buffer, rsi, nullptr ) );
}
DXGI_FORMAT format;
eDataType type;
switch( dataType )
{
case GGML_TYPE_F16:
format = DXGI_FORMAT_R16_FLOAT;
type = eDataType::FP16;
break;
case GGML_TYPE_F32:
format = DXGI_FORMAT_R32_FLOAT;
type = eDataType::FP32;
break;
default:
return E_NOTIMPL;
}
const bool makeUav = ( usage == eBufferUse::ReadWrite );
CHECK( TensorGpuViews::create( buffer, format, totalBytes / cbElement, makeUav ) );
#ifdef _DEBUG
dbgType.type = type;
dbgType.usage = usage;
dbgType.hasInitialData = uploadData;
#endif
return S_OK;
}
HRESULT Tensor::createImmutable( eDataType type, const std::array<int, 4>& size, const void* rsi )
{
size_t elts = (uint32_t)size[ 0 ];
elts *= (uint32_t)size[ 1 ];
elts *= (uint32_t)size[ 2 ];
elts *= (uint32_t)size[ 3 ];
DXGI_FORMAT format;
size_t cbElement;
switch( type )
{
case eDataType::FP16:
format = DXGI_FORMAT_R16_FLOAT;
cbElement = 2;
break;
case eDataType::FP32:
format = DXGI_FORMAT_R32_FLOAT;
cbElement = 4;
break;
default:
return E_NOTIMPL;
}
CComPtr<ID3D11Buffer> buffer;
CHECK( createBuffer( eBufferUse::Immutable, cbElement * elts, &buffer, rsi, nullptr ) );
CHECK( TensorGpuViews::create( buffer, format, elts, false ) );
__m128i v = _mm_loadu_si128( ( const __m128i* )size.data() );
_mm_storeu_si128( ( __m128i* )ne.data(), v );
setDenseStrides();
return S_OK;
}
HRESULT Tensor::create( eDataType type, std::initializer_list<uint32_t> sizeElements, eBufferUse usage, CComPtr<ID3D11Buffer>& buffer, const void* rsi, ID3D11Buffer** ppStagingBuffer )
{
TensorGpuViews::clear();
size_t nDims = sizeElements.size();
if( 0 == nDims || nDims > 4 )
return E_INVALIDARG;
nDims = std::min( nDims, (size_t)4 );
size_t totalElements = 1;
for( size_t i = 0; i < nDims; i++ )
{
uint32_t n = sizeElements.begin()[ i ];
if( n == 0 )
return E_INVALIDARG;
ne[ i ] = n;
totalElements *= n;
}
DXGI_FORMAT format;
size_t cbElement;
switch( type )
{
case eDataType::FP32:
format = DXGI_FORMAT_R32_FLOAT;
cbElement = 4;
break;
case eDataType::FP16:
format = DXGI_FORMAT_R16_FLOAT;
cbElement = 2;
break;
case eDataType::U32:
format = DXGI_FORMAT_R32_UINT;
cbElement = 4;
break;
default:
return E_NOTIMPL;
}
const size_t totalBytes = cbElement * totalElements;
if( totalBytes > INT_MAX )
return DISP_E_OVERFLOW;
for( size_t i = nDims; i < 4; i++ )
ne[ i ] = 1;
TensorShape::setDenseStrides();
CHECK( createBuffer( usage, totalBytes, &buffer, rsi, ppStagingBuffer ) );
CHECK( TensorGpuViews::create( buffer, format, totalBytes / cbElement, true ) );
#ifdef _DEBUG
dbgType.type = type;
dbgType.usage = usage;
dbgType.hasInitialData = ( nullptr != rsi );
#endif
return S_OK;
}
HRESULT Tensor::create( eDataType type, std::initializer_list<uint32_t> sizeElements )
{
CComPtr<ID3D11Buffer> buffer;
return create( type, sizeElements, eBufferUse::ReadWrite, buffer, nullptr, nullptr );
}
HRESULT Tensor::create( eDataType type, const std::array<uint32_t, 4>& sizeElements )
{
std::initializer_list<uint32_t> il( sizeElements.data(), sizeElements.data() + 4 );
return create( type, il );
}
eDataType Tensor::getType() const
{
ID3D11ShaderResourceView* const srv = *this;
if( nullptr == srv )
throw OLE_E_BLANK;
D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc;
srv->GetDesc( &viewDesc );
const DXGI_FORMAT format = viewDesc.Format;
switch( format )
{
case DXGI_FORMAT_R32_FLOAT:
return eDataType::FP32;
case DXGI_FORMAT_R16_FLOAT:
return eDataType::FP16;
case DXGI_FORMAT_R32_UINT:
return eDataType::U32;
}
throw E_NOTIMPL;
}
CComPtr<ID3D11Buffer> Tensor::getBuffer() const
{
ID3D11ShaderResourceView* const srv = *this;
if( nullptr == srv )
throw OLE_E_BLANK;
CComPtr<ID3D11Resource> res;
srv->GetResource( &res );
CComPtr<ID3D11Buffer> buff;
check( res.QueryInterface( &buff ) );
return buff;
}
uint32_t Tensor::dxgiSizeof( DXGI_FORMAT format )
{
switch( format )
{
case DXGI_FORMAT_R16_FLOAT:
return 2;
case DXGI_FORMAT_R32_FLOAT:
case DXGI_FORMAT_R32_UINT:
return 4;
}
throw E_INVALIDARG;
}
void Tensor::downloadImpl( const D3D11_SHADER_RESOURCE_VIEW_DESC& viewDesc, uint32_t countElements, size_t cbElement, void* rdi ) const
{
assert( viewDesc.ViewDimension == D3D_SRV_DIMENSION_BUFFER );
const uint32_t idxFirst = viewDesc.Buffer.FirstElement;
CComPtr<ID3D11Buffer> buff = getBuffer();
D3D11_BUFFER_DESC desc;
buff->GetDesc( &desc );
desc.BindFlags = 0;
desc.Usage = D3D11_USAGE_STAGING;
desc.CPUAccessFlags = D3D11_CPU_ACCESS_READ;
CComPtr<ID3D11Buffer> staging;
check( device()->CreateBuffer( &desc, nullptr, &staging ) );
context()->CopyResource( staging, buff );
MappedResource mapped;
check( mapped.map( staging, true ) );
const uint8_t* rsi = (const uint8_t*)mapped.data();
rsi += cbElement * idxFirst;
memcpy( rdi, rsi, cbElement * countElements );
}
void Tensor::download( std::vector<float>& vec ) const
{
ID3D11ShaderResourceView* const srv = *this;
if( nullptr == srv )
throw OLE_E_BLANK;
D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc;
srv->GetDesc( &viewDesc );
if( viewDesc.Format != DXGI_FORMAT_R32_FLOAT )
throw E_INVALIDARG;
uint32_t countElements = viewDesc.Buffer.NumElements;
vec.resize( countElements );
downloadImpl( viewDesc, countElements, 4, vec.data() );
}
void Tensor::download( std::vector<uint16_t>& vec ) const
{
ID3D11ShaderResourceView* const srv = *this;
if( nullptr == srv )
throw OLE_E_BLANK;
D3D11_SHADER_RESOURCE_VIEW_DESC viewDesc;
srv->GetDesc( &viewDesc );
if( viewDesc.Format != DXGI_FORMAT_R16_FLOAT )
throw E_INVALIDARG;
uint32_t countElements = viewDesc.Buffer.NumElements;
vec.resize( countElements );
downloadImpl( viewDesc, countElements, 2, vec.data() );
}
Tensor Tensor::reshape3d( uint32_t ne0, uint32_t ne1, uint32_t ne2 ) const
{
if( !isContinuous() )
throw E_NOTIMPL;
if( countElements() != ne0 * ne1 * ne2 )
throw E_INVALIDARG;
Tensor res = *this;
res.ne = { ne0, ne1, ne2, 1 };
res.setDenseStrides();
return res;
}
|