Skip to content
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
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 45 additions & 0 deletions compiler/record-hessian/include/record-hessian/HessianObserver.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
/*
Copy link
Contributor

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.

Copy link
Contributor Author

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.

* 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 compiler/record-hessian/include/record-hessian/RecordHessian.h
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__
106 changes: 106 additions & 0 deletions compiler/record-hessian/src/RecordHessian.cpp
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