| 435 | } |
| 436 | |
| 437 | TinyNNStatus init_model_memory(CombineModel* combo_model) { |
| 438 | if (!combo_model) { |
| 439 | return TinyNN_ERROR_NULL_PTR; |
| 440 | } |
| 441 | if (combo_model->have_init) { |
| 442 | return TinyNN_SUCCESS; |
| 443 | } |
| 444 | if (!combo_model->max_tensor_memroy->ptr) { |
| 445 | DeviceModel* model = |
| 446 | combo_model->device_models[combo_model->active_device_model_idx]; |
| 447 | combo_model->max_tensor_memroy->ptr = |
| 448 | model->device.malloc(combo_model->max_tensor_memroy->length_in_byte); |
| 449 | } |
| 450 | //! compute the tensor memory ptr |
| 451 | for (int model_idx = 0; model_idx < combo_model->nr_device_model; ++model_idx) { |
| 452 | DeviceModel* model = combo_model->device_models[model_idx]; |
| 453 | int8_t* tensor_memory = combo_model->max_tensor_memroy->ptr; |
| 454 | TINYNN_ASSERT(tensor_memory); |
| 455 | LOG_DEBUG("Init tensor by offset\n"); |
| 456 | for (int i = 0; i < model->nr_tensor; i++) { |
| 457 | Tensor* tensor = model->tensors + i; |
| 458 | TINYNN_ASSERT(tensor); |
| 459 | if (tensor->is_input) { |
| 460 | tensor->ptr = NULL; |
| 461 | LOG_DEBUG( |
| 462 | "Init model %d input tensor %s to %p\n", model_idx, |
| 463 | tensor->name, tensor->ptr); |
| 464 | } else if (!tensor->is_dynamic) { |
| 465 | tensor->ptr = tensor_memory + tensor->offset; |
| 466 | |
| 467 | LOG_DEBUG( |
| 468 | "Init model %d tensor %s by offset %zu to " |
| 469 | "%p\n", |
| 470 | model_idx, tensor->name, tensor->offset, tensor->ptr); |
| 471 | } else { |
| 472 | tensor->ptr = NULL; |
| 473 | tensor->offset = 0; |
| 474 | } |
| 475 | } |
| 476 | int nr_instruction = model->nr_instruction; |
| 477 | for (int i = 0; i < nr_instruction; i++) { |
| 478 | Instruction* inst = model->instructions + i; |
| 479 | if (inst->tag == TinyNN_INST_OPR) { |
| 480 | Opr* opr = &inst->workload.opr; |
| 481 | opr->workspace.ptr = tensor_memory + opr->workspace.offset; |
| 482 | } |
| 483 | } |
| 484 | } |
| 485 | combo_model->have_init = 1; |
| 486 | return TinyNN_SUCCESS; |
| 487 | } |
| 488 | |
| 489 | // vim: syntax=cpp.doxygen |
no test coverage detected