ONE - On-device Neural Engine
Loading...
Searching...
No Matches
moco::onnx Namespace Reference

Data Structures

class  Constant_V1
 GraphBuilder for Constant(since version 1) node. More...
 
class  Constant_V9
 GraphBuilder for Constant(since version 9) node. More...
 
class  ConstantGraphBuilder
 GraphBuilder for Constant node. More...
 
class  Frontend
 
class  GraphBuilder
 Parent class of onnx operation graph builders. More...
 
class  GraphBuilderContext
 Class to store context to build IR from onnx. More...
 
class  GraphBuilderRegistry
 Class to return graph builder for passed onnx Operator. More...
 
class  Identity_V1
 GraphBuilder for Identity(since version 1) node. More...
 
class  IdentityGraphBuilder
 GraphBuilder for Identity node. More...
 
class  SymbolTable
 Class to store relations of Nodes and string names. More...
 

Functions

loco::DataType as_loco_datatype (const int32_t tensor_dtype)
 
std::string tensor_dtype_as_string (const int32_t tensor_dtype)
 
bool is_default_domain (const std::string domain)
 If domain is empty string or onnx.ai, it is default domain.
 
std::vector< float > get_float_data (const ::onnx::TensorProto &tensor)
 Get float tensor data.
 

Function Documentation

◆ as_loco_datatype()

loco::DataType moco::onnx::as_loco_datatype ( const int32_t  tensor_dtype)

Definition at line 28 of file Convert.cpp.

29{
30 switch (tensor_dtype)
31 {
32 case 0: // UNDEFINED
33 return loco::DataType::Unknown;
34 case 1: // FLOAT
35 return loco::DataType::FLOAT32;
36 case 2: // UINT8
37 return loco::DataType::U8;
38 case 3: // INT8
39 return loco::DataType::S8;
40 case 4: // UINT16
41 return loco::DataType::U16;
42 case 5: // INT16
43 return loco::DataType::S16;
44 case 6: // INT32
45 return loco::DataType::S32;
46 case 7: // INT64
47 return loco::DataType::S64;
48 case 10: // FLOAT16
49 return loco::DataType::FLOAT16;
50 case 11: // DOUBLE
51 return loco::DataType::FLOAT64;
52 case 12: // UINT32
53 return loco::DataType::U32;
54 case 13: // UINT64
55 return loco::DataType::U64;
56
57 case 8: // STRING
58 case 9: // BOOL
59 case 14: // COMPLEX64
60 case 15: // COMPLEX128
61 case 16: // BFLOAT16
62 default:
63 break;
64 }
65 throw std::runtime_error{"Unsupported onnx dtype"};
66}

Referenced by moco::onnx::Constant_V1::build(), and moco::onnx::Constant_V9::build().

◆ get_float_data()

std::vector< float > moco::onnx::get_float_data ( const ::onnx::TensorProto &  tensor)

Get float tensor data.

Parameters
[in]tensorTensor to get float data
Returns
Float vector which stores float tensor data

Definition at line 48 of file Onnxutil.cpp.

49{
50 std::vector<float> data;
51
52 // Exactly one of the fields is used to store the elements of the tensor
53 assert(!(tensor.has_raw_data() && (tensor.float_data_size() > 0)));
54 assert(tensor.has_raw_data() || (tensor.float_data_size() > 0));
55
56 if (tensor.has_raw_data())
57 {
58 const std::string raw_data = tensor.raw_data();
59
60 // If platform is big endian, we should convert data as big endian
61 if (!is_platform_little_endian())
62 {
63 // TODO Revise implementation of this logic. This is too complex.
64 const char *little_endian_bytes = raw_data.c_str();
65 char *big_endian_bytes = reinterpret_cast<char *>(std::malloc(raw_data.size()));
66
67 for (int i = 0; i < raw_data.size(); ++i)
68 big_endian_bytes[i] = little_endian_bytes[i];
69
70 const size_t element_size = sizeof(float);
71 const size_t num_elements = raw_data.size() / element_size;
72 for (size_t i = 0; i < num_elements; ++i)
73 {
74 char *start_byte = big_endian_bytes + i * element_size;
75 char *end_byte = start_byte + element_size - 1;
76
77 for (size_t count = 0; count < element_size / 2; ++count)
78 {
79 char temp = *start_byte;
80 *start_byte = *end_byte;
81 *end_byte = temp;
82 ++start_byte;
83 --end_byte;
84 }
85 }
86
87 data.insert(data.end(), reinterpret_cast<const float *>(big_endian_bytes),
88 reinterpret_cast<const float *>(big_endian_bytes + raw_data.size()));
89
90 std::free(big_endian_bytes);
91 }
92 else
93 {
94 const char *bytes = raw_data.c_str();
95 data.insert(data.end(), reinterpret_cast<const float *>(bytes),
96 reinterpret_cast<const float *>(bytes + raw_data.size()));
97 }
98 }
99 else
100 {
101 for (int i = 0; i < tensor.float_data_size(); ++i)
102 data.push_back(tensor.float_data(i));
103 }
104
105 return data;
106}

◆ is_default_domain()

bool moco::onnx::is_default_domain ( const std::string  domain)

If domain is empty string or onnx.ai, it is default domain.

Parameters
[in]domainThe name of domain
Returns
Whether it is default domain or not

Definition at line 43 of file Onnxutil.cpp.

44{
45 return (domain.compare("") == 0 || domain.compare("onnx.ai") == 0);
46}

◆ tensor_dtype_as_string()

std::string moco::onnx::tensor_dtype_as_string ( const int32_t  tensor_dtype)

Definition at line 68 of file Convert.cpp.

69{
70 switch (tensor_dtype)
71 {
72 case 0: // UNDEFINED
73 return "UNDEFINED";
74 case 1: // FLOAT
75 return "FLOAT";
76 case 2: // UINT8
77 return "UINT8";
78 case 3: // INT8
79 return "INT8";
80 case 4: // UINT16
81 return "UINT16";
82 case 5: // INT16
83 return "INT16";
84 case 6: // INT32
85 return "INT32";
86 case 7: // INT64
87 return "INT64";
88 case 8: // STRING
89 return "STRING";
90 case 9: // BOOL
91 return "BOOL";
92 case 10: // FLOAT16
93 return "FLOAT16";
94 case 11: // DOUBLE
95 return "DOUBLE";
96 case 12: // UINT32
97 return "UINT32";
98 case 13: // UINT64
99 return "UINT64";
100 case 14: // COMPLEX64
101 return "COMPLEX64";
102 case 15: // COMPLEX128
103 return "COMPLEX128";
104 case 16: // BFLOAT16
105 return "BFLOAT16";
106 default:
107 break;
108 }
109 throw std::runtime_error{"Unsupported onnx dtype"};
110}

Referenced by moco::onnx::Constant_V1::validate().