ONE - On-device Neural Engine
Loading...
Searching...
No Matches
luci_interpreter::CircleReferencingConstNodeBuilder Class Reference

Builder creates CircleCustom node with pointer to constants data from Tensor with buffer. More...

#include <CircleReferencingConst.h>

Collaboration diagram for luci_interpreter::CircleReferencingConstNodeBuilder:

Public Member Functions

CircleNodebuild (TensorIndex tensor_index, GraphBuilderContext *ctx) const final
 
- Public Member Functions inherited from luci::TypedNodeBuilder< NodeBuilderType::BUFFER >
NodeBuilderType builder_type () const final
 
- Public Member Functions inherited from luci::NodeBuilderBase
virtual ~NodeBuilderBase ()=default
 

Detailed Description

Builder creates CircleCustom node with pointer to constants data from Tensor with buffer.

Definition at line 31 of file CircleReferencingConst.h.

Member Function Documentation

◆ build()

CircleNode * luci_interpreter::CircleReferencingConstNodeBuilder::build ( TensorIndex  tensor_index,
GraphBuilderContext ctx 
) const
finalvirtual

Implements luci::NodeBuilderBase.

Definition at line 37 of file CircleReferencingConst.cpp.

39{
40 assert(tensor_index >= 0);
41
42 const auto graph = context->graph();
43 const auto reader = context->reader();
44 const auto tensors = reader->tensors();
45 auto const const_tensor = tensors[tensor_index];
46 assert(const_tensor != nullptr);
47 if (const_tensor->is_variable())
48 {
49 // Create CircleVariable for variable
50 return nullptr;
51 }
52
53 auto const buffer = wrap(reader->buffers()[const_tensor->buffer()]->data());
54 auto const const_dims = wrap(const_tensor->shape()); // in NHWC
55 if (const_dims.empty() && buffer.empty())
56 {
57 // unknown shape tensor and scalar tensor
58 return nullptr;
59 }
60
61 // if tensor_index is used as output to some other operator, this is not a constant
62 auto tensoroutputs = context->tensoroutputs();
63 if (tensoroutputs->find(tensor_index))
64 {
65 // other operator output tensor
66 return nullptr;
67 }
68
69 uint32_t num_elements = 1;
70 for (uint32_t r = 0; r < const_dims.size(); ++r)
71 {
72 num_elements = num_elements * const_dims[r];
73 }
74
75 if (buffer.empty() && num_elements > 0)
76 {
77 // normal empty tensor
78 return nullptr;
79 }
80
81 // create CircleReferencingConst
82 auto custom_node = graph->nodes()->create<CircleCustom>(0, 1);
83 {
84 custom_node->custom_code("CircleReferencingConst");
85
86 copy_tensor_attributes(const_tensor, custom_node);
87 custom_node->shape_status(luci::ShapeStatus::VALID);
88
89 // custom options stores size of buffer and pointer's value to buffer's data
90 {
91 std::vector<uint8_t> custom_options(sizeof(ConstDataReference));
92 {
93 auto &const_data_ref = *reinterpret_cast<ConstDataReference *>(custom_options.data());
94 const_data_ref = {buffer.data(), buffer.size()};
95 }
96 custom_node->custom_options(custom_options);
97 }
98 }
99
100 // Output of CircleCustom node presented with CircleConstNode
101 auto out_node = graph->nodes()->create<CircleCustomOut>();
102 {
103 out_node->index(0);
104 out_node->input(custom_node);
105
106 copy_tensor_attributes(const_tensor, out_node);
107 out_node->shape_status(luci::ShapeStatus::VALID);
108 }
109
110 return out_node;
111}
void copy_tensor_attributes(const circle::Tensor *tensor, CircleNode *node)
Copy common tensor attributes such as name, type, etc. to node.
VectorWrapper< T > wrap(const flatbuffers::Vector< T > *vec)
uint32_t num_elements(const Shape &shape)
The number of elements of a feature map of a given shape.
Definition Shape.h:59

References luci::copy_tensor_attributes(), luci::CircleCustom::custom_code(), luci::GraphBuilderContext::graph(), luci::CircleCustomOut::index(), luci::GraphBuilderContext::reader(), luci::GraphBuilderContext::tensoroutputs(), luci::CircleReader::tensors(), luci::VALID, and luci::wrap().


The documentation for this class was generated from the following files: