operator_kernel_configs.h 4.9 KB
Newer Older
X
polish  
Xin Pan 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/* Copyright (c) 2016 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 <algorithm>
18
#include <mutex>
X
polish  
Xin Pan 已提交
19 20
#include <unordered_map>
#include <vector>
21

22
#include "glog/logging.h"
X
polish  
Xin Pan 已提交
23 24 25 26

namespace paddle {
namespace framework {

X
xiaoxiaohehe001 已提交
27 28 29 30 31 32 33 34 35 36
template <typename AlgoT>
struct SearchFuseResult {
  SearchFuseResult() {}
  explicit SearchFuseResult(AlgoT a) : algo(a) {}

  AlgoT algo = static_cast<AlgoT>(0);
  float time = -1.f;
  size_t workspace_size = 0;
};

37
// thread-safe.
X
polish  
Xin Pan 已提交
38 39 40 41 42 43
template <typename TAlgorithm>
class AlgorithmsCache {
 public:
  AlgorithmsCache() : search_times_(0) { hash_.clear(); }
  // Caches the best algorithm for a given
  // combination of tensor dimensions & compute data type.
44 45 46 47 48
  // cudnn_dtype set for different data type
  TAlgorithm GetAlgorithm(const std::vector<int64_t>& dims1,
                          const std::vector<int64_t>& dims2,
                          const std::vector<int>& strides,
                          const std::vector<int>& paddings,
49 50
                          const std::vector<int>& dilations,
                          int algorithmFlags,
51 52
                          int64_t cudnn_dtype,
                          std::function<TAlgorithm()> gen_func);
X
polish  
Xin Pan 已提交
53

54 55 56
  TAlgorithm GetAlgorithm(int64_t area,
                          int search_times,
                          int algorithmFlags,
X
polish  
Xin Pan 已提交
57 58 59 60 61
                          std::function<TAlgorithm()> gen_func);

 private:
  std::unordered_map<int64_t, TAlgorithm> hash_;
  int search_times_;
62
  std::mutex cache_mutex;
X
polish  
Xin Pan 已提交
63 64 65 66
};

template <typename TAlgorithm>
TAlgorithm framework::AlgorithmsCache<TAlgorithm>::GetAlgorithm(
67 68 69 70 71 72 73
    const std::vector<int64_t>& dims1,
    const std::vector<int64_t>& dims2,
    const std::vector<int>& strides,
    const std::vector<int>& paddings,
    const std::vector<int>& dilations,
    int algorithmFlags,
    int64_t cudnn_dtype,
X
polish  
Xin Pan 已提交
74 75 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
    std::function<TAlgorithm()> gen_func) {
  int64_t seed = 0;
  // Hash all of the inputs, use to try and look up a previously
  // discovered algorithm, or fall back to generating a new one.
  std::hash<int64_t> hashFn;
  // do hash like boost
  // https://stackoverflow.com/questions/2590677/how-do-i-combine-hash-values-in-c0x
  for (const auto num : dims1) {
    seed ^= hashFn(num) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
  }

  for (const auto num : dims2) {
    seed ^= hashFn(num) + 0x9e3779b9 + (seed << 6) + (seed >> 2) + 1;
  }

  for (const auto num : strides) {
    seed ^= hashFn(static_cast<int64_t>(num)) + 0x9e3779b9 + (seed << 6) +
            (seed >> 2) + 2;
  }

  for (const auto num : paddings) {
    seed ^= hashFn(static_cast<int64_t>(num)) + 0x9e3779b9 + (seed << 6) +
            (seed >> 2) + 3;
  }

  for (const auto num : dilations) {
    seed ^= hashFn(static_cast<int64_t>(num)) + 0x9e3779b9 + (seed << 6) +
            (seed >> 2) + 4;
  }

  seed ^= hashFn(static_cast<int64_t>(algorithmFlags)) + 0x9e3779b9 +
          (seed << 6) + (seed >> 2) + 5;

107 108 109
  seed ^= hashFn(static_cast<int64_t>(cudnn_dtype)) + 0x9e3779b9 + (seed << 6) +
          (seed >> 2) + 6;

110 111
  VLOG(10) << "seed:" << seed << ", hash_.size:" << hash_.size();

X
polish  
Xin Pan 已提交
112 113
  if (seed == 0) return gen_func();

114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
  TAlgorithm ret;
  auto it = hash_.end();
  bool have_found = false;
  {
    std::lock_guard<std::mutex> lock(cache_mutex);
    it = hash_.find(seed);

    if (it != hash_.end()) {
      ret = it->second;
      have_found = true;
    }
  }

  if (!have_found) {
    ret = gen_func();
    std::lock_guard<std::mutex> lock(cache_mutex);
    hash_[seed] = ret;
X
polish  
Xin Pan 已提交
131
  }
132 133

  return ret;
X
polish  
Xin Pan 已提交
134 135 136 137
}

template <typename TAlgorithm>
TAlgorithm AlgorithmsCache<TAlgorithm>::GetAlgorithm(
138 139 140
    int64_t area,
    int search_times,
    int algorithmFlags,
X
polish  
Xin Pan 已提交
141
    std::function<TAlgorithm()> gen_func) {
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  auto it = hash_.end();
  {
    std::lock_guard<std::mutex> lock(cache_mutex);
    it = hash_.find(area);

    if (it != hash_.end()) {
      return it->second;
    }
  }

  bool gene_flag = false;

  {
    std::lock_guard<std::mutex> lock(cache_mutex);
    gene_flag = (search_times_ < search_times);
X
polish  
Xin Pan 已提交
157
  }
158 159 160 161 162

  TAlgorithm algo{};
  if (gene_flag) {
    algo = gen_func();
    std::lock_guard<std::mutex> lock(cache_mutex);
X
polish  
Xin Pan 已提交
163 164 165 166
    hash_[area] = algo;
    ++search_times_;
    return algo;
  }
167

X
polish  
Xin Pan 已提交
168
  int64_t min = static_cast<uint64_t>(INT_MAX);
169 170 171 172 173 174 175
  {
    std::lock_guard<std::mutex> lock(cache_mutex);
    for (const auto& m : hash_) {
      if (m.first < min) {
        min = m.first;
        algo = m.second;
      }
X
polish  
Xin Pan 已提交
176 177 178 179 180 181 182
    }
  }
  return algo;
}

}  // namespace framework
}  // namespace paddle