library_type.h 2.0 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>
Q
QI JUN 已提交
17 18 19 20 21 22 23

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 已提交
24 25 26 27 28
enum class LibraryType {
  kPlain = 0,
  kMKLDNN = 1,
  kCUDNN = 2,
};
Q
QI JUN 已提交
29

Q
qiaolongfei 已提交
30 31
inline std::string LibraryTypeToString(const LibraryType& library_type) {
  switch (library_type) {
D
dzhwinter 已提交
32
    case LibraryType::kPlain:
Q
qiaolongfei 已提交
33
      return "PLAIN";
D
dzhwinter 已提交
34
    case LibraryType::kMKLDNN:
Q
qiaolongfei 已提交
35
      return "MKLDNN";
D
dzhwinter 已提交
36
    case LibraryType::kCUDNN:
Q
qiaolongfei 已提交
37 38
      return "CUDNN";
    default:
D
dzhwinter 已提交
39 40 41 42 43 44
      PADDLE_THROW("unknown LibraryType %d", static_cast<int>(library_type));
  }
}

inline LibraryType StringToLibraryType(const char* ctype) {
  std::string s(ctype);
D
dzhwinter 已提交
45 46 47
  for (size_t i = 0; i < s.size(); ++i) {
    s[i] = toupper(s[i]);
  }
D
dzhwinter 已提交
48 49 50 51 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;
  } else if (s == std::string("CUDA")) {
    return LibraryType::kPlain;
  } else {
    PADDLE_THROW("Unknown LibraryType %s", s.c_str());
Q
qiaolongfei 已提交
62 63 64 65 66 67 68 69
  }
}

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

Q
QI JUN 已提交
70 71
}  // namespace
}  // framework