layout.h 3.5 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 20
namespace paddle {
namespace experimental {

21 22
// Note: Here the DataLayout is public api for external users, the prefix `k`
// maybe confuse users, so we use all uppercase names
23 24
enum class DataLayout {
  UNDEFINED = 0,
25 26
  // TODO(chenweihang): keep ANY for compatibility, remove it later
  ANY = UNDEFINED,
27 28 29
  NHWC,
  NCHW,
  MKLDNN,
30 31
  SPARSE_COO,
  SPARSE_CSR,
32
  NUM_DATA_LAYOUTS,
33 34
  NDHWC,
  NCDHW,
35
  // See Note [ Why we need ALL in basic kernel key member? ]
36
  ALL_LAYOUT = UNDEFINED,
37
  // Note: Unify phi DataLayout and fluid::framework::DataLayout,
38 39 40 41 42 43 44 45 46 47
  // for compatible with fluid DataLayout, here need prefix `k`
  // 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,
  // and should not be mixed with other meaningful layouts.
  kAnyLayout = ANY,
  kNHWC = NHWC,
  kNCHW = NCHW,
  kMKLDNN = MKLDNN,  // all layouts supported by MKLDNN internally
48 49
  kNDHWC = NDHWC,
  kNCDHW = NCDHW,
50 51
};

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
}  // namespace experimental

// In order to be compatible with the fluid implementation
namespace framework {

using DataLayout = paddle::experimental::DataLayout;

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;
73 74 75 76
  } else if (s == "SPARSE_COO") {
    return DataLayout::SPARSE_COO;
  } else if (s == "SPARSE_CSR") {
    return DataLayout::SPARSE_CSR;
77 78 79 80
  } else if (s == "NDHWC") {
    return DataLayout::kNDHWC;
  } else if (s == "NCDHW") {
    return DataLayout::kNCDHW;
81 82 83 84 85 86
  } else {
    PD_THROW("Unknown data layout type string: ", s, ".");
  }
}

inline std::string DataLayoutToString(const DataLayout& layout) {
87
  switch (layout) {
88 89 90 91 92 93 94 95
    case DataLayout::kNHWC:
      return "NHWC";
    case DataLayout::kNCHW:
      return "NCHW";
    case DataLayout::kAnyLayout:
      return "Undefined(AnyLayout)";
    case DataLayout::kMKLDNN:
      return "MKLDNN";
96 97 98 99
    case DataLayout::SPARSE_COO:
      return "SPARSE_COO";
    case DataLayout::SPARSE_CSR:
      return "SPARSE_CSR";
100 101 102 103
    case DataLayout::kNDHWC:
      return "NDHWC";
    case DataLayout::kNCDHW:
      return "NCDHW";
104
    default:
105
      PD_THROW("Unknown Data Layout type ", static_cast<int>(layout), ".");
106
  }
107 108 109 110 111 112 113
}
}  // namespace framework

namespace experimental {

inline std::ostream& operator<<(std::ostream& os, DataLayout layout) {
  os << framework::DataLayoutToString(layout);
114 115 116 117 118 119
  return os;
}

}  // namespace experimental
}  // namespace paddle

120
namespace phi {
121 122
using DataLayout = paddle::experimental::DataLayout;
}