op_compat_sensible_pass.h 8.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31
/* Copyright (c) 2021 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. */

#pragma once

#include <map>
#include <vector>
#include "paddle/fluid/framework/ir/graph.h"
#include "paddle/fluid/framework/ir/graph_pattern_detector.h"
#include "paddle/fluid/framework/ir/pass.h"

namespace paddle {
namespace framework {
namespace ir {

class OpCompat;

class AttrCompat {
 public:
  AttrCompat(const std::string& attr_name, OpCompat* op_compat)
32
      : optional_(false), attr_name_(attr_name), op_compat_(op_compat) {}
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

  // @{ String-related methods
  //! Assert the attribute is an string in the `candidates` domain.
  AttrCompat& IsStringIn(const std::set<std::string>& candidates);
  //! Assert the attribute is a string and match a custom judging function.
  AttrCompat& IsStringMatch(
      const std::function<bool(const std::string&)>& func);
  // @}

  //! Assert the attribute is an integer in the `candidates` domain.
  AttrCompat& IsIntIn(const std::set<int>& candidates);

  // @{ Number-releated methods
  //! Assert the attribute is a number and > `v`.
  template <typename T>
  AttrCompat& IsNumGT(T v);
  //! Assert the attribute is a number and >= `v`.
  template <typename T>
  AttrCompat& IsNumGE(T v);
  //! Assert the attribute is a number and < `v`.
  template <typename T>
  AttrCompat& IsNumLT(T v);
  //! Assert the attribute is a number and <= `v`.
  template <typename T>
  AttrCompat& IsNumLE(T v);
  //! Assert the attribute is a number and == `v`.
  template <typename T>
  AttrCompat& IsNumEQ(T v);
  //! Assert the attribute is a number and matches a customized judging
  //! function.
  template <typename T>
  AttrCompat& IsNumMatch(bool (*func)(T));
  // @}

  //! Assert the attribute is a boolean value equals `v`.
  AttrCompat& IsBoolEQ(bool v);

  //! Tell whether this attribute is left as default value.
  AttrCompat& IsLeftDefault();

73 74
  AttrCompat& IsOptional();

75 76 77 78 79 80
  //! Jump back to retrieve OpCompat instance.
  OpCompat& End() { return *op_compat_; }

  bool operator()(const OpDesc& op_desc);

 private:
81
  bool optional_;
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 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  std::string attr_name_;
  OpCompat* op_compat_;
  std::vector<std::function<bool(const Attribute&)>> conditions_;
};

class InputOrOutputCompat {
 public:
  InputOrOutputCompat(const std::string& name, OpCompat* op_compat)
      : optional_(false), name_(name), op_compat_(op_compat) {}

  InputOrOutputCompat& IsTensor();
  InputOrOutputCompat& IsOptional();
  bool Optional() const { return optional_; }
  bool operator()(const std::vector<std::string>& input) const;

  //! Jump back to retrieve OpCompat instance.
  OpCompat& End() { return *op_compat_; }

 private:
  bool optional_;
  std::string name_;
  OpCompat* op_compat_;
  std::vector<std::function<bool(const std::vector<std::string>&)>> conditions_;
};

/**
 * OpCompat is a helper class to help define the compatible Op definition.
 *
 * Usage:
 *   OpCompat compat("FC");
 *   compat.AddAttr("in_num_col_dims").IsNumLE(1).End()
 *         .AddAttr("activation_type").IsStringIn({"tanh", "sigmoid"}).End()
 *         .AddInput("Input").IsTensor().End()
 *         .AddInput("W").IsTensor().End()
 *         .AddInput("Bias").IsTensor().IsOptional().End()
 *         .AddOutput("Out").IsTensor().End()
 *
 * All the inference-aware Op defition is as above, all the other attributes not
 * contained in the definition should be set default value or it would be judged
 * incompatible.
 */
class OpCompat {
 public:
  explicit OpCompat(const std::string& op_name) : op_name_(op_name) {}
  explicit OpCompat(std::string&& op_name) : op_name_(std::move(op_name)) {}
  explicit OpCompat(const OpCompat&) = default;
  explicit OpCompat(OpCompat&&) = default;

  AttrCompat& AddAttr(const std::string& attr_name);
  InputOrOutputCompat& AddInput(const std::string& name);
  InputOrOutputCompat& AddOutput(const std::string& name);

  //! Judge whether an OpDesc match the defined Op compatibility.
  bool Judge(const OpDesc& op_desc);
  const std::string& Name() const { return op_name_; }

