MCPcopy Create free account
hub / github.com/ROCm/AMDMIGraphX / parse_tensor

Method parse_tensor

src/onnx/onnx_parser.cpp:696–793  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

694}
695
696literal onnx_parser::parse_tensor(const onnx::TensorProto& t) const
697{
698 auto tensor_shape = parse_tensor_shape(t);
699 const auto& dims = tensor_shape.lens();
700 auto type = tensor_shape.type();
701 const auto& external_data = t.external_data();
702
703 if(not external_data.empty())
704 {
705 const std::string& data_file = external_data.at(0).value();
706 size_t num_data_fields = external_data.size();
707 size_t offset = 0;
708 size_t nbytes = tensor_shape.bytes();
709
710 if(num_data_fields > 1) // if offset field is present
711 {
712 offset = std::stoull(t.external_data().at(1).value());
713 }
714 if(num_data_fields > 2) // if nbytes field is present
715 {
716 nbytes = std::stoull(t.external_data().at(2).value());
717 }
718 std::vector<char> raw_buffer;
719 if(not external_data_path.empty())
720 {
721 raw_buffer = read_buffer(fs::path{external_data_path} / data_file, offset, nbytes);
722 }
723 else
724 {
725 raw_buffer = read_buffer(path / data_file, offset, nbytes);
726 }
727 std::string s(raw_buffer.begin(), raw_buffer.end());
728 return create_literal(type, dims, s.data());
729 }
730
731 if(t.has_raw_data())
732 {
733 const std::string& s = t.raw_data();
734 return create_literal(type, dims, s.data());
735 }
736
737 switch(t.data_type())
738 {
739 case onnx::TensorProto::BOOL: return create_literal(shape::bool_type, dims, t.int32_data());
740
741 // INT4 or UINT4 operate as 8-bit buffers:
742 case onnx::TensorProto::INT4: return create_literal(shape::int8_type, dims, t.int32_data());
743 case onnx::TensorProto::UINT4: return create_literal(shape::uint8_type, dims, t.int32_data());
744
745 case onnx::TensorProto::INT8: return create_literal(shape::int8_type, dims, t.int32_data());
746 case onnx::TensorProto::UINT8: return create_literal(shape::uint8_type, dims, t.int32_data());
747
748 case onnx::TensorProto::INT16: return create_literal(shape::int16_type, dims, t.int32_data());
749 case onnx::TensorProto::UINT16: return create_literal(shape::uint16_type, dims, t.int32_data());
750
751 case onnx::TensorProto::INT32: return create_literal(shape::int32_type, dims, t.int32_data());
752 case onnx::TensorProto::UINT32:
753 return create_literal(shape::uint32_type, dims, t.uint64_data());

Callers 1

parse_intializerFunction · 0.45

Calls 14

parse_tensor_shapeFunction · 0.85
read_bufferFunction · 0.85
lensMethod · 0.80
atMethod · 0.80
create_literalFunction · 0.70
transformFunction · 0.50
typeMethod · 0.45
emptyMethod · 0.45
valueMethod · 0.45
sizeMethod · 0.45
bytesMethod · 0.45
beginMethod · 0.45

Tested by

no test coverage detected