MCPcopy Create free account
hub / github.com/MegEngine/MegCC / parse_model

Function parse_model

runtime/src/parse.c:419–556  ·  view source on GitHub ↗

all resource are allocate here

Source from the content-addressed store, hash-verified

417
418//! all resource are allocate here
419TinyNNStatus parse_model(
420 void* buffer, size_t size, int is_own_buffer, CombineModel* model,
421 int share_weights) {
422 load_kernel_init_function();
423 ns(Model_table_t) fbs_model;
424 if (!(fbs_model = ns(Model_as_root(buffer)))) {
425 LOG_ERROR("Model not available\n");
426 return -1;
427 }
428 if (is_own_buffer) {
429 model->model_ptr = buffer;
430 model->model_len = size;
431 } else {
432 model->model_ptr = NULL;
433 model->model_len = 0;
434 }
435 //! name, model_id, device
436 model->name = get_string(MegCC_Model_name(fbs_model));
437 model->model_id = ns(Model_model_id(fbs_model));
438 model->const_shape = ns(Model_const_shape(fbs_model));
439 LOG_DEBUG(
440 "log model: %s, model id: %zu, const shape: %d\n", model->name,
441 model->model_id, model->const_shape);
442 //! host device used to alloc weight
443 memset(&(model->host_dev), 0, sizeof(Device));
444 model->host_dev.device_type = TinyNN_BARE_METAL;
445 TINYNN_ASSERT(init_device(&(model->host_dev)) == TinyNN_SUCCESS);
446
447 //! parse weight
448 ns(Weight_vec_t) fbs_weights = ns(Model_weight_pool(fbs_model));
449 int nr_weight = ns(Weight_vec_len(fbs_weights));
450 model->nr_origin_weight = nr_weight;
451 model->weights = tinynn_malloc(sizeof(Tensor) * nr_weight);
452 LOG_DEBUG("model weights number: %d\n", nr_weight);
453 memset(model->weights, 0, sizeof(Tensor) * nr_weight);
454 for (int i = 0; i < nr_weight; i++) {
455 LOG_DEBUG("parse weight id: %d\n", i);
456 ns(Weight_table_t) fbs_weight = ns(Weight_vec_at(fbs_weights, i));
457 Tensor* weight = model->weights + i;
458 if (parse_weight(weight, fbs_weight, &(model->host_dev)) != TinyNN_SUCCESS) {
459 LOG_ERROR("parse weight error!\n");
460 goto exit;
461 }
462 }
463
464 //! parse device model
465 ns(DeviceModel_vec_t) fbs_device_models = ns(Model_device_models(fbs_model));
466 int nr_model = ns(DeviceModel_vec_len(fbs_device_models));
467 int nr_valid_device_model = 0;
468 {
469 //! cal max memory for tensor and valid device model number
470 size_t max_tensor_size = 0;
471 for (int i = 0; i < nr_model; i++) {
472 ns(DeviceModel_table_t) fbs_device_model =
473 ns(DeviceModel_vec_at(fbs_device_models, i));
474 ns(Device_enum_t) fbs_device = ns(DeviceModel_device(fbs_device_model));
475 TinyNNDevice device_enum = device_from_fbs(fbs_device);
476 if (!valid_device_check(device_enum)) {

Callers 5

mainFunction · 0.85
import_mgbMethod · 0.85
LITE_load_model_from_memFunction · 0.85

Calls 12

get_stringFunction · 0.85
init_deviceFunction · 0.85
tinynn_mallocFunction · 0.85
parse_weightFunction · 0.85
device_from_fbsFunction · 0.85
valid_device_checkFunction · 0.85
create_runtime_optFunction · 0.85
parse_device_modelFunction · 0.85
tinynn_freeFunction · 0.85
freeMethod · 0.45

Tested by

no test coverage detected