cpu_bfloat16_pass.cc 9.7 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* Copyright (c) 2020 PaddlePaddle Authors. 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 "paddle/fluid/framework/ir/mkldnn/cpu_bfloat16_pass.h"

14
#include <map>
15
#include <string>
16
#include <unordered_map>
17 18 19
#include <vector>

#include "paddle/fluid/framework/ir/graph_pattern_detector.h"
20
#include "paddle/fluid/framework/op_version_registry.h"
21 22 23 24 25 26
#include "paddle/fluid/string/pretty_log.h"

namespace paddle {
namespace framework {
namespace ir {

T
Tomasz Socha 已提交
27 28 29 30 31
namespace {
class Quanter {
 public:
  void AddQuantOps() {
    if (IsNotPermittedOpType()) return;
32

T
Tomasz Socha 已提交
33
    std::vector<std::string> linked_xputs;
34

T
Tomasz Socha 已提交
35 36 37
    for (const auto& logical_xput : op_xputs) {
      std::vector<std::string> quant_xput_names;
      quant_xput_names.reserve(xputs_map.size());
38

T
Tomasz Socha 已提交
39 40
      const auto& logical_xput_name = logical_xput.first;
      if (IsNotPermittedName(logical_xput_name)) continue;
41

T
Tomasz Socha 已提交
42 43 44
      const auto& physical_xputs_names = logical_xput.second;
      for (const auto& physical_xput_name : physical_xputs_names) {
        if (IsAlreadyLinked(linked_xputs, physical_xput_name)) continue;
45

T
Tomasz Socha 已提交
46 47
        VarDesc quant_x_desc(
            patterns::PDNodeName(get_op_type(), get_op_edge()));
48
        auto quant_x_node = graph->CreateVarNode(&quant_x_desc);
T
Tomasz Socha 已提交
49 50 51 52 53 54 55 56 57 58 59 60 61
        const auto xput_name = quant_x_node->Name();
        quant_xput_names.emplace_back(xput_name);

        auto quant_op = create_quant_op(physical_xput_name, xput_name);

        auto physical_xput_node = xputs_map[physical_xput_name];
        link_nodes(physical_xput_node, quant_op, quant_x_node);
        counter++;
        linked_xputs.push_back(physical_xput_name);
      }

      set_edge(logical_xput_name, quant_xput_names);
    }
62 63
  }

T
Tomasz Socha 已提交
64
  int get_counter() const { return counter; }
65

T
Tomasz Socha 已提交
66 67 68
  virtual ~Quanter() = default;

 protected:
69
  Graph* graph;
T
Tomasz Socha 已提交
70 71 72 73 74 75 76
  ir::Node* const op;

  std::map<std::string, ir::Node*> xputs_map;
  const VariableNameMap& op_xputs;

  int counter = 0;

77 78 79 80
  Quanter(Graph* const graph,
          ir::Node* const op,
          const VariableNameMap& op_xputs)
      : graph(graph), op(op), op_xputs(op_xputs) {}
T
Tomasz Socha 已提交
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107

  virtual bool IsNotPermittedOpType() const = 0;
  virtual bool IsNotPermittedName(const std::string& input_name) const = 0;
  virtual std::string get_op_type() const = 0;
  virtual std::string get_op_edge() const = 0;
  virtual void link_nodes(ir::Node* const physical_xput_node,
                          ir::Node* const quant_op,
                          ir::Node* const quant_x_node) = 0;
  virtual void set_edge(const std::string& logical_xput_name,
                        const std::vector<std::string>& quant_xput_names) = 0;

  bool IsAlreadyLinked(const std::vector<std::string>& node_names,
                       const std::string& node_name) const {
    return std::find(node_names.begin(), node_names.end(), node_name) !=
           node_names.end();
  }

