| 446 | } |
| 447 | |
| 448 | ErrorCode Session::updateToModel(Net* net) const { |
| 449 | if (mNeedResize) { |
| 450 | return NOT_SUPPORT; |
| 451 | } |
| 452 | int opSize = net->oplists()->size(); |
| 453 | for (int i = 0; i < opSize; ++i) { |
| 454 | auto op = net->oplists()->GetAs<Op>(i); |
| 455 | if (op->type() != OpType_Const && op->type() != OpType_TrainableParam) { |
| 456 | continue; |
| 457 | } |
| 458 | if (!op->outputIndexes() || op->outputIndexes()->size() != 1) { |
| 459 | continue; |
| 460 | } |
| 461 | auto index = op->outputIndexes()->data()[0]; |
| 462 | auto blob = op->main_as_Blob(); |
| 463 | if (blob->dataType() != DataType_DT_FLOAT) { |
| 464 | continue; |
| 465 | } |
| 466 | std::shared_ptr<Tensor> tensor = mInfo.allTensors[index]; |
| 467 | if (WrapExecution::needWrap(tensor.get(), nullptr)) { |
| 468 | tensor.reset(Tensor::createHostTensorFromDevice(tensor.get(), true)); |
| 469 | if (tensor.get() == nullptr) { |
| 470 | MNN_ERROR("failed to copy trained param from device to host\n"); |
| 471 | return INVALID_VALUE; |
| 472 | } |
| 473 | } |
| 474 | ::memcpy((void*)blob->float32s()->data(), tensor->host<float>(), tensor->size()); |
| 475 | } |
| 476 | |
| 477 | return NO_ERROR; |
| 478 | } |
| 479 | |
| 480 | static void initTensors(std::vector<std::shared_ptr<Tensor>>& tensors, |
| 481 | const std::vector<std::shared_ptr<Tensor>>& tensorSrc) { |