-
Notifications
You must be signed in to change notification settings - Fork 158
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[record-hessian] Introduce Record-hessian #14183
Open
BLee-bot
wants to merge
4
commits into
Samsung:master
Choose a base branch
from
BLee-bot:record-hessian-2
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
58
compiler/record-hessian/include/record-hessian/HessianComputer.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
44
compiler/record-hessian/include/record-hessian/HessianObserver.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
63
compiler/record-hessian/include/record-hessian/HessianVector.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
62
compiler/record-hessian/include/record-hessian/RecordHessian.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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__ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
require("luci") | ||
require("luci-interpreter") | ||
require("dio-hdf5") |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Q) what/where is this file?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be "HessianComputer.cpp"