Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[mio-circle08] Revise to support ext_offset #13708

Merged
merged 1 commit into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(optional)

Suggested change
Reader::Reader(const ::circle::Model *model, const std::vector<char> *rawdata)
Reader::Reader(const ::circle::Model *model, const std::vector<char> *rawdata): Reader(model)
{
_rawdata = rawdata;
}

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, I'm planning to remove old Reader::Reader

{
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