Skip to content

Commit

Permalink
add record-hessian
Browse files Browse the repository at this point in the history
  • Loading branch information
BLee-bot committed Oct 10, 2024
1 parent c507999 commit 7e849aa
Show file tree
Hide file tree
Showing 11 changed files with 945 additions and 0 deletions.
32 changes: 32 additions & 0 deletions compiler/record-hessian/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
file(GLOB_RECURSE SOURCES "src/*.cpp")

add_library(record-hessian STATIC ${SOURCES})

target_include_directories(record-hessian PUBLIC include)
target_include_directories(record-hessian PRIVATE src)
target_link_libraries(record-hessian luci_import)
target_link_libraries(record-hessian luci_env)
target_link_libraries(record-hessian luci_export)
target_link_libraries(record-hessian luci_interpreter)
target_link_libraries(record-hessian luci_log)
target_link_libraries(record-hessian dio_hdf5)
install(TARGETS record-hessian DESTINATION lib)
install(DIRECTORY include/ DESTINATION include
FILES_MATCHING PATTERN "*.h")

if(NOT ENABLE_TEST)
return()
endif(NOT ENABLE_TEST)

nnas_find_package(GTest REQUIRED)
set(TEST_SOURCES, "src/MinMaxComputer.cpp")

file(GLOB_RECURSE TESTS "tests/*.test.cpp")

GTest_AddTest(record_hessian_tests ${TESTS} ${TEST_SOURCES})
target_include_directories(record_hessian_tests PUBLIC include)
target_include_directories(record_hessian_tests PRIVATE src)
target_link_libraries(record_hessian_tests luci_lang)
target_link_libraries(record_hessian_tests nncc_coverage)
target_link_libraries(record_hessian_tests luci_interpreter)
target_link_libraries(record_hessian_tests record-hessian)
3 changes: 3 additions & 0 deletions compiler/record-hessian/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# record-hessian

_record-hessian_ calculates hessian metrix of activations for quantization.
58 changes: 58 additions & 0 deletions compiler/record-hessian/include/record-hessian/HessianComputer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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 __RECORD_HESSIAN_HESSIANCOMPUTER_H__
#define __RECORD_HESSIAN_HESSIANCOMPUTER_H__

#include "HessianVector.h"

#include <luci/IR/CircleNode.h>
#include <luci_interpreter/Interpreter.h>

namespace record_hessian
{

class HessianComputer
{
public:
// Record min/max of node
void recordHessian(const luci::CircleNode *node, const luci_interpreter::Tensor *input_tensor);

void unfold(std::vector<float> &buf, uint32_t input_n, uint32_t input_h, uint32_t input_w,
uint32_t input_c, uint32_t stride_h, uint32_t stride_w, uint32_t dilation_h,
uint32_t dilation_w, uint32_t kernel_oc, uint32_t kernel_h, uint32_t kernel_w,
uint32_t kernel_ic);

std::unique_ptr<HessianMap> getMap() const
{

auto hessian_map = std::make_unique<HessianMap>();

for (auto item : _hessian_map)
{
auto &vec = (*hessian_map)[item.first];
vec = item.second.hessian;
}

return hessian_map;
}

private:
std::unordered_map<const luci::CircleNode *, HessianVector> _hessian_map;
};
} // namespace record_hessian

#endif // __RECORD_HESSIAN_HESSIANCOMPUTER_H__
44 changes: 44 additions & 0 deletions compiler/record-hessian/include/record-hessian/HessianObserver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
/*
* 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 __RECORD_HESSIAN_HESSIANOBSERVER_H__
#define __RECORD_HESSIAN_HESSIANOBSERVER_H__

#include <luci_interpreter/Interpreter.h>
#include <luci_interpreter/core/Tensor.h>
#include <luci/IR/CircleNodes.h>

#include "HessianComputer.h"
namespace record_hessian
{

class HessianObserver : public luci_interpreter::ExecutionObserver
{
public:
HessianObserver() = default;

void postTensorWrite(const luci::CircleNode *node,
const luci_interpreter::Tensor *tensor) override;

std::unique_ptr<HessianMap> hessianData() { return _hessian_computer.getMap(); }

private:
HessianComputer _hessian_computer;
};

} // namespace record_hessian

#endif // __RECORD_HESSIAN_HESSIANOBSERVER_H__
63 changes: 63 additions & 0 deletions compiler/record-hessian/include/record-hessian/HessianVector.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
/*
* 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 __RECORD_HESSIAN_HESSIANVECTOR_H__
#define __RECORD_HESSIAN_HESSIANVECTOR_H__

#include <luci/IR/CircleNodes.h>

#include <cstddef>
#include <unordered_map>
#include <vector>

namespace record_hessian
{

using HessianMap = std::unordered_map<const luci::CircleNode *, std::vector<float>>;

struct HessianVector
{
std::vector<float> hessian;
size_t count = 0;

HessianVector() : count(0) {}

void update(const std::vector<float> &new_hessian)
{
if (count == 0)
{
hessian.resize(new_hessian.size());
}
else if (hessian.size() != new_hessian.size())
{
hessian.resize(new_hessian.size());
}

size_t numel = new_hessian.size();
float alpha = 1.f / static_cast<float>(count + 1);

for (size_t i = 0; i < numel; ++i)
{
hessian[i] = (hessian[i] * count + new_hessian[i]) * alpha;
}

count++;
};
};

} // namespace record_hessian

#endif // __RECORD_HESSIAN_HESSIANVECTOR_H__
62 changes: 62 additions & 0 deletions compiler/record-hessian/include/record-hessian/RecordHessian.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*
* 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 __RECORD_HESSIAN_H__
#define __RECORD_HESSIAN_H__

#include <luci/IR/Module.h>
#include <luci_interpreter/Interpreter.h>

#include "record-hessian/HessianObserver.h"

namespace record_hessian
{

class RecordHessian
{
public:
explicit RecordHessian() {}

~RecordHessian() = default;

void initialize(luci::Module *module);

// TODO Refactor profile functions
std::unique_ptr<HessianMap> profileData(const std::string &input_data_path);

std::unique_ptr<HessianMap> profileDataInParallel(const std::string &input_data_path);

std::unique_ptr<HessianMap> profileRawData(const std::string &input_data_path);

std::unique_ptr<HessianMap> profileRawDataDirectory(const std::string &input_data_path);

std::unique_ptr<HessianMap> profileDataWithRandomInputs(void);

private:
luci_interpreter::Interpreter *getInterpreter() const { return _interpreter.get(); }

// Never return nullptr
HessianObserver *getObserver() const { return _observer.get(); }

luci::Module *_module = nullptr;

std::unique_ptr<luci_interpreter::Interpreter> _interpreter;
std::unique_ptr<HessianObserver> _observer;
};

} // namespace record_hessian

#endif // __RECORD_HESSIAN_H__
3 changes: 3 additions & 0 deletions compiler/record-hessian/requires.cmake
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
require("luci")
require("luci-interpreter")
require("dio-hdf5")
Loading

0 comments on commit 7e849aa

Please sign in to comment.