op_desc.h 6.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
// Copyright (c) 2019 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

/*
 * This file implements a light-weight OpDesc like the framework::OpDesc. We
 * delete the unnecessary methods, and remove the underlying dependencies, such
 * as framework::Operator and boost::varient to make it runnable in mobile.
 */

#include <algorithm>
S
superjomn 已提交
24 25
#include <map>
#include <set>
26 27 28
#include <string>
#include <unordered_map>
#include <vector>
S
superjomn 已提交
29
#include "paddle/fluid/lite/core/framework.pb.h"
Y
Yan Chunwei 已提交
30
#include "paddle/fluid/lite/model_parser/desc_apis.h"
31 32 33 34 35 36
#include "paddle/fluid/lite/utils/all.h"

namespace paddle {
namespace lite {
namespace pb {

37 38
using Attribute =
    variant<int, float, bool, std::vector<std::string>, std::vector<int>>;
39 40 41 42 43 44 45 46
using VariableNameMap = std::map<std::string, std::vector<std::string>>;

/*
 * The lite::OpDesc, an light-weight implementation of wrapper of proto::OpDesc.
 * Unlike the original one in framework::OpDesc, we remove the local members
 * except the desc_, to avoid the inconsistent state, which is normal in the
 * original interface and results in bugs.
 */
Y
Yan Chunwei 已提交
47
class OpDesc : public OpDescAPI {
48 49 50
 public:
  OpDesc() {}

S
superjomn 已提交
51
  explicit OpDesc(const framework::proto::OpDesc &desc) : desc_(desc) {}
52 53 54 55 56 57

  void CopyFrom(const OpDesc &op_desc) { desc_ = op_desc.ReadonlyProto(); }

  framework::proto::OpDesc *Proto() { return &desc_; }
  const framework::proto::OpDesc &ReadonlyProto() const { return desc_; }

Y
Yan Chunwei 已提交
58
  std::string Type() const override { return desc_.type(); }
59

Y
Yan Chunwei 已提交
60
  void SetType(const std::string &type) override { desc_.set_type(type); }
61 62

  // Get the arguments of parameter called `param`
Y
Yan Chunwei 已提交
63
  std::vector<std::string> Input(const std::string &param) const override {
64 65 66
    return GetArguments(desc_.inputs(), param);
  }

Y
Yan Chunwei 已提交
67
  std::vector<std::string> InputArgumentNames() const override {
68 69 70 71
    return GetArgumentNames(desc_.inputs());
  }

  void SetInput(const std::string &param,
Y
Yan Chunwei 已提交
72
                const std::vector<std::string> &args) override {
73 74 75
    SetArgument(desc_.mutable_inputs(), param, args);
  }

Y
Yan Chunwei 已提交
76
  std::vector<std::string> Output(const std::string &param) const override {
77 78 79
    return GetArguments(desc_.outputs(), param);
  }

Y
Yan Chunwei 已提交
80
  std::vector<std::string> OutputArgumentNames() const override {
81 82 83 84
    return GetArgumentNames(desc_.outputs());
  }

  void SetOutput(const std::string &param,
Y
Yan Chunwei 已提交
85
                 const std::vector<std::string> &args) override {
86 87 88
    SetArgument(desc_.mutable_outputs(), param, args);
  }

Y
Yan Chunwei 已提交
89
  bool HasAttr(const std::string &name) const override {
90 91 92 93 94 95 96 97
    const auto &xs = desc_.attrs();
    auto it = std::find_if(xs.begin(), xs.end(),
                           [&](const framework::proto::OpDesc_Attr &x) {
                             return x.name() == name;
                           });
    return it != xs.end();
  }

Y
Yan Chunwei 已提交
98
  AttrType GetAttrType(const std::string &name) const override {
99 100 101 102 103 104
    const auto &xs = desc_.attrs();
    auto it = std::find_if(xs.begin(), xs.end(),
                           [&](const framework::proto::OpDesc_Attr &x) {
                             return x.name() == name;
                           });
    CHECK(it != xs.end());
Y
Yan Chunwei 已提交
105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
#define DEF_ONE(type__)                    \
  case framework::proto::AttrType::type__: \
    return AttrType::type__;

    switch (it->type()) {
      DEF_ONE(INT);
      DEF_ONE(FLOAT);
      DEF_ONE(STRING);
      DEF_ONE(INTS);
      DEF_ONE(FLOATS);
      DEF_ONE(STRINGS);
      DEF_ONE(BOOLEAN);
      DEF_ONE(BOOLEANS);
      DEF_ONE(BLOCK);
      DEF_ONE(LONG);
      DEF_ONE(BLOCKS);
      DEF_ONE(LONGS);
      default:
        LOG(ERROR) << "Unknown attribute type";
        return AttrType::UNK;
    }
#undef DEF_ONE
127 128
  }

Y
Yan Chunwei 已提交
129
  std::vector<std::string> AttrNames() const override {
130 131 132 133 134 135 136 137 138
    std::vector<std::string> res;
    const auto &xs = desc_.attrs();
    std::transform(
        xs.begin(), xs.end(), std::back_inserter(res),
        [](const framework::proto::OpDesc_Attr &x) { return x.name(); });
    return res;
  }

  template <typename T>
Y
Yan Chunwei 已提交
139
  void SetAttr(const std::string &name, const T &v);
140

Y
Yan Chunwei 已提交
141 142
  template <typename T>
  T GetAttr(const std::string &name) const;
143 144 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

 private:
  std::vector<std::string> GetArguments(
      const google::protobuf::RepeatedPtrField<framework::proto::OpDesc_Var>
          &xs,
      const std::string &param) const {
    std::vector<std::string> res;
    auto it = std::find_if(xs.begin(), xs.end(),
                           [&](const framework::proto::OpDesc_Var &it) {
                             return it.parameter() == param;
                           });
    CHECK(it != xs.end());

    const auto &ys = it->arguments();
    std::transform(ys.begin(), ys.end(), std::back_inserter(res),
                   [](const std::string &x) { return x; });
    return res;
  }

  void SetArgument(
      google::protobuf::RepeatedPtrField<framework::proto::OpDesc_Var> *xs,
      const std::string &param, const std::vector<std::string> &args) {
    auto it = std::find_if(xs->begin(), xs->end(),
                           [&](const framework::proto::OpDesc_Var &it) {
                             return it.parameter() == param;
                           });
    if (it == xs->end()) {
      auto *new_arg = xs->Add();
      new_arg->set_parameter(param);
      for (const auto &arg : args) {
        *new_arg->mutable_arguments()->Add() = arg;
      }
    } else {
      it->mutable_arguments()->Clear();
      for (const auto &arg : args) {
        *it->mutable_arguments()->Add() = arg;
      }
    }
  }

  std::vector<std::string> GetArgumentNames(
      const google::protobuf::RepeatedPtrField<framework::proto::OpDesc_Var>
          &xs) const {
    std::vector<std::string> res;
    std::transform(
        xs.begin(), xs.end(), std::back_inserter(res),
        [](const framework::proto::OpDesc_Var &x) { return x.parameter(); });
    return res;
  }

 private:
  framework::proto::OpDesc desc_;
};

S
Superjomn 已提交
197 198 199 200
template <>
void OpDesc::SetAttr<std::string>(const std::string &name,
                                  const std::string &v);

201 202 203 204
template <>
void OpDesc::SetAttr<std::vector<int>>(const std::string &name,
                                       const std::vector<int> &v);

205 206 207
}  // namespace pb
}  // namespace lite
}  // namespace paddle