ONE - On-device Neural Engine
Loading...
Searching...
No Matches
circletensordump::DumpTensorsToHdf5 Class Referencefinal

#include <Dump.h>

Collaboration diagram for circletensordump::DumpTensorsToHdf5:

Public Member Functions

 DumpTensorsToHdf5 ()=default
 
void run (std::ostream &os, const circle::Model *model, const std::string &output_path) override
 
- Public Member Functions inherited from circletensordump::DumpInterface
virtual ~DumpInterface ()=default
 

Detailed Description

Definition at line 46 of file Dump.h.

Constructor & Destructor Documentation

◆ DumpTensorsToHdf5()

circletensordump::DumpTensorsToHdf5::DumpTensorsToHdf5 ( )
default

Member Function Documentation

◆ run()

void circletensordump::DumpTensorsToHdf5::run ( std::ostream &  os,
const circle::Model *  model,
const std::string &  output_path 
)
overridevirtual

HDF5 layout is like below

GROUP "/" ㄴGROUP "tensor name" ㄴDATASET "weights" : Shape (x, y, ...), type(uint8, int16) ㄴDATASET "min" : Shape (n) ㄴDATASET "max" : Shape (n) ㄴDATASET "scale" : Shape (m) ㄴDATASET "zero_point" : Shape (m)

NOTE All Dataset is optional. It means that if tensor doesn't have the data, it won't be created as a Dataset

Implements circletensordump::DumpInterface.

Definition at line 300 of file Dump.cpp.

302{
303 // loads a circle model
304 mio::circle::Reader reader(model);
305 uint32_t num_subgraph = reader.num_subgraph();
306
307 // create a hdf5 file
308 H5::H5File file{output_path, H5F_ACC_TRUNC};
309
310 for (uint32_t subgraph_idx = 0; subgraph_idx < num_subgraph; subgraph_idx++)
311 {
312 reader.select_subgraph(subgraph_idx);
313
314 auto tensors = reader.tensors();
315 for (const auto &tensor : *tensors)
316 {
317 // If tensor does not have name, do nothing.
318 const auto tensor_name = tensor->name();
319 if (tensor_name == nullptr)
320 {
321 assert(false && "There is no tensor name");
322 continue;
323 }
324
325 // create a group for each tensor whose name is its tensor name
326 std::string group_name = ::mangle(tensor_name->c_str());
327 std::unique_ptr<H5::Group> tensor_group =
328 std::make_unique<H5::Group>(file.createGroup(group_name));
329
330 // write a buffer data
331 uint32_t buff_idx = tensor->buffer();
332 auto buff_data_ptr = reader.buffers()->Get(buff_idx)->data();
333 if (buff_data_ptr)
334 {
335 ::write_vector_data_to_hdf5(file, group_name, "weights", ::hdf5_dtype_cast(tensor->type()),
336 buff_data_ptr,
337 ::hdf5_dims_cast(buff_data_ptr, tensor->shape()));
338 }
339
340 // write quantization parameters
341 auto quant_param = tensor->quantization();
342 if (quant_param)
343 {
344 auto min = quant_param->min();
345 ::write_vector_data_to_hdf5(file, group_name, "min", H5::PredType::NATIVE_FLOAT, min,
346 ::hdf5_dims_cast(min));
347 auto max = quant_param->max();
348 ::write_vector_data_to_hdf5(file, group_name, "max", H5::PredType::NATIVE_FLOAT, max,
349 ::hdf5_dims_cast(max));
350 auto scale = quant_param->scale();
351 ::write_vector_data_to_hdf5(file, group_name, "scale", H5::PredType::NATIVE_FLOAT, scale,
352 ::hdf5_dims_cast(scale));
353 auto zero_point = quant_param->zero_point();
354 ::write_vector_data_to_hdf5(file, group_name, "zero_point", H5::PredType::NATIVE_INT64,
355 zero_point, ::hdf5_dims_cast(zero_point));
356 auto quantized_dimension = quant_param->quantized_dimension();
357 ::write_scalar_data_to_hdf5(file, group_name, "quantized_dimension",
358 H5::PredType::NATIVE_INT32, quantized_dimension);
359 }
360 }
361 }
362}
Loads Circle file and provides helpers to access attributes.
Definition Reader.h:39
const char * tensor_name(const circle::Tensor *tensor)
std::string mangle(const std::string &name)
Construct HDF5-compatible dataset name from a given string.
Definition Common.cpp:19

References mio::circle::Reader::buffers(), flatbuffers::Vector< T >::Get(), mangle(), mio::circle::Reader::num_subgraph(), mio::circle::Reader::select_subgraph(), and mio::circle::Reader::tensors().

Referenced by package.infer.session::inference().


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