transpose_functor.h 5.9 KB
Newer Older
1
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
X
xzl 已提交
2

L
Luo Tao 已提交
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
X
xzl 已提交
6

L
Luo Tao 已提交
7
    http://www.apache.org/licenses/LICENSE-2.0
X
xzl 已提交
8

L
Luo Tao 已提交
9 10 11 12 13
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. */
X
xzl 已提交
14 15 16

#pragma once

17
#include <vector>
18

19
#include "paddle/phi/core/tensor_utils.h"
20
#include "paddle/phi/kernels/funcs/aligned_vector.h"
21
#include "paddle/phi/kernels/funcs/math_function.h"
X
xzl 已提交
22

23 24
namespace phi {
namespace funcs {
X
xzl 已提交
25

26 27
enum { kTransposeMKLDNNFP32 = 1, kTransposeMKLDNNINT8 = 2 };

28 29 30 31
enum PermuteType {
  kCopy = 1,
  kTranspose = 2,
  kVecPermute = 3,
L
limingshu 已提交
32
  kGeneralPermute = 4
33 34 35 36 37 38 39
};

constexpr int kBlockRows = 16;
constexpr int kTileSize = 32;

// Simplify the input dims and permute dims if possible.
template <typename T>
L
limingshu 已提交
40
class TranposeTypeClassifier {
41
 public:
L
limingshu 已提交
42 43 44 45 46 47 48 49
  TranposeTypeClassifier(const int sm_count,
                         const size_t rank,
                         const int64_t numel,
                         const std::vector<int32_t>& perm,
                         const std::vector<int64_t>& dims,
                         const T* src,
                         T* dst)
      : perm_(rank), src_dims(rank) {
50 51 52
    SimplifyPermAndDims(rank, dims, perm);
    if (rank_ > 1) {
      vec_size_ = GetPermVecSize(sm_count, src, dst);
L
limingshu 已提交
53 54 55 56 57 58 59
    }
    perm_.resize(rank_);
    src_dims.resize(rank_);
    dst_dims.resize(rank_);

    for (auto i = 0; i < rank_; ++i) {
      dst_dims[i] = src_dims[perm_[i]];
60 61 62
    }
  }

L
limingshu 已提交
63
  int GetRank() const { return rank_; }
64 65 66 67
  int GetVecSize() const { return vec_size_; }
  PermuteType GetPermType() const { return type_; }

  std::vector<int> GetPerm() const { return perm_; }
L
limingshu 已提交
68 69
  std::vector<int64_t> GetSrcDims() const { return src_dims; }
  std::vector<int64_t> GetDstDims() const { return dst_dims; }
70 71

 private:
L
limingshu 已提交
72
  int rank_{1};
73 74
  int vec_size_{1};
  std::vector<int> perm_;
L
limingshu 已提交
75 76
  std::vector<int64_t> src_dims;
  std::vector<int64_t> dst_dims;
77 78 79
  PermuteType type_{kCopy};

