|
| 1 | +/* Copyright (c) 2018 PaddlePaddle Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. */ |
| 14 | + |
| 15 | +#ifdef PADDLE_WITH_NGRAPH |
| 16 | +#include <glog/logging.h> |
| 17 | + |
| 18 | +#include <algorithm> |
| 19 | +#include <map> |
| 20 | + |
| 21 | +#include "paddle/fluid/framework/feed_fetch_type.h" |
| 22 | +#include "paddle/fluid/framework/ngraph_operator.h" |
| 23 | +#include "paddle/fluid/framework/shape_inference.h" |
| 24 | +#include "paddle/fluid/framework/var_desc.h" |
| 25 | +#include "paddle/fluid/framework/var_type.h" |
| 26 | + |
| 27 | +namespace paddle { |
| 28 | +namespace framework { |
| 29 | + |
| 30 | +static std::map<proto::VarType::Type, ngraph::element::Type> pd2ng_type_map = { |
| 31 | + {proto::VarType::FP32, ngraph::element::f32}, |
| 32 | + {proto::VarType::FP64, ngraph::element::f64}, |
| 33 | + {proto::VarType::INT32, ngraph::element::i32}, |
| 34 | + {proto::VarType::INT64, ngraph::element::i64}, |
| 35 | + {proto::VarType::BOOL, ngraph::element::boolean}, |
| 36 | +}; |
| 37 | + |
| 38 | +typedef enum { /* nGraph support state on ops */ |
| 39 | + FULL_TRAIN, /* Support full ops for train */ |
| 40 | + PARTIAL_TRAIN, /* Support partial ops for train */ |
| 41 | + FULL_TEST, /* Support full list of ops for test */ |
| 42 | + PARTIAL_TEST /* Support partial list of ops for test */ |
| 43 | +} op_state; |
| 44 | + |
| 45 | +class NgraphOperator { |
| 46 | + public: |
| 47 | + explicit NgraphOperator(const Scope& scope, const platform::Place& place, |
| 48 | + const std::vector<std::shared_ptr<OperatorBase>>& ops, |
| 49 | + const std::unordered_map< |
| 50 | + std::string, ngraph::element::Type>& var_type_map, |
| 51 | + const std::unordered_set<std::string>& persist, |
| 52 | + const std::unordered_set<std::string>& fetches, |
| 53 | + const std::unordered_set<std::string>& post_op_inputs, |
| 54 | + op_state ng_op_state) |
| 55 | + : scope_(scope), |
| 56 | + place_(place), |
| 57 | + fused_ops_(ops), |
| 58 | + var_type_map_(var_type_map), |
| 59 | + persistables_(persist), |
| 60 | + fetches_(fetches), |
| 61 | + post_op_inputs_(post_op_inputs), |
| 62 | + ng_op_state_(ng_op_state) {} |
| 63 | + |
| 64 | + void Run(const Scope& scope, const platform::Place& place) const; |
| 65 | + |
| 66 | + private: |
| 67 | + static std::unordered_map<std::string, std::shared_ptr<ngraph::Function>> |
| 68 | + func_cache; |
| 69 | + const Scope& scope_; |
| 70 | + const platform::Place& place_; |
| 71 | + std::vector<std::shared_ptr<OperatorBase>> fused_ops_; |
| 72 | + std::unordered_map<std::string, ngraph::element::Type> var_type_map_; |
| 73 | + std::unordered_set<std::string> persistables_; |
| 74 | + std::unordered_set<std::string> fetches_; |
| 75 | + std::unordered_set<std::string> post_op_inputs_; |
| 76 | + op_state ng_op_state_; |
| 77 | +}; |
| 78 | + |
| 79 | +std::vector<std::vector<std::vector<std::unique_ptr<OperatorBase>>::iterator>> |
| 80 | +FusedOperator::FusedOpIntervals( |
| 81 | + std::vector<std::unique_ptr<paddle::framework::OperatorBase>>* ops) { |
| 82 | + std::vector<std::vector<std::vector<std::unique_ptr<OperatorBase>>::iterator>> |
| 83 | + intervals; |
| 84 | + if (ops->empty()) { |
| 85 | + return intervals; |
| 86 | + } |
| 87 | + size_t size = ops->size(); |
| 88 | + size_t left = 0; |
| 89 | + while (left < size && ops.at(left)->Type() != kFeedOpType) { |
| 90 | + ++left; |
| 91 | + } |
| 92 | + if (left == size) { |
| 93 | + return intervals; |
| 94 | + } |
| 95 | + while (left < size && ops->at(left)->Type() == kFeedOpType) { |
| 96 | + ++left; |
| 97 | + } |
| 98 | + |
| 99 | + size_t right = left; |
| 100 | + while (right < size && ops->at(right)->Type() != kFetchOpType) { |
| 101 | + ++right; |
| 102 | + } |
| 103 | + if (right == size) { |
| 104 | + return intervals; |
| 105 | + } |
| 106 | + if (left >= right) return intervals; |
| 107 | + |
| 108 | + // (left, right - 1) represents indices between feed and fetch |
| 109 | + size_t pivot = left; |
| 110 | + while (pivot < right) { |
| 111 | + auto op_type = ops->at(pivot)->Type(); |
| 112 | + if (paddle::framework::NgraphBridge::NG_NODE_MAP.find(op_type) == |
| 113 | + paddle::framework::NgraphBridge::NG_NODE_MAP.end()) { |
| 114 | + ++pivot; |
| 115 | + } else { |
| 116 | + size_t start = pivot, end = start; |
| 117 | + while (pivot < right && |
| 118 | + (paddle::framework::NgraphBridge::NG_NODE_MAP.find( |
| 119 | + ops.at(pivot)->Type()) != |
| 120 | + paddle::framework::NgraphBridge::NG_NODE_MAP.end())) { |
| 121 | + ++pivot; |
| 122 | + ++end; |
| 123 | + } |
| 124 | + std::vector<std::vector<std::unique_ptr<OperatorBase>>::iterator> |
| 125 | + interval = {ops->begin() + start, ops->begin() + end}; |
| 126 | + intervals.push_back(interval); |
| 127 | + } |
| 128 | + } // end while |
| 129 | + |
| 130 | + return intervals; |
| 131 | +} |
| 132 | + |
| 133 | +FusedOperator::FusedOperator( |
| 134 | + const ProgramDesc& prog, size_t block_id, |
| 135 | + std::vector<std::unique_ptr<OperatorBase>>::iterator start, |
| 136 | + std::vector<std::unique_ptr<OperatorBase>>::iterator end, |
| 137 | + const std::string& type, const VariableNameMap& inputs, |
| 138 | + const VariableNameMap& outputs, const AttributeMap& attrs) |
| 139 | + : OperatorBase(type, inputs, outputs, attrs), pdesc(prog), block(block_id) { |
| 140 | + for (std::vector<std::unique_ptr<OperatorBase>>::iterator it = start; |
| 141 | + it != end; ++it) { |
| 142 | + fused_ops_.push_back(std::move(*it)); |
| 143 | + } |
| 144 | + |
| 145 | + for (std::vector<std::unique_ptr<OperatorBase>>::iterator it = end; |
| 146 | + (*it)->Type() != kFetchOpType; ++it) { |
| 147 | + for (auto& var_name_item : (*it)->Inputs()) { |
| 148 | + for (auto& var_name : var_name_item.second) { |
| 149 | + post_op_inputs_.insert(var_name); |
| 150 | + } |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + if ((*(start - 1))->Type() == kFeedOpType && (*end)->Type() == kFetchOpType) { |
| 155 | + is_complete = true; |
| 156 | + } |
| 157 | + |
| 158 | + Process(); |
| 159 | +} |
| 160 | + |
| 161 | +void FusedOperator::Process() { |
| 162 | + auto& bdesc = pdesc_.Block(block_); |
| 163 | + for (auto& var : bdesc.AllVars()) { |
| 164 | + if (!(var->GetType() == proto::VarType::SELECTED_ROWS || |
| 165 | + var->GetType() == proto::VarType::LOD_TENSOR || |
| 166 | + var->GetType() == proto::VarType::LOD_TENSOR_ARRAY)) { |
| 167 | + continue; |
| 168 | + } |
| 169 | + |
| 170 | + auto var_name = var->Name(); |
| 171 | + if (var->Name() == framework::kEmptyVarName) { |
| 172 | + continue; |
| 173 | + } |
| 174 | + |
| 175 | + if (var_name != "fetch" && var_name != "feed") { |
| 176 | + auto pd_type = var->GetDataType(); |
| 177 | + if (pd2ng_type_map.find(pd_type) == pd2ng_type_map.end()) { |
| 178 | + PADDLE_THROW("Data type of var %s not found in pd2ng_type_map", |
| 179 | + var_name); |
| 180 | + } |
| 181 | + var_type_map_[var_name] = pd2ng_type_map[pd_type]; |
| 182 | + } |
| 183 | + |
| 184 | + if (var->Persistable()) { |
| 185 | + persistables_.insert(var->Name()); |
| 186 | + } |
| 187 | + } |
| 188 | + |
| 189 | + for (auto* op : bdesc.AllOps()) { |
| 190 | + if (op->Type() == kFetchOpType) { |
| 191 | + std::string fetch_target_name = op->Input("X")[0]; |
| 192 | + fetches_.insert(fetch_target_name); |
| 193 | + } |
| 194 | + } |
| 195 | +} |
| 196 | + |
| 197 | +void FusedOperator::RunImpl(const Scope& scope, |
| 198 | + const platform::Place& place) const { |
| 199 | + op_state ng_op_state = PARTIAL_TEST; |
| 200 | + auto& bdesc = pdesc_.Block(block_); |
| 201 | + for (auto* op : bdesc.AllOps()) { |
| 202 | + if (op->Type().find("_grad") != std::string::npos) { |
| 203 | + ng_op_state = PARTIAL_TRAIN; |
| 204 | + break; |
| 205 | + } |
| 206 | + } |
| 207 | + |
| 208 | + if (is_full) { |
| 209 | + ng_op_state = ng_op_state == PARTIAL_TEST ? FULL_TEST : FULL_TRAIN; |
| 210 | + } |
| 211 | + |
| 212 | + NgraphOperator ngraph_op(scope, place, fused_ops_, var_type_map_, |
| 213 | + persistables_, fetches_, post_op_inputs_, |
| 214 | + ng_op_state); |
| 215 | + ngraph_op.Run(scope, place); |
| 216 | +} |
| 217 | + |
| 218 | +} // namespace framework |
| 219 | +} // namespace paddle |
| 220 | +#endif |
0 commit comments