MCPcopy Create free account
hub / github.com/OAID/Tengine / LoadTensor

Method LoadTensor

serializer/tengine/v2/tm2_serializer.cpp:531–610  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

529}
530
531bool TmSerializer2::LoadTensor(StaticGraph* graph, const TM2_Tensor* tm_tensor, const TM2_Buffer* tm_buf,
532 void* mmap_buf)
533{
534 /* Set the tensor name */
535 int idx = tm_tensor->tensor_id;
536 std::string tm_tensor_name;
537 if(tm_tensor->offset_s_tname == TM2_NOT_SET)
538 tm_tensor_name = "tensor_" + std::to_string(idx);
539 else
540 {
541 const TM2_String* tm_str = GetTmPtr<TM2_String>(mmap_buf, tm_tensor->offset_s_tname);
542 tm_tensor_name.assign(GetTmPtr<char>(mmap_buf, tm_str->offset_data), tm_str->size - 1);
543 }
544
545 /* Create the static tensor */
546 StaticTensor* tensor;
547 if(tm_tensor->type == kConstTensor)
548 tensor = CreateStaticConstTensor(graph, tm_tensor_name);
549 else
550 tensor = CreateStaticTensor(graph, tm_tensor_name);
551 if(!tensor)
552 {
553 LOG_ERROR() << "Create static const tensor failed: " << tm_tensor_name << "\n";
554 return false;
555 }
556
557 /* Set the dims */
558 if(tm_tensor->offset_vd_dims != TM2_NOT_SET)
559 {
560 const TM2_Vector_dims* v_dims = GetTmPtr<TM2_Vector_dims>(mmap_buf, tm_tensor->offset_vd_dims);
561 if(!v_dims || !(v_dims->v_num))
562 {
563 LOG_ERROR() << "Get tensor dims failed\n";
564 return false;
565 }
566 std::vector<int> dims;
567 for(unsigned int i = 0; i < v_dims->v_num; i++)
568 dims.push_back(v_dims->dims[i]);
569 SetTensorDim(tensor, dims);
570 }
571
572 /* Set the tensor type and the data type */
573 SetTensorType(tensor, tm_tensor->type);
574 SetTensorDataType(tensor, tm_tensor->data_type);
575
576 /* Set the memory size and pointer */
577 if(tm_tensor->type == kConstTensor)
578 {
579 SetTensorSize(tensor, tm_buf->size);
580 void* buf = malloc(tm_buf->size + 128);
581 if(tm_buf->offset_data != TM2_NOT_SET)
582 {
583 memcpy(buf, GetTmPtr<void>(mmap_buf, tm_buf->offset_data), tm_buf->size);
584 }
585
586 SetConstTensorBuffer(tensor, buf);
587 SetConstTensorFileLocation(tensor, -1, 0);
588 }

Callers

nothing calls this directly

Calls 8

CreateStaticConstTensorFunction · 0.85
CreateStaticTensorFunction · 0.85
SetTensorDimFunction · 0.85
SetTensorTypeFunction · 0.85
SetTensorDataTypeFunction · 0.85
SetTensorSizeFunction · 0.85
SetConstTensorBufferFunction · 0.85

Tested by

no test coverage detected