kernel_pool.h 4.4 KB
Newer Older
T
tensor-tang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/* Copyright (c) 2018 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 <memory>  // for shared_ptr
#include <string>
#include <unordered_map>
#include <vector>
T
tensor-tang 已提交
21 22 23
#include "paddle/fluid/operators/jit/gen_base.h"
#include "paddle/fluid/operators/jit/kernel_base.h"
#include "paddle/fluid/operators/jit/kernel_key.h"
T
tensor-tang 已提交
24
#include "paddle/fluid/platform/place.h"
T
tensor-tang 已提交
25 26 27

namespace paddle {
namespace operators {
T
tensor-tang 已提交
28
namespace jit {
T
tensor-tang 已提交
29 30 31

template <KernelType KT>
class JitCodePool {
T
tensor-tang 已提交
32 33
  typedef std::unique_ptr<GenBase> GenBasePtr;
  typedef std::unordered_map<size_t, GenBasePtr> JitCodeMap;
T
tensor-tang 已提交
34

T
tensor-tang 已提交
35
 public:
T
tensor-tang 已提交
36
  JitCodePool() = default;
T
tensor-tang 已提交
37 38 39 40 41
  static JitCodePool& Instance() {
    static thread_local JitCodePool<KT> g_jit_codes;
    return g_jit_codes;
  }

T
tensor-tang 已提交
42
  const JitCodeMap& AllKernels() { return codes_; }
T
tensor-tang 已提交
43 44

  bool Has(size_t key) const { return codes_.find(key) != codes_.end(); }
T
tensor-tang 已提交
45

T
tensor-tang 已提交
46
  void Insert(size_t key, GenBasePtr value) {
T
tensor-tang 已提交
47
    codes_.emplace(key, std::move(value));
T
tensor-tang 已提交
48 49 50
  }

 private:
T
tensor-tang 已提交
51
  JitCodeMap codes_;
T
tensor-tang 已提交
52 53 54
  DISABLE_COPY_AND_ASSIGN(JitCodePool);
};

T
tensor-tang 已提交
55 56 57 58
typedef std::unique_ptr<const Kernel> KernelPtr;
typedef std::unordered_map<KernelKey, std::vector<KernelPtr>, KernelKey::Hash>
    KernelMap;

T
tensor-tang 已提交
59 60 61
class KernelPool {
 public:
  static KernelPool& Instance();
T
tensor-tang 已提交
62
  KernelPool() = default;
T
tensor-tang 已提交
63 64 65 66 67 68 69 70 71 72 73 74 75
  KernelMap& AllKernels() { return pool_; }
  void Insert(const KernelKey& key, KernelPtr value) {
    if (pool_.find(key) == pool_.end()) {
      pool_.emplace(key, std::vector<KernelPtr>());
    }
    pool_.at(key).emplace_back(std::move(value));
  }

 private:
  KernelMap pool_;
  DISABLE_COPY_AND_ASSIGN(KernelPool);
};

T
tensor-tang 已提交
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
// Every kernel should have refer code and it should be used in unit tests,
// so refer kernels should have it's independent kernel pool
class ReferKernelPool {
 public:
  static ReferKernelPool& Instance();
  ReferKernelPool() = default;
  KernelMap& AllKernels() { return pool_; }
  void Insert(const KernelKey& key, KernelPtr value) {
    if (pool_.find(key) == pool_.end()) {
      pool_.emplace(key, std::vector<KernelPtr>());
    }
    pool_.at(key).emplace_back(std::move(value));
  }

 private:
  KernelMap pool_;
  DISABLE_COPY_AND_ASSIGN(ReferKernelPool);
};

// Refer code do not related with attr, and always on CPUPlace
template <KernelType KT, typename T, typename Func, typename Attr>
inline Func GetRefer() {
  auto& ref_pool = ReferKernelPool().Instance().AllKernels();
  KernelKey kkey(KT, platform::CPUPlace());
  auto ref_iter = ref_pool.find(kkey);
  PADDLE_ENFORCE(ref_iter != ref_pool.end(),
                 "Every Kernel should have reference function.");
  auto& ref_impls = ref_iter->second;
  for (auto& impl : ref_impls) {
    auto i = dynamic_cast<const ReferKernel<T, Func, Attr>*>(impl.get());
    if (i) {
      return i->GetFunc();
    }
  }
  return nullptr;
}
T
tensor-tang 已提交
112 113 114

template <KernelType KT, typename T, typename Func, typename Attr,
          typename PlaceType = platform::CPUPlace>
T
tensor-tang 已提交
115 116 117 118 119 120 121
const Func Get(Attr attr) {
  size_t key = GetKey<Attr>(attr);
  auto& codes = JitCodePool<KT>().Instance();
  if (codes.Has(key)) {
    return codes.AllKernels().at(key)->template getCode<Func>();
  }

T
tensor-tang 已提交
122
  if (std::is_same<PlaceType, platform::CPUPlace>::value) {
T
tensor-tang 已提交
123 124 125 126 127 128
    auto p = CreateJitCode<KT, T, Attr>(attr);
    if (p) {
      auto f = p->template getCode<Func>();
      codes.Insert(key, std::move(p));
      return f;
    }
T
tensor-tang 已提交
129 130
  }

T
tensor-tang 已提交
131
  // pool: (KernelKey(type, place), vector<Kernel>)
T
tensor-tang 已提交
132 133 134 135
  auto& pool = KernelPool().Instance().AllKernels();
  KernelKey kkey(KT, PlaceType());
  auto iter = pool.find(kkey);
  if (iter != pool.end()) {
T
tensor-tang 已提交
136 137 138
    auto& impls = iter->second;
    for (auto& impl : impls) {
      auto i = dynamic_cast<const KernelImpl<T, Func, Attr>*>(impl.get());
T
tensor-tang 已提交
139 140 141 142 143 144
      if (i && i->UseMe(attr)) {
        return i->GetFunc();
      }
    }
  }

T
tensor-tang 已提交
145 146
  // The last implementation should be reference function on CPUPlace.
  return GetRefer<KT, T, Func, Attr>();
T
tensor-tang 已提交
147 148
}

T
tensor-tang 已提交
149
}  // namespace jit
T
tensor-tang 已提交
150 151
}  // namespace operators
}  // namespace paddle