Skip to content

Commit

Permalink
rename Allocator to generator
Browse files Browse the repository at this point in the history
  • Loading branch information
zetwhite committed Jul 1, 2024
1 parent 132f050 commit e332c24
Show file tree
Hide file tree
Showing 6 changed files with 78 additions and 42 deletions.
4 changes: 2 additions & 2 deletions runtime/onert/backend/train/BackendContext.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

#include "TensorBuilder.h"
#include "KernelGenerator.h"
#include "ExtraTensorAllocator.h"
#include "ExtraTensorGenerator.h"
#include "ops/BackPropInitializer.h"

#include <backend/basic/train/TrainableBackendContextHelpers.h>
Expand Down Expand Up @@ -152,7 +152,7 @@ backend::train::ITensorRegistry *BackendContext::genTrainingTensors()
}

// allocate extra tensors
ExtraTensorAllocator extra_tensor_generator(tgraph, _tensor_builder, _tensor_registry);
ExtraTensorGenerator extra_tensor_generator(tgraph, _tensor_builder, _tensor_registry);
tgraph.operations().iterate([&](const ir::OperationIndex &, const ir::IOperation &op) {
const auto trainable_op = dynamic_cast<const ir::train::TrainableOperation *>(&op);
trainable_op->accept(extra_tensor_generator);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
* limitations under the License.
*/

#include "ExtraTensorAllocator.h"
#include "ExtraTensorGenerator.h"

#include "ops/BackPropAccumulator.h"
#include "ops/BinaryArithmeticLayer.h"
Expand All @@ -38,7 +38,21 @@ namespace backend
namespace train
{

void ExtraTensorAllocator::handle_requests(const ir::OperationIndex &op_idx,
ExtraTensorGenerator::ExtraTensorGenerator(const ir::train::TrainableGraph &tgraph,
std::shared_ptr<TensorBuilder> &tensor_builder,
std::shared_ptr<ITensorRegistry> &tensor_registry)
: _tgraph(tgraph), _tensor_builder(tensor_builder)
{
_tensor_reg = std::dynamic_pointer_cast<TensorRegistry>(tensor_registry);

for (const auto &index : _tgraph.topolSortOperations())
{
const auto &node = _tgraph.operation(index);
_node_to_idx[&node] = index;
}
};

void ExtraTensorGenerator::handle_requests(const ir::OperationIndex &op_idx,
const ExtraTensorRequests &requests)
{
// use 'auto i' induced to 'integer' -> this causes comparsion error.
Expand All @@ -63,7 +77,7 @@ void ExtraTensorAllocator::handle_requests(const ir::OperationIndex &op_idx,
return;
}

void ExtraTensorAllocator::visit(const ir::train::operation::FullyConnected &node)
void ExtraTensorGenerator::visit(const ir::train::operation::FullyConnected &node)
{
using ir::train::operation::FullyConnected;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@
* limitations under the License.
*/

#ifndef __ONERT_BACKEND_EXTRA_TENSOR_ALLOCATOR_H__
#define __ONERT_BACKEND_EXTRA_TENSOR_ALLOCATOR_H__
#ifndef __ONERT_BACKEND_EXTRA_TENSOR_GENERATOR_H__
#define __ONERT_BACKEND_EXTRA_TENSOR_GENERATOR_H__

// to construct vsitior
#include "ir/train/TrainableOperationVisitor.h"
#include "ir/train/Operations.Include.h"

// ...
#include "ExtraTensorRequest.h"
#include "ir/train/TrainableGraph.h"
#include "ir/train/ITrainableOperation.h"
#include "ir/Index.h"
Expand All @@ -34,45 +35,16 @@ namespace backend
namespace train
{

using ExtraTensor = Tensor;

enum class ExtraTensorLifeTime
{
// TODO: find better way to explain lifetime
DYNAMIC, // << exist during forward() or backward()
STATIC, // << always exists
};

struct ExtraTensorRequest
{
ir::OperandInfo info;
ir::Layout layout;
ExtraTensorLifeTime lifetime;
};

using ExtraTensorRequests = std::vector<ExtraTensorRequest>;

class ExtraTensorAllocator : public ir::train::TrainableOperationVisitor
class ExtraTensorGenerator : public ir::train::TrainableOperationVisitor
{
public:
ExtraTensorAllocator() = delete;
ExtraTensorAllocator(const ir::train::TrainableGraph &tgraph,
ExtraTensorGenerator() = delete;
ExtraTensorGenerator(const ir::train::TrainableGraph &tgraph,
std::shared_ptr<TensorBuilder> &tensor_builder,
std::shared_ptr<ITensorRegistry> &tensor_registry)
: _tgraph(tgraph), _tensor_builder(tensor_builder)
{
_tensor_reg = std::dynamic_pointer_cast<TensorRegistry>(tensor_registry);

for (const auto &index : _tgraph.topolSortOperations())
{
const auto &node = _tgraph.operation(index);
_node_to_idx[&node] = index;
}
};
std::shared_ptr<ITensorRegistry> &tensor_registry);

public:
void visit(const ir::train::operation::FullyConnected &node) override;

// void visit(const train::operation::BinaryArithmetic &node) override;
// void visit(const train::operation::Conv2D &node) override;
// void visit(const train::operation::DepthwiseConv2D &node) override;
Expand All @@ -98,4 +70,4 @@ class ExtraTensorAllocator : public ir::train::TrainableOperationVisitor
} // namespace backend
} // namespace onert

#endif // __ONERT_BACKEND_EXTRA_TENSOR_ALLOCATOR_H__
#endif // __ONERT_BACKEND_EXTRA_TENSOR_GENERATOR_H__
49 changes: 49 additions & 0 deletions runtime/onert/backend/train/ExtraTensorRequest.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* 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 __ONERT_BACKEND_EXTRA_TENSOR_REQUEST_H__
#define __ONERT_BACKEND_EXTRA_TENSOR_REQUEST_H__

#include "ir/OperandInfo.h"

namespace onert
{
namespace backend
{
namespace train
{

enum class ExtraTensorLifeTime
{
// TODO: find better way to explain lifetime
DYNAMIC, // << exist during forward() or backward()
STATIC, // << always exists
};

struct ExtraTensorRequest
{
ir::OperandInfo info;
ir::Layout layout;
ExtraTensorLifeTime lifetime;
};

using ExtraTensorRequests = std::vector<ExtraTensorRequest>;

} // namespace train
} // namespace backend
} // namespace onert

#endif // __ONERT_BACKEND_EXTRA_TENSOR_REQUEST_H__
1 change: 1 addition & 0 deletions runtime/onert/backend/train/TensorBuilder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,7 @@ void TensorBuilder::allocateBackward(void)
_tensor_mgr->allocateBackPropTensors();
_tensor_mgr->allocateGradientTensors();
_tensor_mgr->allocateDisposableBackPropTensors();
_tensor_mgr->allocateExtraTensors();
}

} // namespace train
Expand Down
2 changes: 1 addition & 1 deletion runtime/onert/backend/train/ops/FullyConnectedLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#define __ONERT_BACKEND_TRAIN_OPS_FULLYCONNECTEDLAYER_H__

#include "../ExternalContext.h"
#include "../ExtraTensorAllocator.h"
#include "../ExtraTensorRequest.h"
#include "../Tensor.h"

#include <exec/train/ITrainableFunction.h>
Expand Down

0 comments on commit e332c24

Please sign in to comment.