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

Q
QI JUN 已提交
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. */
#pragma once

W
Wilber 已提交
15
#include <functional>
16
#include <future>  // NOLINT
D
dzhwinter 已提交
17
#include <memory>
Y
yuyang18 已提交
18
#include <mutex>  // NOLINT
19
#include <string>
D
dzhwinter 已提交
20
#include <unordered_map>
21
#include <utility>
22
#include <vector>
W
wanghuancoder 已提交
23

24
#include "paddle/fluid/memory/malloc.h"
W
Wilber 已提交
25
#include "paddle/fluid/platform/device/gpu/gpu_types.h"
26
#include "paddle/phi/backends/context_pool.h"
27
#include "paddle/phi/backends/cpu/cpu_context.h"
28
#include "paddle/phi/backends/custom/custom_context.h"
29 30
#include "paddle/phi/backends/gpu/gpu_decls.h"
#include "paddle/phi/core/device_context.h"
31
#ifdef PADDLE_WITH_CUDA
32
#include "paddle/fluid/platform/device/gpu/gpu_helper.h"
Y
Yi Wang 已提交
33
#include "paddle/fluid/platform/dynload/cublas.h"
34
#include "paddle/fluid/platform/dynload/cublasLt.h"
Y
Yi Wang 已提交
35
#include "paddle/fluid/platform/dynload/cudnn.h"
G
Guo Sheng 已提交
36
#include "paddle/fluid/platform/dynload/cusolver.h"
37
#include "paddle/fluid/platform/dynload/cusparse.h"
38
#include "paddle/phi/backends/gpu/gpu_context.h"
39
#if !defined(__APPLE__) && defined(PADDLE_WITH_NCCL)
W
Wu Yi 已提交
40
#include "paddle/fluid/platform/dynload/nccl.h"
W
Wu Yi 已提交
41
#endif
42
#include "paddle/fluid/platform/device/gpu/gpu_info.h"
Q
QI JUN 已提交
43
#endif
D
dzhwinter 已提交
44

45
#ifdef PADDLE_WITH_HIP
46
#include "paddle/fluid/platform/device/gpu/gpu_helper.h"  // NOLINT
47 48
#include "paddle/fluid/platform/dynload/miopen.h"
#include "paddle/fluid/platform/dynload/rocblas.h"
49
#include "paddle/phi/backends/gpu/gpu_context.h"  // NOLINT
50 51 52
#if !defined(__APPLE__) && defined(PADDLE_WITH_RCCL)
#include "paddle/fluid/platform/dynload/rccl.h"
#endif
53
#include "paddle/fluid/platform/device/gpu/gpu_info.h"  // NOLINT
54 55
#endif

56 57 58 59
#if defined(PADDLE_WITH_XPU_BKCL)
#include "xpu/bkcl.h"
#endif

T
tensor-tang 已提交
60
#ifdef PADDLE_WITH_MKLDNN
61
#include "dnnl.hpp"  // NOLINT
62
#include "paddle/fluid/framework/data_layout.h"
63
#include "paddle/phi/backends/onednn/onednn_context.h"
T
tensor-tang 已提交
64 65
#endif

66
#include <map>
W
wanghuancoder 已提交
67

68
#include "glog/logging.h"
Y
Yi Wang 已提交
69 70
#include "paddle/fluid/platform/enforce.h"
#include "paddle/fluid/platform/place.h"
71

72 73
#include "paddle/phi/backends/device_ext.h"
#include "paddle/phi/backends/stream.h"
74 75

#if !defined(PADDLE_WITH_XPU_KP) || defined(__xpu_on_host__)
Q
qijun 已提交
76
#include "unsupported/Eigen/CXX11/Tensor"
77
#endif
Q
QI JUN 已提交
78

W
wanghuancoder 已提交
79 80 81 82 83
namespace Eigen {
struct DefaultDevice;
struct GpuDevice;
}  // namespace Eigen

