Skip to content

Commit

Permalink
DRAFT CFe to support huge 2G model
Browse files Browse the repository at this point in the history
on-going draft to support huge size > 2G model.

Signed-off-by: SaeHie Park <[email protected]>
  • Loading branch information
seanshpark committed Aug 19, 2024
1 parent dad984b commit 2a52d4f
Show file tree
Hide file tree
Showing 27 changed files with 508 additions and 72 deletions.
6 changes: 5 additions & 1 deletion compiler/circledump/driver/Driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,11 @@ int entry(int argc, char **argv)

std::cout << "Dump: " << circle_path << std::endl << std::endl;

std::cout << circlemodel << std::endl;
circledump::ModelEx modelex;
modelex.model = circlemodel;
modelex.rawdata = &modelData;

std::cout << modelex << std::endl;

return 0;
}
10 changes: 8 additions & 2 deletions compiler/circledump/include/circledump/Dump.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,16 @@
namespace circledump
{

void dump_model(std::ostream &os, const circle::Model *model);
struct ModelEx
{
const circle::Model *model;
const std::vector<char> *rawdata;
};

void dump_model(std::ostream &os, const circledump::ModelEx &model);

} // namespace circledump

std::ostream &operator<<(std::ostream &os, const circle::Model *model);
std::ostream &operator<<(std::ostream &os, const circledump::ModelEx &model);

#endif // __CIRCLEDUMP_DUMP_H__
18 changes: 11 additions & 7 deletions compiler/circledump/src/Dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -341,9 +341,9 @@ void dump_sub_graph(std::ostream &os, mio::circle::Reader &reader)
os << std::endl;
}

void dump_model(std::ostream &os, const circle::Model *model)
void dump_model(std::ostream &os, const circle::Model *model, const std::vector<char> *rawdata)
{
mio::circle::Reader reader(model);
mio::circle::Reader reader(model, rawdata);

uint32_t num_subgraph = reader.num_subgraph();

Expand Down Expand Up @@ -378,13 +378,17 @@ void dump_model(std::ostream &os, const circle::Model *model)
os << std::endl;

// dump buffer
os << "Buffers: B(index) (length) values, if any" << std::endl;
os << "Buffers: B(index) (length) values, if any; (length *) for large" << std::endl;
for (uint32_t i = 0; i < buffers->size(); ++i)
{
bool is_large;
const uint8_t *buff_data;
size_t size = reader.buffer_info(i, &buff_data);
size_t size = reader.buffer_info(i, &buff_data, is_large);

os << "B(" << i << ") (" << size << ") ";
os << "B(" << i << ") (" << size;
if (is_large)
os << " *";
os << ") ";
if (buff_data != nullptr)
{
dump_buffer(os, buff_data, size, 16);
Expand Down Expand Up @@ -460,8 +464,8 @@ void dump_model(std::ostream &os, const circle::Model *model)

} // namespace circledump

std::ostream &operator<<(std::ostream &os, const circle::Model *model)
std::ostream &operator<<(std::ostream &os, const circledump::ModelEx &modelex)
{
circledump::dump_model(os, model);
circledump::dump_model(os, modelex.model, modelex.rawdata);
return os;
}
7 changes: 7 additions & 0 deletions compiler/luci/import/include/luci/Import/CircleReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,18 @@ class CircleReader

public:
bool parse(const circle::Model *model);
bool parse(const circle::Model *model, const uint8_t *data, size_t size);
bool select_subgraph(uint32_t subgraph);

public:
// to access raw file data for Buffer outside of flatbuffer range
const uint8_t *file_data(uint64_t offset) const;

private:
const circle::Model *_model{nullptr};
const circle::SubGraph *_current_subgraph{nullptr};
const uint8_t *_file_data{nullptr};
size_t _file_size{0};
};

} // namespace luci
Expand Down
9 changes: 8 additions & 1 deletion compiler/luci/import/include/luci/Importer.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,19 @@ class Importer final
// DO NOTHING
}

explicit Importer(const uint8_t *data, size_t size) : _file_data{data}, _file_size{size}
{
// DO NOTHING
}

public:
std::unique_ptr<loco::Graph> import(const circle::Model *model) const;
// std::unique_ptr<loco::Graph> import(const circle::Model *model) const;
std::unique_ptr<Module> importModule(const circle::Model *model) const;

private:
const GraphBuilderSource *_source = nullptr;
const uint8_t *_file_data = nullptr;
size_t _file_size = 0;
};

} // namespace luci
Expand Down
20 changes: 20 additions & 0 deletions compiler/luci/import/src/CircleReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -331,6 +331,19 @@ bool CircleReader::parse(const circle::Model *model)
return true;
}

