Skip to content

Commit

Permalink
[record-hessian] This commit introduce record hessian.
Browse files Browse the repository at this point in the history
This commit introduce record hessian.

ONE-DCO-1.0-Signed-off-by: Banseok Lee <[email protected]>
  • Loading branch information
BLee-bot committed Nov 6, 2024
1 parent 85bcba8 commit 92b309a
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
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 "record-hessian/HessianObserver.h"

#include <luci/IR/Module.h>
#include <luci_interpreter/Interpreter.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__
110 changes: 110 additions & 0 deletions compiler/record-hessian/src/RecordHessian.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
/*
* 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 <dio_hdf5/HDF5Importer.h>

#include <iostream>

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

namespace record_hessian
{

void RecordHessian::initialize(luci::Module *module)
{
// To Be Implemented
}

std::unique_ptr<HessianMap> RecordHessian::profileData(const std::string &input_data_path)
{
// To Be Implemented
}

} // namespace record_hessian

0 comments on commit 92b309a

Please sign in to comment.