kernel.h 5.9 KB
Newer Older
S
superjomn 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
// 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>
18
#include <memory>
S
superjomn 已提交
19
#include <set>
S
superjomn 已提交
20
#include <sstream>
S
superjomn 已提交
21
#include <string>
S
superjomn 已提交
22
#include <utility>
S
superjomn 已提交
23
#include <vector>
S
superjomn 已提交
24 25
#include "paddle/fluid/lite/core/context.h"
#include "paddle/fluid/lite/core/target_wrapper.h"
S
superjomn 已提交
26
#include "paddle/fluid/lite/core/type_system.h"
S
superjomn 已提交
27
#include "paddle/fluid/lite/core/types.h"
S
superjomn 已提交
28
#include "paddle/fluid/lite/operators/op_params.h"
S
superjomn 已提交
29 30 31 32 33
#include "paddle/fluid/lite/utils/all.h"

namespace paddle {
namespace lite {

S
update  
superjomn 已提交
34 35
// An base with virtual functions to unify all the kernel implementation on
// different targets.
S
superjomn 已提交
36
class KernelBase {
S
superjomn 已提交
37
 public:
S
superjomn 已提交
38 39 40 41 42 43
  // type_infer_handler is used to inference a output type by considering the
  // input types in the type system.
  using type_infer_handler_t = std::function<const Type*(
      const std::map<std::string, const Type*>& input_types,
      const std::string& out_arg)>;

44 45 46 47 48 49
 protected:
  /// Run some initialization before `Run`, it will invoke after `SetParam` and
  /// `SetContext`, that is both the param_ and context_ are valid.
  virtual void PrepareForRun() {}

  /// Run the kernel. Before Run, both the param_ and context_ should be valid.
S
superjomn 已提交
50
  virtual void Run() = 0;
S
superjomn 已提交
51

52 53 54 55 56 57 58 59 60 61
 public:
  void Launch() {
    if (is_first_epoch_) {
      PrepareForRun();
      is_first_epoch_ = false;
    }

    Run();
  }

62
  void SetContext(std::unique_ptr<KernelContext>&& ctx) {
T
tensor-tang 已提交
63
    ctx_ = std::move(ctx);
S
superjomn 已提交
64 65 66 67 68
  }
  template <typename T>
  void SetParam(T param) {
    param_.set<T>(param);
  }
S
Superjomn 已提交
69 70
  template <typename P>
  P& Param() const {
71
    return *param_.get_mutable<P>();
S
superjomn 已提交
72 73
  }

S
superjomn 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88
  // This is used in the kernels that takes 'kAny' places and inference the
  // output place. For `ScaleCompute` and `IoCopyCompute`, their input types are
  // declared as 'kAny' in some Place field, and the output is also `kAny`, but
  // when in real execution, when takes some non-kAny type as input, the
  // output's kAny-fields can be determained. For example, when the
  // `ScaleCompute` takes `TensorFp32NCHWTy` as input, its output should be also
  // `TensorFp32NCHWTy`. This type inference rule is different for each kernel,
  // so we make it a virtual method.
  // One can custom this handler to make a specific type inference rule for a
  // kernel, or leave the default to force the kernel use the system's
  // type-inference rules.
  virtual std::unique_ptr<type_infer_handler_t> GetTypeInferHandler() {
    return nullptr;
  }

S
superjomn 已提交
89 90 91
  void set_op_type(const std::string& type) { op_type_ = type; }
  const std::string& op_type() const { return op_type_; }

S
superjomn 已提交
92 93
  // Get input declaration Type.
  const Type* GetInputDeclType(const std::string& arg_name);
S
superjomn 已提交
94

S
superjomn 已提交
95 96
  // Get output declaration Type.
  const Type* GetOutputDeclType(const std::string& arg_name);
S
superjomn 已提交
97

S
Superjomn 已提交
98
  void set_alias(const std::string& x) { alias_ = x; }
S
superjomn 已提交
99 100
  const std::string& alias() const { return alias_; }

101
  virtual Place place() const = 0;
S
update  
superjomn 已提交
102 103
  virtual TargetType target() const = 0;
  virtual PrecisionType precision() const = 0;
S
superjomn 已提交
104
  virtual DataLayoutType layout() const = 0;
T
tensor-tang 已提交
105
  const KernelContext* context() const { return ctx_.get(); }
S
superjomn 已提交
106 107
  virtual std::string name() const = 0;

S
superjomn 已提交
108 109 110 111
  // Short human-readable document.
  std::string summary() const;
  // Long human-readable document.
  virtual std::string doc() const { return ""; }
S
superjomn 已提交
112 113
  // Generate the key of the parameter type.
  std::string GenParamTypeKey() const;
114

S
Superjomn 已提交
115 116 117 118 119 120
  std::string SerializedKernelType() const {
    return SerializeKernelType(op_type(), alias(), place());
  }

