Skip to content

Commit

Permalink
Remove hdf5 dependency on minmax embedding
Browse files Browse the repository at this point in the history
  • Loading branch information
hseok-oh committed May 14, 2024
1 parent 5804603 commit 8611538
Show file tree
Hide file tree
Showing 13 changed files with 462 additions and 222 deletions.
6 changes: 0 additions & 6 deletions infra/nnfw/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
16 changes: 2 additions & 14 deletions runtime/onert/core/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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" $<TARGET_FILE_NAME:onert_core>)
Expand Down
1 change: 1 addition & 0 deletions runtime/onert/core/include/util/MinMaxMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ template <typename N, typename Hash = std::hash<N>> 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<N, MinMaxPair, Hash> _minmax_map;
Expand Down
4 changes: 0 additions & 4 deletions runtime/onert/core/src/compiler/ExecutorFactory.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -509,11 +507,9 @@ ExecutorFactory::createLinearExecutor(std::unique_ptr<compiler::LoweredGraph> lo
std::make_unique<exec::TracingObserver>(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<exec::MinMaxRecorder>(
options->minmax_filepath, exec->graph(), exec->getBackendContexts()));
#endif

return exec;
}
Expand Down
113 changes: 113 additions & 0 deletions runtime/onert/core/src/exec/MinMaxData.cc
Original file line number Diff line number Diff line change
@@ -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 <cstdio>
#include <iostream>

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
74 changes: 74 additions & 0 deletions runtime/onert/core/src/exec/MinMaxData.h
Original file line number Diff line number Diff line change
@@ -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 <string>

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__
4 changes: 2 additions & 2 deletions runtime/onert/core/src/exec/MinMaxRecorder.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@
#include "ExecutionObservers.h"
#include "ir/Index.h"
#include "exec/MinMaxMap.h"
#include "../dumper/h5/MinMaxDumper.h"
#include "MinMaxData.h"

#include <memory>

Expand All @@ -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;
};
Expand Down
7 changes: 0 additions & 7 deletions runtime/onert/odc/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,24 +1,17 @@
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")
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
Expand Down
6 changes: 2 additions & 4 deletions runtime/onert/odc/Embedder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/

#include "Embedder.h"
#include "MinMaxReader.h"

#include <luci/CircleExporter.h>
#include <luci/CircleFileExpContract.h>
Expand All @@ -24,11 +25,8 @@
#include <luci/Profile/CircleNodeID.h>
#include <luci/Service/Validate.h>

#include "h5/Reader.h"

#include <cassert>
#include <cmath> // for std::floor
#include <iostream>
#include <string>

namespace
Expand Down Expand Up @@ -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)
{
Expand Down

0 comments on commit 8611538

Please sign in to comment.