-
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 RecordHessian. #14291
Closed
Closed
Changes from 2 commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
23270bb
This commit introduce record hessian.
BLee-bot 00f3073
Split codes into core snippets for small PR.
BLee-bot 3237437
Merge branch 'Samsung:master' into rh-rh-split
BLee-bot 85bcba8
[record-hessian] Introduce HessianObserver. (#14292)
BLee-bot 535e697
This commit introduce record hessian.
BLee-bot 4eacb30
Split codes into core snippets for small PR.
BLee-bot eb7ab96
Merge branch 'rh-rh-split' of https://github.com/BLee-bot/ONE into rh…
BLee-bot 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
45 changes: 45 additions & 0 deletions
45
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,45 @@ | ||
/* | ||
* 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 "record-hessian/HessianComputer.h" | ||
|
||
#include <luci_interpreter/Interpreter.h> | ||
#include <luci_interpreter/core/Tensor.h> | ||
#include <luci/IR/CircleNodes.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__ |
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
/* | ||
* 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); | ||
|
||
private: | ||
luci_interpreter::Interpreter *getInterpreter() const { return _interpreter.get(); } | ||
|
||
// Never return nullptr | ||
HessianObserver *getObserver() const { return _observer.get(); } | ||
|
||
luci::Module *_module; | ||
|
||
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,106 @@ | ||
/* | ||
* 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 "record-hessian/RecordHessian.h" | ||
#include "record-hessian/HessianObserver.h" | ||
|
||
#include <luci/IR/DataTypeHelper.h> | ||
#include <luci/Importer.h> | ||
#include <luci/IR/CircleQuantParam.h> | ||
#include <luci/Log.h> | ||
#include <dio_hdf5/HDF5Importer.h> | ||
|
||
#include <dirent.h> | ||
#include <algorithm> | ||
#include <cmath> | ||
#include <fstream> | ||
#include <numeric> | ||
#include <stdexcept> | ||
#include <iostream> | ||
#include <random> | ||
|
||
using Shape = std::vector<loco::Dimension>; | ||
using DataType = loco::DataType; | ||
|
||
namespace | ||
{ | ||
|
||
// Return a string with no whitespace from both ends | ||
std::string trim(std::string s) | ||
{ | ||
// Trim left side | ||
s.erase(s.begin(), | ||
std::find_if(s.begin(), s.end(), [](unsigned char ch) { return !std::isspace(ch); })); | ||
|
||
// Trim right side | ||
s.erase( | ||
std::find_if(s.rbegin(), s.rend(), [](unsigned char ch) { return !std::isspace(ch); }).base(), | ||
s.end()); | ||
|
||
return s; | ||
} | ||
|
||
uint32_t numElements(const luci::CircleNode *node) | ||
{ | ||
uint32_t num_elements = 1; | ||
for (uint32_t i = 0; i < node->rank(); i++) | ||
num_elements *= node->dim(i).value(); | ||
|
||
return num_elements; | ||
} | ||
|
||
void checkInputDimension(const luci::CircleInput *input) | ||
{ | ||
for (uint32_t i = 0; i < input->rank(); i++) | ||
if (!input->dim(i).known()) | ||
throw std::runtime_error("RecordHessian: " + input->name() + " has unknown dimension"); | ||
|
||
if (numElements(input) == 0) | ||
throw std::runtime_error("RecordHessian: " + input->name() + " is a zero-sized input"); | ||
} | ||
|
||
/** | ||
* @brief getTensorSize will return size in bytes | ||
*/ | ||
template <typename NodeT> size_t getTensorSize(const NodeT *node) | ||
{ | ||
uint32_t tensor_size = luci::size(node->dtype()); | ||
for (uint32_t i = 0; i < node->rank(); ++i) | ||
tensor_size *= node->dim(i).value(); | ||
return tensor_size; | ||
} | ||
|
||
/** | ||
* @brief verifyTypeShape checks the type and the shape of CircleInput | ||
* This throws an exception if type or shape does not match | ||
*/ | ||
void verifyTypeShape(const luci::CircleInput *input_node, const DataType &dtype, const Shape &shape) | ||
{ | ||
// Type check | ||
if (dtype != input_node->dtype()) | ||
throw std::runtime_error("RecordHessian: Wrong input type."); | ||
|
||
if (shape.size() != input_node->rank()) | ||
throw std::runtime_error("RecordHessian: Input rank mismatch."); | ||
|
||
for (uint32_t i = 0; i < shape.size(); i++) | ||
{ | ||
if (not(shape.at(i) == input_node->dim(i))) | ||
throw std::runtime_error("RecordHessian: Input shape mismatch."); | ||
} | ||
} | ||
|
||
} // namespace |
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.
you can split this file to another PR and then rebase this PR to that PR.
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.
Ok I'll split this module to another PR and going to proceed to that PR first.