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"
25 using Shape = luci_interpreter::Shape;
26 using DataType = luci_interpreter::DataType;
31 Shape toInternalShape(const H5::DataSpace &dataspace)
33 int rank = dataspace.getSimpleExtentNdims();
35 std::vector<hsize_t> dims;
37 dataspace.getSimpleExtentDims(dims.data());
40 for (int axis = 0; axis < rank; ++axis)
42 res.dim(axis) = dims[axis];
48 DataType toInternalDtype(const H5::DataType &h5_type)
50 if (h5_type == H5::PredType::IEEE_F32BE || h5_type == H5::PredType::IEEE_F32LE)
52 return DataType::FLOAT32;
54 if (h5_type == H5::PredType::STD_I32BE || h5_type == H5::PredType::STD_I32LE)
58 if (h5_type == H5::PredType::STD_I64BE || h5_type == H5::PredType::STD_I64LE)
62 // Only support three datatypes for now
63 return DataType::Unknown;
66 void readTensorData(H5::DataSet &tensor, uint8_t *buffer)
68 tensor.read(buffer, H5::PredType::NATIVE_UINT8);
71 void readTensorData(H5::DataSet &tensor, float *buffer)
73 tensor.read(buffer, H5::PredType::NATIVE_FLOAT);
76 void readTensorData(H5::DataSet &tensor, int32_t *buffer)
78 tensor.read(buffer, H5::PredType::NATIVE_INT);
81 void readTensorData(H5::DataSet &tensor, int64_t *buffer)
83 tensor.read(buffer, H5::PredType::NATIVE_LONG);
88 namespace record_minmax
91 int32_t HDF5Importer::numInputs(int32_t record_idx)
93 auto records = _value_grp.openGroup(std::to_string(record_idx));
94 return records.getNumObjs();
97 void HDF5Importer::readTensor(int32_t record_idx, int32_t input_idx, void *buffer)
99 auto record = _value_grp.openGroup(std::to_string(record_idx));
100 auto tensor = record.openDataSet(std::to_string(input_idx));
102 readTensorData(tensor, static_cast<uint8_t *>(buffer));
105 void HDF5Importer::readTensor(int32_t record_idx, int32_t input_idx, DataType *dtype, Shape *shape,
108 auto record = _value_grp.openGroup(std::to_string(record_idx));
109 auto tensor = record.openDataSet(std::to_string(input_idx));
111 auto tensor_dtype = tensor.getDataType();
112 *dtype = toInternalDtype(tensor_dtype);
114 auto tensor_shape = tensor.getSpace();
115 *shape = toInternalShape(tensor_shape);
119 case DataType::FLOAT32:
120 readTensorData(tensor, static_cast<float *>(buffer));
123 readTensorData(tensor, static_cast<int32_t *>(buffer));
126 readTensorData(tensor, static_cast<int64_t *>(buffer));
129 throw std::runtime_error{"Unsupported data type for input data (.h5)"};
133 } // namespace record_minmax