-
Notifications
You must be signed in to change notification settings - Fork 158
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
25 changed files
with
526 additions
and
103 deletions.
There are no files selected for viewing
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
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,131 @@ | ||
/* | ||
* 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 "ExtraTensorGenerator.h" | ||
|
||
#include "ExtraTensorIndex.h" | ||
|
||
#include <ir/Operations.h> | ||
#include <util/logging.h> | ||
#include <memory> | ||
|
||
namespace onert | ||
{ | ||
namespace backend | ||
{ | ||
namespace train | ||
{ | ||
|
||
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); | ||
} | ||
|
||
// Move to BackendContext.cc | ||
void ExtraTensorGenerator::register_tensors(ir::OperationIndex op_idx, | ||
std::optional<ExtraTensors> &&tensors) | ||
{ | ||
if (not tensors.has_value()) | ||
return; | ||
|
||
auto extra_tensors = tensors.value(); | ||
|
||
auto &operations = _tgraph->operations(); | ||
|
||
for (size_t i = 0; i < extra_tensors.size(); i++) | ||
{ | ||
// register tensor | ||
ExtraTensorIndex tensor_idx(op_idx, i); | ||
_tensor_builder->registerExtraTensor(tensor_idx, extra_tensors[i]); | ||
|
||
std::stringstream op_info; | ||
op_info << op_idx << "_" << operations.at(op_idx).name(); | ||
VERBOSE(ExtraTensorGenerator) << "register (idx:" << tensor_idx << ") requested from " | ||
<< op_info.str() << std::endl; | ||
} | ||
return; | ||
} | ||
|
||
ExtraTensors ExtraTensorGenerator::getExtraTensors(const ir::OperationIndex &op_index) | ||
{ | ||
ExtraTensors tensors; | ||
|
||
int sub_index = 0; | ||
|
||
ExtraTensorIndex index(op_index, sub_index); | ||
auto tensor = _tensor_reg->getExtraTensor(index); | ||
|
||
while (tensor != nullptr) | ||
{ | ||
sub_index++; | ||
tensors.push_back(tensor); | ||
|
||
ExtraTensorIndex index(op_index, sub_index); | ||
tensor = _tensor_reg->getExtraTensor(index); | ||
|
||
VERBOSE(ExtraTensorGenerator) << "HERE " << op_index << "+" << sub_index << std::endl; | ||
} | ||
|
||
return tensors; | ||
} | ||
|
||
// Move to TensorPlanner | ||
void ExtraTensorGenerator::plan() | ||
{ | ||
// forwarding order | ||
const auto f_order = _tgraph->topolSortOperations(); | ||
for (const auto &op_index : f_order) | ||
{ | ||
auto tensors = getExtraTensors(op_index); | ||
for (auto i = 0u; i < tensors.size(); ++i) | ||
{ | ||
const auto < = tensors[i]->lifetime(); | ||
if (lt == ExtraTensorLifeTime::FORWARD_TO_BACKWARD) | ||
_tensor_builder->notifyFirstUse(ExtraTensorIndex(op_index, i)); | ||
} | ||
} | ||
|
||
// backwarding order | ||
const auto b_order = _tgraph->essentialBackwardOrder(); | ||
for (const auto &op_index : b_order) | ||
{ | ||
|
||
auto tensors = getExtraTensors(op_index); | ||
for (auto i = 0u; i < tensors.size(); ++i) | ||
{ | ||
const auto < = tensors[i]->lifetime(); | ||
if (lt == ExtraTensorLifeTime::BACKWARD) | ||
_tensor_builder->notifyFirstUse(ExtraTensorIndex(op_index, i)); | ||
} | ||
|
||
for (auto i = 0u; i < tensors.size(); ++i) | ||
{ | ||
const auto < = tensors[i]->lifetime(); | ||
if (lt == ExtraTensorLifeTime::FORWARD_TO_BACKWARD || lt == ExtraTensorLifeTime::BACKWARD) | ||
_tensor_builder->notifyLastUse(ExtraTensorIndex(op_index, i)); | ||
} | ||
} | ||
} | ||
|
||
// Move to allocateBackward() | ||
void ExtraTensorGenerator::allocate() { _tensor_builder->allocateExtra(); } | ||
|
||
} // namespace train | ||
} // namespace backend | ||
} // namespace onert |
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,60 @@ | ||
/* | ||
* 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_GENERATOR_H__ | ||
#define __ONERT_BACKEND_EXTRA_TENSOR_GENERATOR_H__ | ||
|
||
#include <ir/train/TrainableGraph.h> | ||
#include <ir/Index.h> | ||
#include <backend/train/ExtraTensor.h> | ||
|
||
#include "TensorBuilder.h" | ||
|
||
namespace onert | ||
{ | ||
namespace backend | ||
{ | ||
namespace train | ||
{ | ||
|
||
class ExtraTensorGenerator | ||
{ | ||
public: | ||
ExtraTensorGenerator() = delete; | ||
|
||
ExtraTensorGenerator(const ir::train::TrainableGraph *tgraph, | ||
std::shared_ptr<TensorBuilder> &tensor_builder, | ||
std::shared_ptr<ITensorRegistry> &tensor_registry); | ||
|
||
public: | ||
// Since register is reserved keyword, use 'register_tensors' intead of 'register' | ||
void register_tensors(ir::OperationIndex idx, std::optional<ExtraTensors> &&tensors); | ||
void plan(); | ||
void allocate(); | ||
|
||
private: | ||
ExtraTensors getExtraTensors(const ir::OperationIndex &op_index); | ||
|
||
const ir::train::TrainableGraph *_tgraph; | ||
std::shared_ptr<TensorBuilder> _tensor_builder; | ||
std::shared_ptr<TensorRegistry> _tensor_reg; | ||
}; | ||
|
||
} // namespace train | ||
} // namespace backend | ||
} // namespace onert | ||
|
||
#endif // __ONERT_BACKEND_EXTRA_TENSOR_GENERATOR_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
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
Oops, something went wrong.