kernel.h 6.2 KB
Newer Older
S
superjomn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
// 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

#include <map>
#include <string>
S
superjomn 已提交
19
#include "paddle/fluid/framework/op_desc.h"
S
superjomn 已提交
20 21
#include "paddle/fluid/lite/core/context.h"
#include "paddle/fluid/lite/core/target_wrapper.h"
S
superjomn 已提交
22
#include "paddle/fluid/lite/core/type_system.h"
S
superjomn 已提交
23
#include "paddle/fluid/lite/core/types.h"
S
superjomn 已提交
24
#include "paddle/fluid/lite/operators/op_params.h"
S
superjomn 已提交
25 26 27 28 29
#include "paddle/fluid/lite/utils/all.h"

namespace paddle {
namespace lite {

S
update  
superjomn 已提交
30 31
// An base with virtual functions to unify all the kernel implementation on
// different targets.
S
superjomn 已提交
32
class KernelBase {
S
superjomn 已提交
33
 public:
S
superjomn 已提交
34
  virtual void Run() = 0;
S
superjomn 已提交
35

S
superjomn 已提交
36 37 38 39
  template <TargetType Target>
  void SetContext(std::unique_ptr<Context<Target>>&& ctx) {
    context_.set<std::unique_ptr<Context<Target>>>(std::move(ctx));
  }
S
superjomn 已提交
40

S
superjomn 已提交
41 42 43 44
  template <typename T>
  void SetParam(T param) {
    param_.set<T>(param);
  }
S
superjomn 已提交
45 46 47

  template <typename Param>
  Param& param() const {
S
superjomn 已提交
48
    return param_.get<Param>();
S
superjomn 已提交
49 50
  }

S
superjomn 已提交
51 52
  void Torch() {}

S
update  
superjomn 已提交
53 54
  virtual TargetType target() const = 0;
  virtual PrecisionType precision() const = 0;
S
superjomn 已提交
55
  virtual DataLayoutType layout() const = 0;
S
update  
superjomn 已提交
56

S
superjomn 已提交
57
  virtual ~KernelBase() = default;
S
update  
superjomn 已提交
58 59

 protected:
S
superjomn 已提交
60 61 62 63
  core::any_context_t context_;
  mutable operators::param_t param_;
};

S
superjomn 已提交
64 65 66 67 68 69 70
/*
 * ParamType is used to represent a data type of a parameter for the kernel. It
 * can represent any Variable data type.
 * The element_type_hash is the hash code of the element, it should be
 * registered in the `TypeSystem`.
 */
struct ParamType {
S
superjomn 已提交
71
  // For unsupported types.
S
superjomn 已提交
72 73
  size_t element_type_hash{};
  Place tensor_place{};
S
superjomn 已提交
74
  const Type* type_;
S
superjomn 已提交
75 76 77 78 79

  ParamType() = default;
  ParamType(size_t element_type_hash) : element_type_hash(element_type_hash) {}
  ParamType(size_t element_type_hash, const Place& place)
      : element_type_hash(element_type_hash), tensor_place(place) {}
S
superjomn 已提交
80
  ParamType(const Type* type) : type_(type) {}
S
superjomn 已提交
81 82 83
};

/*
S
superjomn 已提交
84 85
 * The data types of kernel parameters. It is used to track the type of kernel's
 * inputs and outputs.
S
superjomn 已提交
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
 */
struct ParamTypes {
  std::vector<std::vector<ParamType>> inputs;
  std::vector<std::vector<ParamType>> outputs;

  void RegisterInputType(int offset, const ParamType& type) {
    Register(&inputs, offset, type);
  }

  void RegisterOutputType(int offset, const ParamType& type) {
    Register(&outputs, offset, type);
  }