  static std::string SerializeKernelType(const std::string& op_type,
                                         const std::string& alias,
C
Chunwei 已提交
121
                                         const Place& place);
S
Superjomn 已提交
122

S
Superjomn 已提交
123 124
  static void ParseKernelType(const std::string& kernel_type,
                              std::string* op_type, std::string* alias,
C
Chunwei 已提交
125
                              Place* place);
S
Superjomn 已提交
126

S
superjomn 已提交
127
  virtual ~KernelBase() = default;
S
superjomn 已提交
128
  void Torch() {}
S
superjomn 已提交
129

S
update  
superjomn 已提交
130
 protected:
131
  std::unique_ptr<KernelContext> ctx_{nullptr};
S
superjomn 已提交
132
  mutable operators::param_t param_;
S
superjomn 已提交
133
  // The corresponding op type.
S
superjomn 已提交
134
  std::string op_type_{};
S
Superjomn 已提交
135 136
  // The extra identity to help defficiate a specific kernel, op_type_ + alias_
  // is the unique ID for the kernel.
S
superjomn 已提交
137
  std::string alias_{};
138
  bool is_first_epoch_{true};
S
superjomn 已提交
139 140 141 142 143
};

// Light-weight kernel implementation.
// The OpKernel is designed to implement the specific algorithm on a target
// device.
S
Superjomn 已提交
144 145
// TODO(Superjomn) Consider to add a Platform type to differentiate CUDNN,
// MKLDNN, plain CUDA C implementations.
S
superjomn 已提交
146 147
template <TargetType Target, PrecisionType Precision,
          DataLayoutType DataLayout = DataLayoutType::kNCHW>
S
superjomn 已提交
148
class KernelLite : public KernelBase {
S
superjomn 已提交
149
 public:
S
superjomn 已提交
150 151
  // Run the kernel.
  virtual void Run() { CHECK(false) << "Not Implemented"; }
S
superjomn 已提交
152

S
superjomn 已提交
153 154
  TargetType target() const override { return Target; }
  PrecisionType precision() const override { return Precision; }
S
superjomn 已提交
155
  DataLayoutType layout() const override { return DataLayout; }
156
  Place place() const override { return Place{Target, Precision, DataLayout}; }
S
superjomn 已提交
157
  std::string name() const override;
S
superjomn 已提交
158

S
superjomn 已提交
159 160
  void Touch() {}

S
superjomn 已提交
161 162
  KernelLite() = default;
  virtual ~KernelLite() = default;
S
superjomn 已提交
163 164
};

S
superjomn 已提交
165
template <TargetType Target, PrecisionType Precision, DataLayoutType DataLayout>
S
superjomn 已提交
166
std::string KernelLite<Target, Precision, DataLayout>::name() const {
S
superjomn 已提交
167 168 169 170
  return op_type() + ":" + TargetToStr(Target) + "/" +
         PrecisionToStr(Precision) + "/" + DataLayoutToStr(DataLayout);
}

S
superjomn 已提交
171 172
}  // namespace lite
}  // namespace paddle