Imported Upstream version 1.7.0
[platform/core/ml/nnfw.git] / compiler / record-minmax / src / HDF5Importer.cpp
1 /*
2  * Copyright (c) 2020 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 "HDF5Importer.h"
18
19 #include <H5Cpp.h>
20
21 #include <string>
22 #include <cassert>
23
24 using Shape = luci_interpreter::Shape;
25 using DataType = luci_interpreter::DataType;
26
27 namespace
28 {
29
30 Shape toInternalShape(const H5::DataSpace &dataspace)
31 {
32   int rank = dataspace.getSimpleExtentNdims();
33
34   std::vector<hsize_t> dims;
35   dims.resize(rank, 0);
36   dataspace.getSimpleExtentDims(dims.data());
37
38   Shape res(rank);
39   for (int axis = 0; axis < rank; ++axis)
40   {
41     res.dim(axis) = dims[axis];
42   }
43
44   return res;
45 }
46
47 DataType toInternalDtype(const H5::DataType &h5_type)
48 {
49   if (h5_type == H5::PredType::IEEE_F32BE || h5_type == H5::PredType::IEEE_F32LE)
50   {
51     return DataType::FLOAT32;
52   }
53   if (h5_type == H5::PredType::STD_I32BE || h5_type == H5::PredType::STD_I32LE)
54   {
55     return DataType::S32;
56   }
57   if (h5_type == H5::PredType::STD_I64BE || h5_type == H5::PredType::STD_I64LE)
58   {
59     return DataType::S64;
60   }
61   // Only support three datatypes for now
62   return DataType::Unknown;
63 }
64
65 void readTensorData(H5::DataSet &tensor, uint8_t *buffer)
66 {
67   tensor.read(buffer, H5::PredType::NATIVE_UINT8);
68 }
69
70 void readTensorData(H5::DataSet &tensor, float *buffer)
71 {
72   tensor.read(buffer, H5::PredType::NATIVE_FLOAT);
73 }
74
75 void readTensorData(H5::DataSet &tensor, int32_t *buffer)
76 {
77   tensor.read(buffer, H5::PredType::NATIVE_INT);
78 }
79
80 void readTensorData(H5::DataSet &tensor, int64_t *buffer)
81 {
82   tensor.read(buffer, H5::PredType::NATIVE_LONG);
83 }
84
85 } // namespace
86
87 namespace record_minmax
88 {
89
90 int32_t HDF5Importer::numInputs(int32_t record_idx)
91 {
92   auto records = _value_grp.openGroup(std::to_string(record_idx));
93   return records.getNumObjs();
94 }
95
96 void HDF5Importer::readTensor(int32_t record_idx, int32_t input_idx, void *buffer)
97 {
98   auto record = _value_grp.openGroup(std::to_string(record_idx));
99   auto tensor = record.openDataSet(std::to_string(input_idx));
100
101   readTensorData(tensor, static_cast<uint8_t *>(buffer));
102 }
103
104 void HDF5Importer::readTensor(int32_t record_idx, int32_t input_idx, DataType *dtype, Shape *shape,
105                               void *buffer)
106 {
107   auto record = _value_grp.openGroup(std::to_string(record_idx));
108   auto tensor = record.openDataSet(std::to_string(input_idx));
109
110   auto tensor_dtype = tensor.getDataType();
111   *dtype = toInternalDtype(tensor_dtype);
112
113   auto tensor_shape = tensor.getSpace();
114   *shape = toInternalShape(tensor_shape);
115
116   switch (*dtype)
117   {
118     case DataType::FLOAT32:
119       readTensorData(tensor, static_cast<float *>(buffer));
120       break;
121     case DataType::S32:
122       readTensorData(tensor, static_cast<int32_t *>(buffer));
123       break;
124     case DataType::S64:
125       readTensorData(tensor, static_cast<int64_t *>(buffer));
126       break;
127     default:
128       throw std::runtime_error{"Unsupported data type for input data (.h5)"};
129   }
130 }
131
132 } // namespace record_minmax