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 14, 2024
1 parent 719a6f7 commit 4a0fbd8
Show file tree
Hide file tree
Showing 20 changed files with 505 additions and 206 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;
}
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
3 changes: 2 additions & 1 deletion compiler/mio-tflite2121/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,8 @@ endif(NOT TensorFlowSource_FOUND)
message(STATUS "Build mio-tflite2121: TRUE")
message(STATUS "Build mio-tflite2121: with ${TensorFlowSource_DIR}")

set(SCHEMA_FILE "${TensorFlowSource_DIR}/tensorflow/lite/schema/schema.fbs")
#set(SCHEMA_FILE "${TensorFlowSource_DIR}/tensorflow/lite/schema/schema.fbs")
set(SCHEMA_FILE "${NNAS_PROJECT_SOURCE_DIR}/res/TensorFlowLiteSchema/2.16.1/schema.fbs")

# NOTE Use copy of schema.fbs as to provide unified way for circle also
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/schema.fbs"
Expand Down
Loading

0 comments on commit 4a0fbd8

Please sign in to comment.