-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdmllib.hpp
213 lines (166 loc) · 4.23 KB
/
dmllib.hpp
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
//
#pragma once
class ML;
struct MLRESOURCE
{
CComPtr<ID3D12Resource> b = 0;
// size_t ls = 0;
size_t sz()
{
if (!b)
return 0;
// return ls;
auto de = b->GetDesc();
return de.Width;
}
operator bool()
{
return b && sz();
}
};
struct MLBUFFER
{
MLRESOURCE b;
LPARAM Tag = 0;
static unsigned long long TensorSizeAlign(unsigned long long t)
{
while (t % DML_MINIMUM_BUFFER_TENSOR_ALIGNMENT)
t++;
return t;
}
~MLBUFFER()
{
}
auto GetOutputDesc() {
return ee.GetOutputDesc();
}
dml::Expression ee;
operator dml::Expression()
{
return ee;
}
HRESULT Create(ID3D12Device* d, dml::Expression e)
{
ee = e;
auto x = TensorSizeAlign(e.GetOutputDesc().totalTensorSizeInBytes);
return Create2(d, x, false);
}
HRESULT Create2(ID3D12Device* d3D12Device, size_t x, [[maybe_unused]] bool ForceInternal);
CComPtr<ID3D12Resource> uploadBuffer;
std::vector<char> global_upload;
UINT64 Upload(ML* ml, void* data, size_t by);
DML_BUFFER_BINDING dmb = {};
DML_BINDING_DESC BindingDesc();
HRESULT Download(class ML* ml, size_t j, std::vector<char>& out);
};
enum class BINDING_MODE
{
NONE = 0,
BIND_IN = 1,
BIND_OUT = 2
};
struct MLOP_ITEM
{
LPARAM tag = 0;
unsigned int InputTensorTag = 0; // 1 based
BINDING_MODE BindingMode = BINDING_MODE::NONE;
std::optional<MLBUFFER> buffer;
dml::Expression expr;
std::optional<MLRESOURCE> bds;
operator dml::Expression()
{
return expr;
}
// Cache
DML_BINDING_DESC dbd = {};
DML_BUFFER_BINDING dmb = {};
operator DML_BINDING_DESC ()
{
if (buffer)
return buffer->BindingDesc();
if (bds)
{
dmb.Buffer = bds->b;
dmb.Offset = 0;
dmb.SizeInBytes = bds->sz();
dbd.Type = DML_BINDING_TYPE_BUFFER;
dbd.Desc = &dmb;
return dbd;
}
throw;
}
};
class MLOP
{
private:
MLBUFFER tri, tre, pri, pre;
friend class ML;
friend class NN;
std::shared_ptr<dml::Graph> graph;
ML* ml = 0;
std::vector<DML_BINDING_DESC> bindings_in;
std::vector<DML_BINDING_DESC> bindings_out;
CComPtr<IDMLCompiledOperator> dmlCompiledOperator;
CComPtr<IDMLOperatorInitializer> dmlOperatorInitializer;
DML_BINDING_TABLE_DESC dmlBindingTableDesc{};
CComPtr<IDMLBindingTable> dmlBindingTable;
UINT descriptorCount = 0;
void tapi();
void tape();
HRESULT CreateInitializer(IDMLDevice* dmlDevice);
void Bind();
void TransitionBindings(ID3D12GraphicsCommandList* commandList);
bool ResetToExecute();
UINT FindDescriptorCount();
bool CreateBindingTable(IDMLDevice* dmlDevice, ID3D12DescriptorHeap* descriptorHeap);
std::vector<MLOP_ITEM> items;
public:
std::shared_ptr<dml::Graph> GetGraph()
{
return graph;
}
MLOP(ML* ml);
size_t Count();
MLOP_ITEM& Item(size_t i);
MLOP_ITEM& WithTag(LPARAM tag);
MLOP_ITEM* WithTag2(LPARAM tag);
MLOP& AddInput(dml::TensorDesc td, LPARAM tag = 0, bool NewBuffer = 1, BINDING_MODE Binding = BINDING_MODE::BIND_IN, std::optional<MLRESOURCE> bds = {});
MLOP& AddItem(dml::Expression td, LPARAM tag = 0, bool NewBuffer = 0, BINDING_MODE Binding = BINDING_MODE::NONE, std::optional<MLRESOURCE> bds = {}, uint32_t nit = 0);
MLOP& AddIntermediate(dml::Expression td, LPARAM tag = 0);
MLOP& AddOutput(dml::Expression td, LPARAM tag = 0);
MLOP& Build();
};
class ML
{
private:
bool Debug = 0;
DML_FEATURE_LEVEL dmlMinFeatureLevel = DML_FEATURE_LEVEL_1_0;
public:
CComPtr<ID3D12Device> d3D12Device;
CComPtr<IDMLDevice> dmlDevice;
CComPtr<ID3D12CommandQueue> commandQueue;
CComPtr<ID3D12CommandAllocator> commandAllocator;
CComPtr<ID3D12GraphicsCommandList> commandList;
CComPtr<IDMLCommandRecorder> dmlCommandRecorder;
DML_BINDING_PROPERTIES initializeBindingProperties = {}, executeBindingProperties = {};
CComPtr<ID3D12DescriptorHeap> descriptorHeap;
ML(bool dbg = 0);
void SetDebug(bool d) {
Debug = d;
}
void SetFeatureLevel(DML_FEATURE_LEVEL f) {
dmlMinFeatureLevel = f;
}
HRESULT On(IDXGIAdapter* adapter = 0);
HRESULT InitializeDirect3D12(IDXGIAdapter* adapter = 0);
HRESULT CreateDML();
dml::Expression ConstantValueTensor(dml::Graph& graph, float what, dml::TensorDesc::Dimensions outputSizes);
HRESULT CreateCommandRecorder();
bool CloseExecuteResetWait();
void SetDescriptorHeaps();
bool CreateHeap();
void Record(int what);
void Prepare();
void Run(size_t which = (size_t)-1);
std::vector<MLOP> ops;
};