library_type.h 2.8 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
Q
QI JUN 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15

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
D
dzhwinter 已提交
16
#include <cctype>
17
#include <string>
Q
QI JUN 已提交
18 19 20 21 22 23 24

namespace paddle {
namespace framework {

// For more details about the design of LibraryType, Please refer to
// https://github.com/PaddlePaddle/Paddle/blob/develop/doc/design/operator_kernel_type.md#library

D
dzhwinter 已提交
25 26 27 28
enum class LibraryType {
  kPlain = 0,
  kMKLDNN = 1,
  kCUDNN = 2,
L
Liu-xiandong 已提交
29
  kKP = 3,
D
dzhwinter 已提交
30
};
Q
QI JUN 已提交
31

Q
qiaolongfei 已提交
32 33
inline std::string LibraryTypeToString(const LibraryType& library_type) {
  switch (library_type) {
D
dzhwinter 已提交
34
    case LibraryType::kPlain:
Q
qiaolongfei 已提交
35
      return "PLAIN";
D
dzhwinter 已提交
36
    case LibraryType::kMKLDNN:
Q
qiaolongfei 已提交
37
      return "MKLDNN";
D
dzhwinter 已提交
38
    case LibraryType::kCUDNN:
Q
qiaolongfei 已提交
39
      return "CUDNN";
L
Liu-xiandong 已提交
40 41
    case LibraryType::kKP:
      return "KP";
Q
qiaolongfei 已提交
42
    default:
43 44
      PADDLE_THROW(platform::errors::Unimplemented(
          "Unknown LibraryType code (%d), only supports library type include "
L
Liu-xiandong 已提交
45
          "PLAIN(0), MKLDNN(1), CUDNN(2), KP(3).",
46
          static_cast<int>(library_type)));
D
dzhwinter 已提交
47 48 49 50 51
  }
}

inline LibraryType StringToLibraryType(const char* ctype) {
  std::string s(ctype);
D
dzhwinter 已提交
52 53 54
  for (size_t i = 0; i < s.size(); ++i) {
    s[i] = toupper(s[i]);
  }
D
dzhwinter 已提交
55 56 57 58 59 60 61 62
  if (s == std::string("PLAIN")) {
    return LibraryType::kPlain;
  } else if (s == std::string("MKLDNN")) {
    return LibraryType::kMKLDNN;
  } else if (s == std::string("CUDNN")) {
    return LibraryType::kCUDNN;
    // To be compatible with register macro.
    // CPU, CUDA, PLAIN are same library type.
L
Liu-xiandong 已提交
63 64
  } else if (s == std::string("KP")) {
    return LibraryType::kKP;
D
dzhwinter 已提交
65 66
  } else if (s == std::string("CPU")) {
    return LibraryType::kPlain;
67 68
  } else if (s == std::string("XPU")) {
    return LibraryType::kPlain;
J
jianghaicheng 已提交
69 70
  } else if (s == std::string("IPU")) {
    return LibraryType::kPlain;
71 72
  } else if (s == std::string("NPU")) {
    return LibraryType::kPlain;
D
dzhwinter 已提交
73 74
  } else if (s == std::string("CUDA")) {
    return LibraryType::kPlain;
F
fwenguang 已提交
75 76
  } else if (s == std::string("MLU")) {
    return LibraryType::kPlain;
D
dzhwinter 已提交
77
  } else {
78 79
    PADDLE_THROW(platform::errors::Unimplemented(
        "Unknown LibraryType string (%s), only support library type string "
J
jianghaicheng 已提交
80
        "include PLAIN, MKLDNN, CUDNN, CPU, CUDA and IPU.",
81
        s.c_str()));
Q
qiaolongfei 已提交
82 83 84 85 86 87 88 89
  }
}

inline std::ostream& operator<<(std::ostream& out, LibraryType l) {
  out << LibraryTypeToString(l);
  return out;
}

90 91
}  // namespace framework
}  // namespace paddle