ONE - On-device Neural Engine
Loading...
Searching...
No Matches
HD5ExportAction Class Referencefinal
Collaboration diagram for HD5ExportAction:

Public Member Functions

 HD5ExportAction (const std::string &path)
 
void run (TensorContext &ctx) override
 
- Public Member Functions inherited from nnkit::Action
virtual ~Action ()=default
 

Detailed Description

Definition at line 28 of file Export.cpp.

Constructor & Destructor Documentation

◆ HD5ExportAction()

HD5ExportAction::HD5ExportAction ( const std::string &  path)
inline

Definition at line 31 of file Export.cpp.

31 : _file{path, H5F_ACC_TRUNC}
32 {
33 _value_grp = _file.createGroup(value_grpname());
34 _name_grp = _file.createGroup(name_grpname());
35 }
std::string value_grpname(void)
Return the name of "value group".
Definition Common.cpp:34
std::string name_grpname(void)
Return the name of "name group".
Definition Common.cpp:37

References name_grpname(), and value_grpname().

Member Function Documentation

◆ run()

void HD5ExportAction::run ( TensorContext ctx)
inlineoverridevirtual

Implements nnkit::Action.

Definition at line 38 of file Export.cpp.

39 {
40 for (uint32_t n = 0; n < ctx.size(); ++n)
41 {
43
44 // TODO Support other data types
45 auto fn = [this](const TensorContext &ctx, uint32_t n, const Reader<float> &t) {
46 const auto name = ctx.name(n);
47 const auto shape = ctx.shape(n);
48
49 const auto rank = shape.rank();
50
51 hsize_t dims[rank];
52
53 for (uint32_t axis = 0; axis < rank; ++axis)
54 {
55 dims[axis] = shape.dim(axis);
56 }
57
58 H5::DataSpace dataspace(rank, dims);
59
60 auto dataset =
61 _value_grp.createDataSet(value_filename(n), H5::PredType::IEEE_F32BE, dataspace);
62
63 float *data = new float[nncc::core::ADT::tensor::num_elements(shape)];
64
68
69 LexicalLayout layout{};
70
71 for (IndexEnumerator e{shape}; e.valid(); e.advance())
72 {
73 auto i = e.current();
74 data[layout.offset(shape, i)] = t.at(i);
75 }
76
77 dataset.write(data, H5::PredType::NATIVE_FLOAT);
78
79 delete[] data;
80
81 // Record name
82 {
83 H5::DataSpace name_dataspace(H5S_SCALAR);
84 H5::StrType name_datatype(H5::PredType::C_S1, name.size());
85
86 auto name_attr =
87 _name_grp.createAttribute(value_filename(n), name_datatype, name_dataspace);
88
89 name_attr.write(name_datatype, name);
90 }
91 };
92
93 ctx.getConstFloatTensor(n, fn);
94 }
95 }
uint32_t rank(void) const
Definition Shape.cpp:35
uint64_t num_elements(const Shape &)
Definition Shape.cpp:51
std::string value_filename(uint32_t n)
Return the name of n-th tensor dataset.
Definition Common.cpp:35
virtual nncc::core::ADT::tensor::Shape shape(uint32_t n) const =0
virtual std::string name(uint32_t n) const =0
virtual void getConstFloatTensor(uint32_t n, const TypedReader< float > &cb) const
virtual uint32_t size(void) const =0

References nnkit::TensorContext::getConstFloatTensor(), nnkit::TensorContext::name(), nncc::core::ADT::tensor::num_elements(), nncc::core::ADT::tensor::Shape::rank(), nnkit::TensorContext::shape(), nnkit::TensorContext::size(), and value_filename().

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


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