device_base.cc 12.4 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// Copyright (c) 2022 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.

15
#include "paddle/phi/backends/device_base.h"
16

17
#include "gflags/gflags.h"
18
#include "glog/logging.h"
19
#include "paddle/phi/core/enforce.h"
20 21 22 23 24 25 26

DECLARE_double(fraction_of_gpu_memory_to_use);
DECLARE_uint64(initial_gpu_memory_in_mb);
DECLARE_uint64(reallocate_gpu_memory_in_mb);

constexpr static float fraction_reserve_gpu_memory = 0.05f;

27
namespace phi {
28

29 30
#define INTERFACE_UNIMPLEMENT              \
  PADDLE_THROW(phi::errors::Unimplemented( \
31 32 33 34
      "%s is not implemented on %s device.", __func__, Type()));

// info
size_t DeviceInterface::GetComputeCapability() {
35
  VLOG(10) << Type() << " get compute capability " << 0;
36 37 38 39
  return 0;
}

size_t DeviceInterface::GetRuntimeVersion() {
40
  VLOG(10) << Type() << " get runtime version " << 0;
41 42 43 44
  return 0;
}

size_t DeviceInterface::GetDriverVersion() {
45
  VLOG(10) << Type() << " get driver version " << 0;
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
  return 0;
}

// device manage
void DeviceInterface::Initialize() { INTERFACE_UNIMPLEMENT; }

void DeviceInterface::Finalize() { INTERFACE_UNIMPLEMENT; }

void DeviceInterface::SynchronizeDevice(size_t dev_id) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::InitDevice(size_t dev_id) { INTERFACE_UNIMPLEMENT; }

void DeviceInterface::DeInitDevice(size_t dev_id) { INTERFACE_UNIMPLEMENT; }

void DeviceInterface::SetDevice(size_t dev_id) { INTERFACE_UNIMPLEMENT; }

int DeviceInterface::GetDevice() { INTERFACE_UNIMPLEMENT; }

// stream manage
67 68
void DeviceInterface::CreateStream(size_t dev_id,
                                   stream::Stream* stream,
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
                                   const stream::Stream::Priority& priority,
                                   const stream::Stream::Flag& flag) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::DestroyStream(size_t dev_id, stream::Stream* stream) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::SynchronizeStream(size_t dev_id,
                                        const stream::Stream* stream) {
  INTERFACE_UNIMPLEMENT;
}

bool DeviceInterface::QueryStream(size_t dev_id, const stream::Stream* stream) {
  INTERFACE_UNIMPLEMENT;
  return true;
}

88 89
void DeviceInterface::AddCallback(size_t dev_id,
                                  stream::Stream* stream,
90 91 92 93 94 95 96 97 98 99 100
                                  stream::Stream::Callback* callback) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::StreamWaitEvent(size_t dev_id,
                                      const stream::Stream* stream,
                                      const event::Event* event) {
  INTERFACE_UNIMPLEMENT;
}

// event manage
101 102
void DeviceInterface::CreateEvent(size_t dev_id,
                                  event::Event* event,
103 104 105 106 107 108 109 110
                                  event::Event::Flag flags) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::DestroyEvent(size_t dev_id, event::Event* event) {
  INTERFACE_UNIMPLEMENT;
}

111 112
void DeviceInterface::RecordEvent(size_t dev_id,
                                  const event::Event* event,
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
                                  const stream::Stream* stream) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::SynchronizeEvent(size_t dev_id,
                                       const event::Event* event) {
  INTERFACE_UNIMPLEMENT;
}

bool DeviceInterface::QueryEvent(size_t dev_id, const event::Event* event) {
  INTERFACE_UNIMPLEMENT;
  return true;
}

// memery manage
128 129 130 131 132
void DeviceInterface::MemoryCopyH2D(size_t dev_id,
                                    void* dst,
                                    const void* src,
                                    size_t size,
                                    const stream::Stream* stream) {
133 134 135
  INTERFACE_UNIMPLEMENT;
}

136 137 138 139 140
void DeviceInterface::MemoryCopyD2H(size_t dev_id,
                                    void* dst,
                                    const void* src,
                                    size_t size,
                                    const stream::Stream* stream) {
141 142 143
  INTERFACE_UNIMPLEMENT;
}

144 145 146 147 148
void DeviceInterface::MemoryCopyD2D(size_t dev_id,
                                    void* dst,
                                    const void* src,
                                    size_t size,
                                    const stream::Stream* stream) {
149 150 151
  INTERFACE_UNIMPLEMENT;
}

152 153 154 155 156
void DeviceInterface::MemoryCopyP2P(const Place& dst_place,
                                    void* dst,
                                    size_t src_id,
                                    const void* src,
                                    size_t size,
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
                                    const stream::Stream* stream) {
  INTERFACE_UNIMPLEMENT;
}

void* DeviceInterface::MemoryAllocate(size_t dev_id, size_t size) {
  INTERFACE_UNIMPLEMENT;
  return nullptr;
}

void DeviceInterface::MemoryDeallocate(size_t dev_id, void* ptr, size_t size) {
  INTERFACE_UNIMPLEMENT;
}

void* DeviceInterface::MemoryAllocateHost(size_t dev_id, size_t size) {
  INTERFACE_UNIMPLEMENT;
  return nullptr;
}

175 176
void DeviceInterface::MemoryDeallocateHost(size_t dev_id,
                                           void* ptr,
177 178 179 180 181 182 183 184 185
                                           size_t size) {
  INTERFACE_UNIMPLEMENT;
}

void* DeviceInterface::MemoryAllocateUnified(size_t dev_id, size_t size) {
  INTERFACE_UNIMPLEMENT;
  return nullptr;
}

186 187
void DeviceInterface::MemoryDeallocateUnified(size_t dev_id,
                                              void* ptr,
188 189 190 191
                                              size_t size) {
  INTERFACE_UNIMPLEMENT;
}

192 193 194
void DeviceInterface::MemorySet(size_t dev_id,
                                void* ptr,
                                uint8_t value,
195 196 197 198 199 200 201 202 203 204 205 206 207 208
                                size_t size) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::MemoryStats(size_t dev_id, size_t* total, size_t* free) {
  INTERFACE_UNIMPLEMENT;
}

size_t DeviceInterface::GetMinChunkSize(size_t dev_id) {
  INTERFACE_UNIMPLEMENT;
}

size_t DeviceInterface::AllocSize(size_t dev_id, bool realloc) {
  size_t available_to_alloc = AvailableAllocSize(dev_id);
209 210 211
  PADDLE_ENFORCE_GT(available_to_alloc,
                    0,
                    phi::errors::ResourceExhausted(
212 213 214 215 216 217
                        "Not enough available %s memory.", Type()));
  // If FLAGS_initial_gpu_memory_in_mb is 0, then initial memory will be
  // allocated by fraction
  size_t flag_mb = realloc ? FLAGS_reallocate_gpu_memory_in_mb
                           : FLAGS_initial_gpu_memory_in_mb;
  size_t alloc_bytes =
218 219 220
      (flag_mb > 0ul
           ? flag_mb << 20
           : available_to_alloc * FLAGS_fraction_of_gpu_memory_to_use);
221 222 223
  PADDLE_ENFORCE_GE(available_to_alloc,
                    alloc_bytes,
                    phi::errors::ResourceExhausted(
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244
                        "Not enough available %s memory.", Type()));
  return alloc_bytes;
}

size_t DeviceInterface::AvailableAllocSize(size_t dev_id) {
  size_t total = 0;
  size_t available = 0;
  MemoryStats(dev_id, &total, &available);
  size_t reserving =
      static_cast<size_t>(fraction_reserve_gpu_memory * available);
  // If available size is less than minimum chunk size, no usable memory exists
  size_t available_to_alloc = available - reserving;
  size_t min_chunk_size = GetMinChunkSize(dev_id);
  if (available_to_alloc < min_chunk_size) {
    available_to_alloc = 0;
  }
  return available_to_alloc;
}

size_t DeviceInterface::GetInitAllocSize(size_t dev_id) {
  size_t init_alloc_size = AllocSize(dev_id, false);
245
  VLOG(10) << Type() << " init alloc size " << (init_alloc_size >> 20) << "M";
246 247 248 249 250
  return init_alloc_size;
}

size_t DeviceInterface::GetReallocSize(size_t dev_id) {
  size_t realloc_size = AllocSize(dev_id, true);
251
  VLOG(10) << Type() << " realloc size " << (realloc_size >> 20) << "M";
252 253 254 255 256 257
  return realloc_size;
}

size_t DeviceInterface::GetMaxAllocSize(size_t dev_id) {
  size_t max_alloc_size =
      std::max(GetInitAllocSize(dev_id), GetReallocSize(dev_id));
258
  VLOG(10) << Type() << " max alloc size " << (max_alloc_size >> 20) << "M";
259 260 261 262 263
  return max_alloc_size;
}

size_t DeviceInterface::GetMaxChunkSize(size_t dev_id) {
  size_t max_chunk_size = GetMaxAllocSize(dev_id);
264
  VLOG(10) << Type() << " max chunk size " << (max_chunk_size >> 20) << "M";
265 266 267 268
  return max_chunk_size;
}

size_t DeviceInterface::GetExtraPaddingSize(size_t dev_id) {
269
  VLOG(10) << Type() << " extra padding size " << 0;
270 271 272
  return 0;
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
void DeviceInterface::CCLDestroyComm(ccl::CCLComm ccl_comm) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::CCLCommInitRank(size_t num_ranks,
                                      ccl::CCLRootId* root_id,
                                      size_t rank_id,
                                      ccl::CCLComm* ccl_comm) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::CCLGetUniqueId(ccl::CCLRootId* root_id) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::CCLBroadcast(void* data,
                                   size_t num,
                                   ccl::CCLDataType data_type,
                                   size_t root,
                                   const ccl::CCLComm& ccl_comm,
                                   const stream::Stream& stream) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::CCLAllReduce(void* in_data,
                                   void* out_data,
                                   size_t num,
                                   ccl::CCLDataType data_type,
                                   ccl::CCLReduceOp reduce_op,
                                   const ccl::CCLComm& ccl_comm,
                                   const stream::Stream& stream) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::CCLReduce(void* in_data,
                                void* out_data,
                                size_t num,
                                ccl::CCLDataType data_type,
                                ccl::CCLReduceOp reduce_op,
312
                                size_t root_id,
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358
                                const ccl::CCLComm& ccl_comm,
                                const stream::Stream& stream) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::CCLAllGather(void* in_data,
                                   void* out_data,
                                   size_t num,
                                   ccl::CCLDataType data_type,
                                   const ccl::CCLComm& ccl_comm,
                                   const stream::Stream& stream) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::CCLReduceScatter(void* in_data,
                                       void* out_data,
                                       size_t num,
                                       ccl::CCLDataType data_type,
                                       ccl::CCLReduceOp op,
                                       const ccl::CCLComm& ccl_comm,
                                       const stream::Stream& stream) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::CCLGroupStart() { INTERFACE_UNIMPLEMENT; }

void DeviceInterface::CCLGroupEnd() { INTERFACE_UNIMPLEMENT; }

void DeviceInterface::CCLSend(void* sendbuf,
                              size_t num,
                              ccl::CCLDataType data_type,
                              size_t dst_rank,
                              const ccl::CCLComm& ccl_comm,
                              const stream::Stream& stream) {
  INTERFACE_UNIMPLEMENT;
}

void DeviceInterface::CCLRecv(void* recvbuf,
                              size_t num,
                              ccl::CCLDataType data_type,
                              size_t src_rank,
                              const ccl::CCLComm& ccl_comm,
                              const stream::Stream& stream) {
  INTERFACE_UNIMPLEMENT;
}

359 360 361 362 363 364 365 366 367 368 369 370
// blas
void DeviceInterface::BlasAXPBY(size_t dev_id,
                                const stream::Stream& stream,
                                paddle::experimental::DataType dtype,
                                size_t numel,
                                float alpha,
                                void* x,
                                float beta,
                                void* y) {
  INTERFACE_UNIMPLEMENT;
}

371 372
#undef INTERFACE_UNIMPLEMENT

373
}  // namespace phi