MCPcopy Create free account
hub / github.com/MegEngine/MegEngine / dump_tensor

Method dump_tensor

src/serialization/impl/serializer_oss_v2.cpp:403–470  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

401}
402
403void GraphDumperOSSV2::dump_tensor(
404 const std::string& name, const HostTensorND& tensor, TensorWriteMethod method,
405 TensorFormat format) {
406 using namespace flatbuffers;
407 using Meth = TensorWriteMethod;
408 mgb_assert(
409 (method == Meth::VALUE_ANONYMOUS) ^ (!name.empty()),
410 "name must be non-empty for non Meth::VALUE_ANONYMOUS tensors");
411
412 bool has_value = method != Meth::META_INPUT;
413 bool should_keep_name = true;
414 switch (method) {
415 case Meth::VALUE_ANONYMOUS:
416 should_keep_name = false;
417 break;
418 case Meth::VALUE_SHARED:
419 should_keep_name = m_config.keep_param_name;
420 ++m_nr_shared_tensor;
421 if (m_config.keep_param_name) {
422 mgb_assert(
423 m_used_param_names.insert(name).second,
424 "duplicated VALUE_SHARED tensor name: %s", name.c_str());
425 m_cur_rst.params.emplace_back(name);
426 }
427 break;
428 case Meth::META_INPUT:
429 case Meth::VALUE_INPUT:
430 mgb_assert(!name.empty(), "empty input tensor name");
431 mgb_assert(
432 m_used_input_names.insert(name).second,
433 "duplicated input tensor name: %s", name.c_str());
434 m_cur_rst.inputs.emplace_back(name);
435 break;
436 }
437
438 auto& layout = tensor.layout();
439 flatbuffers::Offset<flatbuffers::Vector<uint8_t>> data;
440 if (has_value) {
441 check_tensor_value_valid(name, tensor);
442 auto&& dumper = m_config.tensor_value_dumper;
443 if (dumper) {
444 std::vector<uint8_t> out_vec;
445 auto temp_out_file = OutputFile::make_vector_proxy(&out_vec);
446 dumper(*temp_out_file, *m_cur_opr, tensor);
447 data = m_builder.CreateVector(
448 reinterpret_cast<uint8_t*>(out_vec.data()), out_vec.size());
449 m_cur_rst.tensor_value_bytes += out_vec.size();
450 } else {
451 data = m_builder.CreateVector(
452 reinterpret_cast<uint8_t*>(tensor.raw_ptr()),
453 layout.span().high_byte);
454 m_cur_rst.tensor_value_bytes += layout.span().high_byte;
455 }
456 }
457
458 auto fbname = should_keep_name ? m_builder.CreateSharedString(name) : 0;
459 auto fshape = m_builder.CreateVectorScalarCast<uint32_t>(layout.shape, layout.ndim);
460 auto fcomp_node = fbs::v2::CreateCompNode(

Callers

nothing calls this directly

Calls 14

check_tensor_value_validFunction · 0.85
build_dtypeFunction · 0.85
CreateTensorClass · 0.85
emplace_backMethod · 0.80
spanMethod · 0.80
to_string_logicalMethod · 0.80
emptyMethod · 0.45
insertMethod · 0.45
layoutMethod · 0.45
dataMethod · 0.45
sizeMethod · 0.45

Tested by

no test coverage detected