convert_utils.cc 3.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13
/* 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. */
14 15 16

#include "paddle/pten/core/compat/convert_utils.h"

17 18 19 20
#include "paddle/pten/backends/gpu/gpu_info.h"
#include "paddle/pten/backends/xpu/xpu_info.h"
#include "paddle/pten/common/place.h"
#include "paddle/pten/core/compat/op_utils.h"
21

22 23 24 25
#ifdef PADDLE_WITH_CUSTOM_DEVICE
#include "paddle/fluid/platform/device/device_manager.h"
#endif

26 27
namespace pten {

28 29 30 31 32
Backend TransToPtenBackend(const pten::Place& place) {
  if (place.GetType() == pten::AllocationType::CPU) {
    return Backend::CPU;
  } else if (place.GetType() == pten::AllocationType::GPU) {
    return Backend::GPU;
33 34 35 36
  } else if (place.GetType() == pten::AllocationType::CUSTOM) {
    return static_cast<Backend>(
        static_cast<size_t>(Backend::NUM_BACKENDS) +
        GetOrRegisterGlobalDeviceTypeId(place.GetDeviceType()));
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
  } else {
    return Backend::UNDEFINED;
  }
}

pten::Place TransToPtenPlace(const Backend& backend, bool set_device_id) {
  // NOTE(zhiqiu): GetCurrentDeviceId not always success, and device id is not
  // always needed.
  // So, add set_device_id parameter here.
  switch (backend) {
    case pten::Backend::CPU:
      return pten::CPUPlace();
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
    case pten::Backend::GPU:
      return pten::GPUPlace(
          set_device_id ? pten::backends::gpu::GetCurrentDeviceId() : 0);
#endif
#ifdef PADDLE_WITH_MKLDNN
    case pten::Backend::MKLDNN:
      return pten::CPUPlace();
#endif
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
    case pten::Backend::CUDNN:
      return pten::GPUPlace(
          set_device_id ? pten::backends::gpu::GetCurrentDeviceId() : 0);
#endif
#if defined(PADDLE_WITH_XPU)
    case pten::Backend::XPU:
      return pten::XPUPlace(
          set_device_id ? pten::backends::xpu::GetXPUCurrentDeviceId() : 0);
#endif
68 69 70 71 72 73 74 75 76 77 78 79 80
    default: {
#ifdef PADDLE_WITH_CUSTOM_DEVICE
      size_t device_type_id_ = static_cast<size_t>(backend) -
                               static_cast<size_t>(Backend::NUM_BACKENDS);
      std::string device_type = pten::GetGlobalDeviceType(device_type_id_);
      if (!device_type.empty()) {
        return pten::CustomPlace(
            device_type,
            set_device_id
                ? paddle::platform::DeviceManager::GetDevice(device_type)
                : 0);
      }
#endif
81 82 83
      PADDLE_THROW(pten::errors::Unimplemented(
          "Unsupported backend `%s` when casting it to paddle place type.",
          backend));
84
    }
85 86 87
  }
}

88 89
std::string TransToPtenKernelName(const std::string& fluid_op_name) {
  return OpUtilsMap::Instance().GetBaseKernelName(fluid_op_name);
Y
YuanRisheng 已提交
90 91
}

92
const std::string& TransToFluidOpName(const std::string& pten_kernel_name) {
93 94 95
  auto& base_kernel_name_map = OpUtilsMap::Instance().base_kernel_name_map();
  auto it = std::find_if(base_kernel_name_map.begin(),
                         base_kernel_name_map.end(),
96 97 98
                         [&pten_kernel_name](const auto& pair) {
                           return pair.second == pten_kernel_name;
                         });
99
  if (it != base_kernel_name_map.end()) {
100 101 102 103 104
    return it->first;
  }
  return pten_kernel_name;
}

105
}  // namespace pten