Skip to content

Commit

Permalink
[luci/export] Finialize extended buffer (#13880)
Browse files Browse the repository at this point in the history
This will introduce finalizeWithExtendedBuffer method to generate export data.

ONE-DCO-1.0-Signed-off-by: SaeHie Park <[email protected]>
  • Loading branch information
seanshpark authored Sep 2, 2024
1 parent df118e3 commit 6e9485f
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 0 deletions.
63 changes: 63 additions & 0 deletions compiler/luci/export/src/CircleExporterImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,8 @@ void CircleExporterImpl::exportModule(Module *module)
md._ext_buffer = true;
exportModuleData(module, md);
}

finalizeWithExtendedBuffer(md);
}

bool CircleExporterImpl::exportModuleData(Module *module, SerializedModelData &md)
Expand Down Expand Up @@ -245,6 +247,67 @@ bool CircleExporterImpl::exportModuleData(Module *module, SerializedModelData &m
return true;
}

void CircleExporterImpl::finalizeWithExtendedBuffer(SerializedModelData &md)
{
_ext_buffer = md._ext_buffer;
if (!_ext_buffer)
return;

_fb_data_with_ext.clear();

auto align16 = [](size_t &v) {
while (v % 16 != 0)
v++;
};

// get total memory for flatbuffer + all buffer_data
size_t result_size = _builder.GetSize();
align16(result_size);
for (auto &it : md._buffer_data_map)
{
SerializedModelData::BufferData &buffer_data = it.second;
result_size += buffer_data.size();
align16(result_size);
}
align16(result_size);
result_size += 16; // for safety

std::string result;
const char *buff_ptr = reinterpret_cast<const char *>(_builder.GetBufferPointer());

auto padalign16 = [](std::string &str) {
while (str.size() % 16 != 0)
str += '\0';
};

result.reserve(result_size);
result.append(buff_ptr, _builder.GetSize());

auto mutable_model = circle::GetMutableModel(result.data());
auto mutable_buffers = mutable_model->mutable_buffers();

// pad to be 16 bytes aligned
padalign16(result);
for (auto &it : md._buffer_data_map)
{
int32_t buffer_index = it.first;
SerializedModelData::BufferData &buffer_data = it.second;
uint64_t offset = result.size();
uint64_t size = buffer_data.size();

circle::Buffer *mutable_buffer = mutable_buffers->GetMutableObject(buffer_index);
mutable_buffer->mutate_offset(offset);
mutable_buffer->mutate_size(size);

result.append(buffer_data.begin(), buffer_data.end());
padalign16(result);
}
padalign16(result);

// use final result
_fb_data_with_ext = result;
}

const char *CircleExporterImpl::getBufferPointer() const
{
return reinterpret_cast<const char *>(_builder.GetBufferPointer());
Expand Down
7 changes: 7 additions & 0 deletions compiler/luci/export/src/CircleExporterImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,15 @@ class CircleExporterImpl
*/
bool exportModuleData(Module *module, SerializedModelData &md);

/**
* @brief finalizes file stream with extended buffer from internal buffer
*/
void finalizeWithExtendedBuffer(SerializedModelData &md);

private:
flatbuffers::FlatBufferBuilder _builder;
bool _ext_buffer = false;
std::string _fb_data_with_ext;
};

} // namespace luci
Expand Down

0 comments on commit 6e9485f

Please sign in to comment.