device_context.cc 11.9 KB
Newer Older
1
/* Copyright (c) 2016 PaddlePaddle Authors. All Rights Reserved.
2 3
Copyright (c) 2022 NVIDIA Corporation. All rights reserved.

Q
qijun 已提交
4 5 6 7
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
8

Q
qijun 已提交
9 10 11 12 13
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

Y
Yi Wang 已提交
15
#include "paddle/fluid/platform/device_context.h"
16

W
Wilber 已提交
17
#include <functional>
18
#include <memory>
19
#include <set>
20

21 22
#include "glog/logging.h"
#include "paddle/fluid/platform/device/device_wrapper.h"
W
Wilber 已提交
23
#include "paddle/fluid/platform/place.h"
24 25
#include "paddle/fluid/platform/profiler.h"
#include "paddle/fluid/platform/profiler/event_tracing.h"
26
#include "paddle/phi/core/allocator.h"
27
#include "paddle/phi/core/expect.h"
28
#include "paddle/phi/core/generator.h"
29

30
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
31
#include "paddle/fluid/memory/allocation/cuda_device_context_allocator.h"
S
sneaxiy 已提交
32
#include "paddle/fluid/platform/cuda_device_guard.h"
33
#include "paddle/phi/backends/gpu/gpu_context.h"
34
#endif
35

F
fwenguang 已提交
36 37 38 39
#ifdef PADDLE_WITH_MLU
#include "paddle/fluid/platform/device/mlu/device_context.h"
#include "paddle/fluid/platform/device/mlu/device_context_allocator.h"
#endif
40

Q
qijun 已提交
41 42 43
namespace paddle {
namespace platform {

44 45 46 47 48 49 50
DeviceType Place2DeviceType(const platform::Place& place) {
  if (platform::is_cpu_place(place)) {
    return platform::DeviceType::CPU;
  } else if (platform::is_gpu_place(place)) {
    return platform::DeviceType::CUDA;
  } else if (platform::is_xpu_place(place)) {
    return platform::DeviceType::XPU;
51 52
  } else if (platform::is_ipu_place(place)) {
    return platform::DeviceType::IPU;
53 54
  } else if (platform::is_npu_place(place)) {
    return platform::DeviceType::NPU;
F
fwenguang 已提交
55 56
  } else if (platform::is_mlu_place(place)) {
    return platform::DeviceType::MLU;
57 58
  } else if (platform::is_custom_place(place)) {
    return platform::DeviceType::CUSTOM_DEVICE;
59 60 61 62 63 64
  } else {
    PADDLE_THROW(platform::errors::Unavailable(
        "Unsupported place %s to convert into platform::DeviceType.", place));
  }
}

65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
template <typename DevCtx>
typename std::enable_if<!std::is_same<DevCtx, phi::GPUContext>::value,
                        DevCtx*>::type
ConstructDevCtx(const phi::Place& p, /*unused*/ int stream_priority = 0) {
  return new DevCtx(p);
}

template <typename DevCtx>
typename std::enable_if<std::is_same<DevCtx, phi::GPUContext>::value,
                        DevCtx*>::type
ConstructDevCtx(const phi::Place& p, int stream_priority) {
  return new DevCtx(p, /*init=*/true, stream_priority);
}
#else
template <typename DevCtx>
DevCtx* ConstructDevCtx(const phi::Place& p,
                        /*unused*/ int stream_priority) {
  return new DevCtx(p);
}
#endif

template <typename DevCtx>
inline std::unique_ptr<DeviceContext> CreateDeviceContext(
    const phi::Place& p,
    bool disable_setting_default_stream_for_allocator,
    int stream_priority) {
  using PtrType = std::unique_ptr<DeviceContext>;

  DevCtx* dev_ctx = ConstructDevCtx<DevCtx>(p, stream_priority);
  auto& instance = paddle::memory::allocation::AllocatorFacade::Instance();
  if (p.GetType() == phi::AllocationType::GPU) {
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
    auto* cuda_ctx = dynamic_cast<phi::GPUContext*>(dev_ctx);
    PADDLE_ENFORCE_NOT_NULL(
        cuda_ctx,
        phi::errors::InvalidArgument(
            "Failed to dynamic_cast dev_ctx into phi::GPUContext."));

    if (!disable_setting_default_stream_for_allocator) {
      instance.SetDefaultStream(GPUPlace(p.GetDeviceId()), cuda_ctx->stream());
    }
    dev_ctx->SetAllocator(instance.GetAllocator(p, cuda_ctx->stream()).get());
    dev_ctx->SetPinnedAllocator(
        instance.GetAllocator(phi::GPUPinnedPlace()).get());

    cuda_ctx->PartialInitWithAllocator();
    dev_ctx->SetGenerator(phi::DefaultCUDAGenerator(p.GetDeviceId()).get());
#endif
  } else if (p.GetType() == phi::AllocationType::XPU) {
#if defined(PADDLE_WITH_XPU)
    dev_ctx->SetAllocator(instance.GetAllocator(p).get());
    dev_ctx->SetGenerator(phi::DefaultXPUGenerator(p.GetDeviceId()).get());
#endif
  } else {
    dev_ctx->SetAllocator(instance.GetAllocator(p).get());
    dev_ctx->SetGenerator(phi::DefaultCPUGenerator().get());
  }
  dev_ctx->SetHostGenerator(phi::DefaultCPUGenerator().get());
  dev_ctx->SetHostAllocator(instance.GetAllocator(phi::CPUPlace()).get());
  dev_ctx->SetZeroAllocator(instance.GetZeroAllocator(p).get());
  dev_ctx->SetHostZeroAllocator(
      instance.GetZeroAllocator(phi::CPUPlace()).get());
  return PtrType(dev_ctx);
}