  virtual ir::Node* create_quant_op(const std::string& input_name,
                                    const std::string& output_name) const {
    OpDesc op_desc;
    op_desc.SetType(get_op_type());

    op_desc.SetInput("Input", std::vector<std::string>({input_name}));
    op_desc.SetOutput("Output", std::vector<std::string>({output_name}));
    op_desc.SetAttr("Scale", 1.f);
    op_desc.SetAttr("Shift", 0.0f);
    op_desc.SetAttr("bfloat16", true);
108 109 110 111 112
    op_desc.SetAttr("output_format",
                    op->Op()->HasAttr("data_layout")
                        ? op->Op()->GetAttr("data_layout")
                        : std::string("NCHW"));
    return graph->CreateOpNode(&op_desc);  // OpDesc will be copied.
T
Tomasz Socha 已提交
113 114 115 116 117 118 119 120 121 122 123 124 125
  }

  void UnlinkNodes(ir::Node* a, ir::Node* b) const {
    a->outputs.erase(std::remove(a->outputs.begin(), a->outputs.end(), b),
                     a->outputs.end());
    b->inputs.erase(std::remove(b->inputs.begin(), b->inputs.end(), a),
                    b->inputs.end());
  }
};

class Quantizer final : public Quanter {
 public:
  Quantizer(Graph* const graph, ir::Node* const op)
126
      : Quanter(graph, op, op->Op()->Inputs()) {
T
Tomasz Socha 已提交
127 128
    auto inputs = op->inputs;
    PADDLE_ENFORCE_GE(
129 130
        inputs.size(),
        1,
T
Tomasz Socha 已提交
131
        platform::errors::InvalidArgument(
132 133
            "OP(%s)'s inputs(%d) must be equal or greater than 1.",
            op->Name(),
T
Tomasz Socha 已提交
134 135 136
            inputs.size()));

    for (auto input : inputs) xputs_map[input->Name()] = input;
137
  }
138

T
Tomasz Socha 已提交
139 140 141 142 143 144 145 146 147
 protected:
  bool IsNotPermittedOpType() const override { return false; }

  // Checking whether a reorder from FP32 to BF16
  // should be added before the input to the operator
  bool IsNotPermittedName(const std::string& input_name) const override {
    // Only the inputs listed in \"permitted_names\"
    // requires quanitization before the bfloat16 operator.
    // Other inputs, such as Filter and Bias are reordered in the kernel.
148 149
    const std::vector<std::string> permitted_names = {
        "X", "Y", "Input", "ResidualData"};
T
Tomasz Socha 已提交
150 151

    return std::none_of(
152 153
        permitted_names.begin(),
        permitted_names.end(),
T
Tomasz Socha 已提交
154 155 156 157 158 159
        [&input_name](const std::string& name) { return name == input_name; });
  }

  std::string get_op_type() const override { return "quantize"; };
  std::string get_op_edge() const override { return "out"; };

160 161
  void link_nodes(ir::Node* const physical_xput_node,
                  ir::Node* const quant_op,
T
Tomasz Socha 已提交
162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177
                  ir::Node* const quant_x_node) override {
    UnlinkNodes(physical_xput_node, op);
    IR_NODE_LINK_TO(physical_xput_node, quant_op);
    IR_NODE_LINK_TO(quant_op, quant_x_node);
    IR_NODE_LINK_TO(quant_x_node, op);
  }

