ONE - On-device Neural Engine
Loading...
Searching...
No Matches
tflite2circle::CircleModel Class Reference

#include <CircleModel.h>

Public Member Functions

 CircleModel (void)=delete
 
 CircleModel (FlatBufBuilder &fb, const std::vector< char > &fr)
 
void load_offsets (const tflite::Model *tfl_model)
 
void model_build (void) const
 
void finalize (void)
 
const char * base (void) const
 
size_t size (void) const
 

Detailed Description

Definition at line 93 of file CircleModel.h.

Constructor & Destructor Documentation

◆ CircleModel() [1/2]

tflite2circle::CircleModel::CircleModel ( void  )
delete

◆ CircleModel() [2/2]

tflite2circle::CircleModel::CircleModel ( FlatBufBuilder fb,
const std::vector< char > &  fr 
)

Definition at line 413 of file CircleModel.cpp.

414 : _version{0}, _description{fb->CreateString("ONE-tflite2circle")}, _fb{fb}, _file_raw{fr}
415{
416 // NOTHING TODO
417}

Member Function Documentation

◆ base()

const char * tflite2circle::CircleModel::base ( void  ) const

Definition at line 509 of file CircleModel.cpp.

510{
511 if (_buffer_data_map.empty())
512 return reinterpret_cast<const char *>(_fb->GetBufferPointer());
513 return reinterpret_cast<const char *>(_fb_data_with_ext.data());
514}

◆ finalize()

void tflite2circle::CircleModel::finalize ( void  )

Definition at line 451 of file CircleModel.cpp.

452{
453 if (_buffer_data_map.empty())
454 return;
455
456 auto align16 = [](size_t &v) {
457 while (v % 16 != 0)
458 v++;
459 };
460
461 // get total memory for flatbuffer + all buffer_data
462 size_t result_size = _fb->GetSize();
463 align16(result_size);
464 for (auto &it : _buffer_data_map)
465 {
466 BufferData &buffer_data = it.second;
467 result_size += buffer_data.size();
468 align16(result_size);
469 }
470 align16(result_size);
471 result_size += 16; // for safety
472
473 std::string result;
474 const char *buff_ptr = reinterpret_cast<const char *>(_fb->GetBufferPointer());
475
476 auto padalign16 = [](std::string &str) {
477 while (str.size() % 16 != 0)
478 str += '\0';
479 };
480
481 result.reserve(result_size);
482 result.append(buff_ptr, _fb->GetSize());
483
484 auto mutable_model = circle::GetMutableModel(result.data());
485 auto mutable_buffers = mutable_model->mutable_buffers();
486
487 // pad to be 16 bytes aligned
488 padalign16(result);
489 for (auto &it : _buffer_data_map)
490 {
491 int32_t buffer_index = it.first;
492 BufferData &buffer_data = it.second;
493 uint64_t offset = result.size();
494 uint64_t size = buffer_data.size();
495
496 circle::Buffer *mutable_buffer = mutable_buffers->GetMutableObject(buffer_index);
497 mutable_buffer->mutate_offset(offset);
498 mutable_buffer->mutate_size(size);
499
500 result.append(buffer_data.begin(), buffer_data.end());
501 padalign16(result);
502 }
503 padalign16(result);
504
505 // use final result
506 _fb_data_with_ext = result;
507}
size_t size(void) const
__global uchar * offset(const Image *img, int x, int y)
Definition helpers.h:540
result
Definition infer.py:103
str
Definition infer.py:18
std::vector< uint8_t > BufferData
Definition CircleModel.h:58

References offset(), and size().

◆ load_offsets()

void tflite2circle::CircleModel::load_offsets ( const tflite::Model *  tfl_model)

Definition at line 419 of file CircleModel.cpp.

420{
421 _operator_codes_offset = std::make_unique<Offset<OperatorCodeLink>>(_fb);
422 _subGraphs_offset = std::make_unique<Offset<SubGraphLink>>(_fb);
423 _buffers_offset = std::make_unique<Offset<BufferLink>>(_fb);
424 _metadata_buffer_offset = std::make_unique<Offset<MetaDataBufferLink>>(_fb);
425
426 _subGraphs_offset->set_signature_defs(tfl_model->signature_defs());
427 _buffers_offset->set_buffer_data_map(&_buffer_data_map);
428 _buffers_offset->set_file_raw(&_file_raw);
429
430 _operator_codes_offset->build(tfl_model->operator_codes());
431 _subGraphs_offset->build(tfl_model->subgraphs());
432 _buffers_offset->build(tfl_model->buffers());
433 _metadata_buffer_offset->build(tfl_model->metadata_buffer());
434}

◆ model_build()

void tflite2circle::CircleModel::model_build ( void  ) const

Definition at line 436 of file CircleModel.cpp.

437{
438 circle::ModelBuilder model_builder{*_fb};
439
440 model_builder.add_version(_version);
441 model_builder.add_description(_description);
442 model_builder.add_operator_codes(_operator_codes_offset->offset());
443 model_builder.add_subgraphs(_subGraphs_offset->offset());
444 model_builder.add_buffers(_buffers_offset->offset());
445 model_builder.add_metadata_buffer(_metadata_buffer_offset->offset());
446
447 auto model = model_builder.Finish();
448 circle::FinishModelBuffer(*_fb, model);
449}

◆ size()

size_t tflite2circle::CircleModel::size ( void  ) const

Definition at line 516 of file CircleModel.cpp.

517{
518 if (_buffer_data_map.empty())
519 return _fb->GetSize();
520 return _fb_data_with_ext.size();
521}

Referenced by finalize().


The documentation for this class was generated from the following files: