You need to sign in or sign up before continuing.
prune.cc 6.5 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Y
Yang Yang 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

Y
Yi Wang 已提交
15
#include "paddle/fluid/framework/prune.h"
Y
Yang Yang 已提交
16 17 18 19

#include <algorithm>
#include <set>
#include <string>
K
Kexin Zhao 已提交
20
#include <unordered_map>
Y
Yang Yang 已提交
21 22 23 24 25 26 27 28 29 30
#include <vector>

#include <glog/logging.h>

namespace paddle {
namespace framework {

const std::string kFeedOpType = "feed";
const std::string kFetchOpType = "fetch";

31
bool HasDependentVar(const proto::OpDesc& op_desc,
Y
Yang Yang 已提交
32 33 34 35 36 37 38 39 40 41 42
                     const std::set<std::string>& dependent_vars) {
  for (auto& var : op_desc.outputs()) {
    for (auto& argu : var.arguments()) {
      if (dependent_vars.count(argu) != 0) {
        return true;
      }
    }
  }
  return false;
}

43
bool IsTarget(const proto::OpDesc& op_desc) {
Y
Yang Yang 已提交
44 45 46 47 48 49
  if (op_desc.has_is_target()) {
    return op_desc.is_target();
  }
  return false;
}

K
Kexin Zhao 已提交
50 51 52 53 54 55 56 57 58
int GetSubBlockIndex(const proto::OpDesc& op_desc) {
  for (auto& attr : op_desc.attrs()) {
    if (attr.type() == proto::AttrType::BLOCK) {
      PADDLE_ENFORCE(attr.has_block_idx());
      return attr.block_idx();
    }
  }
  return -1;
}
Y
Yang Yang 已提交
59

K
Kexin Zhao 已提交
60 61 62 63 64 65 66 67 68 69 70 71
bool HasSubBlock(const proto::OpDesc& op_desc) {
  return GetSubBlockIndex(op_desc) > 0;
}

// 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
void prune_impl(const proto::ProgramDesc& input, proto::ProgramDesc* output,
                int block_id, int parent_block_id,
                std::set<std::string>& dependent_vars) {
Y
Yang Yang 已提交
72
  auto& block = input.blocks(block_id);
Y
Yang Yang 已提交
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
  auto& ops = block.ops();

  bool expect_feed = true;
  for (auto& op_desc : ops) {
    PADDLE_ENFORCE(op_desc.type() != kFeedOpType || expect_feed,
                   "All FeedOps are at the beginning of the ProgramDesc");
    expect_feed = (op_desc.type() == kFeedOpType);
  }

  bool expect_fetch = true;
  for (auto op_iter = ops.rbegin(); op_iter != ops.rend(); ++op_iter) {
    auto& op_desc = *op_iter;
    PADDLE_ENFORCE(op_desc.type() != kFetchOpType || expect_fetch,
                   "All FetchOps must at the end of the ProgramDesc");
    expect_fetch = (op_desc.type() == kFetchOpType);
  }

  std::vector<bool> should_run;
  for (auto op_iter = ops.rbegin(); op_iter != ops.rend(); ++op_iter) {
    auto& op_desc = *op_iter;
Y
Yang Yang 已提交
93
    if (IsTarget(op_desc) || HasDependentVar(op_desc, dependent_vars)) {
Y
Yang Yang 已提交
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
      // insert its input to the dependency graph
      for (auto& var : op_desc.inputs()) {
        for (auto& argu : var.arguments()) {
          dependent_vars.insert(argu);
        }
      }
      should_run.push_back(true);
    } else {
      should_run.push_back(false);
    }
  }

  // since we are traversing the ProgramDesc in reverse order
  // we reverse the should_run vector
  std::reverse(should_run.begin(), should_run.end());

K
Kexin Zhao 已提交
110 111 112 113 114 115
  // copy the current block from input to output
  auto* block_field = output->mutable_blocks();
  *block_field->Add() = input.blocks(block_id);

  int output_block_id = output->blocks_size() - 1;
  auto* output_block = output->mutable_blocks(output_block_id);
116 117
  output_block->set_idx(output_block_id);
  output_block->set_parent_idx(parent_block_id);
K
Kexin Zhao 已提交
118 119

  auto* op_field = output_block->mutable_ops();
Y
Yang Yang 已提交
120 121 122
  op_field->Clear();
  for (size_t i = 0; i < should_run.size(); ++i) {
    if (should_run[i]) {
K
Kexin Zhao 已提交
123 124 125 126 127
      auto* op = op_field->Add();
      *op = input.blocks(block_id).ops(i);
      if (HasSubBlock(*op)) {
        // create sub_block_dependent_vars here to help prune the sub block
        std::set<std::string> sub_block_dependent_vars;
128
        for (auto& var : op->inputs()) {
K
Kexin Zhao 已提交
129 130 131 132
          for (auto& argu : var.arguments()) {
            sub_block_dependent_vars.insert(argu);
          }
        }
133
        for (auto& var : op->outputs()) {
K
Kexin Zhao 已提交
134 135 136 137 138 139 140 141 142
          for (auto& argu : var.arguments()) {
            sub_block_dependent_vars.insert(argu);
          }
        }
        // GetSubBlockIndex(*op) is the idx of the sub_block in the input desc
        // output_block_id is the idx of the current block in the output desc
        prune_impl(input, output, GetSubBlockIndex(*op), output_block_id,
                   sub_block_dependent_vars);
      }
Y
Yang Yang 已提交
143 144
    }
  }
K
Kexin Zhao 已提交
145

K
Kexin Zhao 已提交
146 147 148
  // remove the VarDescs in BlockDesc that are not referenced in
  // the pruned OpDescs
  std::unordered_map<std::string, proto::VarDesc> var_map;
K
Kexin Zhao 已提交
149
  auto* var_field = output->mutable_blocks(output_block_id)->mutable_vars();
K
Kexin Zhao 已提交
150 151
  for (const auto& var : *var_field) {
    var_map[var.name()] = var;
K
Kexin Zhao 已提交
152 153
  }

154
  std::set<std::string> var_names;
K
Kexin Zhao 已提交
155 156
  for (const auto& op : *op_field) {
    auto& input_field = op.inputs();
K
Kexin Zhao 已提交
157 158
    for (auto& input_var : input_field) {
      for (auto& arg : input_var.arguments()) {
159 160 161
        if (var_map.count(arg) != 0) {
          var_names.insert(arg);
        }
K
Kexin Zhao 已提交
162 163 164
      }
    }
    auto& output_field = op.outputs();
K
Kexin Zhao 已提交
165 166
    for (auto& output_var : output_field) {
      for (auto& arg : output_var.arguments()) {
167 168 169
        if (var_map.count(arg) != 0) {
          var_names.insert(arg);
        }
K
Kexin Zhao 已提交
170 171 172
      }
    }
  }
173 174 175 176 177

  var_field->Clear();
  for (const auto& name : var_names) {
    *var_field->Add() = var_map[name];
  }
Y
Yang Yang 已提交
178
}
Y
Yang Yang 已提交
179

180
// TODO(fengjiayi): Prune() could be inplaced to avoid unnecessary copies
181
void Prune(const proto::ProgramDesc& input, proto::ProgramDesc* output) {
182
  std::set<std::string> dependent_vars;
K
fix bug  
Kexin Zhao 已提交
183
  output->clear_blocks();
184
  prune_impl(input, output, 0, -1, dependent_vars);
Y
Yang Yang 已提交
185 186
}

187 188
void inference_optimize_impl(proto::ProgramDesc* input, int block_id) {
  auto* op_field = input->mutable_blocks(block_id)->mutable_ops();
189
  for (auto& op_desc : *op_field) {
190 191 192 193
    for (auto& attr : *op_desc.mutable_attrs()) {
      if (attr.name() == "is_test") {
        attr.set_b(true);
        break;
194 195 196 197 198
      }
    }
  }
}

199 200
void InferenceOptimize(const proto::ProgramDesc& input,
                       proto::ProgramDesc* output) {
201 202 203 204 205 206
  *output = input;
  int num_blocks = output->blocks_size();
  PADDLE_ENFORCE_GT(num_blocks, 0, "ProgramDesc must have at least one block");
  for (int i = 0; i < num_blocks; ++i) {
    inference_optimize_impl(output, i);
  }
207 208
}

Y
Yang Yang 已提交
209 210
}  // namespace framework
}  // namespace paddle