kernel.h 3.1 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>
S
superjomn 已提交
18
#include <set>
S
superjomn 已提交
19
#include <string>
S
superjomn 已提交
20
#include <vector>
S
superjomn 已提交
21
#include "paddle/fluid/framework/op_desc.h"
S
superjomn 已提交
22 23
#include "paddle/fluid/lite/core/context.h"
#include "paddle/fluid/lite/core/target_wrapper.h"
S
superjomn 已提交
24
#include "paddle/fluid/lite/core/type_system.h"
S
superjomn 已提交
25
#include "paddle/fluid/lite/core/types.h"
S
superjomn 已提交
26
#include "paddle/fluid/lite/operators/op_params.h"
S
superjomn 已提交
27 28 29 30 31
#include "paddle/fluid/lite/utils/all.h"

namespace paddle {
namespace lite {

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

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

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

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

S
superjomn 已提交
53 54 55
  void set_op_type(const std::string& type) { op_type_ = type; }
  const std::string& op_type() const { return op_type_; }

S
superjomn 已提交
56 57
  void Torch() {}

58
  virtual Place place() const = 0;
S
update  
superjomn 已提交
59 60
  virtual TargetType target() const = 0;
  virtual PrecisionType precision() const = 0;
S
superjomn 已提交
61
  virtual DataLayoutType layout() const = 0;
S
update  
superjomn 已提交
62

S
superjomn 已提交
63 64
  virtual std::string name() const = 0;

S
superjomn 已提交
65
  virtual ~KernelBase() = default;
S
update  
superjomn 已提交
66 67

 protected:
S
superjomn 已提交
68 69
  core::any_context_t context_;
  mutable operators::param_t param_;
S
superjomn 已提交
70 71
  // The corresponding op type.
  std::string op_type_;
S
superjomn 已提交
72 73 74 75 76
};

// Light-weight kernel implementation.
// The OpKernel is designed to implement the specific algorithm on a target
// device.
S
superjomn 已提交
77 78
template <TargetType Target, PrecisionType Precision,
          DataLayoutType DataLayout = DataLayoutType::kNCHW>
S
superjomn 已提交
79 80
class OpKernel : public KernelBase {
 public:
S
superjomn 已提交
81 82
  // Set runtime context.
  void SetContext(std::unique_ptr<KernelContext>&& ctx) { ctx_ = ctx; }
S
superjomn 已提交
83

S
superjomn 已提交
84 85
  // Run the kernel.
  virtual void Run() { CHECK(false) << "Not Implemented"; }
S
superjomn 已提交
86

S
superjomn 已提交
87 88
  TargetType target() const override { return Target; }
  PrecisionType precision() const override { return Precision; }
S
superjomn 已提交
89
  DataLayoutType layout() const override { return DataLayout; }
90
  Place place() const override { return Place{Target, Precision, DataLayout}; }
S
superjomn 已提交
91 92 93 94
  std::string name() const override {
    return op_type() + ":" + TargetToStr(Target) + "/" +
           PrecisionToStr(Precision) + "/" + DataLayoutToStr(DataLayout);
  }
S
superjomn 已提交
95

S
superjomn 已提交
96 97
  void Touch() {}

S
superjomn 已提交
98 99
  OpKernel() = default;
  virtual ~OpKernel() = default;
S
superjomn 已提交
100 101 102

 protected:
  std::unique_ptr<KernelContext> ctx_;
S
superjomn 已提交
103 104 105 106
};

}  // namespace lite
}  // namespace paddle