helper.h 3.7 KB
Newer Older
L
Liangliang He 已提交
1
// Copyright 2018 Xiaomi, Inc.  All rights reserved.
2
//
L
Liangliang He 已提交
3 4 5
// 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
6
//
L
Liangliang He 已提交
7 8 9 10 11 12 13
//     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.
14 15 16

#ifndef MACE_KERNELS_OPENCL_HELPER_H_
#define MACE_KERNELS_OPENCL_HELPER_H_
L
Liangliang He 已提交
17

W
wuchenghui 已提交
18 19 20
#include <string>
#include <vector>

21
#include "mace/core/future.h"
L
Liangliang He 已提交
22 23
#include "mace/core/runtime/opencl/cl2_header.h"
#include "mace/core/runtime/opencl/opencl_runtime.h"
24
#include "mace/core/types.h"
25
#include "mace/utils/utils.h"
26 27 28 29

namespace mace {
namespace kernels {

30
const float kMaxKernelExeTime = 1000.0;  // microseconds
31

32 33
const int32_t kBaseGPUMemCacheSize = 16384;

34
enum BufferType {
35
  CONV2D_FILTER = 0,
36 37 38 39 40 41
  IN_OUT_CHANNEL = 1,
  ARGUMENT = 2,
  IN_OUT_HEIGHT = 3,
  IN_OUT_WIDTH = 4,
  WINOGRAD_FILTER = 5,
  DW_CONV2D_FILTER = 6,
L
liuqi 已提交
42
  WEIGHT_HEIGHT = 7,
L
liuqi 已提交
43
  WEIGHT_WIDTH = 8,
44 45 46 47
};

void CalImage2DShape(const std::vector<index_t> &shape, /* NHWC */
                     const BufferType type,
W
wuchenghui 已提交
48
                     std::vector<size_t> *image_shape);
49

50
std::vector<index_t> CalWinogradShape(const std::vector<index_t> &shape,
51 52
                                      const BufferType type);

53
std::string DtToCLCMDDt(const DataType dt);
54

55
std::string DtToUpstreamCLCMDDt(const DataType dt);
56

57 58 59
std::string DtToCLDt(const DataType dt);

std::string DtToUpstreamCLDt(const DataType dt);
60

W
wuchenghui 已提交
61
void TuningOrRun3DKernel(const cl::Kernel &kernel,
L
liuqi 已提交
62 63
                         const std::string tuning_key,
                         const uint32_t *gws,
64
                         const std::vector<uint32_t> &lws,
L
liuqi 已提交
65 66
                         StatsFuture *future);

W
wuchenghui 已提交
67
void TuningOrRun2DKernel(const cl::Kernel &kernel,
L
liuqi 已提交
68 69
                         const std::string tuning_key,
                         const uint32_t *gws,
70
                         const std::vector<uint32_t> &lws,
L
liuqi 已提交
71 72
                         StatsFuture *future);

L
Liangliang He 已提交
73 74 75 76 77 78 79 80 81 82 83
inline void SetFuture(StatsFuture *future, const cl::Event &event) {
  if (future != nullptr) {
    future->wait_fn = [event](CallStats *stats) {
      event.wait();
      if (stats != nullptr) {
        OpenCLRuntime::Global()->GetCallStats(event, stats);
      }
    };
  }
}

84 85 86 87 88
inline bool LimitKernelTime() {
  const char *flag = getenv("MACE_LIMIT_OPENCL_KERNEL_TIME");
  return flag != nullptr && strlen(flag) == 1 && flag[0] == '1';
}

L
liuqi 已提交
89 90 91 92 93 94 95
template <typename T>
bool IsVecEqual(const std::vector<T> &input0,
                const std::vector<T> &input1) {
  return ((input0.size() == input1.size()) &&
      (std::equal(input0.begin(), input0.end(), input1.begin())));
}

96
template <typename T>
L
Liangliang He 已提交
97
void AppendToStream(std::stringstream *ss, const std::string &delimiter, T v) {
L
liuqi 已提交
98
  (*ss) << v;
L
Liangliang He 已提交
99 100
}

101
template <typename T, typename... Args>
L
Liangliang He 已提交
102 103 104 105
void AppendToStream(std::stringstream *ss,
                    const std::string &delimiter,
                    T first,
                    Args... args) {
L
liuqi 已提交
106 107
  (*ss) << first << delimiter;
  AppendToStream(ss, delimiter, args...);
L
Liangliang He 已提交
108 109
}

110
template <typename... Args>
L
Liangliang He 已提交
111 112 113 114 115 116
std::string Concat(Args... args) {
  std::stringstream ss;
  AppendToStream(&ss, "_", args...);
  return ss.str();
}

117 118 119 120 121
std::vector<uint32_t> Default2DLocalWS(const uint32_t *gws,
                                       const uint32_t kwg_size);
std::vector<uint32_t> Default3DLocalWS(const uint32_t *gws,
                                       const uint32_t kwg_size);

122
}  // namespace kernels
L
Liangliang He 已提交
123 124
}  // namespace mace
#endif  // MACE_KERNELS_OPENCL_HELPER_H_