Skip to content

Commit

Permalink
[tfldump] Revise Reader to support ext_offset (#13707)
Browse files Browse the repository at this point in the history
This will revise Reader class to support Buffer offset/size attributes
for file size > 2G.

ONE-DCO-1.0-Signed-off-by: SaeHie Park <[email protected]>
  • Loading branch information
seanshpark authored Aug 20, 2024
1 parent 90e7838 commit a1ce518
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
59 changes: 59 additions & 0 deletions compiler/tfldump/src/Read.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@

#include <mio_tflite2121/Helper.h>

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

Expand All @@ -39,6 +40,23 @@ Reader::Reader(const tflite::Model *model)
}
}

Reader::Reader(const tflite::Model *model, const std::vector<char> *rawdata)
{
_rawdata = rawdata;

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

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

size_t Reader::buffer_info(uint32_t buf_idx, const uint8_t **buff_data)
{
*buff_data = nullptr;
Expand All @@ -61,6 +79,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 &ext_offset)
{
*buff_data = nullptr;
ext_offset = false;

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())
{
*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;
}

tflite::BuiltinOperator Reader::builtin_code(const tflite::Operator *op) const
{
uint32_t index = op->opcode_index();
Expand Down
4 changes: 4 additions & 0 deletions compiler/tfldump/src/Read.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class Reader

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

Reader() = delete;

Expand All @@ -69,6 +70,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);
tflite::BuiltinOperator builtin_code(const tflite::Operator *op) const;
std::string opcode_name(const tflite::Operator *op) const;

Expand All @@ -80,6 +82,8 @@ class Reader
private:
uint32_t _version;

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

const TFliteSubGraphs_t *_subgraphs{nullptr};
const TFliteBuffers_t *_buffers{nullptr};
const TFliteTensors_t *_tensors{nullptr};
Expand Down

0 comments on commit a1ce518

Please sign in to comment.