template <typename DevCtx>
inline void EmplaceDeviceContext(
133 134
    std::map<Place, std::shared_future<std::unique_ptr<DeviceContext>>>*
        place_to_device_context,
135
    const phi::Place& place,
136 137
    bool disable_setting_default_stream_for_allocator,
    int stream_priority) {
138 139 140 141 142 143 144 145 146 147 148 149
  // lazy evaluation. i.e., only create device context at first `Get`
  place_to_device_context->emplace(
      place,
      std::async(std::launch::deferred,
                 CreateDeviceContext<DevCtx>,
                 place,
                 disable_setting_default_stream_for_allocator,
                 stream_priority));
}

void EmplaceDeviceContexts(
    std::map<Place, std::shared_future<std::unique_ptr<DeviceContext>>>*
150
        place_to_device_context,
151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
    const std::vector<phi::Place>& places,
    bool disable_setting_default_stream_for_allocator,
    int stream_priority) {
  PADDLE_ENFORCE_GT(
      places.size(),
      0,
      phi::errors::InvalidArgument("The number of platform places should "
                                   "be larger than 0. But received %d.",
                                   places.size()));
  std::set<Place> set;
  for (auto& p : places) {
    set.insert(p);
  }
  for (auto& place : set) {
    if (place.GetType() == phi::AllocationType::CPU) {
#ifdef PADDLE_WITH_MKLDNN
      EmplaceDeviceContext<phi::OneDNNContext>(
          place_to_device_context,
          place,
          disable_setting_default_stream_for_allocator,
          /*unused*/ stream_priority);
172
#else
173 174 175 176 177
      EmplaceDeviceContext<phi::CPUContext>(
          place_to_device_context,
          place,
          disable_setting_default_stream_for_allocator,
          /*unused*/ stream_priority);
F
fwenguang 已提交
178
#endif
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228
    } else if (place.GetType() == phi::AllocationType::GPU) {
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
      EmplaceDeviceContext<phi::GPUContext>(
          place_to_device_context,
          place,
          disable_setting_default_stream_for_allocator,
          stream_priority);
#else
      PADDLE_THROW(
          phi::errors::Unimplemented("GPUPlace is not supported. Please "
                                     "re-compile with WITH_GPU option."));
#endif
    } else if (place.GetType() == phi::AllocationType::XPU) {
#ifdef PADDLE_WITH_XPU
      EmplaceDeviceContext<phi::XPUContext>(
          place_to_device_context,
          place,
          disable_setting_default_stream_for_allocator,
          /*unused*/ stream_priority);
#else
      PADDLE_THROW(
          phi::errors::Unimplemented("XPUPlace is not supported. Please "
                                     "re-compile with WITH_XPU option."));
#endif
    } else if (place.GetType() == phi::AllocationType::CUSTOM) {
#ifdef PADDLE_WITH_CUSTOM_DEVICE
      EmplaceDeviceContext<phi::CustomContext>(
          place_to_device_context,
          place,
          disable_setting_default_stream_for_allocator,
          /*unused*/ stream_priority);
#else
      PADDLE_THROW(phi::errors::Unimplemented(
          "CustomPlace is not supported. Please re-compile with "
          "WITH_CUSTOM_DEVICE "
          "option."));
#endif
    } else if (platform::is_cuda_pinned_place(place)) {
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
      EmplaceDeviceContext<CUDAPinnedDeviceContext>(
          place_to_device_context,
          place,
          disable_setting_default_stream_for_allocator,
          /*unused*/ stream_priority);
#else
      PADDLE_THROW(platform::errors::Unimplemented(
          "CUDAPlace is not supported. Please re-compile with WITH_GPU "
          "option."));
#endif
    } else if (platform::is_mlu_place(place)) {
F
fwenguang 已提交
229
#ifdef PADDLE_WITH_MLU
230 231 232 233 234
      EmplaceDeviceContext<MLUDeviceContext>(
          place_to_device_context,
          place,
          disable_setting_default_stream_for_allocator,
          /*unused*/ stream_priority);
F
fwenguang 已提交
235
#else
236 237 238
      PADDLE_THROW(
          platform::errors::Unimplemented("MLUPlace is not supported. Please "
                                          "re-compile with WITH_MLU option."));
J
jianghaicheng 已提交
239
#endif
240
    } else if (platform::is_ipu_place(place)) {
J
jianghaicheng 已提交
241
#ifdef PADDLE_WITH_IPU
242 243 244 245 246
      EmplaceDeviceContext<IPUDeviceContext>(
          place_to_device_context,
          place,
          disable_setting_default_stream_for_allocator,
          /*unused*/ stream_priority);
J
jianghaicheng 已提交
247
#else
248 249 250
      PADDLE_THROW(
          platform::errors::Unimplemented("IPUPlace is not supported. Please "
                                          "re-compile with WITH_IPU option."));
251
#endif
252
    } else if (platform::is_npu_place(place)) {
253
#ifdef PADDLE_WITH_ASCEND_CL
254 255 256 257 258
      EmplaceDeviceContext<NPUDeviceContext>(
          place_to_device_context,
          place,
          disable_setting_default_stream_for_allocator,
          /*unused*/ stream_priority);
259
#else
260 261 262
      PADDLE_THROW(platform::errors::Unimplemented(
          "NPUPlace is not supported. Please "
          "re-compile with WITH_ASCEND_CL option."));
263
#endif
264
    } else if (platform::is_npu_pinned_place(place)) {
265
#ifdef PADDLE_WITH_ASCEND_CL
266 267 268 269 270
      EmplaceDeviceContext<NPUPinnedDeviceContext>(
          place_to_device_context,
          place,
          disable_setting_default_stream_for_allocator,
          /*unused*/ stream_priority);
271
#else
272 273 274 275
      PADDLE_THROW(platform::errors::Unimplemented(
          "NPUPinnedPlace is not supported. Please re-compile with "
          "WITH_ASCEND_CL "
          "option."));
D
dzhwinter 已提交
276
#endif
277
    }
D
dzhwinter 已提交
278 279 280
  }
}

