Imported Upstream version 1.25.0
[platform/core/ml/nnfw.git] / runtime / onert / core / include / compiler / train / LoweredTrainableGraph.h
1 /*
2  * Copyright (c) 2023 Samsung Electronics Co., Ltd. All Rights Reserved
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 #ifndef __ONERT_COMPILER_TRAIN_LOWERED_TRAINABLE_GRAPH_H__
18 #define __ONERT_COMPILER_TRAIN_LOWERED_TRAINABLE_GRAPH_H__
19
20 #include "compiler/BackendResolver.h"
21 #include "compiler/CompilerOptions.h"
22 #include "compiler/GraphLowerInfo.h"
23 #include "compiler/ILoweredGraph.h"
24 #include "ir/train/TrainableGraph.h"
25
26 namespace onert
27 {
28 namespace compiler
29 {
30 namespace train
31 {
32
33 // TODO Unify with LoweredGraph
34 /**
35  * @brief Class that contains lowering information on graph.
36  *        In addition, after lowering, operands in graph will be set to "dynamic"
37  *        if the shape of output of an operation cannot be decided at compilation time.
38  */
39 class LoweredTrainableGraph : public ILoweredGraph
40 {
41 public:
42   LoweredTrainableGraph(ir::train::TrainableGraph &graph, const compiler::CompilerOptions &options);
43
44   // TODO Remove const_cast
45   ir::Graph &graph() override { return const_cast<ir::Graph &>(_trainable_graph.graph()); }
46   const ir::Graph &graph() const override { return _trainable_graph.graph(); }
47   ir::train::TrainableGraph &trainable_graph() { return _trainable_graph; }
48   const ir::train::TrainableGraph &trainable_graph() const { return _trainable_graph; }
49   const compiler::GraphLowerInfo &lower_info() const override { return _lower_info_map; }
50   compiler::GraphLowerInfo &lower_info() override { return _lower_info_map; }
51   std::shared_ptr<ir::OperationIndexMap<int64_t>> indexed_ranks() { return _indexed_ranks; }
52
53   void setHasDynamicTensor(ir::OperationIndex, bool has_dynamic) override
54   {
55     if (has_dynamic)
56       throw std::runtime_error("LoweredTrainableGraph does not support dynamic tensors yet");
57   }
58   bool getHasDynamicTensor(ir::OperationIndex) const override { return false; }
59
60 private:
61   void makeLowerInfo(const compiler::BackendResolver &backend_resolver);
62   void dumpLowerInfo();
63   void lowerGraph(const compiler::CompilerOptions &options);
64
65 private:
66   /**
67    *  @brief  Copy of target graph for lowering
68    *  @note   It uses copy of graph, not reference.
69    *          It allows the original graph can be compiled multiple times.
70    */
71   ir::train::TrainableGraph _trainable_graph;
72   std::shared_ptr<ir::OperationIndexMap<int64_t>> _indexed_ranks;
73   compiler::GraphLowerInfo _lower_info_map;
74 };
75
76 } // namespace train
77 } // namespace compiler
78 } // namespace onert
79
80 #endif // __ONERT_COMPILER_TRAIN_LOWERED_TRAINABLE_GRAPH_H__