kernel.h 1.8 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
superjomn 已提交
29
class KernelBase {
S
superjomn 已提交
30
 public:
S
superjomn 已提交
31
  virtual void Run() = 0;
S
superjomn 已提交
32

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

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

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

S
superjomn 已提交
48 49 50 51 52 53 54 55 56 57 58 59
 protected:
  virtual ~KernelBase() = default;
  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 已提交
60 61
  virtual void Run() { CHECK(false) << "Not Implemented"; }

S
superjomn 已提交
62
  OpKernel() = default;
S
superjomn 已提交
63

S
superjomn 已提交
64
  virtual ~OpKernel() = default;
S
superjomn 已提交
65 66 67 68
};

}  // namespace lite
}  // namespace paddle