3929c8d90de7e06cc33b439d368de6e2a26a9282
[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
26 {
27 nnpkg_run::TensorShape getShape(H5::DataSet &data_set)
28 {
29   std::vector<hsize_t> h5_shape; // hsize_t is unsigned long long
30   H5::DataSpace data_space = data_set.getSpace();
31   int rank = data_space.getSimpleExtentNdims();
32   h5_shape.resize(rank);
33
34   // read shape info from H5 file
35   data_space.getSimpleExtentDims(h5_shape.data(), NULL);
36
37   nnpkg_run::TensorShape shape;
38   for (auto dim : h5_shape)
39     shape.emplace_back(static_cast<int>(dim));
40
41   return shape;
42 }
43 } // namespace
44
45 namespace nnpkg_run
46 {
47 static const char *h5_value_grpname = "value";
48
49 std::vector<TensorShape> H5Formatter::readTensorShapes(const std::string &filename)
50 {
51   uint32_t num_inputs;
52   NNPR_ENSURE_STATUS(nnfw_input_size(session_, &num_inputs));
53   std::vector<TensorShape> tensor_shapes;
54
55   try
56   {
57     H5::Exception::dontPrint();
58
59     H5::H5File file(filename, H5F_ACC_RDONLY);
60     H5::Group value_group = file.openGroup(h5_value_grpname);
61
62     // Constraints: if there are n data set names, they should be unique and
63     //              one of [ "0", "1", .. , "n-1" ]
64     for (uint32_t i = 0; i < num_inputs; ++i)
65     {
66       H5::DataSet data_set = value_group.openDataSet(std::to_string(i));
67       H5::DataType type = data_set.getDataType();
68       auto shape = getShape(data_set);
69
70       tensor_shapes.emplace_back(shape);
71     }
72
73     return tensor_shapes;
74   }
75   catch (const H5::Exception &e)
76   {
77     H5::Exception::printErrorStack();
78     std::exit(-1);
79   }
80   catch (const std::exception &e)
81   {
82     std::cerr << e.what() << std::endl;
83     std::exit(-1);
84   }
85 }
86
87 void H5Formatter::loadInputs(const std::string &filename, std::vector<Allocation> &inputs)
88 {
89   uint32_t num_inputs;
90   NNPR_ENSURE_STATUS(nnfw_input_size(session_, &num_inputs));
91   try
92   {
93     // Turn off the automatic error printing.
94     H5::Exception::dontPrint();
95
96     H5::H5File file(filename, H5F_ACC_RDONLY);
97     H5::Group value_group = file.openGroup(h5_value_grpname);
98     for (uint32_t i = 0; i < num_inputs; ++i)
99     {
100       nnfw_tensorinfo ti;
101       NNPR_ENSURE_STATUS(nnfw_input_tensorinfo(session_, i, &ti));
102
103       // TODO Add Assert(nnfw shape, h5 file shape size)
104
105       // allocate memory for data
106       auto bufsz = bufsize_for(&ti);
107       inputs[i].alloc(bufsz);
108
109       H5::DataSet data_set = value_group.openDataSet(std::to_string(i));
110       H5::DataType type = data_set.getDataType();
111       switch (ti.dtype)
112       {
113         case NNFW_TYPE_TENSOR_FLOAT32:
114           if (type == H5::PredType::IEEE_F32BE || type == H5::PredType::IEEE_F32LE)
115             data_set.read(inputs[i].data(), H5::PredType::NATIVE_FLOAT);
116           else
117             throw std::runtime_error("model input type is f32. But h5 data type is different.");
118           break;
119         case NNFW_TYPE_TENSOR_INT32:
120           if (type == H5::PredType::STD_I32BE || type == H5::PredType::STD_I32LE)
121             data_set.read(inputs[i].data(), H5::PredType::NATIVE_INT32);
122           else
123             throw std::runtime_error("model input type is i32. But h5 data type is different.");
124           break;
125         case NNFW_TYPE_TENSOR_INT64:
126           if (type == H5::PredType::STD_I64BE || type == H5::PredType::STD_I64LE)
127             data_set.read(inputs[i].data(), H5::PredType::NATIVE_INT64);
128           else
129             throw std::runtime_error("model input type is i64. But h5 data type is different.");
130           break;
131         case NNFW_TYPE_TENSOR_QUANT8_ASYMM:
132         case NNFW_TYPE_TENSOR_BOOL:
133         case NNFW_TYPE_TENSOR_UINT8:
134           if (type == H5::PredType::STD_U8BE || type == H5::PredType::STD_U8LE)
135             data_set.read(inputs[i].data(), H5::PredType::NATIVE_UINT8);
136           else
137             throw std::runtime_error(
138                 "model input type is qasymm8, bool or uint8. But h5 data type is different.");
139           break;
140         default:
141           throw std::runtime_error("nnpkg_run can load f32, i32, qasymm8, bool and uint8.");
142       }
143       NNPR_ENSURE_STATUS(nnfw_set_input(session_, i, ti.dtype, inputs[i].data(), bufsz));
144       NNPR_ENSURE_STATUS(nnfw_set_input_layout(session_, i, NNFW_LAYOUT_CHANNELS_LAST));
145     }
146   }
147   catch (const H5::Exception &e)
148   {
149     H5::Exception::printErrorStack();
150     std::exit(-1);
151   }
152   catch (const std::exception &e)
153   {
154     std::cerr << e.what() << std::endl;
155     std::exit(-1);
156   }
157 };
158
159 void H5Formatter::dumpOutputs(const std::string &filename, std::vector<Allocation> &outputs)
160 {
161   uint32_t num_outputs;
162   NNPR_ENSURE_STATUS(nnfw_output_size(session_, &num_outputs));
163   try
164   {
165     // Turn off the automatic error printing.
166     H5::Exception::dontPrint();
167
168     H5::H5File file(filename, H5F_ACC_TRUNC);
169     H5::Group value_group = file.createGroup(h5_value_grpname);
170     for (uint32_t i = 0; i < num_outputs; i++)
171     {
172       nnfw_tensorinfo ti;
173       NNPR_ENSURE_STATUS(nnfw_output_tensorinfo(session_, i, &ti));
174       std::vector<hsize_t> dims(ti.rank);
175       for (uint32_t j = 0; j < ti.rank; ++j)
176       {
177         if (ti.dims[j] >= 0)
178           dims[j] = static_cast<hsize_t>(ti.dims[j]);
179         else
180         {
181           std::cerr << "Negative dimension in output tensor" << std::endl;
182           exit(-1);
183         }
184       }
185       H5::DataSpace data_space(ti.rank, dims.data());
186       switch (ti.dtype)
187       {
188         case NNFW_TYPE_TENSOR_FLOAT32:
189         {
190           H5::DataSet data_set =
191               value_group.createDataSet(std::to_string(i), H5::PredType::IEEE_F32BE, data_space);
192           data_set.write(outputs[i].data(), H5::PredType::NATIVE_FLOAT);
193           break;
194         }
195         case NNFW_TYPE_TENSOR_INT32:
196         {
197           H5::DataSet data_set =
198               value_group.createDataSet(std::to_string(i), H5::PredType::STD_I32LE, data_space);
199           data_set.write(outputs[i].data(), H5::PredType::NATIVE_INT32);
200           break;
201         }
202         case NNFW_TYPE_TENSOR_INT64:
203         {
204           H5::DataSet data_set =
205               value_group.createDataSet(std::to_string(i), H5::PredType::STD_I64LE, data_space);
206           data_set.write(outputs[i].data(), H5::PredType::NATIVE_INT64);
207           break;
208         }
209         case NNFW_TYPE_TENSOR_UINT8:
210         case NNFW_TYPE_TENSOR_QUANT8_ASYMM:
211         {
212           H5::DataSet data_set =
213               value_group.createDataSet(std::to_string(i), H5::PredType::STD_U8BE, data_space);
214           data_set.write(outputs[i].data(), H5::PredType::NATIVE_UINT8);
215           break;
216         }
217         case NNFW_TYPE_TENSOR_BOOL:
218         {
219           H5::DataSet data_set =
220               value_group.createDataSet(std::to_string(i), H5::PredType::STD_U8LE, data_space);
221           data_set.write(outputs[i].data(), H5::PredType::NATIVE_INT8);
222           break;
223         }
224         default:
225           throw std::runtime_error("nnpkg_run can dump f32, i32, qasymm8, bool and uint8.");
226       }
227     }
228   }
229   catch (const H5::Exception &e)
230   {
231     H5::Exception::printErrorStack();
232     std::exit(-1);
233   }
234   catch (const std::runtime_error &e)
235   {
236     std::cerr << "Error during dumpOutputs on nnpackage_run : " << e.what() << std::endl;
237     std::exit(-1);
238   }
239 };
240
241 } // end of namespace nnpkg_run