diff --git a/compiler/luci/export/include/luci/CircleExporter.h b/compiler/luci/export/include/luci/CircleExporter.h index 0584c623cfa..de064ea8444 100644 --- a/compiler/luci/export/include/luci/CircleExporter.h +++ b/compiler/luci/export/include/luci/CircleExporter.h @@ -36,14 +36,9 @@ class CircleExporter virtual ~Contract() = default; public: // Client -> Exporter - // Input Graph (to be exported) - // Exporter expects a loco graph that consists of Circle nodes - virtual loco::Graph *graph(void) const = 0; - // Input Module (to be exported) // Exporter expects a luci module that consists of loco graphs - // TODO make this pure virtual - virtual luci::Module *module(void) const; + virtual luci::Module *module(void) const = 0; public: // Exporter -> Client // Exporter calls store for export data diff --git a/compiler/luci/export/include/luci/CircleFileExpContract.h b/compiler/luci/export/include/luci/CircleFileExpContract.h index 8ef1b5e0cf6..a06ebe9963e 100644 --- a/compiler/luci/export/include/luci/CircleFileExpContract.h +++ b/compiler/luci/export/include/luci/CircleFileExpContract.h @@ -40,7 +40,6 @@ struct CircleFileExpContract : public luci::CircleExporter::Contract virtual ~CircleFileExpContract() = default; public: - loco::Graph *graph(void) const final { return nullptr; } luci::Module *module(void) const final { return _module; } public: diff --git a/compiler/luci/export/src/CircleExporter.cpp b/compiler/luci/export/src/CircleExporter.cpp index 125df780212..6f5066823c2 100644 --- a/compiler/luci/export/src/CircleExporter.cpp +++ b/compiler/luci/export/src/CircleExporter.cpp @@ -26,9 +26,6 @@ namespace luci { -// TODO remove this -Module *CircleExporter::Contract::module(void) const { return nullptr; } - CircleExporter::CircleExporter() { // NOTHING TO DO @@ -48,17 +45,10 @@ bool CircleExporter::invoke(Contract *contract) const return contract->store(ptr, size); } - auto graph = contract->graph(); - if (graph == nullptr) - return false; - - CircleExporterImpl impl(graph); - - const char *ptr = impl.getBufferPointer(); - const size_t size = impl.getBufferSize(); + // NOTE some unit tests calls with nullptr module, cannot add assert here + // TODO fix those unit tests and add assert(false) - // we just send one time - return contract->store(ptr, size); + return false; } } // namespace luci diff --git a/compiler/luci/export/src/CircleExporter.test.cpp b/compiler/luci/export/src/CircleExporter.test.cpp index 5898f9d653e..c76d983972d 100644 --- a/compiler/luci/export/src/CircleExporter.test.cpp +++ b/compiler/luci/export/src/CircleExporter.test.cpp @@ -33,12 +33,12 @@ class SampleGraphContract : public luci::CircleExporter::Contract SampleGraphContract() : luci::CircleExporter::Contract(), _buffer(new std::vector) { // create needed entities - _g = loco::make_graph(); - auto graph_input = _g->inputs()->create(); - auto graph_output = _g->outputs()->create(); - input_node = _g->nodes()->create(); - output_node = _g->nodes()->create(); - relu_node = _g->nodes()->create(); + auto g = loco::make_graph(); + auto graph_input = g->inputs()->create(); + auto graph_output = g->outputs()->create(); + input_node = g->nodes()->create(); + output_node = g->nodes()->create(); + relu_node = g->nodes()->create(); // link nodes and link them to graph relu_node->features(input_node); @@ -57,9 +57,12 @@ class SampleGraphContract : public luci::CircleExporter::Contract graph_output->shape({1, 2, 3, 4}); graph_output->dtype(loco::DataType::FLOAT32); + + _m = std::unique_ptr{new luci::Module}; + _m->add(std::move(g)); } - loco::Graph *graph(void) const override { return _g.get(); } + luci::Module *module(void) const override { return _m.get(); } public: bool store(const char *ptr, const size_t size) const override @@ -77,7 +80,7 @@ class SampleGraphContract : public luci::CircleExporter::Contract luci::CircleRelu *relu_node; private: - std::unique_ptr _g; + std::unique_ptr _m; std::unique_ptr> _buffer; }; diff --git a/compiler/luci/export/src/CircleExporterImpl.cpp b/compiler/luci/export/src/CircleExporterImpl.cpp index 2a7384663ac..c8f2aa06529 100644 --- a/compiler/luci/export/src/CircleExporterImpl.cpp +++ b/compiler/luci/export/src/CircleExporterImpl.cpp @@ -133,7 +133,6 @@ namespace luci using namespace circle; using namespace flatbuffers; -CircleExporterImpl::CircleExporterImpl(loco::Graph *graph) { exportGraph(graph); } CircleExporterImpl::CircleExporterImpl(Module *module) { exportModule(module); } ::flatbuffers::Offset<::circle::SubGraph> @@ -148,61 +147,6 @@ CircleExporterImpl::exportSubgraph(SerializedGraphData &gd) return subgraph; } -void CircleExporterImpl::exportGraph(loco::Graph *graph) -{ - // do graph optimization - optimize(graph); - - _builder.Clear(); - - SerializedModelData md; - SerializedGraphData gd; - - // This version is taken from comment in fbs - constexpr uint32_t version = 0; - - // set Subgraph name - gd._name = graph->name(); - - // TODO set this value properly - gd._data_format = circle::DataFormat::DataFormat_CHANNELS_LAST; - - // prepare model data - prepareModelData(_builder, md); - - // parse graph into SerializedModelData structure - exportOpDefinedTensors(graph, _builder, md, gd); - - // NOTE Invoke these register functions only after each node is annotated with its tensor_index - registerGraphInputTensors(graph, gd); - registerGraphOutputTensors(graph, gd); - - exportNodes(graph, _builder, md, gd); - - // encode operator codes - auto operator_codes = encodeOperatorCodes(_builder, md._operator_codes); - - // Subgraphs - Offset subgraph = exportSubgraph(gd); - auto subgraphs = _builder.CreateVector(std::vector>{subgraph}); - - // Description - std::string description_str = "nnpackage"; - auto description = _builder.CreateString(description_str); - - // Metadata - auto metadata_vec = createCircleMetadataVector(_builder, md); - auto metadata = _builder.CreateVector(std::vector>(metadata_vec)); - - // create array of buffers - auto buffers = _builder.CreateVector(md._buffers); - - // Model - auto model_offset = CreateModel(_builder, version, operator_codes, subgraphs, description, - buffers, 0 /* metadata_buffer */, metadata); - FinishModelBuffer(_builder, model_offset); -} - void CircleExporterImpl::exportModule(Module *module) { assert(module->size() > 0); diff --git a/compiler/luci/export/src/CircleExporterImpl.h b/compiler/luci/export/src/CircleExporterImpl.h index 069f62afd4a..5911fadba43 100644 --- a/compiler/luci/export/src/CircleExporterImpl.h +++ b/compiler/luci/export/src/CircleExporterImpl.h @@ -38,7 +38,6 @@ class CircleExporterImpl CircleExporterImpl() = delete; ~CircleExporterImpl() = default; - explicit CircleExporterImpl(loco::Graph *graph); explicit CircleExporterImpl(Module *module); /** @@ -59,12 +58,6 @@ class CircleExporterImpl */ flatbuffers::Offset exportSubgraph(SerializedGraphData &gd); - /** - * @brief root function that writes graph into internal buffer - * @param graph - */ - void exportGraph(loco::Graph *graph); - /** * @brief root function that writes Module into internal buffer * @param module diff --git a/compiler/luci/tester/src/WriteTester.cpp b/compiler/luci/tester/src/WriteTester.cpp index 0d3a1efa220..e5b31e16dc6 100644 --- a/compiler/luci/tester/src/WriteTester.cpp +++ b/compiler/luci/tester/src/WriteTester.cpp @@ -42,11 +42,6 @@ void show_error_message(const char *progname, std::ostream &os, const std::strin struct CircleExpContract : public luci::CircleExporter::Contract { public: - CircleExpContract(loco::Graph *graph, const std::string &filename) - : _graph(graph), _filepath(filename) - { - // NOTHING TO DO - } CircleExpContract(luci::Module *module, const std::string &filename) : _module(module), _filepath(filename) { @@ -55,15 +50,12 @@ struct CircleExpContract : public luci::CircleExporter::Contract virtual ~CircleExpContract() = default; public: - loco::Graph *graph(void) const final { return _graph; } - luci::Module *module(void) const final { return _module; } public: bool store(const char *ptr, const size_t size) const final; private: - loco::Graph *_graph{nullptr}; luci::Module *_module{nullptr}; const std::string _filepath; };