op_version_registry.h 4.6 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) 2016 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 <memory>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>

#include <boost/any.hpp>
#include "paddle/fluid/framework/framework.pb.h"
#include "paddle/fluid/platform/enforce.h"

namespace paddle {
namespace framework {
namespace compatible {

struct OpUpdateRecord {
32 33 34 35 36 37 38
  enum class Type {
    kInvalid = 0,
    kModifyAttr,
    kNewAttr,
    kNewInput,
    kNewOutput
  };
39 40 41 42 43 44
  Type type_;
  std::string remark_;
};

struct ModifyAttr : OpUpdateRecord {
  ModifyAttr(const std::string& name, const std::string& remark,
45
             const boost::any& default_value)
46 47 48 49 50 51 52 53 54 55
      : 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_;
};
56

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

 private:
  std::string name_;
66
  boost::any default_value_;
67 68
};

69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84
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_;
};

85 86 87 88 89 90 91 92 93
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;
  }

94 95 96 97
  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)));
98 99 100
    return *this;
  }

101 102 103 104 105 106 107 108 109 110 111 112
  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;
  }

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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
 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;
  }

 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) {
    if (op_version_map_.find(op_type) != op_version_map_.end()) {
      PADDLE_THROW("'%s' is registered in operator version more than once.",
                   op_type);
    }
    op_version_map_.insert({op_type, OpVersion()});
    return op_version_map_[op_type];
  }

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

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

}  // 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)