ONE - On-device Neural Engine
Loading...
Searching...
No Matches
Reader.cpp
Go to the documentation of this file.
1/*
2 * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "Reader.h"
18
19#include <cstdio>
20#include <stdexcept>
21
22namespace
23{
24bool exists(hid_t id, const char *path) { return H5Lexists(id, path, H5P_DEFAULT) > 0; }
25} // namespace
26
27namespace minmax_embedder
28{
29namespace h5
30{
31static const char *h5_value_grpname = "value";
32
33Reader::Reader(const std::string &filepath) : _file(filepath, H5F_ACC_RDONLY)
34{
35 _val_grp = _file.openGroup(h5_value_grpname);
36}
37
38// TODO: Handle multiple output
39MinMaxVectors Reader::read(int model_idx, int subg_idx, int op_idx) const
40{
41 MinMaxVectors mmv;
42 float minmax[2];
43 auto num_run = _val_grp.getNumObjs();
44 for (uint32_t r = 0; r < num_run; ++r)
45 {
46 // check whether minmax exists
47 char path[128]; // 128 is enough to print "/value/run_%d/model_%d/subg_%d/op_%d" + null
48 snprintf(path, 128, "/value/run_%d/model_%d/subg_%d/op_%d", r, model_idx, subg_idx, op_idx);
49 if (!exists(_file.getId(), path))
50 continue;
51 auto run_grp = _val_grp.openGroup(std::string("run_") + std::to_string(r));
52 auto model_grp = run_grp.openGroup(std::string("model_") + std::to_string(model_idx));
53 auto subg_grp = model_grp.openGroup(std::string("subg_") + std::to_string(subg_idx));
54 auto op_dset = subg_grp.openDataSet(std::string("op_") + std::to_string(op_idx));
55 H5::DataType dtype = op_dset.getDataType();
56 if (not(dtype == H5::PredType::IEEE_F32BE || dtype == H5::PredType::IEEE_F32LE))
57 throw std::runtime_error{"dtype of min, max in h5 is not float."};
58 op_dset.read(minmax, H5::PredType::NATIVE_FLOAT);
59 mmv.min_vector.emplace_back(minmax[0]);
60 mmv.max_vector.emplace_back(minmax[1]);
61 }
62 return mmv;
63}
64
65MinMaxVectors Reader::read_input(int model_idx, int subg_idx, int input_idx) const
66{
67 MinMaxVectors mmv;
68 float minmax[2];
69 auto num_run = _val_grp.getNumObjs();
70 for (uint32_t r = 0; r < num_run; ++r)
71 {
72 // check whether minmax exists
73 char path[128]; // 128 is enough to print "/value/run_%d/model_%d/subg_%d/input_%d" + null
74 snprintf(path, 128, "/value/run_%d/model_%d/subg_%d/input_%d", r, model_idx, subg_idx,
75 input_idx);
76 if (!exists(_file.getId(), path))
77 continue;
78 auto run_grp = _val_grp.openGroup(std::string("run_") + std::to_string(r));
79 auto model_grp = run_grp.openGroup(std::string("model_") + std::to_string(model_idx));
80 auto subg_grp = model_grp.openGroup(std::string("subg_") + std::to_string(subg_idx));
81 auto op_dset = subg_grp.openDataSet(std::string("input_") + std::to_string(input_idx));
82
83 H5::DataType dtype = op_dset.getDataType();
84 if (not(dtype == H5::PredType::IEEE_F32BE || dtype == H5::PredType::IEEE_F32LE))
85 throw std::runtime_error{"dtype of min, max in h5 is not float."};
86 op_dset.read(minmax, H5::PredType::NATIVE_FLOAT);
87 mmv.min_vector.emplace_back(minmax[0]);
88 mmv.max_vector.emplace_back(minmax[1]);
89 }
90 return mmv;
91}
92
93} // namespace h5
94} // namespace minmax_embedder
MinMaxVectors read_input(int model_idx, int subg_idx, int input_idx) const
Returns minmax recording for input {model_idx, subg_idx, input_idx}.
Definition Reader.cpp:65
Reader(const std::string &filepath)
Definition Reader.cpp:33
MinMaxVectors read(int model_idx, int subg_idx, int op_idx) const
Returns minmax recording for op {model_idx, subg_idx, op_idx}.
Definition Reader.cpp:39
This file contains Reader class.
std::vector< float > max_vector
Definition Reader.h:56
std::vector< float > min_vector
Definition Reader.h:55