op_version_registry.h 10.1 KB
Newer Older
1
/* Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24

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 <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include <boost/any.hpp>
#include "paddle/fluid/framework/framework.pb.h"
25
#include "paddle/fluid/framework/op_version_proto.h"
26 27 28 29 30 31 32
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace framework {
namespace compatible {

struct OpUpdateRecord {
33 34 35 36 37
  enum class Type {
    kInvalid = 0,
    kModifyAttr,
    kNewAttr,
    kNewInput,
W
Wilber 已提交
38 39
    kNewOutput,
    kBugfixWithBehaviorChanged,
40
  };
41 42 43 44 45 46
  Type type_;
  std::string remark_;
};

struct ModifyAttr : OpUpdateRecord {
  ModifyAttr(const std::string& name, const std::string& remark,
47
             const boost::any& default_value)
48 49 50 51 52 53 54 55 56 57
      : OpUpdateRecord({Type::kModifyAttr, remark}),
        name_(name),
        default_value_(default_value) {
    // TODO(Shixiaowei02): Check the data type with proto::OpDesc.
  }

 private:
  std::string name_;
  boost::any default_value_;
};
58

59
struct NewAttr : OpUpdateRecord {
60
  NewAttr(const std::string& name, const std::string& remark,
61
          const boost::any& default_value)
62 63 64
      : OpUpdateRecord({Type::kNewAttr, remark}),
        name_(name),
        default_value_(default_value) {}
65 66 67

 private:
  std::string name_;
68
  boost::any default_value_;
69 70
};

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
struct NewInput : OpUpdateRecord {
  NewInput(const std::string& name, const std::string& remark)
      : OpUpdateRecord({Type::kNewInput, remark}), name_(name) {}

 private:
  std::string name_;
};

struct NewOutput : OpUpdateRecord {
  NewOutput(const std::string& name, const std::string& remark)
      : OpUpdateRecord({Type::kNewOutput, remark}), name_(name) {}

 private:
  std::string name_;
};

W
Wilber 已提交
87 88 89 90 91
struct BugfixWithBehaviorChanged : OpUpdateRecord {
  explicit BugfixWithBehaviorChanged(const std::string& remark)
      : OpUpdateRecord({Type::kBugfixWithBehaviorChanged, remark}) {}
};

92 93 94 95 96 97 98 99 100
class OpVersionDesc {
 public:
  OpVersionDesc& ModifyAttr(const std::string& name, const std::string& remark,
                            boost::any default_value) {
    infos_.push_back(std::shared_ptr<OpUpdateRecord>(
        new compatible::ModifyAttr(name, remark, default_value)));
    return *this;
  }

101 102 103 104
  OpVersionDesc& NewAttr(const std::string& name, const std::string& remark,
                         boost::any default_value) {
    infos_.push_back(std::shared_ptr<OpUpdateRecord>(
        new compatible::NewAttr(name, remark, default_value)));
105 106 107
    return *this;
  }

108 109 110 111 112 113 114 115 116 117 118 119
  OpVersionDesc& NewInput(const std::string& name, const std::string& remark) {
    infos_.push_back(std::shared_ptr<OpUpdateRecord>(
        new compatible::NewInput(name, remark)));
    return *this;
  }

  OpVersionDesc& NewOutput(const std::string& name, const std::string& remark) {
    infos_.push_back(std::shared_ptr<OpUpdateRecord>(
        new compatible::NewOutput(name, remark)));
    return *this;
  }

W
Wilber 已提交
120 121 122 123 124 125
  OpVersionDesc& BugfixWithBehaviorChanged(const std::string& remark) {
    infos_.push_back(std::shared_ptr<OpUpdateRecord>(
        new compatible::BugfixWithBehaviorChanged(remark)));
    return *this;
  }

126 127 128 129 130 131 132 133 134 135 136
 private:
  std::vector<std::shared_ptr<OpUpdateRecord>> infos_;
};

class OpVersion {
 public:
  OpVersion& AddCheckpoint(const std::string& note,
                           const OpVersionDesc& op_version_desc) {
    checkpoints_.push_back(Checkpoint({note, op_version_desc}));
    return *this;
  }
S
Shang Zhizhou 已提交
137 138 139
  uint32_t GetVersionID() const {
    return static_cast<uint32_t>(checkpoints_.size());
  }
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155

 private:
  struct Checkpoint {
    std::string note_;
    OpVersionDesc op_version_desc_;
  };
  std::vector<Checkpoint> checkpoints_;
};

class OpVersionRegistrar {
 public:
  static OpVersionRegistrar& GetInstance() {
    static OpVersionRegistrar instance;
    return instance;
  }
  OpVersion& Register(const std::string& op_type) {
156 157 158 159
    PADDLE_ENFORCE_EQ(
        op_version_map_.find(op_type), op_version_map_.end(),
        platform::errors::AlreadyExists(
            "'%s' is registered in operator version more than once.", op_type));
160 161 162
    op_version_map_.insert({op_type, OpVersion()});
    return op_version_map_[op_type];
  }
163 164 165
  const std::unordered_map<std::string, OpVersion>& GetVersionMap() {
    return op_version_map_;
  }
S
Shang Zhizhou 已提交
166 167 168 169 170 171 172
  uint32_t GetVersionID(const std::string& op_type) const {
    auto it = op_version_map_.find(op_type);
    if (it == op_version_map_.end()) {
      return 0;
    }
    return it->second.GetVersionID();
  }
173 174 175 176 177 178 179 180

 private:
  std::unordered_map<std::string, OpVersion> op_version_map_;

  OpVersionRegistrar() = default;
  OpVersionRegistrar& operator=(const OpVersionRegistrar&) = delete;
};

181 182 183 184 185 186 187 188
inline void SaveOpVersions(
    const std::unordered_map<std::string, OpVersion>& src,
    pb::OpVersionMap* dst) {
  for (const auto& pair : src) {
    (*dst)[pair.first].SetVersionID(pair.second.GetVersionID());
  }
}

S
Shang Zhizhou 已提交
189 190 191 192 193 194 195 196 197 198 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 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307
class OpVersionComparator {
 public:
  virtual bool operator()() = 0;
  virtual ~OpVersionComparator() = default;
};

#define ADD_OP_VERSION_COMPARATOR(cmp_name, cmp_math)                   \
  class OpVersion##cmp_name##Comparator : public OpVersionComparator {  \
   public:                                                              \
    explicit OpVersion##cmp_name##Comparator(const std::string op_name, \
                                             uint32_t target_version)   \
        : op_name_(op_name), target_version_(target_version) {}         \
    virtual bool operator()() {                                         \
      return OpVersionRegistrar::GetInstance().GetVersionID(op_name_)   \
          cmp_math target_version_;                                     \
    }                                                                   \
    virtual ~OpVersion##cmp_name##Comparator() {}                       \
                                                                        \
   private:                                                             \
    std::string op_name_;                                               \
    uint32_t target_version_;                                           \
  };

ADD_OP_VERSION_COMPARATOR(LE, <=);
ADD_OP_VERSION_COMPARATOR(EQ, ==);
ADD_OP_VERSION_COMPARATOR(GE, >=);
ADD_OP_VERSION_COMPARATOR(NE, !=);

class OpVersionComparatorCombination {
 public:
  OpVersionComparatorCombination() {}

  OpVersionComparatorCombination& LE(const std::string& op_name,
                                     int target_version) {
    op_version_comparators_.push_back(std::shared_ptr<OpVersionComparator>(
        new OpVersionLEComparator(op_name, target_version)));
    return *this;
  }
  OpVersionComparatorCombination& EQ(const std::string& op_name,
                                     int target_version) {
    op_version_comparators_.push_back(std::shared_ptr<OpVersionComparator>(
        new OpVersionEQComparator(op_name, target_version)));
    return *this;
  }
  OpVersionComparatorCombination& GE(const std::string& op_name,
                                     int target_version) {
    op_version_comparators_.push_back(std::shared_ptr<OpVersionComparator>(
        new OpVersionGEComparator(op_name, target_version)));
    return *this;
  }
  OpVersionComparatorCombination& NE(const std::string& op_name,
                                     int target_version) {
    op_version_comparators_.push_back(std::shared_ptr<OpVersionComparator>(
        new OpVersionNEComparator(op_name, target_version)));
    return *this;
  }

  bool IsMatched() const {
    for (const auto& cmp : op_version_comparators_) {
      if (!(*cmp)()) {
        return false;
      }
    }
    return true;
  }

 private:
  std::vector<std::shared_ptr<OpVersionComparator>> op_version_comparators_;
};

class PassVersionCheckers {
 public:
  PassVersionCheckers& AddCombination(
      const OpVersionComparatorCombination& combinations) {
    pass_version_checkers_.push_back(combinations);
    return *this;
  }
  bool IsPassCompatible() const {
    if (pass_version_checkers_.empty()) {
      return true;
    }
    for (const auto& checker : pass_version_checkers_) {
      if (checker.IsMatched()) {
        return true;
      }
    }
    return false;
  }

 private:
  std::vector<OpVersionComparatorCombination> pass_version_checkers_;
};

class PassVersionCheckerRegistrar {
 public:
  static PassVersionCheckerRegistrar& GetInstance() {
    static PassVersionCheckerRegistrar instance;
    return instance;
  }
  PassVersionCheckers& Register(const std::string& pass_name) {
    return pass_version_checkers_map_[pass_name];
  }
  bool IsPassCompatible(const std::string& fuse_pass_name) const {
    auto iter = pass_version_checkers_map_.find(fuse_pass_name);
    if (iter == pass_version_checkers_map_.end()) {
      return true;
    }
    return iter->second.IsPassCompatible();
  }

 private:
  std::unordered_map<std::string, PassVersionCheckers>
      pass_version_checkers_map_;

  PassVersionCheckerRegistrar() = default;
  PassVersionCheckerRegistrar& operator=(const PassVersionCheckerRegistrar&) =
      delete;
};

308 309 310 311 312 313 314 315 316
}  // namespace compatible
}  // namespace framework
}  // namespace paddle

#define REGISTER_OP_VERSION(op_type)                                       \
  static paddle::framework::compatible::OpVersion                          \
      RegisterOpVersion__##op_type =                                       \
          paddle::framework::compatible::OpVersionRegistrar::GetInstance() \
              .Register(#op_type)
S
Shang Zhizhou 已提交
317 318 319 320 321 322

#define REGISTER_PASS_CAPABILITY(pass_name)                        \
  static auto RegisterOpPassVersionChecker__##pass_name =          \
      paddle::framework::compatible::PassVersionCheckerRegistrar:: \
          GetInstance()                                            \
              .Register(#pass_name)