Skip to content

Commit

Permalink
Split codes into core snippets for small PR.
Browse files Browse the repository at this point in the history
Split codes into core snippets for small PR.

ONE-DCO-1.0-Signed-off-by: Banseok Lee <[email protected]>
  • Loading branch information
BLee-bot committed Nov 4, 2024
1 parent 23270bb commit 00f3073
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 147 deletions.
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 @@
/*
* 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__
85 changes: 0 additions & 85 deletions compiler/record-hessian/src/RecordHessian.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,88 +104,3 @@ void verifyTypeShape(const luci::CircleInput *input_node, const DataType &dtype,
}

} // namespace

namespace record_hessian
{

void RecordHessian::initialize(luci::Module *module)
{
// Create and initialize interpreters and observers

_module = module;

auto interpreter = std::make_unique<luci_interpreter::Interpreter>(module);
auto observer = std::make_unique<HessianObserver>();

interpreter->attachObserver(observer.get());

_observer = std::move(observer);
_interpreter = std::move(interpreter);
}

std::unique_ptr<HessianMap> RecordHessian::profileData(const std::string &input_data_path)
{
try
{
dio::hdf5::HDF5Importer importer(input_data_path);
importer.importGroup("value");

bool is_raw_data = importer.isRawData();

const auto num_records = importer.numData();
if (num_records == 0)
throw std::runtime_error("RecordHessian: The input data file does not contain any record.");

const auto input_nodes = loco::input_nodes(_module->graph());
const auto num_inputs = input_nodes.size();

for (int32_t record_idx = 0; record_idx < num_records; record_idx++)
{
if (num_inputs != static_cast<uint32_t>(importer.numInputs(record_idx)))
throw std::runtime_error("RecordHessian: Wrong number of inputs.");

std::cout << "Recording " << record_idx << "'th data for hessian." << std::endl;

for (uint32_t input_idx = 0; input_idx < num_inputs; input_idx++)
{
const auto *input_node = loco::must_cast<const luci::CircleInput *>(input_nodes[input_idx]);
assert(input_node->index() == input_idx);
checkInputDimension(input_node);
std::vector<char> input_data(getTensorSize(input_node));

if (!is_raw_data)
{
DataType dtype;
Shape shape;
importer.readTensor(record_idx, input_idx, &dtype, &shape, input_data.data(),
input_data.size());

// Check the type and the shape of the input data is valid
verifyTypeShape(input_node, dtype, shape);
}
else
{
// Skip type/shape check for raw data
importer.readTensor(record_idx, input_idx, input_data.data(), input_data.size());
}

// TODO: Input data is copied twice (file -> buffer (input_data) -> interpreter inputs)
// We can redcue the copy by directly writing data from file to interpreter inputs
getInterpreter()->writeInputTensor(input_node, input_data.data(), input_data.size());
}

getInterpreter()->interpret();
}

std::cout << "Recording finished. Number of recorded data: " << num_records << std::endl;
}
catch (const H5::Exception &e)
{
H5::Exception::printErrorStack();
throw std::runtime_error("RecordHessian: HDF5 error occurred.");
}

return getObserver()->hessianData();
}

} // namespace record_hessian
62 changes: 0 additions & 62 deletions compiler/record-hessian/src/RecordHessian.test.cpp

This file was deleted.

0 comments on commit 00f3073

Please sign in to comment.