block_id is the idx of the current block in the input desc parent_block_id is the idx of the parent of the current block in the output desc, -1 means the current block is global block dependent_vars is passed recursively from the parent block to the child block to help pruning
| 224 | // dependent_vars is passed recursively from the parent block to |
| 225 | // the child block to help pruning |
| 226 | void prune_impl(const proto::ProgramDesc& input, |
| 227 | proto::ProgramDesc* output, |
| 228 | int block_id, |
| 229 | int parent_block_id, |
| 230 | std::unordered_set<std::string>* dependent_vars, |
| 231 | const std::set<std::string> feed_var_names, |
| 232 | std::map<int, int>* pruned_origin_block_id_map) { |
| 233 | auto& block = input.blocks(block_id); |
| 234 | auto& ops = block.ops(); |
| 235 | auto add_dependent_var = [&](const std::string& name) { |
| 236 | if (feed_var_names.count(name) == 0) dependent_vars->insert(name); |
| 237 | }; |
| 238 | |
| 239 | bool expect_feed = true; |
| 240 | for (auto& op_desc : ops) { |
| 241 | PADDLE_ENFORCE_EQ( |
| 242 | op_desc.type() != kFeedOpType || expect_feed, |
| 243 | true, |
| 244 | common::errors::PreconditionNotMet( |
| 245 | "All FeedOps are at the beginning of the ProgramDesc")); |
| 246 | expect_feed = (op_desc.type() == kFeedOpType); |
| 247 | } |
| 248 | |
| 249 | bool expect_fetch = true; |
| 250 | for (auto op_iter = ops.rbegin(); op_iter != ops.rend(); ++op_iter) { |
| 251 | auto& op_desc = *op_iter; |
| 252 | PADDLE_ENFORCE_EQ(op_desc.type() != kFetchOpType || expect_fetch, |
| 253 | true, |
| 254 | common::errors::PreconditionNotMet( |
| 255 | "All FetchOps must at the end of the ProgramDesc")); |
| 256 | expect_fetch = (op_desc.type() == kFetchOpType); |
| 257 | } |
| 258 | |
| 259 | std::vector<bool> should_run; |
| 260 | for (auto op_iter = ops.rbegin(); op_iter != ops.rend(); ++op_iter) { |
| 261 | auto& op_desc = *op_iter; |
| 262 | |
| 263 | // TODO(wanghaipeng03) reconstruct the following if/else block |
| 264 | // to extract common code |
| 265 | // |
| 266 | // bool should_run_flag = false; |
| 267 | // if (IsTarget........) { |
| 268 | // should_run_flag = true; |
| 269 | // } else { |
| 270 | // if (parent......) { |
| 271 | // for (....) { |
| 272 | // for (.....) { |
| 273 | // if (.....) { |
| 274 | // should_run_flag = true; |
| 275 | // } |
| 276 | // } |
| 277 | // } |
| 278 | // } |
| 279 | // } |
| 280 | // |
| 281 | // should_run.push_back(should_run_flag); |
| 282 | // if (should_run_flag) { |
| 283 | // for (auto & var: op_desc.inputs()) { |
no test coverage detected