convert_utils.cc 3.7 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
#include "paddle/phi/core/compat/convert_utils.h"
16

17 18 19 20
#include "paddle/phi/backends/gpu/gpu_info.h"
#include "paddle/phi/backends/xpu/xpu_info.h"
#include "paddle/phi/common/place.h"
#include "paddle/phi/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
namespace phi {
27

28 29
Backend TransToPtenBackend(const phi::Place& place) {
  if (place.GetType() == phi::AllocationType::CPU) {
30
    return Backend::CPU;
31
  } else if (place.GetType() == phi::AllocationType::GPU) {
32
    return Backend::GPU;
33 34
  } else if (place.GetType() == phi::AllocationType::XPU) {
    return Backend::XPU;
35
  } else if (place.GetType() == phi::AllocationType::CUSTOM) {
36 37 38
    return static_cast<Backend>(
        static_cast<size_t>(Backend::NUM_BACKENDS) +
        GetOrRegisterGlobalDeviceTypeId(place.GetDeviceType()));
39 40 41 42 43
  } else {
    return Backend::UNDEFINED;
  }
}

44
phi::Place TransToPtenPlace(const Backend& backend, bool set_device_id) {
45 46 47 48
  // NOTE(zhiqiu): GetCurrentDeviceId not always success, and device id is not
  // always needed.
  // So, add set_device_id parameter here.
  switch (backend) {
49 50
    case phi::Backend::CPU:
      return phi::CPUPlace();
51
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
52 53 54
    case phi::Backend::GPU:
      return phi::GPUPlace(
          set_device_id ? phi::backends::gpu::GetCurrentDeviceId() : 0);
55 56
#endif
#ifdef PADDLE_WITH_MKLDNN
57 58
    case phi::Backend::MKLDNN:
      return phi::CPUPlace();
59 60
#endif
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
61
    case phi::Backend::GPUDNN:
62 63
      return phi::GPUPlace(
          set_device_id ? phi::backends::gpu::GetCurrentDeviceId() : 0);
64 65
#endif
#if defined(PADDLE_WITH_XPU)
66 67 68
    case phi::Backend::XPU:
      return phi::XPUPlace(
          set_device_id ? phi::backends::xpu::GetXPUCurrentDeviceId() : 0);
69
#endif
70 71 72 73
    default: {
#ifdef PADDLE_WITH_CUSTOM_DEVICE
      size_t device_type_id_ = static_cast<size_t>(backend) -
                               static_cast<size_t>(Backend::NUM_BACKENDS);
74
      std::string device_type = phi::GetGlobalDeviceType(device_type_id_);
75
      if (!device_type.empty()) {
76
        return phi::CustomPlace(
77 78 79 80 81 82
            device_type,
            set_device_id
                ? paddle::platform::DeviceManager::GetDevice(device_type)
                : 0);
      }
#endif
83
      PADDLE_THROW(phi::errors::Unimplemented(
84 85
          "Unsupported backend `%s` when casting it to paddle place type.",
          backend));
86
    }
87 88 89
  }
}

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

94
const std::string& TransToFluidOpName(const std::string& pten_kernel_name) {
95 96 97
  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(),
98 99 100
                         [&pten_kernel_name](const auto& pair) {
                           return pair.second == pten_kernel_name;
                         });
101
  if (it != base_kernel_name_map.end()) {
102 103 104 105 106
    return it->first;
  }
  return pten_kernel_name;
}

107
}  // namespace phi