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