convert_utils.cc 4.8 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
  auto allocation_type = place.GetType();
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52
  switch (allocation_type) {
    case phi::AllocationType::GPU:
      return Backend::GPU;
    case AllocationType::CPU:
      return Backend::CPU;
    case AllocationType::GPUPINNED:
      return Backend::GPU;
    case AllocationType::XPU:
      return Backend::XPU;
    case AllocationType::NPU:
      return Backend::NPU;
    case AllocationType::IPU:
      return Backend::IPU;
    case AllocationType::MLU:
      return Backend::MLU;
    case AllocationType::CUSTOM:
      return static_cast<Backend>(
          static_cast<size_t>(Backend::NUM_BACKENDS) +
          GetOrRegisterGlobalDeviceTypeId(place.GetDeviceType()));
    default:
      PADDLE_THROW(phi::errors::InvalidArgument(
          "Unsupported transform %s to phi Backend.", place));
53 54 55
  }
}

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

108
const std::string& TransToPhiKernelName(const std::string& fluid_op_name) {
109
  return OpUtilsMap::Instance().GetBaseKernelName(fluid_op_name);
Y
YuanRisheng 已提交
110 111
}

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

125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
#ifdef PADDLE_WITH_MKLDNN
dnnl::memory::data_type TransToMKLDNNDataType(
    const paddle::experimental::DataType& dtype) {
  switch (dtype) {
    case DataType::FLOAT32:
      return dnnl::memory::data_type::f32;
    case DataType::BFLOAT16:
      return dnnl::memory::data_type::bf16;
    case DataType::INT8:
      return dnnl::memory::data_type::s8;
    case DataType::UINT8:
      return dnnl::memory::data_type::u8;
    case DataType::INT32:
      return dnnl::memory::data_type::s32;
    default:
      return dnnl::memory::data_type::undef;
  }
}
#endif

145
}  // namespace phi