Skip to content

Commit

Permalink
[luci/export] Tidy export with graph (#13727)
Browse files Browse the repository at this point in the history
This will tidy export with graph as this is not used anymore.

ONE-DCO-1.0-Signed-off-by: SaeHie Park <[email protected]>
  • Loading branch information
seanshpark authored Aug 21, 2024
1 parent 34257bf commit 41ea171
Show file tree
Hide file tree
Showing 7 changed files with 15 additions and 99 deletions.
7 changes: 1 addition & 6 deletions compiler/luci/export/include/luci/CircleExporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 0 additions & 1 deletion compiler/luci/export/include/luci/CircleFileExpContract.h
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
16 changes: 3 additions & 13 deletions compiler/luci/export/src/CircleExporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,6 @@
namespace luci
{

// TODO remove this
Module *CircleExporter::Contract::module(void) const { return nullptr; }

CircleExporter::CircleExporter()
{
// NOTHING TO DO
Expand All @@ -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
19 changes: 11 additions & 8 deletions compiler/luci/export/src/CircleExporter.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,12 @@ class SampleGraphContract : public luci::CircleExporter::Contract
SampleGraphContract() : luci::CircleExporter::Contract(), _buffer(new std::vector<char>)
{
// create needed entities
_g = loco::make_graph();
auto graph_input = _g->inputs()->create();
auto graph_output = _g->outputs()->create();
input_node = _g->nodes()->create<luci::CircleInput>();
output_node = _g->nodes()->create<luci::CircleOutput>();
relu_node = _g->nodes()->create<luci::CircleRelu>();
auto g = loco::make_graph();
auto graph_input = g->inputs()->create();
auto graph_output = g->outputs()->create();
input_node = g->nodes()->create<luci::CircleInput>();
output_node = g->nodes()->create<luci::CircleOutput>();
relu_node = g->nodes()->create<luci::CircleRelu>();

// link nodes and link them to graph
relu_node->features(input_node);
Expand All @@ -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<luci::Module>{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
Expand All @@ -77,7 +80,7 @@ class SampleGraphContract : public luci::CircleExporter::Contract
luci::CircleRelu *relu_node;

private:
std::unique_ptr<loco::Graph> _g;
std::unique_ptr<luci::Module> _m;
std::unique_ptr<std::vector<char>> _buffer;
};

Expand Down
56 changes: 0 additions & 56 deletions compiler/luci/export/src/CircleExporterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand All @@ -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> subgraph = exportSubgraph(gd);
auto subgraphs = _builder.CreateVector(std::vector<Offset<SubGraph>>{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<Offset<Metadata>>(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);
Expand Down
7 changes: 0 additions & 7 deletions compiler/luci/export/src/CircleExporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class CircleExporterImpl
CircleExporterImpl() = delete;
~CircleExporterImpl() = default;

explicit CircleExporterImpl(loco::Graph *graph);
explicit CircleExporterImpl(Module *module);

/**
Expand All @@ -59,12 +58,6 @@ class CircleExporterImpl
*/
flatbuffers::Offset<circle::SubGraph> 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
Expand Down
8 changes: 0 additions & 8 deletions compiler/luci/tester/src/WriteTester.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
};
Expand Down

0 comments on commit 41ea171

Please sign in to comment.