Imported Upstream version 1.8.0
[platform/core/ml/nnfw.git] / tests / tools / nnpackage_run / src / h5formatter.cc
1 /*
2  * Copyright (c) 2019 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 "h5formatter.h"
18 #include "nnfw.h"
19 #include "nnfw_util.h"
20
21 #include <iostream>
22 #include <stdexcept>
23 #include <H5Cpp.h>
24
25 namespace nnpkg_run
26 {
27 static const char *h5_value_grpname = "value";
28
29 void H5Formatter::loadInputs(const std::string &filename, std::vector<Allocation> &inputs)
30 {
31   uint32_t num_inputs;
32   NNPR_ENSURE_STATUS(nnfw_input_size(session_, &num_inputs));
33   try
34   {
35     // Turn off the automatic error printing.
36     H5::Exception::dontPrint();
37
38     H5::H5File file(filename, H5F_ACC_RDONLY);
39     H5::Group value_group = file.openGroup(h5_value_grpname);
40     for (uint32_t i = 0; i < num_inputs; ++i)
41     {
42       nnfw_tensorinfo ti;
43       NNPR_ENSURE_STATUS(nnfw_input_tensorinfo(session_, i, &ti));
44       // allocate memory for data
45       auto bufsz = bufsize_for(&ti);
46       inputs[i].alloc(bufsz);
47
48       H5::DataSet data_set = value_group.openDataSet(std::to_string(i));
49       H5::DataType type = data_set.getDataType();
50       switch (ti.dtype)
51       {
52         case NNFW_TYPE_TENSOR_FLOAT32:
53           if (type == H5::PredType::IEEE_F32BE || type == H5::PredType::IEEE_F32LE)
54             data_set.read(inputs[i].data(), H5::PredType::NATIVE_FLOAT);
55           else
56             throw std::runtime_error("model input type is f32. But h5 data type is different.");
57           break;
58         case NNFW_TYPE_TENSOR_INT32:
59           if (type == H5::PredType::STD_I32BE || type == H5::PredType::STD_I32LE)
60             data_set.read(inputs[i].data(), H5::PredType::NATIVE_INT32);
61           else
62             throw std::runtime_error("model input type is i32. But h5 data type is different.");
63           break;
64         case NNFW_TYPE_TENSOR_INT64:
65           if (type == H5::PredType::STD_I64BE || type == H5::PredType::STD_I64LE)
66             data_set.read(inputs[i].data(), H5::PredType::NATIVE_INT64);
67           else
68             throw std::runtime_error("model input type is i64. But h5 data type is different.");
69           break;
70         case NNFW_TYPE_TENSOR_QUANT8_ASYMM:
71         case NNFW_TYPE_TENSOR_BOOL:
72         case NNFW_TYPE_TENSOR_UINT8:
73           if (type == H5::PredType::STD_U8BE || type == H5::PredType::STD_U8LE)
74             data_set.read(inputs[i].data(), H5::PredType::NATIVE_UINT8);
75           else
76             throw std::runtime_error(
77                 "model input type is qasymm8, bool or uint8. But h5 data type is different.");
78           break;
79         default:
80           throw std::runtime_error("nnpkg_run can load f32, i32, qasymm8, bool and uint8.");
81       }
82       NNPR_ENSURE_STATUS(nnfw_set_input(session_, i, ti.dtype, inputs[i].data(), bufsz));
83       NNPR_ENSURE_STATUS(nnfw_set_input_layout(session_, i, NNFW_LAYOUT_CHANNELS_LAST));
84     }
85   }
86   catch (const H5::Exception &e)
87   {
88     H5::Exception::printErrorStack();
89     std::exit(-1);
90   }
91   catch (const std::exception &e)
92   {
93     std::cerr << e.what() << std::endl;
94     std::exit(-1);
95   }
96 };
97
98 void H5Formatter::dumpOutputs(const std::string &filename, std::vector<Allocation> &outputs)
99 {
100   uint32_t num_outputs;
101   NNPR_ENSURE_STATUS(nnfw_output_size(session_, &num_outputs));
102   try
103   {
104     // Turn off the automatic error printing.
105     H5::Exception::dontPrint();
106
107     H5::H5File file(filename, H5F_ACC_TRUNC);
108     H5::Group value_group = file.createGroup(h5_value_grpname);
109     for (uint32_t i = 0; i < num_outputs; i++)
110     {
111       nnfw_tensorinfo ti;
112       NNPR_ENSURE_STATUS(nnfw_output_tensorinfo(session_, i, &ti));
113       std::vector<hsize_t> dims(ti.rank);
114       for (uint32_t j = 0; j < ti.rank; ++j)
115       {
116         if (ti.dims[j] >= 0)
117           dims[j] = static_cast<hsize_t>(ti.dims[j]);
118         else
119         {
120           std::cerr << "Negative dimension in output tensor" << std::endl;
121           exit(-1);
122         }
123       }
124       H5::DataSpace data_space(ti.rank, dims.data());
125       switch (ti.dtype)
126       {
127         case NNFW_TYPE_TENSOR_FLOAT32:
128         {
129           H5::DataSet data_set =
130               value_group.createDataSet(std::to_string(i), H5::PredType::IEEE_F32BE, data_space);
131           data_set.write(outputs[i].data(), H5::PredType::NATIVE_FLOAT);
132           break;
133         }
134         case NNFW_TYPE_TENSOR_INT32:
135         {
136           H5::DataSet data_set =
137               value_group.createDataSet(std::to_string(i), H5::PredType::STD_I32LE, data_space);
138           data_set.write(outputs[i].data(), H5::PredType::NATIVE_INT32);
139           break;
140         }
141         case NNFW_TYPE_TENSOR_INT64:
142         {
143           H5::DataSet data_set =
144               value_group.createDataSet(std::to_string(i), H5::PredType::STD_I64LE, data_space);
145           data_set.write(outputs[i].data(), H5::PredType::NATIVE_INT64);
146           break;
147         }
148         case NNFW_TYPE_TENSOR_UINT8:
149         case NNFW_TYPE_TENSOR_QUANT8_ASYMM:
150         {
151           H5::DataSet data_set =
152               value_group.createDataSet(std::to_string(i), H5::PredType::STD_U8BE, data_space);
153           data_set.write(outputs[i].data(), H5::PredType::NATIVE_UINT8);
154           break;
155         }
156         case NNFW_TYPE_TENSOR_BOOL:
157         {
158           H5::DataSet data_set =
159               value_group.createDataSet(std::to_string(i), H5::PredType::STD_I8LE, data_space);
160           data_set.write(outputs[i].data(), H5::PredType::NATIVE_INT8);
161           break;
162         }
163         default:
164           throw std::runtime_error("nnpkg_run can dump f32, i32, qasymm8, bool and uint8.");
165       }
166     }
167   }
168   catch (const H5::Exception &e)
169   {
170     H5::Exception::printErrorStack();
171     std::exit(-1);
172   }
173   catch (const std::runtime_error &e)
174   {
175     std::cerr << "Error during dumpOutputs on nnpackage_run : " << e.what() << std::endl;
176     std::exit(-1);
177   }
178 };
179
180 } // end of namespace nnpkg_run