2 * Copyright (c) 2020 Samsung Electronics Co., Ltd. All Rights Reserved
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include "HDF5Importer.h"
24 using Shape = luci_interpreter::Shape;
25 using DataType = luci_interpreter::DataType;
30 Shape toInternalShape(const H5::DataSpace &dataspace)
32 int rank = dataspace.getSimpleExtentNdims();
34 std::vector<hsize_t> dims;
36 dataspace.getSimpleExtentDims(dims.data());
39 for (int axis = 0; axis < rank; ++axis)
41 res.dim(axis) = dims[axis];
47 DataType toInternalDtype(const H5::DataType &h5_type)
49 if (h5_type == H5::PredType::IEEE_F32BE || h5_type == H5::PredType::IEEE_F32LE)
51 return DataType::FLOAT32;
53 if (h5_type == H5::PredType::STD_I32BE || h5_type == H5::PredType::STD_I32LE)
57 if (h5_type == H5::PredType::STD_I64BE || h5_type == H5::PredType::STD_I64LE)
61 // Only support three datatypes for now
62 return DataType::Unknown;
65 void readTensorData(H5::DataSet &tensor, uint8_t *buffer)
67 tensor.read(buffer, H5::PredType::NATIVE_UINT8);
70 void readTensorData(H5::DataSet &tensor, float *buffer)
72 tensor.read(buffer, H5::PredType::NATIVE_FLOAT);
75 void readTensorData(H5::DataSet &tensor, int32_t *buffer)
77 tensor.read(buffer, H5::PredType::NATIVE_INT);
80 void readTensorData(H5::DataSet &tensor, int64_t *buffer)
82 tensor.read(buffer, H5::PredType::NATIVE_LONG);
87 namespace record_minmax
90 int32_t HDF5Importer::numInputs(int32_t record_idx)
92 auto records = _value_grp.openGroup(std::to_string(record_idx));
93 return records.getNumObjs();
96 void HDF5Importer::readTensor(int32_t record_idx, int32_t input_idx, void *buffer)
98 auto record = _value_grp.openGroup(std::to_string(record_idx));
99 auto tensor = record.openDataSet(std::to_string(input_idx));
101 readTensorData(tensor, static_cast<uint8_t *>(buffer));
104 void HDF5Importer::readTensor(int32_t record_idx, int32_t input_idx, DataType *dtype, Shape *shape,
107 auto record = _value_grp.openGroup(std::to_string(record_idx));
108 auto tensor = record.openDataSet(std::to_string(input_idx));
110 auto tensor_dtype = tensor.getDataType();
111 *dtype = toInternalDtype(tensor_dtype);
113 auto tensor_shape = tensor.getSpace();
114 *shape = toInternalShape(tensor_shape);
118 case DataType::FLOAT32:
119 readTensorData(tensor, static_cast<float *>(buffer));
122 readTensorData(tensor, static_cast<int32_t *>(buffer));
125 readTensorData(tensor, static_cast<int64_t *>(buffer));
128 throw std::runtime_error{"Unsupported data type for input data (.h5)"};
132 } // namespace record_minmax