bool CircleReader::parse(const circle::Model *model, const uint8_t *data, size_t size)
{
assert(model != nullptr);

// for direct pointer access
_model = model;

_file_data = data;
_file_size = size;

return true;
}

bool CircleReader::select_subgraph(uint32_t sgindex)
{
if (num_subgraph() <= sgindex)
Expand All @@ -349,6 +362,13 @@ bool CircleReader::select_subgraph(uint32_t sgindex)
return true;
}

const uint8_t *CircleReader::file_data(uint64_t offset) const
{
assert(_file_data);
assert(_file_size);
return (_file_data == nullptr) ? nullptr : _file_data + offset;
}

template <typename T>
VectorWrapper<T>::VectorWrapper(const flatbuffers::Vector<T> *ptr) : _vector(ptr)
{
Expand Down
4 changes: 3 additions & 1 deletion compiler/luci/import/src/Importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ Importer::Importer()
// DO NOTHING
}

/*
std::unique_ptr<loco::Graph> Importer::import(const circle::Model *model) const
{
auto graph = loco::make_graph();
Expand Down Expand Up @@ -300,6 +301,7 @@ std::unique_ptr<loco::Graph> Importer::import(const circle::Model *model) const
return graph;
}
*/

std::unique_ptr<Module> Importer::importModule(const circle::Model *model) const
{
Expand All @@ -314,7 +316,7 @@ std::unique_ptr<Module> Importer::importModule(const circle::Model *model) const
}

CircleReader reader;
if (!reader.parse(model))
if (!reader.parse(model, _file_data, _file_size))
return nullptr;

for (uint32_t g = 0; g < reader.num_subgraph(); ++g)
Expand Down
7 changes: 5 additions & 2 deletions compiler/luci/import/src/ImporterEx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,10 @@ std::unique_ptr<Module> ImporterEx::importVerifyModule(const std::string &input_
return nullptr;
}

flatbuffers::Verifier verifier{reinterpret_cast<uint8_t *>(model_data.data()), model_data.size()};
auto data_data = reinterpret_cast<uint8_t *>(model_data.data());
auto data_size = model_data.size();

flatbuffers::Verifier verifier{data_data, data_size};
if (!circle::VerifyModelBuffer(verifier))
{
std::cerr << "ERROR: Invalid input file '" << input_path << "'" << std::endl;
Expand All @@ -54,7 +57,7 @@ std::unique_ptr<Module> ImporterEx::importVerifyModule(const std::string &input_
return nullptr;
}

Importer importer;
Importer importer(data_data, data_size);
return importer.importModule(circle_model);
}

Expand Down
41 changes: 39 additions & 2 deletions compiler/luci/import/src/Nodes/CircleConst.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@

#include "luci/Import/Nodes/CircleConst.h"

#include "luci/Import/CircleReader.h"

#include <luci/IR/Nodes/CircleConst.h>
#include <luci/Log.h>

Expand All @@ -27,6 +29,8 @@
#include <string>
#include <vector>

#include <string.h>

namespace
{

Expand Down Expand Up @@ -156,8 +160,41 @@ CircleNode *CircleConstNodeBuilder::build(TensorIndex tensor_index,
return nullptr;
}

assert(reader->buffers()[const_tensor->buffer()] != nullptr);
const auto buffer = wrap(reader->buffers()[const_tensor->buffer()]->data());
const auto r_buffers = reader->buffers();
const auto c_buffer = const_tensor->buffer();
assert(r_buffers[c_buffer] != nullptr);
const auto r_buffer = r_buffers[c_buffer];
// temporary buffer to provide raw data from file
// must have life time same or longer than 'buffer' variable
std::vector<uint8_t> temp_buffer;
// const auto buffer = wrap(r_buffer->data());
luci::VectorWrapper<uint8_t> buffer(nullptr);
if (r_buffer->offset() > 1)
{
uint32_t r_size = static_cast<uint32_t>(r_buffer->size());
// match binary level to flatbuffers::Vector
temp_buffer.resize(r_size + sizeof(uint32_t));

uint8_t *t_data = temp_buffer.data();
const uint8_t *f_data = reader->file_data(r_buffer->offset());
if (f_data == nullptr)
{
// NOTE this shouldn't happen
assert(false);
return nullptr;
}
memcpy(t_data, &r_size, sizeof(r_size));
t_data = t_data + sizeof(r_size);
memcpy(t_data, f_data, r_buffer->size());

using fbv_t = flatbuffers::Vector<uint8_t>;
const fbv_t *v_data = reinterpret_cast<const fbv_t *>(temp_buffer.data());
buffer = wrap(v_data);
}
else
{
buffer = wrap(r_buffer->data());
}
const auto const_dims = wrap(const_tensor->shape()); // in NHWC
if (const_dims.size() == 0 && buffer.empty())
{
Expand Down
3 changes: 2 additions & 1 deletion compiler/luci/tester/src/ReadModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ std::unique_ptr<luci::Module> ReadModule(std::string &input_path)
return nullptr;
}

luci::Importer importer;
auto *data_data = reinterpret_cast<uint8_t *>(model_data.data());
luci::Importer importer(data_data, model_data.size());
auto module = importer.importModule(circle_model);
assert(module->size() > 0);

Expand Down
2 changes: 2 additions & 0 deletions compiler/luci/tests/test.lst
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ addread(Conv2D_000)
addread(Conv2D_001)
addread(Conv2D_002)
addread(Conv2D_003)
addread(Conv2D_006)
addread(Conv2D_U8_000)
addread(Conv2D_U8_001)
addread(Cos_000)
Expand Down Expand Up @@ -270,6 +271,7 @@ addwrite(Conv2D_000)
addwrite(Conv2D_001)
addwrite(Conv2D_002)
addwrite(Conv2D_003)
addwrite(Conv2D_006)
addwrite(Conv2D_U8_000)
addwrite(Conv2D_U8_001)
addwrite(Cos_000)
Expand Down
2 changes: 1 addition & 1 deletion compiler/mio-circle08/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/schema.fbs"
DEPENDS "${SCHEMA_FILE}"
)

