layout.h 4.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
/* Copyright (c) 2021 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

17
#include "paddle/phi/api/ext/exception.h"
18 19

namespace phi {
20

21 22 23 24 25 26 27 28 29 30 31
// Note: The original design of paddle DataLayout is confusing.
// It contains two levels of "layout", one is the data layout
// at the Tensor level, including Dense, Sparse, etc., and the other
// is the format at the data level, including NHWC, NCHW, etc.,
// these should belong to the concept of "data format".
// The concepts of these two levels are mixed into an enumeration class,
// which leads to some strange execution scheduling logic.
// It needs to be refactored in the future.
// In order to maintain compatibility, we still use the design of the
// original framework here.

32 33
// Note: Here the DataLayout is public api for external users, the prefix `k`
// maybe confuse users, so we use all uppercase names
34

35 36
enum class DataLayout {
  UNDEFINED = 0,
37 38
  // TODO(chenweihang): keep ANY for compatibility, remove it later
  ANY = UNDEFINED,
39 40
  NHWC,
  NCHW,
41 42
  NCDHW,
  NDHWC,
43
  ONEDNN,
44 45
  SPARSE_COO,
  SPARSE_CSR,
J
Jack Zhou 已提交
46
  PSTRING_UNION,
47

48
  NUM_DATA_LAYOUTS,
49

50
  // See Note [ Why we need ALL in basic kernel key member? ]
51
  ALL_LAYOUT = UNDEFINED,
52

53
  // Note: Unify phi DataLayout and fluid::framework::DataLayout,
54
  // for compatible with fluid DataLayout, here need prefix `k`
55

56 57 58 59
  // Note: The original `kAnyLayout (enum value 2)` is a strange design.
  // `kAnyLayout` originally cannot represent any kind of Layout,
  // at the same time, it can also represent any Layout.
  // Strictly, it means "default" or "undefined" layout,
60 61
  // and should not be mixed with other meaningful layouts

62 63 64
  kAnyLayout = ANY,
  kNHWC = NHWC,
  kNCHW = NCHW,
65
  kMKLDNN = ONEDNN,  // all layouts supported by ONEDNN internally
66 67
  kNDHWC = NDHWC,
  kNCDHW = NCDHW,
68 69
};

70 71 72 73 74 75 76 77 78 79 80 81 82 83
inline DataLayout StringToDataLayout(const std::string& str) {
  std::string s(str);
  for (size_t i = 0; i < s.size(); ++i) {
    s[i] = toupper(s[i]);
  }

  if (s == "NHWC") {
    return DataLayout::kNHWC;
  } else if (s == "NCHW") {
    return DataLayout::kNCHW;
  } else if (s == "ANYLAYOUT") {
    return DataLayout::kAnyLayout;
  } else if (s == "MKLDNNLAYOUT") {
    return DataLayout::kMKLDNN;
84 85 86 87
  } else if (s == "SPARSE_COO") {
    return DataLayout::SPARSE_COO;
  } else if (s == "SPARSE_CSR") {
    return DataLayout::SPARSE_CSR;
88 89
  } else if (s == "NDHWC") {
    return DataLayout::kNDHWC;
J
Jack Zhou 已提交
90 91
  } else if (s == "PSTRING_UNION") {
    return DataLayout::PSTRING_UNION;
92 93
  } else if (s == "NCDHW") {
    return DataLayout::kNCDHW;
94 95 96 97 98 99
  } else {
    PD_THROW("Unknown data layout type string: ", s, ".");
  }
}

inline std::string DataLayoutToString(const DataLayout& layout) {
100
  switch (layout) {
101 102 103 104 105 106 107 108
    case DataLayout::kNHWC:
      return "NHWC";
    case DataLayout::kNCHW:
      return "NCHW";
    case DataLayout::kAnyLayout:
      return "Undefined(AnyLayout)";
    case DataLayout::kMKLDNN:
      return "MKLDNN";
109 110 111 112
    case DataLayout::SPARSE_COO:
      return "SPARSE_COO";
    case DataLayout::SPARSE_CSR:
      return "SPARSE_CSR";
113 114 115 116
    case DataLayout::kNDHWC:
      return "NDHWC";
    case DataLayout::kNCDHW:
      return "NCDHW";
J
Jack Zhou 已提交
117 118
    case DataLayout::PSTRING_UNION:
      return "PSTRING_UNION";
119
    default:
120
      PD_THROW("Unknown Data Layout type ", static_cast<int>(layout), ".");
121
  }
122 123 124
}

inline std::ostream& operator<<(std::ostream& os, DataLayout layout) {
125
  os << DataLayoutToString(layout);
126 127 128
  return os;
}

129
}  // namespace phi
130

131 132 133 134 135
namespace paddle {
// In order to be compatible with the original custom operator Tensor interface
using DataLayout = phi::DataLayout;

}  // namespace paddle