 private:
  std::string op_name_;
140
  std::unordered_map<std::string, AttrCompat> attr_compats_;
141 142
  std::unordered_map<std::string, InputOrOutputCompat> input_compats_;
  std::unordered_map<std::string, InputOrOutputCompat> output_compats_;
143 144
  std::unordered_set<std::string> extra_attrs_;
  bool is_first_judge_ = true;
145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197
};

/**
 * OpCompatSensiblePass is a base class for all the passes thouse is sensitive
 * to Op update.
 * There are two methods to help tell the compability of an Op
 *   bool IsCompat(const GraphPatternDetector::subgraph_t& subgraph, Graph* g);
 *   bool IsCompat(const OpDesc& op_desc);
 *
 * One can register the related Op compabilities using
 *   void AddOpCompat(OpCompat&& judger);
 *
 * Most of the Passes are used for fusing ops, so we define a method for such
 * scenerios.
 *   void AccessSubgraph(const GraphPatternDetector::subgraph_t& subgraph,
 Graph* g);
 * It will check the Op compatibility automatically.
 * For other scenirios, one should call `IsCompat` by himself.
 *
 * A FC fuse pass example:
 * class FcFusePass : public OpCompatSensiblePass {
 *  public:
 *   FcFusePass() {
 *     // define Mul op compatiblity.
 *     AddOpCompat(OpCompat("Mul"))
 *        .AddInput("Input").IsTensor().End()
 *        .AddAttr("in_num_col_dims").IsNumGE(1);
 *     AddOpCompat(OpCompat("Add")). ...;
 *     // There are multiple activation implemention.
 *     AddOpCompat(OpCompat("Tanh")). ...;
 *     AddOpCompat(OpCompat("Sigmoid")). ...;
 *   }
 *
 *   // override the subgraph access method
 *   virtual bool AccessSubgraphImpl(
 *   const GraphPatternDetector::subgraph_t& subgraph,
 *         Graph* g) override { ... }
 *
 *   // Call the AccessSubgraph method in main procedure of this Pass.
 * };
 */
class OpCompatSensiblePass : public Pass {
 protected:
  /**
   * Developer should push the compatibility `teller` for each kind of Op in the
   * subgraph.
   * NOTE One should add all the related op compatiblity in the construct so
   * that all the following methods are valid.
   */
  OpCompat& AddOpCompat(OpCompat&& op_compat);

  //! Tell the Op compability of a subgraph.
  bool IsCompat(const GraphPatternDetector::subgraph_t& subgraph,
198
                Graph* g) const;
199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266

  //! Tell the op compatibility of a single Op.
  bool IsCompat(const OpDesc& op_desc) const {
    if (!op_compat_judgers_.count(op_desc.Type())) return false;
    return op_compat_judgers_.at(op_desc.Type())->Judge(op_desc);
  }

 private:
  std::map<std::string, std::unique_ptr<OpCompat>> op_compat_judgers_;
};

template <typename T>
AttrCompat& AttrCompat::IsNumGT(T v) {
  conditions_.emplace_back([v](const Attribute& attr) -> bool {
    T value = BOOST_GET_CONST(T, attr);
    return value > v;
  });
  return *this;
}

template <typename T>
AttrCompat& AttrCompat::IsNumGE(T v) {
  conditions_.emplace_back([v](const Attribute& attr) -> bool {
    T value = BOOST_GET_CONST(T, attr);
    return value >= v;
  });
  return *this;
}

template <typename T>
AttrCompat& AttrCompat::IsNumLT(T v) {
  conditions_.emplace_back([v](const Attribute& attr) -> bool {
    T value = BOOST_GET_CONST(T, attr);
    return value < v;
  });
  return *this;
}

template <typename T>
AttrCompat& AttrCompat::IsNumLE(T v) {
  conditions_.emplace_back([v](const Attribute& attr) -> bool {
    T value = BOOST_GET_CONST(T, attr);
    return value <= v;
  });
  return *this;
}

template <typename T>
AttrCompat& AttrCompat::IsNumEQ(T v) {
  conditions_.emplace_back([v](const Attribute& attr) -> bool {
    T value = BOOST_GET_CONST(T, attr);
    return value == v;
  });
  return *this;
}

template <typename T>
AttrCompat& AttrCompat::IsNumMatch(bool (*func)(T)) {
  conditions_.emplace_back([func](const Attribute& attr) -> bool {
    T value = BOOST_GET_CONST(T, attr);
    return func(value);
  });
  return *this;
}

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