19#include "ShapeInference.h"
43 TFLTensorInfo() =
default;
46 void name(
const std::string &name) { _name = name; }
47 const std::string &name(
void)
const {
return _name; }
50 const circle::TensorType &
dtype(
void)
const {
return _dtype; }
51 void dtype(
const circle::TensorType &dtype) { _dtype =
dtype; }
63 circle::TensorType _dtype{circle::TensorType_FLOAT32};
71using TFLTensorContext = std::vector<TFLTensorInfo>;
84 if (encoder !=
nullptr)
86 auto perm = encoder->perm();
95 if (encoder !=
nullptr)
97 auto perm = encoder->perm();
106 if (decoder !=
nullptr)
108 auto perm = decoder->perm();
123 return canonical_node->accept(&d);
128void allocateCircleTensor(
loco::Node *node, TFLTensorContext &ctx)
132 assert(node->
arity() == 1 && node->
arg(0) !=
nullptr);
139 auto tensor_name =
"t_" + std::to_string(tensor_index);
141 TFLTensorInfo tensor_info;
143 tensor_info.name(tensor_name);
151 ctx.emplace_back(tensor_info);
162 assert(shape.
_rank_known &&
"unknown number of dimensions is not supported");
168 return CreateBuffer(builder);
171template <
typename NodeT>
174 return CreateBuffer(builder);
177template <loco::DataType DT>
183 std::vector<NativeType> raw_data;
184 const uint32_t
size = c->
size<DT>();
185 raw_data.reserve(
size);
186 for (uint32_t i = 0; i <
size; ++i)
188 raw_data.push_back(c->
at<DT>(i));
190 const size_t raw_size =
size *
sizeof(NativeType);
191 auto array_offset = builder.
CreateVector(
reinterpret_cast<uint8_t *
>(raw_data.data()), raw_size);
192 return CreateBuffer(builder, array_offset);
198 if (c->dtype() == loco::DataType::FLOAT32)
200 return encodeOpBufferByDType<loco::DataType::FLOAT32>(builder, c);
202 else if (c->dtype() == loco::DataType::S32)
204 return encodeOpBufferByDType<loco::DataType::S32>(builder, c);
214namespace circle_detail
221 auto shape_offset = encodeShape(builder,
info.shape());
224 auto buffer =
info.tfl_content() ==
nullptr ? encodeOpBuffer(builder)
225 : encodeOpBuffer(builder,
info.tfl_content());
227 auto buffer_id =
static_cast<uint32_t
>(gd.
_buffers.size());
231 auto tensor_offset = CreateTensor(builder, shape_offset,
info.dtype(), buffer_id, name_offset,
233 gd.
_tensors.push_back(tensor_offset);
238 TFLTensorContext tensor_ctx;
242 allocateCircleTensor(node, tensor_ctx);
251 auto buffer = encodeOpBuffer(builder);
254 for (
const auto &tensor_info : tensor_ctx)
#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...
Offset< String > CreateString(const char *str, size_t len)
Store a string in the buffer, which can contain any binary data.
Offset< Vector< T > > CreateVector(const T *v, size_t len)
Serialize an array into a FlatBuffer vector.
Create a "Bias" from a "Tensor".
Create a value from constant byte array.
Create a tensor from a feature map.
Create a feature map from a tensor.
Create a filter from a tensor.
Logical unit of computation.
virtual Node * arg(uint32_t N) const =0
Access N-th argument node.
virtual uint32_t arity(void) const =0
Return the number of arguments.
Class to build tensor data.
uint32_t size(void) const
const loco::DataTypeImpl< DT >::Type & at(uint32_t n) const
volatile const char info[]
void exportOpDefinedTensors(loco::Graph *g, FlatBufferBuilder &builder, SerializedModelData &gd)
create Tensors corresponding to results of all nodes in graph
void exportOpDefinedTensor(const TFLTensorInfo &info, FlatBufferBuilder &builder, SerializedModelData &gd)
TFLTensorIndex get_tensor_index(loco::Node *node)
void set_tensor_index(loco::Node *node, const TFLTensorIndex &tensor_id)
bool isNHWC(Permutation *perm)
std::vector< loco::Node * > postorder_traversal(const std::vector< loco::Node * > &roots)
Generate postorder traversal sequence starting from "roots".
std::vector< Node * > output_nodes(Graph *)
const char * tensor_name(const circle::Tensor *tensor)
std::vector< int32_t > _dims
static ShapeDescription get(loco::Node *node)
std::vector< flatbuffers::Offset< circle::Tensor > > _tensors
std::vector< flatbuffers::Offset< circle::Buffer > > _buffers
static circle::TensorType get(loco::Node *node)
virtual T visit(Node *)
Default fallback.
C++ scalar type corresponding to each DataType.