conv_base_helper.h 4.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2022 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>
#include <array>
#include <memory>
#include <string>
#include <vector>
22

23 24 25
#include "paddle/fluid/framework/conv_search_cache.h"
#include "paddle/fluid/operators/conv_cudnn_op_cache.h"
#include "paddle/phi/backends/gpu/gpu_context.h"
26
#include "paddle/phi/kernels/autotune/cache.h"
27 28 29 30

namespace paddle {
namespace operators {

31
using Tensor = phi::DenseTensor;
32 33 34 35 36 37 38 39 40 41
using DataLayout = platform::DataLayout;
using framework::AlgorithmsCache;
using framework::ConvSearchCache;

template <typename T>
using ScalingParamType = typename platform::CudnnDataType<T>::ScalingParamType;

// As the container of searchAlgorithm::Find() result.
template <typename AlgoT>
struct SearchResult {
42
  SearchResult() {}
H
hong 已提交
43

44
  explicit SearchResult(AlgoT a) : algo(a) {}
H
hong 已提交
45 46
  explicit SearchResult(AlgoT a, float t, size_t size)
      : algo(a), time(t), workspace_size(size) {}
47

48 49 50
  AlgoT algo = static_cast<AlgoT>(0);
  float time = -1.f;
  size_t workspace_size = 0;
51
  bool exhaustive_search = false;
52 53
};

54 55 56
template <typename T>
static std::ostream& operator<<(std::ostream& out, const std::vector<T>& v) {
  out << "[";
57 58 59 60 61 62 63 64 65
  bool is_first = true;
  for (auto const& tmp : v) {
    if (is_first) {
      out << tmp;
      is_first = false;
    } else {
      out << ", " << tmp;
    }
  }
66 67 68 69
  out << "]";
  return out;
}

70 71 72 73 74 75 76
// As the container of conv relevant descriptors.
template <typename HandleT, typename DataT>
struct ConvArgsBase {
  HandleT handle;
  platform::TensorDescriptor idesc, odesc;
  platform::FilterDescriptor wdesc;
  platform::ConvolutionDescriptor cdesc;
77
  const phi::DenseTensor *x, *w, *o;
78 79 80 81 82 83 84 85 86
  DataT cudnn_dtype;

  // strides
  std::vector<int> s;
  // paddings
  std::vector<int> p;
  // dilations
  std::vector<int> d;

H
hong 已提交
87 88 89 90 91 92
  // groups
  int group;

  // data foramt
  DataLayout data_layout;

93 94 95
  ConvArgsBase(const phi::DenseTensor* x,
               const phi::DenseTensor* w,
               const phi::DenseTensor* o,
96 97 98
               const std::vector<int> s,
               const std::vector<int> p,
               const std::vector<int> d,
H
hong 已提交
99 100 101 102 103 104 105 106 107 108 109 110
               DataT dtype,
               int g,
               DataLayout layout)
      : x(x),
        w(w),
        o(o),
        s(s),
        p(p),
        d(d),
        cudnn_dtype(dtype),
        group(g),
        data_layout(layout) {}
111 112

  template <typename T>
H
hong 已提交
113
  phi::autotune::ConvCacheKey Convert2ConvCacheKey() const {
114 115 116
    auto x_shape = phi::vectorize(x->dims());
    auto w_shape = phi::vectorize(w->dims());
    VLOG(10) << "[ConvArgs] x_dims=" << x_shape << ", w_dims=" << w_shape
H
hong 已提交
117
             << ", strides=" << s << ", paddings=" << p << ", dilations=" << d
118
             << ", data=" << paddle::experimental::CppTypeToDataType<T>::Type()
H
hong 已提交
119 120 121 122
             << ", group=" << group
             << ", data layout=" << static_cast<int64_t>(data_layout);

    return phi::autotune::ConvCacheKey(
123 124 125 126 127
        x_shape,
        w_shape,
        p,
        s,
        d,
H
hong 已提交
128 129 130
        paddle::experimental::CppTypeToDataType<T>::Type(),
        group,
        static_cast<int64_t>(data_layout));
131
  }
132 133 134
};

static inline void GetNCDHW(const framework::DDim& dims,
135 136 137 138 139 140
                            const DataLayout& layout,
                            int* N,
                            int* C,
                            int* D,
                            int* H,
                            int* W) {
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
  *N = dims[0];
  *C = layout == DataLayout::kNCHW ? dims[1] : dims[dims.size() - 1];
  int i = layout == DataLayout::kNCHW ? 0 : 1;
  if (dims.size() == 5) {
    *D = dims[2 - i];
    *H = dims[3 - i];
    *W = dims[4 - i];
  } else {
    *D = 1;
    *H = dims[2 - i];
    *W = dims[3 - i];
  }
}

}  // namespace operators
}  // namespace paddle