kernel.h 5.6 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 22
#include "paddle/fluid/lite/core/context.h"
#include "paddle/fluid/lite/core/target_wrapper.h"
#include "paddle/fluid/lite/core/types.h"
S
superjomn 已提交
23
#include "paddle/fluid/lite/operators/op_params.h"
S
superjomn 已提交
24 25 26 27 28
#include "paddle/fluid/lite/utils/all.h"

namespace paddle {
namespace lite {

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

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

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

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

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

S
update  
superjomn 已提交
52 53 54
  virtual TargetType target() const = 0;
  virtual PrecisionType precision() const = 0;

S
superjomn 已提交
55
  virtual ~KernelBase() = default;
S
update  
superjomn 已提交
56 57

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

S
superjomn 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 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 140 141 142 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
/*
 * 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 {
  size_t element_type_hash{};
  Place tensor_place{};

  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) {}
};

/*
 * The data types of kernel parameters.
 */
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:
  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) {
      ParamTypeRegistry::Global().Register(
          kernel_type_, Place{target, precision, layout}, offset, ptype);
      return *this;
    }

    bool Finalize() { return true; }

   private:
    std::string kernel_type_;
  };

  void Register(const std::string& kernel_type, const Place& place, int offset,
                ParamType data_type) {}

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

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

 private:
  ParamTypeRegistry() = default;

 public:
  enum class IO : int { kInput = 0, kOutput };
  // Identification for a Kernel.
  struct KernelIdT {
    std::string kernel_type;
    Place place;
    IO io;
    int offset;
  };

  using key_t = KernelIdT;
  struct KeyCmp {
    bool operator()(const key_t& a, const key_t& b) const;
  };

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

S
superjomn 已提交
176 177 178
// Light-weight kernel implementation.
// The OpKernel is designed to implement the specific algorithm on a target
// device.
S
superjomn 已提交
179 180
template <TargetType Target, PrecisionType Precision,
          DataLayoutType DataLayout = DataLayoutType::kNCHW>
S
superjomn 已提交
181 182
class OpKernel : public KernelBase {
 public:
S
superjomn 已提交
183 184
  // Set runtime context.
  void SetContext(std::unique_ptr<KernelContext>&& ctx) { ctx_ = ctx; }
S
superjomn 已提交
185

S
superjomn 已提交
186 187
  // Run the kernel.
  virtual void Run() { CHECK(false) << "Not Implemented"; }
S
superjomn 已提交
188

S
superjomn 已提交
189 190 191
  TargetType target() const override { return Target; }
  PrecisionType precision() const override { return Precision; }

S
superjomn 已提交
192 193
  void Touch() {}

S
superjomn 已提交
194 195
  OpKernel() = default;
  virtual ~OpKernel() = default;
S
superjomn 已提交
196 197 198

 protected:
  std::unique_ptr<KernelContext> ctx_;
S
superjomn 已提交
199 200 201 202
};

}  // namespace lite
}  // namespace paddle