-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMiopenLegacyPlugin.cpp
More file actions
428 lines (345 loc) · 15.3 KB
/
MiopenLegacyPlugin.cpp
File metadata and controls
428 lines (345 loc) · 15.3 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
// Copyright © Advanced Micro Devices, Inc., or its affiliates.
// SPDX-License-Identifier: MIT
#include <iostream>
#include <miopen/miopen.h>
#include <hipdnn_sdk/logging/Logger.hpp>
#include <hipdnn_sdk/plugin/EnginePluginApi.h>
#include <hipdnn_sdk/plugin/PluginApi.h>
#include <hipdnn_sdk/plugin/PluginDataTypeHelpers.hpp>
#include <hipdnn_sdk/plugin/PluginHelpers.hpp>
#include <hipdnn_sdk/plugin/PluginLastErrorManager.hpp>
#include <hipdnn_sdk/plugin/flatbuffer_utilities/EngineConfigWrapper.hpp>
#include <hipdnn_sdk/plugin/flatbuffer_utilities/GraphWrapper.hpp>
#include "EngineManager.hpp"
#include "HipdnnEnginePluginExecutionContext.hpp"
#include "HipdnnEnginePluginHandle.hpp"
#include "MiopenContainer.hpp"
#include "MiopenHandleFactory.hpp"
static const char* pluginName = "miopen_legacy_plugin";
static const char* pluginVersion = "1.0.0";
using namespace hipdnn_plugin;
using namespace miopen_legacy_plugin;
// NOLINTNEXTLINE
thread_local char PluginLastErrorManager::s_lastError[HIPDNN_PLUGIN_ERROR_STRING_MAX_LENGTH] = "";
// Keep a weak pointer to the MiopenContainer thats made when we create a plugin handle.
// The original shared_ptr is then stored on the handle so that it can be used for the lifecycle
// of the handle. If we create another handle, then we can use the weak pointer to get access
// to the existing MiopenContainer. If all handles are destroyed, then this allows us to properly
// clean up the container without having to fully unload the plugin.
std::weak_ptr<MiopenContainer> miopenContainerLifecyclePtr;
extern "C" {
hipdnnPluginStatus_t hipdnnPluginGetName(const char** name)
{
LOG_API_ENTRY("name_ptr={:p}", static_cast<void*>(name));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(name);
*name = pluginName;
LOG_API_SUCCESS(apiName, "pluginName={:p}", static_cast<void*>(name));
});
}
hipdnnPluginStatus_t hipdnnPluginGetVersion(const char** version)
{
LOG_API_ENTRY("versionPtr={:p}", static_cast<void*>(version));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(version);
*version = pluginVersion;
LOG_API_SUCCESS(apiName, "version={:p}", static_cast<void*>(version));
});
}
hipdnnPluginStatus_t hipdnnPluginGetType(hipdnnPluginType_t* type)
{
LOG_API_ENTRY("typePtr={:p}", static_cast<void*>(type));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(type);
*type = HIPDNN_PLUGIN_TYPE_ENGINE;
LOG_API_SUCCESS(apiName, "type={}", *type);
});
}
void hipdnnPluginGetLastErrorString(const char** errorStr)
{
LOG_API_ENTRY("errorStrPtr={:p}", static_cast<void*>(errorStr));
hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(errorStr);
*errorStr = PluginLastErrorManager::getLastError();
LOG_API_SUCCESS(apiName, "errorStr={:p}", static_cast<void*>(errorStr));
});
}
// Once plugins are loaded via plugin manager then logging will work for them
hipdnnPluginStatus_t hipdnnPluginSetLoggingCallback(hipdnnCallback_t callback)
{
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(callback);
hipdnn::logging::initializeCallbackLogging(pluginName, callback);
LOG_API_SUCCESS(apiName, "", "");
});
}
hipdnnPluginStatus_t
hipdnnEnginePluginGetAllEngineIds(int64_t* engineIds, uint32_t maxEngines, uint32_t* numEngines)
{
LOG_API_ENTRY("engineIds={:p}, maxEngines={}, numEngines={:p}",
static_cast<void*>(engineIds),
maxEngines,
static_cast<void*>(numEngines));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
if(maxEngines != 0)
{
throwIfNull(engineIds);
}
throwIfNull(numEngines);
// For now, we will just return a single engine ID.
auto allEngineIds = std::vector<int64_t>({1});
if(allEngineIds.size() > std::numeric_limits<uint32_t>::max())
{
throw HipdnnPluginException(HIPDNN_PLUGIN_STATUS_INTERNAL_ERROR,
"Number of engines exceeds maximum uint32_t value.");
}
if(maxEngines == 0)
{
*numEngines = static_cast<uint32_t>(allEngineIds.size());
}
else
{
*numEngines = 0;
for(auto engineId : allEngineIds)
{
if(*numEngines == maxEngines)
{
*numEngines = static_cast<uint32_t>(allEngineIds.size());
HIPDNN_LOG_INFO("Maximum number of engines reached ({}), ignoring additional "
"engines, numEngines count: {}",
maxEngines,
*numEngines);
break;
}
engineIds[*numEngines] = engineId;
(*numEngines)++;
}
}
LOG_API_SUCCESS(apiName, "numEngines={}", *numEngines);
});
}
hipdnnPluginStatus_t hipdnnEnginePluginCreate(hipdnnEnginePluginHandle_t* handle)
{
LOG_API_ENTRY("handle_ptr={:p}", static_cast<void*>(handle));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(handle);
miopen_legacy_plugin::MiopenHandleFactory::createMiopenHandle(handle);
auto miopenContainerPtr = miopenContainerLifecyclePtr.lock();
if(miopenContainerPtr != nullptr)
{
(*handle)->miopenContainer = miopenContainerPtr;
}
else
{
static std::mutex s_miopenContainerMutex;
std::lock_guard<std::mutex> lock(s_miopenContainerMutex);
// if we do have a race condition that results in threads getting locked, we want to
// ensure that we only create one instance. Therefore, the second thread to get
// through will just read from the weak pointer rather than create a new instance.
miopenContainerPtr = miopenContainerLifecyclePtr.lock();
if(miopenContainerPtr != nullptr)
{
(*handle)->miopenContainer = miopenContainerPtr;
}
else
{
(*handle)->miopenContainer = std::make_shared<MiopenContainer>();
miopenContainerLifecyclePtr = (*handle)->miopenContainer;
}
}
LOG_API_SUCCESS(apiName, "createdHandle={:p}", static_cast<void*>(*handle));
});
}
hipdnnPluginStatus_t hipdnnEnginePluginDestroy(hipdnnEnginePluginHandle_t handle)
{
LOG_API_ENTRY("handle={:p}", static_cast<void*>(handle));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(handle);
miopen_legacy_plugin::MiopenHandleFactory::destroyMiopenHandle(handle);
delete handle;
handle = nullptr;
LOG_API_SUCCESS(apiName, "", "");
});
}
hipdnnPluginStatus_t hipdnnEnginePluginSetStream(hipdnnEnginePluginHandle_t handle,
hipStream_t stream)
{
LOG_API_ENTRY(
"handle={:p}, stream_id={:p}", static_cast<void*>(handle), static_cast<void*>(stream));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(handle);
handle->setStream(stream);
LOG_API_SUCCESS(apiName, "", "");
});
}
hipdnnPluginStatus_t
hipdnnEnginePluginGetApplicableEngineIds(hipdnnEnginePluginHandle_t handle,
const hipdnnPluginConstData_t* opGraph,
int64_t* engineIds,
uint32_t maxEngines,
uint32_t* numEngines)
{
LOG_API_ENTRY("handle={:p}, opGraph={:p}, engineIds={:p}, maxEngines={}, numEngines={:p}",
static_cast<void*>(handle),
static_cast<const void*>(opGraph),
static_cast<void*>(engineIds),
maxEngines,
static_cast<void*>(numEngines));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(handle);
throwIfNull(opGraph);
if(maxEngines != 0)
{
throwIfNull(engineIds);
}
throwIfNull(numEngines);
auto& engineManager = handle->getEngineManager();
GraphWrapper opGraphWrapper(opGraph->ptr, opGraph->size);
auto applicableEngines = engineManager.getApplicableEngineIds(*handle, opGraphWrapper);
*numEngines = 0;
for(auto& engineId : applicableEngines)
{
if(*numEngines == maxEngines)
{
*numEngines = static_cast<uint32_t>(applicableEngines.size());
HIPDNN_LOG_INFO("Maximum number of engines reached ({}), ignoring additional "
"engines, numEngines count: {}",
maxEngines,
*numEngines);
break;
}
engineIds[*numEngines] = engineId;
(*numEngines)++;
}
LOG_API_SUCCESS(apiName, "numEngines={}", *numEngines);
});
}
hipdnnPluginStatus_t hipdnnEnginePluginGetEngineDetails(hipdnnEnginePluginHandle_t handle,
int64_t engineId,
const hipdnnPluginConstData_t* opGraph,
hipdnnPluginConstData_t* engineDetails)
{
LOG_API_ENTRY("handle={:p}, engineId={}, opGraph={:p}, engineDetails={:p}",
static_cast<void*>(handle),
engineId,
static_cast<const void*>(opGraph),
static_cast<void*>(engineDetails));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(handle);
throwIfNull(opGraph);
throwIfNull(engineDetails);
auto& engineManager = handle->getEngineManager();
GraphWrapper opGraphWrapper(opGraph->ptr, opGraph->size);
engineManager.getEngineDetails(*handle, opGraphWrapper, engineId, *engineDetails);
LOG_API_SUCCESS(apiName, "engineDetails->ptr={:p}", engineDetails->ptr);
});
}
hipdnnPluginStatus_t hipdnnEnginePluginDestroyEngineDetails(hipdnnEnginePluginHandle_t handle,
hipdnnPluginConstData_t* engineDetails)
{
LOG_API_ENTRY("handle={:p}, engineDetails={}",
static_cast<void*>(handle),
static_cast<void*>(engineDetails));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(handle);
throwIfNull(engineDetails);
throwIfNull(engineDetails->ptr);
handle->removeEngineDetailsDetachedBuffer(engineDetails->ptr);
LOG_API_SUCCESS(apiName, "engineDetails->ptr={:p}", engineDetails->ptr);
});
}
hipdnnPluginStatus_t hipdnnEnginePluginGetWorkspaceSize(hipdnnEnginePluginHandle_t handle,
const hipdnnPluginConstData_t* engineConfig,
const hipdnnPluginConstData_t* opGraph,
size_t* workspaceSize)
{
LOG_API_ENTRY("handle={:p}, engineConfig={:p}, opGraph={:p}, workspaceSize={:p}",
static_cast<void*>(handle),
static_cast<const void*>(engineConfig),
static_cast<const void*>(opGraph),
static_cast<void*>(workspaceSize));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(handle);
throwIfNull(engineConfig);
throwIfNull(opGraph);
throwIfNull(workspaceSize);
auto& engineManager = handle->getEngineManager();
EngineConfigWrapper engineConfigWrapper(engineConfig->ptr, engineConfig->size);
GraphWrapper opGraphWrapper(opGraph->ptr, opGraph->size);
*workspaceSize = engineManager.getWorkspaceSize(
*handle, engineConfigWrapper.engineId(), opGraphWrapper);
LOG_API_SUCCESS(apiName, "workspaceSize={}", *workspaceSize);
});
}
hipdnnPluginStatus_t
hipdnnEnginePluginCreateExecutionContext(hipdnnEnginePluginHandle_t handle,
const hipdnnPluginConstData_t* engineConfig,
const hipdnnPluginConstData_t* opGraph,
hipdnnEnginePluginExecutionContext_t* executionContext)
{
LOG_API_ENTRY("handle={:p}, engineConfig={:p}, opGraph={:p}, executionContext={:p}",
static_cast<void*>(handle),
static_cast<const void*>(engineConfig),
static_cast<const void*>(opGraph),
static_cast<void*>(executionContext));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(handle);
throwIfNull(engineConfig);
throwIfNull(opGraph);
throwIfNull(executionContext);
GraphWrapper opGraphWrapper(opGraph->ptr, opGraph->size);
EngineConfigWrapper engineConfigWrapper(engineConfig->ptr, engineConfig->size);
auto& engineManager = handle->getEngineManager();
auto context = new HipdnnEnginePluginExecutionContext;
try
{
engineManager.initializeExecutionContext(
*handle, opGraphWrapper, engineConfigWrapper, *context);
}
catch(...)
{
delete context;
throw;
}
*executionContext = context;
LOG_API_SUCCESS(
apiName, "created_execution_context={:p}", static_cast<void*>(*executionContext));
});
}
hipdnnPluginStatus_t
hipdnnEnginePluginDestroyExecutionContext(hipdnnEnginePluginHandle_t handle,
hipdnnEnginePluginExecutionContext_t executionContext)
{
LOG_API_ENTRY("handle={:p}, executionContext={:p}",
static_cast<void*>(handle),
static_cast<void*>(executionContext));
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(handle);
throwIfNull(executionContext);
delete executionContext;
LOG_API_SUCCESS(apiName, "destroyed executionContext", "");
});
}
hipdnnPluginStatus_t
hipdnnEnginePluginExecuteOpGraph(hipdnnEnginePluginHandle_t handle,
hipdnnEnginePluginExecutionContext_t executionContext,
void* workspace,
const hipdnnPluginDeviceBuffer_t* deviceBuffers,
uint32_t numDeviceBuffers)
{
LOG_API_ENTRY("handle={:p}, executionContext={:p}, workspace={:p}, deviceBuffers={:p}, "
"numDeviceBuffers={}",
static_cast<void*>(handle),
static_cast<void*>(executionContext),
workspace,
static_cast<const void*>(deviceBuffers),
numDeviceBuffers);
return hipdnn_plugin::tryCatch([&, apiName = __func__]() {
throwIfNull(handle);
throwIfNull(executionContext);
throwIfNull(deviceBuffers);
executionContext->plan().execute(*handle, deviceBuffers, numDeviceBuffers, workspace);
LOG_API_SUCCESS(apiName, "executed graph", "");
});
}
} // extern "C"