84
#ifdef PADDLE_WITH_XPU
85 86
#include "paddle/fluid/platform/device/xpu/xpu_header.h"
#include "paddle/fluid/platform/device/xpu/xpu_info.h"
87
#include "paddle/phi/backends/xpu/xpu_context.h"
88 89
#endif

Q
QI JUN 已提交
90 91 92
namespace paddle {
namespace platform {

93 94 95
enum DeviceType {
  CPU = 0,
  CUDA = 1,
96 97
  NPU = 2,
  XPU = 3,
J
jianghaicheng 已提交
98
  IPU = 4,
99
  CUSTOM_DEVICE = 6,
F
fwenguang 已提交
100

101
  MAX_DEVICE_TYPES = 7,
102 103
};

104 105
DeviceType Place2DeviceType(const platform::Place& place);

106 107 108
constexpr DeviceType kCPU = DeviceType::CPU;
constexpr DeviceType kCUDA = DeviceType::CUDA;
constexpr DeviceType kXPU = DeviceType::XPU;
J
jianghaicheng 已提交
109
constexpr DeviceType kIPU = DeviceType::IPU;
110
constexpr DeviceType kCUSTOM_DEVICE = DeviceType::CUSTOM_DEVICE;
111

112
using DeviceContext = phi::DeviceContext;
Q
QI JUN 已提交
113

J
jianghaicheng 已提交
114 115
// Graphcore IPU
#ifdef PADDLE_WITH_IPU
116 117 118
class IPUDeviceContext
    : public DeviceContext,
      public phi::TypeInfoTraits<DeviceContext, IPUDeviceContext> {
J
jianghaicheng 已提交
119 120 121 122 123
 public:
  IPUDeviceContext() = delete;
  explicit IPUDeviceContext(IPUPlace place);
  virtual ~IPUDeviceContext();
  Eigen::DefaultDevice* eigen_device() const { return nullptr; }
W
Wilber 已提交
124
  const Place& GetPlace() const override;
J
jianghaicheng 已提交
125 126 127
  /*! \brief  Wait for all operations completion in the stream. */
  void Wait() const override;

128 129
  static const char* name() { return "IPUDeviceContext"; }

J
jianghaicheng 已提交
130 131 132
 private:
  IPUPlace place_;
};
F
fwenguang 已提交
133
#endif
J
jianghaicheng 已提交
134

135
#ifdef PADDLE_WITH_XPU
Q
QingshuChen 已提交
136
namespace xpu = baidu::xpu::api;
137
using XPUDeviceContext = phi::XPUContext;
138 139
#endif

140
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
141
using CUDAPinnedDeviceContext = phi::GPUPinnedContext;
Q
QI JUN 已提交
142
#endif
Q
qijun 已提交
143

144
#ifdef PADDLE_WITH_CUSTOM_DEVICE
145
using CustomDeviceContext = phi::CustomContext;
146 147
#endif

148
void EmplaceDeviceContexts(
149 150
    std::map<Place, std::shared_future<std::unique_ptr<DeviceContext>>>*
        place_to_device_context,
151
    const std::vector<phi::Place>& places,
152 153
    bool disable_setting_default_stream_for_allocator,
    int stream_priority);
154

155
using DeviceContextPool = phi::DeviceContextPool;
156

157 158
}  // namespace platform
}  // namespace paddle
159

160
namespace phi {
D
dzhwinter 已提交
161

162 163 164 165 166 167
#ifdef PADDLE_WITH_IPU
template <>
struct DefaultDeviceContextType<phi::IPUPlace> {
  using TYPE = paddle::platform::IPUDeviceContext;
};
#endif
Y
Yang Yu 已提交
168

169 170 171 172
#if defined(PADDLE_WITH_CUDA) || defined(PADDLE_WITH_HIP)
template <>
struct DefaultDeviceContextType<phi::GPUPinnedPlace> {
  using TYPE = paddle::platform::CUDAPinnedDeviceContext;
D
dzhwinter 已提交
173
};
174
#endif
D
dzhwinter 已提交
175

176
}  //  namespace phi