  void SimplifyPermAndDims(const size_t rank,
L
limingshu 已提交
80
                           const std::vector<int64_t>& in_dims,
81
                           const std::vector<int32_t>& perm) {
L
limingshu 已提交
82
    int64_t combined_dims[phi::DDim::kMaxRank];
83 84
    int valid_map[phi::DDim::kMaxRank];

L
limingshu 已提交
85 86
    // Merge consecutive dims to the fist one dim and
    // leave original dim to be 1. Example below :
87 88
    // perm: [2, 3, 0, 1], origin_dims : [4, 8, 2, 5]
    // new_dims: [4, 8, 2, 5] -> [32, 1, 10, 1]
L
limingshu 已提交
89
    int start_perm_idx = 0;
90
    while (start_perm_idx < rank) {
L
limingshu 已提交
91
      const int start_dim_idx = perm[start_perm_idx];
92
      combined_dims[start_dim_idx] = in_dims[start_dim_idx];
L
limingshu 已提交
93
      int end_perm_idx = start_perm_idx + 1;
94 95 96

      while (end_perm_idx < rank &&
             perm[end_perm_idx] == perm[end_perm_idx - 1] + 1) {
L
limingshu 已提交
97
        const int end_dim_idx = perm[end_perm_idx];
98 99 100 101 102 103 104 105 106 107 108
        combined_dims[start_dim_idx] *= in_dims[end_dim_idx];
        combined_dims[end_dim_idx] = 1;
        end_perm_idx += 1;
      }
      start_perm_idx = end_perm_idx;
    }

    // Reorder combined dims and marked useless dim as -1.
    // for example, if combined dims is [32, 1, 10, 1],
    // valid_map is [0, -1, 1, -1] and generate simplified
    // dims as [32, 10]
L
limingshu 已提交
109
    int valid_dim_idx = 0;
110
    bool sequential_flag = false;
L
limingshu 已提交
111
    for (auto i = 0; i < rank; ++i) {
112 113 114 115 116 117
      const int src_dim = combined_dims[i];
      if (src_dim == 1) {
        valid_map[i] = -1;
      } else {
        sequential_flag = true;
        valid_map[i] = valid_dim_idx;
L
limingshu 已提交
118
        src_dims[valid_dim_idx] = src_dim;
119 120 121 122 123
        valid_dim_idx += 1;
      }
    }

    if (valid_dim_idx == 0) {
L
limingshu 已提交
124
      src_dims[0] = 1;
125 126 127 128 129 130 131 132
      perm_[0] = 0;
      return;
    } else if (valid_dim_idx == 1) {
      type_ = PermuteType::kCopy;
    }

    // Acquire simplified perm with help of combined dims
    // and original perm, finally simplified perm is [1, 0]
L
limingshu 已提交
133 134
    int perm_idx = 0;
    for (auto i = 0; i < rank; ++i) {
135 136 137 138 139 140 141 142 143 144 145 146
      const int mapped = valid_map[perm[i]];
      if (mapped >= 0) {
        perm_[perm_idx] = mapped;
        perm_idx += 1;
      }
    }
    rank_ = valid_dim_idx;
  }

  int GetPermVecSize(const int sm_count, const T* src, T* dst) {
    // For gerneal_permute kernel, there is good chance for
    // vectorized write.
L
limingshu 已提交
147
    type_ = PermuteType::kGeneralPermute;
148 149 150 151 152 153
    int vec_size = phi::GetVectorizedSize<T>(dst);

    // While the last dim is fixed, there is good chance for
    // both vectorized read and write.
    if (perm_[rank_ - 1] == rank_ - 1) {
      int tmp_size = std::min(vec_size, phi::GetVectorizedSize<T>(src));
L
limingshu 已提交
154
      tmp_size = GetDimVesSize(tmp_size, src_dims[rank_ - 1]);
155 156 157 158 159 160 161 162 163 164 165
      if (tmp_size > 1) {
        type_ = kVecPermute;
        vec_size = tmp_size;
      }
    }

    // Once only transpose at the last 2 dims, there is good
    // chance for vectorized read.
    if ((rank_ == 2 && perm_[1] == 0 && perm_[0] == 1) ||
        (rank_ == 3 && perm_[2] == 1 && perm_[1] == 2)) {
      type_ = PermuteType::kTranspose;
L
limingshu 已提交
166 167 168 169 170
      int tmp_vec = std::min(vec_size, phi::GetVectorizedSize<T>(src));
      // With bytes limitation of shared_memory, the VecSize shall be
      // restricted for the type whose byte-size is less than 8 (double).
      vec_size =
          sizeof(T) > 8 ? 1 : GetDimVesSize(tmp_vec, src_dims[rank_ - 1]);
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
    }
    return vec_size;
  }

  // To find if highest common divisor and make it as vec_size.
  int GetDimVesSize(const int vec_size, const size_t target_dim) {
    int dim_vec_size = 1;
    for (auto size = vec_size; size > 0; size /= 2) {
      if (target_dim % size == 0) {
        dim_vec_size = size;
        break;
      }
    }
    return dim_vec_size;
  }
};

188 189
}  // namespace funcs
}  // namespace phi