convert_utils.cc 4.4 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
#include "paddle/phi/core/enforce.h"
22

23
#ifdef PADDLE_WITH_CUSTOM_DEVICE
24
#include "paddle/phi/backends/device_manager.h"
25 26
#endif

27
namespace phi {
28

29
Backend TransToPhiBackend(const phi::Place& place) {
30 31
  auto allocation_type = place.GetType();
  if (allocation_type == phi::AllocationType::CPU) {
32
    return Backend::CPU;
33
  } else if (allocation_type == phi::AllocationType::GPU) {
34
    return Backend::GPU;
35 36
  } else if (allocation_type == phi::AllocationType::GPUPINNED) {
    return Backend::GPU;
37
  } else if (allocation_type == phi::AllocationType::XPU) {
38
    return Backend::XPU;
39 40
  } else if (allocation_type == phi::AllocationType::NPU) {
    return Backend::NPU;
A
Allen Guo 已提交
41 42
  } else if (allocation_type == phi::AllocationType::IPU) {
    return Backend::IPU;
43
  } else if (allocation_type == phi::AllocationType::CUSTOM) {
44 45 46
    return static_cast<Backend>(
        static_cast<size_t>(Backend::NUM_BACKENDS) +
        GetOrRegisterGlobalDeviceTypeId(place.GetDeviceType()));
47
  } else {
48 49
    PADDLE_THROW(phi::errors::InvalidArgument(
        "Unsupported transform %s to phi Backend.", place));
50 51 52
  }
}

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

105
std::string TransToPhiKernelName(const std::string& fluid_op_name) {
106
  return OpUtilsMap::Instance().GetBaseKernelName(fluid_op_name);
Y
YuanRisheng 已提交
107 108
}

109
const std::string& TransToFluidOpName(const std::string& phi_kernel_name) {
110 111 112
  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(),
113 114
                         [&phi_kernel_name](const auto& pair) {
                           return pair.second == phi_kernel_name;
115
                         });
116
  if (it != base_kernel_name_map.end()) {
117 118
    return it->first;
  }
119
  return phi_kernel_name;
120 121
}

122
}  // namespace phi