| 339 | } |
| 340 | |
| 341 | TinyNNStatus init_model_weights(CombineModel* combo_model) { |
| 342 | if (!combo_model) { |
| 343 | return TinyNN_ERROR_NULL_PTR; |
| 344 | } |
| 345 | //! init weights with the init function |
| 346 | if (combo_model->nr_device_model == 1) { |
| 347 | init_signle_device_model(combo_model); |
| 348 | } else if (is_likely_multi_device_model(combo_model)) { |
| 349 | init_multi_likely_device_model(combo_model); |
| 350 | } else { |
| 351 | //! compute the tensor memory ptr |
| 352 | for (int model_idx = 0; model_idx < combo_model->nr_device_model; ++model_idx) { |
| 353 | DeviceModel* model = combo_model->device_models[model_idx]; |
| 354 | //! run opr init function |
| 355 | int nr_instruction = model->nr_instruction; |
| 356 | LOG_DEBUG("execute weight preprocess\n"); |
| 357 | int total_processed_weights = |
| 358 | count_total_processed_weights_number(combo_model, model_idx); |
| 359 | LOG_DEBUG("calc total_processed_weights done\n"); |
| 360 | if (total_processed_weights < 1) { |
| 361 | continue; |
| 362 | } |
| 363 | //! allocate the processed memory |
| 364 | model->nr_processed_weight = total_processed_weights; |
| 365 | model->processed_weights = |
| 366 | tinynn_malloc(total_processed_weights * sizeof(Tensor)); |
| 367 | memset(model->processed_weights, 0, |
| 368 | total_processed_weights * sizeof(Tensor)); |
| 369 | |
| 370 | int processed_weights_index = 0; |
| 371 | for (int i = 0; i < nr_instruction; i++) { |
| 372 | Instruction* inst = model->instructions + i; |
| 373 | if (inst->tag == TinyNN_INST_OPR) { |
| 374 | Opr* opr = &inst->workload.opr; |
| 375 | int nr_weights = get_ins_nr_processed_weights(opr, model); |
| 376 | |
| 377 | if (nr_weights > 0) { |
| 378 | Tensor* new_weight = |
| 379 | model->processed_weights + processed_weights_index; |
| 380 | size_t weight_length = |
| 381 | get_opr_weights_process_size(opr, model, new_weight); |
| 382 | int size = 0; |
| 383 | LOG_DEBUG( |
| 384 | "opr symbol %s preprocess weight id %d " |
| 385 | "need " |
| 386 | "memory:%zu\n", |
| 387 | opr->type, i, weight_length); |
| 388 | new_weight->ptr = model->device.malloc(weight_length); |
| 389 | new_weight->size = weight_length; |
| 390 | new_weight->is_weight = 1; |
| 391 | new_weight->is_shared = 0; |
| 392 | processed_weights_index++; |
| 393 | |
| 394 | //! call the init function |
| 395 | LOG_DEBUG("opr symbol %s preprocess weights.\n", opr->type); |
| 396 | |
| 397 | int init_index = opr->init_func; |
| 398 | InitFunc init = init_kernels[init_index]; |