46 CircleTensorInfo() =
default;
49 void name(
const std::string &name) { _name =
name; }
50 const std::string &
name(
void)
const {
return _name; }
53 const circle::TensorType &
dtype(
void)
const {
return _dtype; }
54 void dtype(
const circle::TensorType &dtype) { _dtype =
dtype; }
75 bool is_variable(
void)
const {
return _is_variable; }
81 circle::TensorType _dtype{circle::TensorType_FLOAT32};
90 bool _is_variable =
false;
93class CircleTensorContext
96 CircleTensorContext() =
default;
99 void emplace_back(CircleTensorInfo &
ti)
101 assert(_names.find(
ti.name()) == _names.end());
102 _tis.emplace_back(
ti);
103 _names.insert(
ti.name());
105 size_t size(
void)
const {
return _tis.size(); }
106 std::vector<CircleTensorInfo>::iterator
begin(
void) {
return _tis.begin(); }
107 std::vector<CircleTensorInfo>::iterator
end(
void) {
return _tis.end(); }
110 bool exist(
const std::string &name)
const {
return _names.find(name) != _names.end(); }
113 std::vector<CircleTensorInfo> _tis;
114 std::set<std::string> _names;
170 MultiOutputDetector(CircleTensorContext &ctx) : _ctx(ctx) {}
176 assert(outs.size() == count);
178 for (
auto out : outs)
201 if (node->merge_outputs())
276 CircleTensorContext &_ctx;
282 throw std::runtime_error(
"allocateCIrcleTensor Failed : node is nullptr");
303 MultiOutputDetector d(ctx);
319 assert(shape.
_rank_known &&
"unknown number of dimensions is not supported");
332 assert(shape.
_rank_known &&
"unknown number of dimensions is not supported");
336 if (shape.
_dims.at(
i) == -1)
337 return builder.CreateVector(shape.
_dims);
339 return flatbuffers::Offset<Vector<int32_t>>();
347template <
typename NodeT>
354template <loco::DataType DT>
355flatbuffers::Offset<circle::Buffer>
360 std::vector<NativeType> raw_data;
362 raw_data.reserve(
size);
365 raw_data.push_back(
c->at<
DT>(
i));
380 return circle::CreateBuffer(builder, 0 , 1 , 1 );
393flatbuffers::Offset<circle::Buffer>
397 const uint32_t count =
c->size<loco::DataType::STRING>();
401 auto &value =
c->at<loco::DataType::STRING>(
i);
409 std::vector<uint8_t> raw_data;
412 auto *
i32d =
reinterpret_cast<int32_t *
>(raw_data.data());
413 int32_t start =
sizeof(int32_t) * (count + 2);
415 std::vector<int32_t> offsets;
419 offsets.push_back(start);
422 auto &value =
c->at<loco::DataType::STRING>(
i);
425 offsets.push_back(
offset);
431 int32_t length = offsets[
i + 1] - offsets[
i];
432 auto &value =
c->at<loco::DataType::STRING>(
i);
441template <loco::DataType DT>
442flatbuffers::Offset<circle::Buffer>
447 std::vector<uint8_t> raw_data(
raw_size);
453 raw_data[
i] =
data & 0x0f;
458 raw_data[
i] |=
data << 4;
472 case loco::DataType::FLOAT32:
474 case loco::DataType::S4:
476 case loco::DataType::S8:
478 case loco::DataType::S16:
480 case loco::DataType::S32:
482 case loco::DataType::S64:
484 case loco::DataType::U4:
486 case loco::DataType::U8:
488 case loco::DataType::BOOL:
490 case loco::DataType::STRING:
505flatbuffers::Offset<circle::QuantizationParameters>
509 if (quantparam ==
nullptr and mx_quantparam ==
nullptr)
514 if (quantparam !=
nullptr)
515 throw std::runtime_error(
"Affine quantparam can not exist with MX quantparam.");
517 auto mx_quantize = circle::CreateMXQuantization(builder, mx_quantparam->
axis);
520 return circle::CreateQuantizationParameters(
521 builder, 0 , 0 , 0 , 0 ,
522 circle::QuantizationDetails::QuantizationDetails_MXQuantization,
mx_quantize.Union(),
526 flatbuffers::Offset<flatbuffers::Vector<float>> min;
527 flatbuffers::Offset<flatbuffers::Vector<float>> max;
528 flatbuffers::Offset<flatbuffers::Vector<float>>
scale;
529 flatbuffers::Offset<flatbuffers::Vector<int64_t>> zero_point;
530 if (quantparam->
min.size() && quantparam->
max.size())
532 min = builder.CreateVector(quantparam->
min);
533 max = builder.CreateVector(quantparam->
max);
535 if (quantparam->
scale.size() && quantparam->
zerop.size())
537 scale = builder.CreateVector(quantparam->
scale);
538 zero_point = builder.CreateVector(quantparam->
zerop);
541 return circle::CreateQuantizationParameters(builder, min, max, scale, zero_point,
542 circle::QuantizationDetails::QuantizationDetails_NONE,
546flatbuffers::Offset<circle::SparsityParameters>
549 if (sparsityparam ==
nullptr)
552 std::vector<flatbuffers::Offset<circle::DimensionMetadata>>
dim_metadata_vec;
564 auto dim_metadata = circle::CreateDimensionMetadata(
570 return circle::CreateSparsityParametersDirect(builder, &sparsityparam->
traversal_order,
576 assert(lhs->dtype() ==
DT);
577 assert(rhs->dtype() ==
DT);
588 if (lhs->dtype() != rhs->dtype())
591 if (lhs->rank() != rhs->rank())
595 if (!(lhs->dim(
i) == rhs->dim(
i)))
598 switch (lhs->dtype())
600 case loco::DataType::FLOAT32:
603 case loco::DataType::S4:
606 case loco::DataType::S8:
609 case loco::DataType::S16:
612 case loco::DataType::S32:
615 case loco::DataType::S64:
618 case loco::DataType::U4:
621 case loco::DataType::U8:
624 case loco::DataType::BOOL:
639 for (
auto key_value : md._cached_buffer_id)
675 if (
info.shape_status() == ShapeStatus::VALID)
687 auto name_offset = builder.CreateString(
info.name());
722 auto nodes = g->nodes();
740 exportOpDefinedTensor(
tensor_info, builder, md, gd);
746 auto nodes = g->nodes();
#define INTERNAL_EXN_V(msg, val)
@ brief throw internal exception with message and value
Helper class to hold data needed in creation of a FlatBuffer. To serialize data, you typically call o...
Logical unit of computation.
BIDIRECTIONAL_SEQUENCE_LSTM in Circle.
Virtual CIRCLEBIDIRECTIONAL_SEQUENCE_LSTM_OUT in Circle.
Class to build tensor data.
const loco::DataTypeImpl< DT >::Type & at(uint32_t n) const
uint32_t size(void) const
Virtual CIRCLECUSTOMOUT in Circle.
Virtual CIRCLEIFOUT in Circle.
NON_MAX_SUPPRESSION_V4 in Circle.
Virtual NONMAXSUPPRESSIONV4OUT in Circle.
NON_MAX_SUPPRESSION_V5 in Circle.
Virtual NONMAXSUPPRESSIONV5OUT in Circle.
CircleOutputExclude is used to specifying not exported nodes.
CircleNode for Output of the Graph.
Virtual CIRCLESPLITOUT in Circle.
Virtual CIRCLESPLITVOUT in Circle.
Virtual CIRCLETOPKV2OUT in Circle.
Virtual CIRCLEUNIQUEOUT in Circle.
Virtual CIRCLEUNPACKOUT in Circle.
Virtual CircleVariable in Circle for 'variable' Tensor.
Virtual CIRCLEWHILEOUT in Circle.
__global uchar * offset(const Image *img, int x, int y)
volatile const char info[]
void exportOpDefinedTensor(const TFLTensorInfo &info, FlatBufferBuilder &builder, SerializedModelData &gd)
const T * data(const std::vector< T, Alloc > &v)
std::vector< loco::Node * > postorder_traversal(const std::vector< loco::Node * > &roots)
Generate postorder traversal sequence starting from "roots".
std::set< Node * > succs(const Node *node)
Enumerate all the successors of a given node.
T must_cast(FeatureEncoder *node)
A helper dynamic_cast that throws when failed.
std::vector< Node * > output_nodes(Graph *)
flatbuffers::Offset< void > to_circle_sparse_index_vector(flatbuffers::FlatBufferBuilder &fb, const SparseIndexVector &sparse_idx_vec)
bool check_size_limit(const flatbuffers::FlatBufferBuilder &fb, const uint64_t data_size)
void set_tensor_index(loco::Node *node, const CircleTensorIndex &tensor_id)
ShapeDescription to_shape_description(const luci::CircleNode *node)
circle::DimensionType to_circle_dimensiontype(luci::DimensionType type)
const char * tensor_name(const circle::Tensor *tensor)
circle::SparseIndexVector to_circle_sparse_index_vector_type(luci::SparseIndexVectorType type)
T must_cast(loco::Node *node)
int32_t CircleTensorIndex
circle::TensorType to_circle_tensortype(loco::DataType type)
void exportOpDefinedTensors(loco::Graph *g, FlatBufferBuilder &builder, SerializedModelData &md, SerializedGraphData &gd)
create Tensors corresponding to results of all nodes in graph
void clear_tensor_index(loco::Node *node)
void prepareModelData(FlatBufferBuilder &builder, SerializedModelData &md)
one time preparation for SerializedModelData
void clearExportInfo(loco::Graph *g)
clear temporary export information annotated to graph nodes
ShapeStatus
ShapeStatus is to remember circle node shape status.
ShapeIterator end(const Shape &s)
C++ scalar type corresponding to each DataType.
CircleQuantParam * quantparam(void) const
SparsityParam * sparsityparam(void) const
NodeName name(void) const
CircleMXQuantParam * mx_quantparam(void) const
ShapeStatus shape_status(void) const
virtual T visit(CircleNode *)
Default fallback.
std::vector< float > scale
int32_t quantized_dimension
std::vector< int64_t > zerop
std::vector< flatbuffers::Offset< circle::Tensor > > _tensors
std::map< luci::CircleConst *, uint32_t > _cached_buffer_id
flatbuffers::Offset< circle::Buffer > _empty_buffer
std::vector< flatbuffers::Offset< circle::Buffer > > _buffers
MapBufferData _buffer_data_map
std::vector< uint8_t > BufferData
std::vector< int32_t > _dims
std::vector< DimMetaData > dim_metadata
std::vector< int32_t > block_map
std::vector< int32_t > traversal_order