place.cc 4.7 KB
Newer Older
1
/* Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 3 4 5 6 7 8 9 10 11 12 13 14

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. */

15
#include "paddle/phi/common/place.h"
16

17 18
#include <sstream>
#include <string>
19
#include <unordered_map>
20

21 22
#include "glog/logging.h"

23
#include "paddle/phi/api/ext/exception.h"
24

25
namespace phi {
26 27 28

const char *AllocationTypeStr(AllocationType type) {
  switch (type) {
29 30
    case AllocationType::UNDEFINED:
      return "undefined";
31 32 33 34 35
    case AllocationType::CPU:
      return "cpu";
    case AllocationType::GPU:
      return "gpu";
    case AllocationType::GPUPINNED:
36
      return "gpu_pinned";
37 38 39 40 41
    case AllocationType::XPU:
      return "xpu";
    case AllocationType::NPU:
      return "npu";
    case AllocationType::NPUPINNED:
42
      return "npu_pinned";
43 44 45 46 47
    case AllocationType::IPU:
      return "ipu";
    case AllocationType::MLU:
      return "mlu";
    default:
48
      PD_THROW("Invalid phi device type.");
49 50 51
      return {};
  }
}
52 53

std::string Place::DebugString() const {
54 55
  std::ostringstream os;
  os << "Place(";
56 57 58 59 60
  if (alloc_type_ == AllocationType::CUSTOM) {
    os << GetGlobalDeviceType(device_type_id_);
  } else {
    os << AllocationTypeStr(alloc_type_);
  }
61 62 63 64 65 66 67 68 69 70 71 72 73
  if (alloc_type_ == AllocationType::GPUPINNED ||
      alloc_type_ == AllocationType::NPUPINNED ||
      alloc_type_ == AllocationType::CPU) {
    os << ")";
  } else {
    os << ":" << std::to_string(device) << ")";
  }
  return os.str();
}

std::ostream &operator<<(std::ostream &os, const Place &p) {
  os << p.DebugString();
  return os;
74 75
}

76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
static std::unordered_map<std::string, size_t> global_registered_device_type_id;
static std::unordered_map<size_t, std::string> global_registered_device_type;

size_t GetOrRegisterGlobalDeviceTypeId(const std::string &device_type) {
  if (device_type.empty()) return 0;
  if (global_registered_device_type_id.find(device_type) ==
      global_registered_device_type_id.end()) {
    size_t device_type_id = global_registered_device_type_id.size() + 1;
    global_registered_device_type_id[device_type] = device_type_id;
    global_registered_device_type[device_type_id] = device_type;
  }
  return global_registered_device_type_id[device_type];
}

std::string GetGlobalDeviceType(size_t device_type_id) {
91 92 93
  if (global_registered_device_type.find(device_type_id) ==
      global_registered_device_type.end())
    return "";
94 95 96
  return global_registered_device_type[device_type_id];
}

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112
constexpr static int kAllocationTypeBitLength = 8;
constexpr static int kDeviceTypeIDBitLength = 8;
constexpr static int kDeviceIDBitLength = 8;

uint32_t Place::Hash::operator()(const Place &place) const {
  uint32_t hash_value = 0;
  // |----31-24------|-----23-16------|-----15-08----|---7-0----|
  // | For extension | AllocationType | DeviceTypeID | DeviceID |
  hash_value |= (static_cast<uint8_t>(place.alloc_type_)
                 << (kDeviceIDBitLength + kDeviceTypeIDBitLength));
  hash_value |=
      (static_cast<uint8_t>(place.device_type_id_) << kDeviceIDBitLength);
  hash_value |= static_cast<uint8_t>(place.device);
  return hash_value;
}

113 114 115 116 117 118 119 120 121 122
Place::Place(paddle::PlaceType type)
    : device(0),
      alloc_type_(static_cast<AllocationType>(type)),
      device_type_id_(GetOrRegisterGlobalDeviceTypeId("")) {
  LOG_FIRST_N(WARNING, 1)
      << "The `paddle::PlaceType::kCPU/kGPU` is deprecated since version "
         "2.3, and will be removed in version 2.4! Please use "
         "`paddle::CPUPlace()/GPUPlace()` to represent the place type.";
}

123
}  // namespace phi
124 125 126

namespace paddle {

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
bool operator==(const Place &place, PlaceType place_type) {
  LOG_FIRST_N(WARNING, 1)
      << "The `paddle::PlaceType::kCPU/kGPU` is deprecated since version "
         "2.3, and will be removed in version 2.4! Please use "
         "`Tensor::is_cpu()/is_gpu()` method to determine the type of place.";
  return place.GetType() == static_cast<AllocationType>(place_type);
}

bool operator==(PlaceType place_type, const Place &place) {
  LOG_FIRST_N(WARNING, 1)
      << "The `paddle::PlaceType::kCPU/kGPU` is deprecated since version "
         "2.3, and will be removed in version 2.4! Please use "
         "`Tensor::is_cpu()/is_gpu()` method to determine the type of place.";
  return static_cast<AllocationType>(place_type) == place.GetType();
}
142 143

}  // namespace paddle