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

namespace paddle {
namespace lite {

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

39 40
  void SetContext(std::unique_ptr<KernelContext>&& ctx) {
    context_ = std::move(ctx);
S
superjomn 已提交
41
  }
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;
62
  const KernelContext* context() const { return context_.get(); }
S
update  
superjomn 已提交
63

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

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

68 69 70 71 72 73 74
  std::string DebugString() const {
    std::stringstream ss;
    ss << op_type() << ":" << TargetToStr(target()) << "/"
       << PrecisionToStr(precision()) << "/" << DataLayoutToStr(layout());
    return ss.str();
  }

S
update  
superjomn 已提交
75
 protected:
76
  std::unique_ptr<KernelContext> context_;
S
superjomn 已提交
77
  mutable operators::param_t param_;
S
superjomn 已提交
78 79
  // The corresponding op type.
  std::string op_type_;
S
superjomn 已提交
80 81 82 83 84
};

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

S
superjomn 已提交
92 93
  // Run the kernel.
  virtual void Run() { CHECK(false) << "Not Implemented"; }
S
superjomn 已提交
94

S
superjomn 已提交
95 96
  TargetType target() const override { return Target; }
  PrecisionType precision() const override { return Precision; }
S
superjomn 已提交
97
  DataLayoutType layout() const override { return DataLayout; }
98
  Place place() const override { return Place{Target, Precision, DataLayout}; }
S
superjomn 已提交
99 100 101 102
  std::string name() const override {
    return op_type() + ":" + TargetToStr(Target) + "/" +
           PrecisionToStr(Precision) + "/" + DataLayoutToStr(DataLayout);
  }
S
superjomn 已提交
103

S
superjomn 已提交
104 105
  void Touch() {}

S
superjomn 已提交
106 107
  OpKernel() = default;
  virtual ~OpKernel() = default;
S
superjomn 已提交
108 109 110

 protected:
  std::unique_ptr<KernelContext> ctx_;
S
superjomn 已提交
111 112 113 114
};

}  // namespace lite
}  // namespace paddle