 private:
  void Register(std::vector<std::vector<ParamType>>* ts, int offset,
                ParamType type) {
    CHECK_GE(offset, 0) << "invalid offset";
    CHECK_GE(offset, 50) << "invalid offset";
    for (size_t i = 0; i < offset - inputs.size() + 1; i++) {
      ts->emplace_back();
    }
    ts->at(offset).emplace_back(type);
  }
};

/*
 * The ParamTypeRegistry help register the input and output data types for all
 * the kernels. It is made singleton so that all the objects of the same kernel
 * can share the same information.
 *
 * Usage:
 * for register a kernel for FC operator.
 * ParamTypeRegistry::Global().Register(
 *        "fc", {TARGET(kCUDA), PRECISION(kFloat)}, 0,
 *        {typeid(Tensor), {TARGET(kCUDA)}});
 */
class ParamTypeRegistry {
 public:
S
superjomn 已提交
124 125
  enum class IO : int { kInput = 0, kOutput };

S
superjomn 已提交
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
  template <TargetType target, PrecisionType precision,
            DataLayoutType layout = DataLayoutType::kNCHW>
  /*
   * Helper class for registering a ParamType for a Kernel.
   * Usage:
   *
   * NewInstance<TARGET(kHost), PRECISION(kFloat)>("fc")
   *   .BindInput(0, {typeid(Tensor).hash_code(), {TARGET(kHost)})
   *   .BindInput(1, {typeid(Tensor).hash_code(), {TARGET(kHost),
   *                                               PRECISION(kFloat)});
   */
  struct NewInstance {
    NewInstance(const std::string& kernel_type) : kernel_type_(kernel_type) {}

    NewInstance& BindInput(int offset, const ParamType& ptype) {
S
superjomn 已提交
141 142 143 144 145 146
      ParamTypeRegistry::Global().Register<IO::kInput>(
          kernel_type_, Place{target, precision, layout}, offset, ptype);
      return *this;
    }
    NewInstance& BindOutput(int offset, const ParamType& ptype) {
      ParamTypeRegistry::Global().Register<IO::kOutput>(
S
superjomn 已提交
147 148 149 150 151 152 153 154 155 156
          kernel_type_, Place{target, precision, layout}, offset, ptype);
      return *this;
    }

    bool Finalize() { return true; }

   private:
    std::string kernel_type_;
  };

S
superjomn 已提交
157
  template <IO io>
S
superjomn 已提交
158
  void Register(const std::string& kernel_type, const Place& place, int offset,
S
superjomn 已提交
159 160 161 162
                ParamType data_type) {
    KernelIdTy key{kernel_type, place, io, offset};
    types_[key] = data_type;
  }
S
superjomn 已提交
163 164 165 166 167 168 169 170 171 172 173 174 175

  ParamType Retrive(const Place& place, int offset);

  static ParamTypeRegistry& Global() {
    static ParamTypeRegistry x;
    return x;
  }

 private:
  ParamTypeRegistry() = default;

 public:
  // Identification for a Kernel.
S
superjomn 已提交
176
  struct KernelIdTy {
S
superjomn 已提交
177 178 179 180 181 182
    std::string kernel_type;
    Place place;
    IO io;
    int offset;
  };

S
superjomn 已提交
183
  using key_t = KernelIdTy;
S
superjomn 已提交
184 185 186 187 188 189 190 191
  struct KeyCmp {
    bool operator()(const key_t& a, const key_t& b) const;
  };

 private:
  std::map<key_t, ParamType, ParamTypeRegistry::KeyCmp> types_;
};

S
superjomn 已提交
192 193 194
// Light-weight kernel implementation.
// The OpKernel is designed to implement the specific algorithm on a target
// device.
S
superjomn 已提交
195 196
template <TargetType Target, PrecisionType Precision,
          DataLayoutType DataLayout = DataLayoutType::kNCHW>
S
superjomn 已提交
197 198
class OpKernel : public KernelBase {
 public:
S
superjomn 已提交
199 200
  // Set runtime context.
  void SetContext(std::unique_ptr<KernelContext>&& ctx) { ctx_ = ctx; }
S
superjomn 已提交
201

S
superjomn 已提交
202 203
  // Run the kernel.
  virtual void Run() { CHECK(false) << "Not Implemented"; }
S
superjomn 已提交
204

S
superjomn 已提交
205 206
  TargetType target() const override { return Target; }
  PrecisionType precision() const override { return Precision; }
S
superjomn 已提交
207
  DataLayoutType layout() const override { return DataLayout; }
S
superjomn 已提交
208

S
superjomn 已提交
209 210
  void Touch() {}

S
superjomn 已提交
211 212
  OpKernel() = default;
  virtual ~OpKernel() = default;
S
superjomn 已提交
213 214 215

 protected:
  std::unique_ptr<KernelContext> ctx_;
S
superjomn 已提交
216 217 218 219
};

}  // namespace lite
}  // namespace paddle