FlatBuffers_Target(mio_circle08
FlatBuffersMuteable_Target(mio_circle08
OUTPUT_DIR "${CMAKE_CURRENT_BINARY_DIR}/gen/mio/circle"
INCLUDE_DIR "${CMAKE_CURRENT_BINARY_DIR}/gen"
SCHEMA_DIR "${CMAKE_CURRENT_BINARY_DIR}"
Expand Down
4 changes: 4 additions & 0 deletions compiler/mio-circle08/include/mio_circle/Reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ class Reader

public:
Reader(const ::circle::Model *model);
Reader(const ::circle::Model *model, const std::vector<char> *rawdata);

Reader() = delete;

Expand All @@ -65,6 +66,7 @@ class Reader
uint32_t num_subgraph() const { return _subgraphs->size(); }

size_t buffer_info(uint32_t buf_idx, const uint8_t **buff_data);
size_t buffer_info(uint32_t buf_idx, const uint8_t **buff_data, bool &is_large);
::circle::BuiltinOperator builtin_code(const ::circle::Operator *op) const;
std::string opcode_name(const ::circle::Operator *op) const;
std::vector<int32_t> outputs(const ::circle::Operator *op) const;
Expand All @@ -79,6 +81,8 @@ class Reader
private:
uint32_t _version;

const std::vector<char> *_rawdata{nullptr};

const CircleSubGraphs_t *_subgraphs{nullptr};
const CircleBuffers_t *_buffers{nullptr};
const CircleTensors_t *_tensors{nullptr};
Expand Down
63 changes: 63 additions & 0 deletions compiler/mio-circle08/src/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,28 @@ Reader::Reader(const ::circle::Model *model)
}
}

Reader::Reader(const ::circle::Model *model, const std::vector<char> *rawdata)
{
if (model == nullptr)
{
throw std::runtime_error("Invalid model");
}

_rawdata = rawdata;

_version = model->version();
_subgraphs = model->subgraphs();
_buffers = model->buffers();
_metadata = model->metadata();
_signature_defs = model->signature_defs();

auto opcodes = model->operator_codes();
for (const ::circle::OperatorCode *opcode : *opcodes)
{
_op_codes.push_back(opcode);
}
}

size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
{
if (buff_data != nullptr)
Expand Down Expand Up @@ -73,6 +95,47 @@ size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
return 0;
}

size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data, bool &is_large)
{
is_large = false;

if (buff_data != nullptr)
{
*buff_data = nullptr;
}

if (buf_idx == 0)
return 0;

if (auto *buffer = (*_buffers)[buf_idx])
{
auto buffer_offset = buffer->offset();
if (buffer->offset() > 1)
{
assert(_rawdata);
if (_rawdata == nullptr)
return 0;

is_large = true;
*buff_data = reinterpret_cast<const uint8_t *>(&_rawdata->at(buffer_offset));
return buffer->size();
}
else if (auto *array = buffer->data())
{
if (size_t size = array->size())
{
if (buff_data != nullptr)
{
*buff_data = reinterpret_cast<const uint8_t *>(array->data());
}
return size;
}
}
}

return 0;
}

::circle::BuiltinOperator Reader::builtin_code(const ::circle::Operator *op) const
{
uint32_t index = op->opcode_index();
Expand Down
Loading

0 comments on commit 2a52d4f

Please sign in to comment.