From 861153850b3fe123c1a36e657f3e7f55f8126ffe Mon Sep 17 00:00:00 2001 From: Hyeongseok Oh Date: Tue, 14 May 2024 10:28:22 +0900 Subject: [PATCH] Remove hdf5 dependency on minmax embedding --- infra/nnfw/CMakeLists.txt | 6 - runtime/onert/core/CMakeLists.txt | 16 +- runtime/onert/core/include/util/MinMaxMap.h | 1 + .../core/src/compiler/ExecutorFactory.cc | 4 - runtime/onert/core/src/exec/MinMaxData.cc | 113 +++++++++++ runtime/onert/core/src/exec/MinMaxData.h | 74 +++++++ runtime/onert/core/src/exec/MinMaxRecorder.h | 4 +- runtime/onert/odc/CMakeLists.txt | 7 - runtime/onert/odc/Embedder.cc | 6 +- runtime/onert/odc/MinMaxReader.cc | 188 ++++++++++++++++++ runtime/onert/odc/MinMaxReader.h | 80 ++++++++ runtime/onert/odc/h5/Reader.cc | 98 --------- runtime/onert/odc/h5/Reader.h | 87 -------- 13 files changed, 462 insertions(+), 222 deletions(-) create mode 100644 runtime/onert/core/src/exec/MinMaxData.cc create mode 100644 runtime/onert/core/src/exec/MinMaxData.h create mode 100644 runtime/onert/odc/MinMaxReader.cc create mode 100644 runtime/onert/odc/MinMaxReader.h delete mode 100644 runtime/onert/odc/h5/Reader.cc delete mode 100644 runtime/onert/odc/h5/Reader.h diff --git a/infra/nnfw/CMakeLists.txt b/infra/nnfw/CMakeLists.txt index 2561c33fa2b..2e09c534ffa 100644 --- a/infra/nnfw/CMakeLists.txt +++ b/infra/nnfw/CMakeLists.txt @@ -101,12 +101,6 @@ if(ENABLE_STRICT_BUILD) target_compile_options(nnfw_common INTERFACE -Werror -Wall -Wextra) endif(ENABLE_STRICT_BUILD) -macro(nnfw_strict_build TARGET) - if(ENABLE_STRICT_BUILD) - target_compile_options(${TARGET} PRIVATE -Werror -Wall -Wextra) - endif(ENABLE_STRICT_BUILD) -endmacro(nnfw_strict_build) - # TODO Replace using default build option setting in cmake/buildtool/config/config_linux.cmake # to link nnfw_coverage on each module which want to check coverage add_library(nnfw_coverage INTERFACE) diff --git a/runtime/onert/core/CMakeLists.txt b/runtime/onert/core/CMakeLists.txt index fa12d6063e1..7b0decccf79 100644 --- a/runtime/onert/core/CMakeLists.txt +++ b/runtime/onert/core/CMakeLists.txt @@ -2,12 +2,8 @@ file(GLOB_RECURSE SOURCES "src/*.cc") file(GLOB_RECURSE TESTS "*.test.cc") list(REMOVE_ITEM SOURCES ${TESTS}) -if(NOT BUILD_MINMAX_H5DUMPER) - file(GLOB_RECURSE SRC_TO_REMOVE "src/dumper/h5/*.cc") - list(REMOVE_ITEM SOURCES ${SRC_TO_REMOVE}) - file(GLOB_RECURSE SRC_TO_REMOVE "src/exec/MinMaxRecorder.cc") - list(REMOVE_ITEM SOURCES ${SRC_TO_REMOVE}) -endif(NOT BUILD_MINMAX_H5DUMPER) +file(GLOB_RECURSE SRC_TO_REMOVE "src/dumper/h5/*.cc") +list(REMOVE_ITEM SOURCES ${SRC_TO_REMOVE}) add_library(onert_core SHARED ${SOURCES}) set_target_properties(onert_core PROPERTIES POSITION_INDEPENDENT_CODE ON) @@ -34,14 +30,6 @@ nnfw_find_package(Ruy REQUIRED) target_link_libraries(onert_core PRIVATE ruy) target_link_libraries(onert_core INTERFACE ruy_instrumentation) -# H5 Minmax Dumper -if(BUILD_MINMAX_H5DUMPER) - nnfw_find_package(HDF5 REQUIRED) - target_compile_definitions(onert_core PRIVATE MINMAX_H5DUMPER=1) - target_include_directories(onert_core PRIVATE ${HDF5_INCLUDE_DIRS}) - target_link_libraries(onert_core PRIVATE ${HDF5_CXX_LIBRARIES}) -endif(BUILD_MINMAX_H5DUMPER) - if(CMAKE_BUILD_TYPE_LC STREQUAL "release") add_custom_command(TARGET onert_core POST_BUILD COMMAND ${CMAKE_STRIP} "--strip-unneeded" $) diff --git a/runtime/onert/core/include/util/MinMaxMap.h b/runtime/onert/core/include/util/MinMaxMap.h index 2245f84b0b0..62fd77ab2c5 100644 --- a/runtime/onert/core/include/util/MinMaxMap.h +++ b/runtime/onert/core/include/util/MinMaxMap.h @@ -36,6 +36,7 @@ template > class MinMaxMap void append(N node, float min, float max) { _minmax_map[node] = {min, max}; } auto begin() const { return _minmax_map.begin(); } auto end() const { return _minmax_map.end(); } + auto size() const { return _minmax_map.size(); } private: std::unordered_map _minmax_map; diff --git a/runtime/onert/core/src/compiler/ExecutorFactory.cc b/runtime/onert/core/src/compiler/ExecutorFactory.cc index 60d45dea10d..e0b90e8dac9 100644 --- a/runtime/onert/core/src/compiler/ExecutorFactory.cc +++ b/runtime/onert/core/src/compiler/ExecutorFactory.cc @@ -26,9 +26,7 @@ #include "../exec/ExecTime.h" #include "../exec/ExecutionObservers.h" #include "../exec/LinearExecutor.h" -#ifdef MINMAX_H5DUMPER #include "../exec/MinMaxRecorder.h" -#endif #include "../exec/ParallelExecutor.h" #include "../exec/train/TrainableExecutor.h" #include "../ir/OperationCloner.h" @@ -509,11 +507,9 @@ ExecutorFactory::createLinearExecutor(std::unique_ptr lo std::make_unique(options->trace_filepath, exec->graph(), tracing_ctx); exec->addObserver(std::move(ctp)); } -#ifdef MINMAX_H5DUMPER if (!options->minmax_filepath.empty()) exec->addObserver(std::make_unique( options->minmax_filepath, exec->graph(), exec->getBackendContexts())); -#endif return exec; } diff --git a/runtime/onert/core/src/exec/MinMaxData.cc b/runtime/onert/core/src/exec/MinMaxData.cc new file mode 100644 index 00000000000..631bf766112 --- /dev/null +++ b/runtime/onert/core/src/exec/MinMaxData.cc @@ -0,0 +1,113 @@ + +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "MinMaxData.h" + +#include +#include + +namespace onert +{ +namespace exec +{ + +RawMinMaxDumper::RawMinMaxDumper(const std::string &filename) : _filename(filename) {} + +void RawMinMaxDumper::dump(const exec::IOMinMaxMap &input_minmax, + const exec::OpMinMaxMap &op_minmax) const +{ + // Find file is already exist for modifying + auto file = std::fopen(_filename.c_str(), "rb+"); + uint32_t runs = 1; + if (!file) + { + // If file is not exist, create new file + file = std::fopen(_filename.c_str(), "wb+"); + + // Write run count + std::fwrite(&runs, sizeof(uint32_t), 1, file); + } + else + { + // Read run count + auto read_size = std::fread(&runs, sizeof(uint32_t), 1, file); + if (read_size != 1) + throw std::runtime_error{"Failed to read run count"}; + runs++; + + // Overwrite run count + std::fseek(file, 0, SEEK_SET); + std::fwrite(&runs, sizeof(uint32_t), 1, file); + + // Go to end of file to append new data + std::fseek(file, 0, SEEK_END); + } + + uint32_t input_count = input_minmax.size(); + uint32_t op_count = op_minmax.size(); + + // std::cout << "[MinMaxData] runs: " << runs << std::endl; + // std::cout << "[MinMaxData] input_count: " << input_count << std::endl; + // std::cout << "[MinMaxData] op_count: " << op_count << std::endl; + + // Write op_count and input_count + std::fwrite(&op_count, sizeof(uint32_t), 1, file); + std::fwrite(&input_count, sizeof(uint32_t), 1, file); + + // For each op + for (auto &&elem : op_minmax) + { + const uint32_t model_idx = 0; + const uint32_t subg_idx = elem.first.first.value(); + const uint32_t op_idx = elem.first.second.value(); + + // Write model/subg/op index + std::fwrite(&model_idx, sizeof(uint32_t), 1, file); + std::fwrite(&subg_idx, sizeof(uint32_t), 1, file); + std::fwrite(&op_idx, sizeof(uint32_t), 1, file); + + // Write min/max + std::fwrite(elem.second.data, sizeof(float), 2, file); + + // std::cout << "[MinMaxData] OP [" << model_idx << ", " << subg_idx << ", " << op_idx + // << "]: " << elem.second.data[0] << ", " << elem.second.data[1] << std::endl; + } + + // For each input + for (auto &&elem : input_minmax) + { + const uint32_t model_idx = 0; + const uint32_t subg_idx = elem.first.first.value(); + const uint32_t input_idx = elem.first.second.value(); + + // Write model/subg/input index + std::fwrite(&model_idx, sizeof(uint32_t), 1, file); + std::fwrite(&subg_idx, sizeof(uint32_t), 1, file); + std::fwrite(&input_idx, sizeof(uint32_t), 1, file); + + // Write min/max + std::fwrite(elem.second.data, sizeof(float), 2, file); + + // std::cout << "[MinMaxData] Input [" << model_idx << ", " << subg_idx << ", " << input_idx + // << "]: " << elem.second.data[0] << ", " << elem.second.data[1] << std::endl; + } + + fclose(file); +} + +} // namespace exec +} // namespace onert diff --git a/runtime/onert/core/src/exec/MinMaxData.h b/runtime/onert/core/src/exec/MinMaxData.h new file mode 100644 index 00000000000..aed40211040 --- /dev/null +++ b/runtime/onert/core/src/exec/MinMaxData.h @@ -0,0 +1,74 @@ + +/* + * Copyright (c) 2024 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __ONERT_EXEC_MINMAX_DATA_H__ +#define __ONERT_EXEC_MINMAX_DATA_H__ + +#include "exec/MinMaxMap.h" + +#include + +namespace onert +{ +namespace exec +{ + +// Because IOMinMaxMap and OpMinMaxMap does not have the ordering and size information, +// we need to dump model, subgraph id for each minmax + +// File structure +// uint32_t num of runs + +// For each run +// uint32_t num of operations +// uint32_t num of inputs + +// For each operation +// uint32_t model id +// uint32_t subgraph id +// uint32_t operation id +// float min +// float max + +// For each input +// uint32_t model id +// uint32_t subgraph id +// uint32_t input id +// float min +// float max + +class RawMinMaxDumper +{ +public: + RawMinMaxDumper(const std::string &filename); + /** + * @brief Dump input minmax map + * + * @param[in] in_minmax input minmax map + * @param[in] op_minmax op minmax map + */ + + void dump(const exec::IOMinMaxMap &in_minmax, const exec::OpMinMaxMap &op_minmax) const; + +private: + std::string _filename; +}; + +} // namespace exec +} // namespace onert + +#endif // __ONERT_EXEC_MINMAX_DATA_H__ diff --git a/runtime/onert/core/src/exec/MinMaxRecorder.h b/runtime/onert/core/src/exec/MinMaxRecorder.h index ba7c70f9c7d..b96cdbaad9e 100644 --- a/runtime/onert/core/src/exec/MinMaxRecorder.h +++ b/runtime/onert/core/src/exec/MinMaxRecorder.h @@ -20,7 +20,7 @@ #include "ExecutionObservers.h" #include "ir/Index.h" #include "exec/MinMaxMap.h" -#include "../dumper/h5/MinMaxDumper.h" +#include "MinMaxData.h" #include @@ -47,7 +47,7 @@ class MinMaxRecorder : public IExecutionObserver private: const ir::Graph &_graph; const backend::BackendContexts &_backend_contexts; - dumper::h5::MinMaxDumper _h5dumper; + RawMinMaxDumper _h5dumper; OpMinMaxMap _op_minmax; IOMinMaxMap _input_minmax; }; diff --git a/runtime/onert/odc/CMakeLists.txt b/runtime/onert/odc/CMakeLists.txt index be9978e8ce4..13ade89bc34 100644 --- a/runtime/onert/odc/CMakeLists.txt +++ b/runtime/onert/odc/CMakeLists.txt @@ -1,13 +1,8 @@ nnfw_find_package(Luci QUIET) -nnfw_find_package(HDF5 QUIET) if(NOT Luci_FOUND) message(STATUS "Luci not found. Skip onert_odc") return() endif() -if(NOT HDF5_FOUND) - message(STATUS "HDF5 not found. Skip onert_odc") - return() -endif() file(GLOB_RECURSE SOURCES "*.cc") file(GLOB_RECURSE TESTS "*.test.cc") @@ -15,10 +10,8 @@ list(REMOVE_ITEM SOURCES ${TESTS}) add_library(onert_odc SHARED ${SOURCES}) target_link_libraries(onert_odc PRIVATE onert_core luci::import luci::export luci::pass luci::loco) -target_link_libraries(onert_odc PRIVATE ${HDF5_CXX_LIBRARIES}) target_link_libraries(onert_odc PRIVATE nnfw_common) target_link_libraries(onert_odc PRIVATE nnfw_coverage) -target_include_directories(onert_odc PRIVATE ${HDF5_INCLUDE_DIRS}) if(CMAKE_BUILD_TYPE_LC STREQUAL "release") add_custom_command(TARGET onert_odc POST_BUILD diff --git a/runtime/onert/odc/Embedder.cc b/runtime/onert/odc/Embedder.cc index a06d7ad87ba..bc5278f2d58 100644 --- a/runtime/onert/odc/Embedder.cc +++ b/runtime/onert/odc/Embedder.cc @@ -15,6 +15,7 @@ */ #include "Embedder.h" +#include "MinMaxReader.h" #include #include @@ -24,11 +25,8 @@ #include #include -#include "h5/Reader.h" - #include #include // for std::floor -#include #include namespace @@ -82,7 +80,7 @@ void Embedder::embed(luci::Module *module, const std::string &minmax_path, if (module == nullptr) throw std::runtime_error{"Input module is nullptr"}; - h5::Reader mmr{minmax_path}; + MinMaxReader mmr{minmax_path}; for (size_t idx = 0; idx < module->size(); ++idx) { diff --git a/runtime/onert/odc/MinMaxReader.cc b/runtime/onert/odc/MinMaxReader.cc new file mode 100644 index 00000000000..44e5f922a7a --- /dev/null +++ b/runtime/onert/odc/MinMaxReader.cc @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "MinMaxReader.h" + +#include +#include +#include + +namespace +{ + +void inline readMMFile(void *ptr, size_t size, size_t count, FILE *fp, const std::string &err_msg) +{ + if (fread(ptr, size, count, fp) != count) + throw std::runtime_error(err_msg); +} + +} // namespace + +namespace onert +{ +namespace odc +{ + +MinMaxReader::MinMaxReader(const std::string &filepath) : _filepath(filepath) +{ + // DO NOTHING +} + +// TODO: Handle multiple output +MinMaxVectors MinMaxReader::read(uint32_t model_idx, uint32_t subg_idx, uint32_t op_idx) const +{ + // Find file to read + auto file = std::fopen(_filepath.c_str(), "rb"); + if (!file) + throw std::runtime_error("Cannot open file: " + _filepath); + + // Read num_run + uint32_t num_run = 0; + readMMFile(&num_run, sizeof(uint32_t), 1, file, "Cannot read num_run from file"); + + // std::cout << "[MinMaxRead] runs: " << num_run << std::endl; + + MinMaxVectors mmv; + float minmax[2]; + size_t data_size = sizeof(float) * 2 + sizeof(uint32_t) * 3; + + for (uint32_t r = 0; r < num_run; ++r) + { + // Read num of operations and num of inputs + uint32_t num_op = 0; + readMMFile(&num_op, sizeof(uint32_t), 1, file, "Cannot read num of operations"); + uint32_t num_input = 0; + readMMFile(&num_input, sizeof(uint32_t), 1, file, "Cannot read num of inputs"); + + // std::cout << "[MinMaxRead] run no:" << r << " num_op:" << num_op << " num_input:" << + // num_input + // << std::endl; + + // Find operation + for (uint32_t i = 0; i < num_op; ++i) + { + uint32_t model_id_from_file = 0; + uint32_t subg_idx_from_file = 0; + uint32_t op_idx_from_file = 0; + + readMMFile(&model_id_from_file, sizeof(uint32_t), 1, file, "Cannot read model_id from file"); + readMMFile(&subg_idx_from_file, sizeof(uint32_t), 1, file, "Cannot read subg_idx from file"); + readMMFile(&op_idx_from_file, sizeof(uint32_t), 1, file, "Cannot read op_idx from file"); + + if (model_id_from_file == model_idx && subg_idx_from_file == subg_idx && + op_idx_from_file == op_idx) + { + // Read minmax data + readMMFile(minmax, sizeof(float), 2, file, "Cannot read minmax data from file"); + mmv.min_vector.emplace_back(minmax[0]); + mmv.max_vector.emplace_back(minmax[1]); + + // std::cout << "[MinMaxRead] OP [" << model_id_from_file << ", " << subg_idx_from_file << + // ", " + // << op_idx_from_file << "]: " << minmax[0] << ", " << minmax[1] << std::endl; + + // Skip remain operation minmax data + uint32_t remain_elem = num_op - i - 1; + std::fseek(file, data_size * remain_elem, SEEK_CUR); + break; + } + + // Skip minmax data + std::fseek(file, sizeof(float) * 2, SEEK_CUR); + } + + // Skip input minmax data + std::fseek(file, data_size * num_input, SEEK_CUR); + } + + fclose(file); + return mmv; +} + +MinMaxVectors MinMaxReader::read_input(uint32_t model_idx, uint32_t subg_idx, + uint32_t input_idx) const +{ + // Find file to read + auto file = std::fopen(_filepath.c_str(), "rb"); + if (!file) + throw std::runtime_error("Cannot open file: " + _filepath); + + // Read num_run + uint32_t num_run = 0; + readMMFile(&num_run, sizeof(uint32_t), 1, file, "Cannot read num_run from file"); + + // std::cout << "[MinMaxRead] runs: " << num_run << std::endl; + + MinMaxVectors mmv; + float minmax[2]; + size_t data_size = sizeof(float) * 2 + sizeof(uint32_t) * 3; + + for (uint32_t r = 0; r < num_run; ++r) + { + // Read num of operations and num of inputs + uint32_t num_op = 0; + readMMFile(&num_op, sizeof(uint32_t), 1, file, "Cannot read num of operations"); + uint32_t num_input = 0; + readMMFile(&num_input, sizeof(uint32_t), 1, file, "Cannot read num of inputs"); + + // std::cout << "[MinMaxRead] run no:" << r << " num_op:" << num_op << " num_input:" << + // num_input + // << std::endl; + + // Skip operation minmax data + std::fseek(file, data_size * num_op, SEEK_CUR); + + // Find operation + for (uint32_t i = 0; i < num_input; ++i) + { + uint32_t model_id_from_file = 0; + uint32_t subg_idx_from_file = 0; + uint32_t input_idx_from_file = 0; + + readMMFile(&model_id_from_file, sizeof(uint32_t), 1, file, "Cannot read model_id from file"); + readMMFile(&subg_idx_from_file, sizeof(uint32_t), 1, file, "Cannot read subg_idx from file"); + readMMFile(&input_idx_from_file, sizeof(uint32_t), 1, file, + "Cannot read input_idx from file"); + + if (model_id_from_file == model_idx && subg_idx_from_file == subg_idx && + input_idx_from_file == input_idx) + { + // Read minmax data + readMMFile(minmax, sizeof(float), 2, file, "Cannot read minmax data from file"); + mmv.min_vector.emplace_back(minmax[0]); + mmv.max_vector.emplace_back(minmax[1]); + + // std::cout << "[MinMaxRead] Input [" << model_id_from_file << ", " << subg_idx_from_file + // << ", " << input_idx_from_file << "]: " << minmax[0] << ", " << minmax[1] + // << std::endl; + + // Skip remain input minmax data + uint32_t remain_elem = num_input - i - 1; + std::fseek(file, data_size * remain_elem, SEEK_CUR); + break; + } + + // Skip minmax data + std::fseek(file, sizeof(float) * 2, SEEK_CUR); + } + } + + fclose(file); + return mmv; +} + +} // namespace odc +} // namespace onert diff --git a/runtime/onert/odc/MinMaxReader.h b/runtime/onert/odc/MinMaxReader.h new file mode 100644 index 00000000000..4f6d5fde517 --- /dev/null +++ b/runtime/onert/odc/MinMaxReader.h @@ -0,0 +1,80 @@ +/* + * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef __ONERT_ODC_MINMAX_READER_H__ +#define __ONERT_ODC_MINMAX_READER_H__ + +#include +#include +#include + +namespace onert +{ +namespace odc +{ + +// File structure +// uint32_t num of runs + +// For each run +// uint32_t num of operations +// uint32_t num of inputs + +// For each operation +// uint32_t model id +// uint32_t subgraph id +// uint32_t operation id +// float min +// float max + +// For each input +// uint32_t model id +// uint32_t subgraph id +// uint32_t input id +// float min +// float max + +struct MinMaxVectors +{ + std::vector min_vector; + std::vector max_vector; +}; + +class MinMaxReader +{ +public: + MinMaxReader(const std::string &filepath); + /** + * @brief Returns minmax recording for op {model_idx, subg_idx, op_idx} + * + * @return MinMaxVectors + */ + MinMaxVectors read(uint32_t model_idx, uint32_t subg_idx, uint32_t op_idx) const; + /** + * @brief Returns minmax recording for input {model_idx, subg_idx, input_idx} + * + * @return MinMaxVectors + */ + MinMaxVectors read_input(uint32_t model_idx, uint32_t subg_idx, uint32_t input_idx) const; + +private: + std::string _filepath; +}; + +} // namespace odc +} // namespace onert + +#endif // __ONERT_ODC_MINMAX_READER_H__ diff --git a/runtime/onert/odc/h5/Reader.cc b/runtime/onert/odc/h5/Reader.cc deleted file mode 100644 index bac8c8c5923..00000000000 --- a/runtime/onert/odc/h5/Reader.cc +++ /dev/null @@ -1,98 +0,0 @@ -/* - * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "Reader.h" - -#include -#include - -namespace -{ -bool exists(hid_t id, const char *path) { return H5Lexists(id, path, H5P_DEFAULT) > 0; } -} // namespace - -namespace onert -{ -namespace odc -{ -namespace h5 -{ -static const char *h5_value_grpname = "value"; - -Reader::Reader(const std::string &filepath) - : _file(filepath, H5F_ACC_RDONLY), _val_grp(_file.openGroup(h5_value_grpname)) -{ - // DO NOTHING -} - -// TODO: Handle multiple output -MinMaxVectors Reader::read(int model_idx, int subg_idx, int op_idx) const -{ - MinMaxVectors mmv; - float minmax[2]; - auto num_run = _val_grp.getNumObjs(); - for (uint32_t r = 0; r < num_run; ++r) - { - // check whether minmax exists - char path[128]; // 128 is enough to print "/value/run_%d/model_%d/subg_%d/op_%d" + null - snprintf(path, 128, "/value/run_%d/model_%d/subg_%d/op_%d", r, model_idx, subg_idx, op_idx); - if (!exists(_file.getId(), path)) - continue; - auto run_grp = _val_grp.openGroup(std::string("run_") + std::to_string(r)); - auto model_grp = run_grp.openGroup(std::string("model_") + std::to_string(model_idx)); - auto subg_grp = model_grp.openGroup(std::string("subg_") + std::to_string(subg_idx)); - auto op_dset = subg_grp.openDataSet(std::string("op_") + std::to_string(op_idx)); - H5::DataType dtype = op_dset.getDataType(); - if (not(dtype == H5::PredType::IEEE_F32BE || dtype == H5::PredType::IEEE_F32LE)) - throw std::runtime_error{"dtype of min, max in h5 is not float."}; - op_dset.read(minmax, H5::PredType::NATIVE_FLOAT); - mmv.min_vector.emplace_back(minmax[0]); - mmv.max_vector.emplace_back(minmax[1]); - } - return mmv; -} - -MinMaxVectors Reader::read_input(int model_idx, int subg_idx, int input_idx) const -{ - MinMaxVectors mmv; - float minmax[2]; - auto num_run = _val_grp.getNumObjs(); - for (uint32_t r = 0; r < num_run; ++r) - { - // check whether minmax exists - char path[128]; // 128 is enough to print "/value/run_%d/model_%d/subg_%d/input_%d" + null - snprintf(path, 128, "/value/run_%d/model_%d/subg_%d/input_%d", r, model_idx, subg_idx, - input_idx); - if (!exists(_file.getId(), path)) - continue; - auto run_grp = _val_grp.openGroup(std::string("run_") + std::to_string(r)); - auto model_grp = run_grp.openGroup(std::string("model_") + std::to_string(model_idx)); - auto subg_grp = model_grp.openGroup(std::string("subg_") + std::to_string(subg_idx)); - auto op_dset = subg_grp.openDataSet(std::string("input_") + std::to_string(input_idx)); - - H5::DataType dtype = op_dset.getDataType(); - if (not(dtype == H5::PredType::IEEE_F32BE || dtype == H5::PredType::IEEE_F32LE)) - throw std::runtime_error{"dtype of min, max in h5 is not float."}; - op_dset.read(minmax, H5::PredType::NATIVE_FLOAT); - mmv.min_vector.emplace_back(minmax[0]); - mmv.max_vector.emplace_back(minmax[1]); - } - return mmv; -} - -} // namespace h5 -} // namespace odc -} // namespace onert diff --git a/runtime/onert/odc/h5/Reader.h b/runtime/onert/odc/h5/Reader.h deleted file mode 100644 index 12e80aa39c4..00000000000 --- a/runtime/onert/odc/h5/Reader.h +++ /dev/null @@ -1,87 +0,0 @@ -/* - * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef __ONERT_ODC_H5_READER_H__ -#define __ONERT_ODC_H5_READER_H__ - -#include -#include -#include -#include - -namespace onert -{ -namespace odc -{ -namespace h5 -{ -// The hierachy of single model minmax h5 file -// -// GROUP / -// GROUP value -// └── GROUP run_{idx} -// └── GROUP model_{idx} -// └── GROUP subg_{idx} -// ├── DATASET op_{idx} -// │ DATATYPE Float32 -// │ DATASPACE (2) -// │ DATA { min, max } -// └── DATASET input_{idx} -// DATATYPE Float32 -// DATASPACE (2) -// DATA { min, max } -// GROUP name (optional, for debug) -// └── GROUP model_{idx} -// └── GROUP subg_{idx} -// ├── ATTRIBUTE op_{idx} -// │ DATATYPE String -// │ DATA { "op/name"} -// └── ATTRIBUTE input_{idx} -// DATATYPE String -// DATA { "input/name"} -struct MinMaxVectors -{ - std::vector min_vector; - std::vector max_vector; -}; - -class Reader -{ -public: - Reader(const std::string &filepath); - /** - * @brief Returns minmax recording for op {model_idx, subg_idx, op_idx} - * - * @return MinMaxVectors - */ - MinMaxVectors read(int model_idx, int subg_idx, int op_idx) const; - /** - * @brief Returns minmax recording for input {model_idx, subg_idx, input_idx} - * - * @return MinMaxVectors - */ - MinMaxVectors read_input(int model_idx, int subg_idx, int input_idx) const; - -private: - H5::H5File _file; - H5::Group _val_grp; -}; - -} // namespace h5 -} // namespace odc -} // namespace onert - -#endif // __ONERT_ODC_H5_READER_H__