J
jianghaicheng 已提交
281
#ifdef PADDLE_WITH_IPU
A
Allen Guo 已提交
282
IPUDeviceContext::IPUDeviceContext(IPUPlace place) : place_(place) {}
J
jianghaicheng 已提交
283

W
Wilber 已提交
284
const Place& IPUDeviceContext::GetPlace() const { return place_; }
A
Allen Guo 已提交
285

J
jianghaicheng 已提交
286 287 288 289 290 291
void IPUDeviceContext::Wait() const {
  /*! \brief  Wait for all operations completion in the stream. */
}

IPUDeviceContext::~IPUDeviceContext() {}

292 293
#endif

294 295 296 297 298 299 300
#ifdef PADDLE_WITH_ASCEND_CL
NPUDeviceContext::NPUDeviceContext(NPUPlace place) : place_(place) {
  NPUDeviceGuard guard(place_.device);
  // PADDLE_ENFORCE_NPU_SUCCESS(aclrtCreateContext(&context_, place_.device));
  // NOTE(zhiqiu): Usually, no need to create context explicitly,
  // ACL creates a default context which contains 1 default stream
  // and 1 sync strean after aclrtSetDevice.
301
  platform::GetCurrentNPUContext(&context_);
302 303 304 305 306 307 308
  stream_.reset(new stream::NPUStream(place));
}

NPUDeviceContext::~NPUDeviceContext() {
  // NPUDeviceGuard guard(place_.device);
  // PADDLE_ENFORCE_NPU_SUCCESS(aclrtDestroyContext(context_));
}
309

310
void NPUDeviceContext::Wait() const {
311 312
  platform::RecordEvent record_event(
      "NPUDeviceContext/wait", platform::TracerEventType::UserDefined, 2);
313 314
  VLOG(4) << "NPU context(" << this << ")  Wait";
  stream_->Wait();
315 316 317 318
}

aclrtStream NPUDeviceContext::stream() const { return stream_->raw_stream(); }

W
Wilber 已提交
319
const Place& NPUDeviceContext::GetPlace() const { return place_; }
320 321

aclrtContext NPUDeviceContext::context() const { return context_; }
322 323 324 325 326 327 328 329 330 331 332 333 334 335

NPUPinnedDeviceContext::NPUPinnedDeviceContext() {
  eigen_device_.reset(new Eigen::DefaultDevice());
}

NPUPinnedDeviceContext::NPUPinnedDeviceContext(NPUPinnedPlace place)
    : place_(place) {
  eigen_device_.reset(new Eigen::DefaultDevice());
}

Eigen::DefaultDevice* NPUPinnedDeviceContext::eigen_device() const {
  return eigen_device_.get();
}

W
Wilber 已提交
336
const Place& NPUPinnedDeviceContext::GetPlace() const { return place_; }
337

338 339
#endif

Q
qijun 已提交
340
}  // namespace platform
Q
qijun 已提交
341
}  // namespace paddle