library_type.h 2.6 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 29
enum class LibraryType {
  kPlain = 0,
  kMKLDNN = 1,
  kCUDNN = 2,
};
Q
QI JUN 已提交
30

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

inline LibraryType StringToLibraryType(const char* ctype) {
  std::string s(ctype);
D
dzhwinter 已提交
49 50 51
  for (size_t i = 0; i < s.size(); ++i) {
    s[i] = toupper(s[i]);
  }
D
dzhwinter 已提交
52 53 54 55 56 57 58 59 60 61
  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.
  } else if (s == std::string("CPU")) {
    return LibraryType::kPlain;
62 63
  } else if (s == std::string("XPU")) {
    return LibraryType::kPlain;
J
jianghaicheng 已提交
64 65
  } else if (s == std::string("IPU")) {
    return LibraryType::kPlain;
66 67
  } else if (s == std::string("NPU")) {
    return LibraryType::kPlain;
D
dzhwinter 已提交
68 69 70
  } else if (s == std::string("CUDA")) {
    return LibraryType::kPlain;
  } else {
71 72
    PADDLE_THROW(platform::errors::Unimplemented(
        "Unknown LibraryType string (%s), only support library type string "
J
jianghaicheng 已提交
73
        "include PLAIN, MKLDNN, CUDNN, CPU, CUDA and IPU.",
74
        s.c_str()));
Q
qiaolongfei 已提交
75 76 77 78 79 80 81 82
  }
}

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

83 84
}  // namespace framework
}  // namespace paddle