-
Notifications
You must be signed in to change notification settings - Fork 106
/
Copy pathcontext.cpp
406 lines (368 loc) · 15.7 KB
/
context.cpp
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
/*************************************************************************
* Copyright (C) [2022] by Cambricon, Inc.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*************************************************************************/
#include "cstring"
#include "core/context.h"
#include "core/logging.h"
#include "core/mlu_env.h"
#include "core/runtime/device.h"
#include "core/tensor.h"
#include "core/tool.h"
#include "kernels/kernel.h"
#define DEP_CHECK_LOG(level) \
mluop::logging::LogMessage(__FILE__, __LINE__, 4, level, "MLUOP", true, \
true, true, true) \
.stream()
namespace mluop {
// see cnrt_function.c deviceCoreVersion for more info.
static struct deviceName name_list_table[] = {
{"MLU270", MLUOP_MLU270},
{"MLU290", MLUOP_MLU290},
{"MLU370", MLUOP_MLU370},
{"MLU500", MLUOP_MLU590}
// {"MLU100", MLUOP_MLU100}, // mluop not support mlu100 only for error
// case.
};
// once cnrtGetDeviceProperties() update and not use
// device_ordinal, update this funciton.
mluOpDevType_t convertDeviceName(char *name) {
struct deviceName *pName = NULL;
int num = sizeof(name_list_table) / sizeof(struct deviceName);
if (CONTEXT_DEVICENAME_LEAST_SIZE > strlen(name)) {
LOG(ERROR)
<< "get device name failed. device name too short. device name = "
<< name << "\n";
return MLUOP_UNKNOWN_DEVICE;
}
for (int i = 0; i < num; i++) {
pName = &name_list_table[i];
if (0 == strncmp(pName->name, name, strlen(pName->name)) ||
(i == num - 1 &&
0 >= strncmp(pName->name, name, CONTEXT_DEVICENAME_LEAST_SIZE))) {
return pName->type;
}
}
LOG(ERROR) << "get device name failed. return unknown device. device name = "
<< name << "\n";
return MLUOP_UNKNOWN_DEVICE;
}
} // namespace mluop
mluOpStatus_t mluOpCheckDependency(bool need_check_min, bool need_check_max,
DepCheckLevel level) {
if (!IF_CHECK_DEP_VERSION) {
VLOG(5) << "Skip check version dependency.";
return MLUOP_STATUS_SUCCESS;
}
int mluop_major = 0, mluop_minor = 0, mluop_patch = 0;
mluOpGetLibVersion(&mluop_major, &mluop_minor, &mluop_patch);
int cnrt_major = 0, cnrt_minor = 0, cnrt_patch = 0;
cnrtGetLibVersion(&cnrt_major, &cnrt_minor, &cnrt_patch);
int cndrv_major = 0, cndrv_minor = 0, cndrv_patch = 0;
cnGetLibVersion(&cndrv_major, &cndrv_minor, &cndrv_patch);
bool cnrt_max_check = false;
bool cnrt_min_check = false;
bool cndrv_max_check = false;
bool cndrv_min_check = false;
if (need_check_min) {
cnrt_min_check = (cnrt_major > MLUOP_DEP_CNRT_MIN_MAJOR) ||
(cnrt_major == MLUOP_DEP_CNRT_MIN_MAJOR &&
cnrt_minor >= MLUOP_DEP_CNRT_MIN_MINOR);
if (!cnrt_min_check) {
DEP_CHECK_LOG(level) << "Current CNRT version: " << cnrt_major << "."
<< cnrt_minor << "." << cnrt_patch;
DEP_CHECK_LOG(level) << "CNRT version is too low, please upgrade CNRT to "
<< MLUOP_DEP_CNRT_MIN_MAJOR << "."
<< MLUOP_DEP_CNRT_MIN_MINOR << "."
<< MLUOP_DEP_CNRT_MIN_PATCH
<< " or higher. For more details, "
<< "please check the dependency rules in "
"Cambricon-MLUOP-Release-Notes.";
}
cndrv_min_check = (cndrv_major > MLUOP_DEP_CNDRV_MIN_MAJOR) ||
(cndrv_major == MLUOP_DEP_CNDRV_MIN_MAJOR &&
cndrv_minor >= MLUOP_DEP_CNDRV_MIN_MINOR);
if (!cndrv_min_check) {
DEP_CHECK_LOG(level) << "Current CNDRV version: " << cndrv_major << "."
<< cndrv_minor << "." << cndrv_patch;
DEP_CHECK_LOG(level)
<< "CNDRV version is too low, please upgrade CNDRV to "
<< MLUOP_DEP_CNDRV_MIN_MAJOR << "." << MLUOP_DEP_CNDRV_MIN_MINOR
<< "." << MLUOP_DEP_CNDRV_MIN_PATCH
<< " or higher. For more details, "
<< "please check the dependency rules in "
"Cambricon-MLUOP-Release-Notes.";
}
if (((!cnrt_min_check) || (!cndrv_min_check)) && level == ERROR) {
return MLUOP_STATUS_NOT_INITIALIZED;
}
}
if (need_check_max) {
cnrt_max_check = (cnrt_major < MLUOP_DEP_CNRT_MAX_MAJOR) ||
(cnrt_major == MLUOP_DEP_CNRT_MAX_MAJOR &&
cnrt_minor <= MLUOP_DEP_CNRT_MAX_MINOR);
if (!cnrt_max_check) {
DEP_CHECK_LOG(level) << "Current CNRT version: " << cnrt_major << "."
<< cnrt_minor << "." << cnrt_patch;
DEP_CHECK_LOG(level)
<< "CNRT version is too high, please downgrade CNRT to "
<< MLUOP_DEP_CNRT_MAX_MAJOR << "." << MLUOP_DEP_CNRT_MAX_MINOR << "."
<< MLUOP_DEP_CNRT_MAX_PATCH << " or higher. For more details, "
<< "please check the dependency rules in "
"Cambricon-MLUOP-Release-Notes.";
}
cndrv_max_check = (cndrv_major < MLUOP_DEP_CNDRV_MAX_MAJOR) ||
(cndrv_major == MLUOP_DEP_CNDRV_MAX_MAJOR &&
cndrv_minor <= MLUOP_DEP_CNDRV_MAX_MINOR);
if (!cndrv_max_check) {
DEP_CHECK_LOG(level) << "Current CNDRV version: " << cndrv_major << "."
<< cndrv_minor << "." << cndrv_patch;
DEP_CHECK_LOG(level)
<< "CNDRV version is too high, please downgrade CNDRV to "
<< MLUOP_DEP_CNDRV_MAX_MAJOR << "." << MLUOP_DEP_CNDRV_MAX_MINOR
<< "." << MLUOP_DEP_CNDRV_MAX_PATCH
<< " or higher. For more details, "
<< "please check the dependency rules in "
"Cambricon-MLUOP-Release-Notes.";
}
if (((!cnrt_max_check) || (!cndrv_max_check)) && level == ERROR) {
return MLUOP_STATUS_NOT_INITIALIZED;
}
}
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API mluOpCreate(mluOpHandle_t *handle) {
PARAM_CHECK("[mluOpCreate]", handle != NULL);
if (MLUOP_STATUS_SUCCESS != mluOpCheckDependency(true, false, ERROR)) {
LOG(ERROR) << "Check version dependency failed in mluOpCreate function.";
return MLUOP_STATUS_NOT_INITIALIZED;
}
CNdev mlu_dev;
int32_t cluster_num = 0;
int32_t core_num_per_cluster = 0;
int32_t nram_size = 0;
int32_t wram_size = 0;
int32_t sram_size = 0;
char device_name[CONTEXT_DEVICENAME_BUFFER_SIZE] = "";
mluOpContext *ctx = new (std::nothrow) mluOpContext();
CNcontext drv_ctx;
CNctxConfigParam ctx_conf_param;
INTERNAL_CHECK("[mluOpCreate]", CN_SUCCESS == cnCtxGetCurrent(&drv_ctx));
INTERNAL_CHECK("[mluOpCreate]", CN_SUCCESS == cnCtxGetDevice(&mlu_dev));
INTERNAL_CHECK("[mluOpCreate]",
CN_SUCCESS == cnSharedContextAcquire(&drv_ctx, mlu_dev));
INTERNAL_CHECK(
"[mluOpCreate]",
CN_SUCCESS == cnDeviceGetAttribute(&cluster_num,
CN_DEVICE_ATTRIBUTE_MAX_CLUSTER_COUNT,
mlu_dev));
INTERNAL_CHECK(
"[mluOpCreate]",
CN_SUCCESS ==
cnDeviceGetAttribute(&core_num_per_cluster,
CN_DEVICE_ATTRIBUTE_MAX_CORE_COUNT_PER_CLUSTER,
mlu_dev));
INTERNAL_CHECK(
"[mluOpCreate]",
CN_SUCCESS == cnDeviceGetAttribute(&nram_size,
CN_DEVICE_ATTRIBUTE_NRAM_SIZE_PER_CORE,
mlu_dev));
INTERNAL_CHECK(
"[mluOpCreate]",
CN_SUCCESS == cnDeviceGetAttribute(
&wram_size,
CN_DEVICE_ATTRIBUTE_WEIGHT_RAM_SIZE_PER_CORE, mlu_dev));
INTERNAL_CHECK(
"[mluOpCreate]",
CN_SUCCESS == cnDeviceGetAttribute(
&sram_size,
CN_DEVICE_ATTRIBUTE_MAX_SHARED_RAM_SIZE_PER_CLUSTER,
mlu_dev));
INTERNAL_CHECK(
"[mluOpCreate]",
CN_SUCCESS == cnDeviceGetName(device_name, CONTEXT_DEVICENAME_BUFFER_SIZE,
mlu_dev));
// ClusterLimitCapability and JobLimitCapability
INTERNAL_CHECK("[mluOpCreate]",
CN_SUCCESS == cnGetCtxConfigParam(
drv_ctx, CN_CTX_CONFIG_VISIBLE_CLUSTER_NUM,
&ctx_conf_param));
ctx->capability_cluster_num = (int32_t)ctx_conf_param.visibleClusterNumber;
INTERNAL_CHECK(
"[mluOpCreate]",
CN_SUCCESS == cnGetCtxConfigParam(drv_ctx, CN_CTX_CONFIG_UNION_LIMIT,
&ctx_conf_param));
// Set parallel job num
if (MLUOP_STATUS_SUCCESS != ctx->initJobNum(drv_ctx, "[mluOpCreate]")) {
return MLUOP_STATUS_INTERNAL_ERROR;
}
ctx->capability_job_limit = (int32_t)ctx_conf_param.unionLimit;
ctx->arch = mluop::convertDeviceName(
device_name); // warning: possible return unknown.
ctx->sram_size = sram_size - REM_FOR_STACK;
ctx->device = mlu_dev;
ctx->cluster_num = cluster_num;
ctx->core_num_per_cluster = core_num_per_cluster;
ctx->nram_size = nram_size - REM_FOR_STACK;
if (ctx->arch == 290) {
#ifdef CONV_WARM_UP
ctx->wram_size = wram_size - 8 * 1024;
#else
ctx->wram_size = wram_size;
#endif
} else {
ctx->wram_size = wram_size;
}
if (ctx->arch < 322) {
ctx->round_mode = MLUOP_ROUND_HALF_OFF_ZERO;
} else {
// round to even is supported by hardware since arch >= 322.
// default usage mode is round to even when arch >= 322,
// which change accuracy because of using round off zero before.
ctx->round_mode = MLUOP_ROUND_HALF_TO_EVEN;
}
ctx->atomics_mode =
MLUOP_ATOMICS_NOT_ALLOWED; // note: mluop disallows atomics by defalut.
*handle = ctx;
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API
mluOpUpdateContextInformation(mluOpHandle_t handle) {
PARAM_CHECK("[mluOpUpdateContextInformation]", handle != NULL);
CNctxConfigParam ctx_conf_param;
CNcontext drv_ctx;
INTERNAL_CHECK(
"[mluOpUpdateContextInformation]",
CN_SUCCESS == cnQueueGetContext((CNqueue)(handle->queue), &drv_ctx));
INTERNAL_CHECK("[mluOpUpdateContextInformation]",
CN_SUCCESS == cnGetCtxConfigParam(
drv_ctx, CN_CTX_CONFIG_VISIBLE_CLUSTER_NUM,
&ctx_conf_param));
handle->capability_cluster_num = (int32_t)ctx_conf_param.visibleClusterNumber;
INTERNAL_CHECK(
"[mluOpUpdateContextInformation]",
CN_SUCCESS == cnGetCtxConfigParam(drv_ctx, CN_CTX_CONFIG_UNION_LIMIT,
&ctx_conf_param));
#if TARGET_MLU_ARCH == 520 // run 3225/5223 using 3226
handle->capability_job_limit = CN_KERNEL_CLASS_BLOCK;
#else
handle->capability_job_limit = (int32_t)ctx_conf_param.unionLimit;
#endif
if (MLUOP_STATUS_SUCCESS !=
handle->initJobNum(drv_ctx, "[mluOpUpdateContextInformation]")) {
return MLUOP_STATUS_INTERNAL_ERROR;
}
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API
mluOpSetAtomicsMode(mluOpHandle_t handle, mluOpAtomicsMode_t atomics_mode) {
PARAM_CHECK("[mluOpSetAtomicsMode]", handle != NULL);
handle->atomics_mode = atomics_mode;
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API
mluOpGetAtomicsMode(mluOpHandle_t handle, mluOpAtomicsMode_t *atomics_mode) {
PARAM_CHECK("[mluOpGetAtomicsMode]", handle != NULL);
PARAM_CHECK("[mluOpGetAtomicsMode]", atomics_mode != NULL);
*atomics_mode = handle->atomics_mode;
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API mluOpDestroy(mluOpHandle_t handle) {
PARAM_CHECK("[mluOpDestroy]", handle != NULL);
delete handle;
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API mluOpSetQueue(mluOpHandle_t handle,
cnrtQueue_t queue) {
PARAM_CHECK("[mluOpSetQueue]", handle != NULL);
// note, queue could be NULL
handle->queue = queue;
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API mluOpGetQueue(mluOpHandle_t handle,
cnrtQueue_t *queue) {
PARAM_CHECK("[mluOpGetQueue]", handle != NULL);
PARAM_CHECK("[mluOpGetQueue]", queue != NULL);
*queue = handle->queue;
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API mluOpGetDevice(mluOpHandle_t handle,
CNdev *device) {
PARAM_CHECK("[mluOpGetDevice]", handle != NULL);
PARAM_CHECK("[mluOpGetDevice]", device != NULL);
*device = handle->device;
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API mluOpSetQuantizeRoundMode(
mluOpHandle_t handle, mluOpQuantizeRoundMode_t round_mode) {
PARAM_CHECK("[mluOpSetQuantizeRoundMode]", handle != NULL);
PARAM_CHECK("[mluOpSetQuantizeRoundMode]",
round_mode == MLUOP_ROUND_HALF_TO_EVEN ||
round_mode == MLUOP_ROUND_HALF_OFF_ZERO ||
round_mode == MLUOP_ROUND_HALF_UP);
if (handle->arch < 322) {
if (round_mode == MLUOP_ROUND_HALF_TO_EVEN) {
LOG(ERROR)
<< "[mluOpSetQuantizeRoundMode] Unsupported rounding mode on MLU200";
return MLUOP_STATUS_BAD_PARAM;
}
}
handle->round_mode = round_mode;
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API mluOpGetQuantizeRoundMode(
mluOpHandle_t handle, mluOpQuantizeRoundMode_t *round_mode) {
PARAM_CHECK("[mluOpGetQuantizeRoundMode]", handle != NULL);
PARAM_CHECK("[mluOpGetQuantizeRoundMode]", round_mode != NULL);
*round_mode = handle->round_mode;
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API mluOpGetReservedMemSize(uint64_t *mem_size) {
PARAM_CHECK("[mluOpGetReservedMemSize]", mem_size != NULL);
uint64_t default_reserved_size = 2081ULL * 1024 * 1024;
uint64_t env_size =
mluop::getUintEnvVar("MLUOP_MEM_POOL_SIZE", default_reserved_size);
*mem_size = static_cast<uint64_t>(env_size);
return MLUOP_STATUS_SUCCESS;
}
mluOpStatus_t MLUOP_WIN_API mluOpGetContextParam(mluOpHandle_t handle,
CNctxConfigParamType type,
CNctxConfigParam *param) {
PARAM_CHECK("[mluOpGetContextParam]", handle != NULL);
PARAM_CHECK("[mluOpGetContextParam]", param != NULL);
PARAM_CHECK("[mluOpGetContextParam]",
type == CN_CTX_CONFIG_VISIBLE_CLUSTER_NUM ||
type == CN_CTX_CONFIG_UNION_LIMIT);
if (type == CN_CTX_CONFIG_VISIBLE_CLUSTER_NUM) {
param->visibleClusterNumber = handle->capability_cluster_num;
} else if (type == CN_CTX_CONFIG_UNION_LIMIT) {
param->unionLimit = static_cast<KernelClass>(handle->capability_job_limit);
} else {
LOG(ERROR) << "[mluOpGetContextParam] Unsupported type";
return MLUOP_STATUS_BAD_PARAM;
}
return MLUOP_STATUS_SUCCESS;
}
void MLUOP_WIN_API mluOpGetLibVersion(int *major, int *minor, int *patch) {
*major = MLUOP_MAJOR;
*minor = MLUOP_MINOR;
*patch = MLUOP_PATCHLEVEL;
}