Skip to content

Commit

Permalink
[mio-circle08] Revise to support ext_offset (#13708)
Browse files Browse the repository at this point in the history
This will revise to support ext_offset with
- enable mutable methods with generated schema
- enable to read Buffer offset/size attributes

ONE-DCO-1.0-Signed-off-by: SaeHie Park <[email protected]>
  • Loading branch information
seanshpark authored Aug 20, 2024
1 parent a1ce518 commit 6b99385
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
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 &ext_offset);
::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
71 changes: 71 additions & 0 deletions compiler/mio-circle08/src/Reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#include "mio_circle/Reader.h"
#include "mio_circle/Helper.h"

#include <iostream>
#include <sstream>
#include <string>

Expand Down Expand Up @@ -45,6 +46,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 +96,54 @@ 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 &ext_offset)
{
ext_offset = 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); // make debug break for invalid case
if (_rawdata == nullptr)
return 0;

ext_offset = 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;
}
}
else
{
if (buffer->offset() == 1 && buffer->size() == 1)
{
std::cerr << "Buffer " << buf_idx << " has invalid offset/size." << std::endl;
}
}
}

return 0;
}

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

0 comments on commit 6b99385

Please sign in to comment.