backend.h 5.9 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
/* 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

#include <ostream>

19 20
#include "paddle/phi/api/ext/exception.h"
#include "paddle/phi/common/place.h"
21 22 23 24 25 26 27 28 29 30 31 32 33 34

namespace paddle {
namespace experimental {

/**
 * [ Why need Backend? ]
 *
 * Backend not only means place. Backend is a superset of place.
 *
 * Place cannot indicate the difference in calculation methods on the device,
 * but in order to make the boundary of the kernel clearer and the function
 * more specific, we need to distinguish the calculation method.
 *
 * Such as the kernel for CPU device, it can be a native CPU kernel,
35
 * or a kernel implemented by oneDNN library.
36 37 38 39 40 41 42 43 44
 *
 * Note(chenweihang): HIP is not needed now, we can added it if needed
 * in the future
 */
enum class Backend : uint8_t {
  UNDEFINED = 0,

  // basic kernel backend
  CPU,
45 46
  // the third library backend
  ONEDNN,
47

48
  // acceleration device's backend
49
  GPU,
50 51 52 53
  // the third library backend
  GPUDNN,  // cuDNN and hipDNN

  // various acceleration devices' backends
54 55
  XPU,  // XPU currently does not exist at the same time as CUDA
  NPU,  // NPU currently does not exist at the same time as CUDA
56
  MLU,  // MLU currently does not exist at the same time as CUDA
57
  IPU,
58

59 60 61
  // paddle kernel primitives backend
  KPS,

62 63 64
  // custom device reference
  CUSTOM,

65 66
  // end of backend types
  NUM_BACKENDS,
67 68

  /**
69
   * [ Why we need ALL in basic kernel key member? ]
70 71 72 73 74 75 76 77 78
   *
   * For Tensor, ALL represents an illegal Backend, but for Kernel, some
   * kernels may be device-independent by nature, such as reshape; and when
   * and some kernels are also device-independent when implemented based on
   * primitive API.
   *
   * Note: ALL_BACKEND only used for Kernel registration and selection
   */
  ALL_BACKEND = UNDEFINED,
79 80 81 82 83 84 85 86 87 88
};

inline std::ostream& operator<<(std::ostream& os, Backend backend) {
  switch (backend) {
    case Backend::UNDEFINED:
      os << "Undefined";
      break;
    case Backend::CPU:
      os << "CPU";
      break;
89 90
    case Backend::GPU:
      os << "GPU";
91 92 93 94 95 96 97
      break;
    case Backend::XPU:
      os << "XPU";
      break;
    case Backend::NPU:
      os << "NPU";
      break;
98 99 100
    case Backend::MLU:
      os << "MLU";
      break;
101 102
    case Backend::ONEDNN:
      os << "ONEDNN";
103
      break;
104 105
    case Backend::GPUDNN:
      os << "GPUDNN";
106
      break;
107 108 109
    case Backend::KPS:
      os << "KPS";
      break;
A
Allen Guo 已提交
110 111 112
    case Backend::IPU:
      os << "IPU";
      break;
113 114 115
    case Backend::CUSTOM:
      os << "CUSTOM";
      break;
116 117 118
    default: {
      size_t device_type_id_ = static_cast<size_t>(backend) -
                               static_cast<size_t>(Backend::NUM_BACKENDS);
119 120 121
      std::string device_type =
          phi::CustomRegisteredDeviceMap::Instance().GetGlobalDeviceType(
              device_type_id_);
122 123 124 125 126 127 128
      if (!device_type.empty()) {
        os << device_type;
      } else {
        PD_THROW(
            "Invalid enum backend type `", static_cast<int>(backend), "`.");
      }
    }
129 130 131 132
  }
  return os;
}

133 134 135 136 137 138 139 140 141 142 143 144 145
inline Backend StringToBackend(const char* backend_cstr) {
  std::string s(backend_cstr);
  if (s == std::string("Undefined")) {
    return Backend::UNDEFINED;
  }
  if (s == std::string("CPU")) {
    return Backend::CPU;
  } else if (s == std::string("GPU")) {
    return Backend::GPU;
  } else if (s == std::string("XPU")) {
    return Backend::XPU;
  } else if (s == std::string("NPU")) {
    return Backend::NPU;
146 147
  } else if (s == std::string("MLU")) {
    return Backend::MLU;
148 149
  } else if (s == std::string("OneDNN")) {
    return Backend::ONEDNN;
150 151
  } else if (s == std::string("GPUDNN")) {
    return Backend::GPUDNN;
152
  } else if (s == std::string("KPS")) {
153 154 155 156 157 158
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
    // NOTE(chenweihang) KPS is not yet a complete backend, and it still needs
    // to be converted
    // to GPU in the GPU environment
    return Backend::GPU;
#else
159
    return Backend::KPS;
160
#endif
A
Allen Guo 已提交
161 162
  } else if (s == std::string("IPU")) {
    return Backend::IPU;
163 164
  } else if (s == std::string("Custom")) {
    return Backend::CUSTOM;
165 166
  } else {
    return static_cast<Backend>(static_cast<size_t>(Backend::NUM_BACKENDS) +
167 168
                                phi::CustomRegisteredDeviceMap::Instance()
                                    .GetOrRegisterGlobalDeviceTypeId(s));
169 170 171
  }
}

172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193
inline std::string BackendToString(const Backend& backend) {
  switch (backend) {
    case Backend::UNDEFINED:
      return "Undefined(ALL_BACKEND)";
    case Backend::CPU:
      return "CPU";
    case Backend::GPU:
      return "GPU";
    case Backend::XPU:
      return "XPU";
    case Backend::NPU:
      return "NPU";
    case Backend::MLU:
      return "MLU";
    case Backend::ONEDNN:
      return "ONEDNN";
    case Backend::GPUDNN:
      return "GPUDNN";
    case Backend::KPS:
      return "KPS";
    case Backend::IPU:
      return "IPU";
194 195
    case Backend::CUSTOM:
      return "CUSTOM";
196
    default: {
197 198
      size_t device_type_id_ = static_cast<size_t>(backend) -
                               static_cast<size_t>(Backend::NUM_BACKENDS);
199 200 201
      std::string device_type =
          phi::CustomRegisteredDeviceMap::Instance().GetGlobalDeviceType(
              device_type_id_);
202 203 204 205 206 207
      if (!device_type.empty()) {
        return device_type;
      } else {
        PD_THROW(
            "Invalid enum backend type `", static_cast<int>(backend), "`.");
      }
208
    }
209 210 211
  }
}

212 213 214
}  // namespace experimental
}  // namespace paddle

215
namespace phi {
216 217
using Backend = paddle::experimental::Backend;
}