  void set_edge(const std::string& logical_xput_name,
                const std::vector<std::string>& quant_xput_names) override {
    op->Op()->SetInput(logical_xput_name, quant_xput_names);
  }
};

class DeQuantizer final : public Quanter {
 public:
  DeQuantizer(Graph* const graph, ir::Node* const op)
178
      : Quanter(graph, op, op->Op()->Outputs()) {
T
Tomasz Socha 已提交
179 180
    auto outputs = op->outputs;
    PADDLE_ENFORCE_GE(
181 182
        outputs.size(),
        1,
T
Tomasz Socha 已提交
183
        platform::errors::InvalidArgument(
184 185
            "OP(%s)'s outputs(%d) must be equal or greater than 1.",
            op->Name(),
T
Tomasz Socha 已提交
186 187 188
            outputs.size()));

    for (auto output : outputs) xputs_map[output->Name()] = output;
189
  }
190

T
Tomasz Socha 已提交
191 192 193 194 195
 protected:
  bool IsNotPermittedOpType() const override {
    // Prior_box operator output is always FP32 so no dequantization is needed.
    return op->Op()->Type() == "prior_box";
  }
196

T
Tomasz Socha 已提交
197 198 199
  // Checking whether a reorder from BF16 to FP32
  // should be added after the output to the operator
  bool IsNotPermittedName(const std::string& output_name) const override {
200 201
    std::unordered_map<std::string, std::vector<std::string>> block_list{
        {"layer_norm",
T
Tomasz Socha 已提交
202 203
         {"Mean", "Variance"}},     // not used in inference in MKLDNN
        {"fc", {"ResidualData"}}};  // artifical output, already dequantized
204 205 206 207 208

    std::vector<std::string> blocked_outputs{"XShape"};  // blocklist for any op
    auto op_name = op->Name();
    if (block_list.count(op_name)) {
      const auto& op_blocklist = block_list[op_name];
209 210
      blocked_outputs.insert(
          blocked_outputs.begin(), op_blocklist.begin(), op_blocklist.end());
211 212
    }

213 214
    return std::any_of(blocked_outputs.begin(),
                       blocked_outputs.end(),
215 216 217
                       [&output_name](const std::string& name) {
                         return name == output_name;
                       });
T
Tomasz Socha 已提交
218 219 220 221
  }

  std::string get_op_type() const override { return "dequantize"; };
  std::string get_op_edge() const override { return "in"; };
222

223 224
  void link_nodes(ir::Node* const physical_xput_node,
                  ir::Node* const quant_op,
T
Tomasz Socha 已提交
225 226 227 228 229
                  ir::Node* const quant_x_node) override {
    UnlinkNodes(op, physical_xput_node);
    IR_NODE_LINK_TO(quant_op, physical_xput_node);
    IR_NODE_LINK_TO(quant_x_node, quant_op);
    IR_NODE_LINK_TO(op, quant_x_node);
230 231
  }

T
Tomasz Socha 已提交
232 233 234 235
  void set_edge(const std::string& logical_xput_name,
                const std::vector<std::string>& quant_xput_names) override {
    op->Op()->SetOutput(logical_xput_name, quant_xput_names);
  }
236

T
Tomasz Socha 已提交
237 238 239 240 241
  ir::Node* create_quant_op(const std::string& input_name,
                            const std::string& output_name) const override {
    return Quanter::create_quant_op(output_name, input_name);
  }
};
242
}  // namespace
T
Tomasz Socha 已提交
243 244 245 246 247
using string::PrettyLogDetail;

void CPUBFloat16Pass::ApplyImpl(ir::Graph* graph) const {
  int quantize_counter = 0;
  int dequantize_counter = 0;
248

249
  GraphPatternDetector gpd;
T
Tomasz Socha 已提交
250 251
  patterns::Bloat16Ops Bloat16Ops{gpd.mutable_pattern(), "Bloat16Ops"};
  Bloat16Ops();
252
  auto handler = [&](const GraphPatternDetector::subgraph_t& subgraph,
T
Tomasz Socha 已提交
253 254 255 256 257 258 259 260 261 262
                     Graph* graph) {
    GET_IR_NODE_FROM_SUBGRAPH(op, op, Bloat16Ops);

    Quantizer quantizer(graph, op);
    quantizer.AddQuantOps();
    quantize_counter += quantizer.get_counter();

    DeQuantizer dequantizer(graph, op);
    dequantizer.AddQuantOps();
    dequantize_counter += dequantizer.get_counter();
263 264
  };
  gpd(graph, handler);
265

T
Tomasz Socha 已提交
266 267
  PrettyLogDetail("---    added %d quantize ops before bfloat16 op",
                  quantize_counter);
268 269
  PrettyLogDetail("---    added %d dequantize ops after bfloat16 op",
                  dequantize_counter);
270 271 272 273 274 275 276
}

}  // namespace ir
}  // namespace framework
}  // namespace paddle

REGISTER_PASS(cpu_bfloat16_pass, paddle::framework::ir::CPUBFloat16Pass);
277 278 279 280 281

REGISTER_PASS_CAPABILITY(cpu_bfloat16_pass)
    .AddCombination(
        paddle::framework::compatible::OpVersionComparatorCombination().GE(
            "quantize", 1));