kernel.h 2.3 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 62 63 64 65 66 67
  core::any_context_t context_;
  mutable operators::param_t param_;
};

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

S
superjomn 已提交
71 72
  // Run the kernel.
  virtual void Run() { CHECK(false) << "Not Implemented"; }
S
superjomn 已提交
73

S
superjomn 已提交
74 75 76
  TargetType target() const override { return Target; }
  PrecisionType precision() const override { return Precision; }

S
superjomn 已提交
77 78
  OpKernel() = default;
  virtual ~OpKernel() = default;
S
superjomn 已提交
79 80 81

 protected:
  std::unique_ptr<KernelContext> ctx_;
S
superjomn 已提交
82 83 84 85
};

}  // namespace lite
}  // namespace paddle