pytorch  1.8.2
About: PyTorch provides Tensor computation (like NumPy) with strong GPU acceleration and Deep Neural Networks (in Python) built on a tape-based autograd system. LTS (Long Term Support) release.
  Fossies Dox: pytorch-1.8.2.tar.gz  ("unofficial" and yet experimental doxygen-generated source code documentation)  

context_base.h
Go to the documentation of this file.
1#pragma once
2
3#include <array>
4#include <cstdlib>
5#include <ctime>
6#include <memory>
7#include <unordered_map>
8
9#include <c10/macros/Macros.h>
10#include <c10/core/Allocator.h>
11#include <c10/util/typeid.h>
12#include <c10/util/Exception.h>
13#include <c10/util/Registry.h>
14#include <c10/core/CopyBytes.h>
15
16#include "caffe2/core/common.h"
17#include "caffe2/core/logging.h"
19
20namespace caffe2 {
21class Event;
22
23} // namespace caffe2
24namespace at {
25
26class BaseContext;
27
28/**
29 * Virtual interface for the Context class in Caffe2.
30 *
31 * A Context defines all the necessities to run an operator on a specific
32 * device. Specific Context classes needs to implement all the pure virtual
33 * functions in the BaseContext class.
34 * TODO: add docs after this is finalized.
35 */
37 public:
38 virtual ~BaseContext() noexcept {}
39
40 virtual Device device() const = 0;
41
42 /* Sorry for the naming, will get rid of this in future diff */
43 virtual DeviceType device_type() const = 0;
44
45 virtual void SwitchToDevice(int /*stream_id*/) = 0;
46
47 inline void SwitchToDevice() {
48 SwitchToDevice(0);
49 }
50
51 virtual void WaitEvent(const caffe2::Event& ev) = 0;
52
53 virtual void Record(caffe2::Event* ev, const char* err_msg = nullptr)
54 const = 0;
55
56 virtual void FinishDeviceComputation() = 0;
57
58 // This used to be arbitrary cross-device copy, but it turns out everyone
59 // did direct CPU-X copy, so we just make three functions for it (to avoid
60 // double dispatch). This will get obsoleted by C10. where copies
61 // will be proper operators (and get to rely on multiple dispatch there.)
62 virtual void CopyBytesSameDevice(
63 size_t nbytes,
64 const void* src,
65 void* dst) = 0;
66
67 virtual void CopyBytesFromCPU(size_t nbytes, const void* src, void* dst) = 0;
68
69 virtual void CopyBytesToCPU(size_t nbytes, const void* src, void* dst) = 0;
70
71 template <typename T>
72 inline void CopySameDevice(size_t n, const T* src, T* dst) {
73 static_assert(
75 "CopySameDevice requires fundamental types");
76 CopyBytesSameDevice(
77 n * sizeof(T), static_cast<const void*>(src), static_cast<void*>(dst));
78 }
79
80 template <typename T>
81 inline void CopyFromCPU(size_t n, const T* src, T* dst) {
82 static_assert(
84 "CopyFromCPU requires fundamental types");
85 CopyBytesFromCPU(
86 n * sizeof(T), static_cast<const void*>(src), static_cast<void*>(dst));
87 }
88
89 template <typename T>
90 inline void CopyToCPU(size_t n, const T* src, T* dst) {
91 static_assert(
92 c10::guts::is_fundamental<T>::value, "CopyToCPU requires fundamental types");
93 CopyBytesToCPU(
94 n * sizeof(T), static_cast<const void*>(src), static_cast<void*>(dst));
95 }
96
97 virtual bool SupportsNonFundamentalTypes() const {
98 return false;
99 }
100
101 inline void EnforceMetaCopyOK() {
103 SupportsNonFundamentalTypes(), "Context requires fundamental types");
104 }
105
107 const caffe2::TypeMeta meta,
108 size_t n,
109 const void* src,
110 void* dst) {
111 if (meta.copy()) {
112 EnforceMetaCopyOK();
113 meta.copy()(src, dst, n);
114 } else {
115 CopyBytesSameDevice(n * meta.itemsize(), src, dst);
116 }
117 }
118
120 const caffe2::TypeMeta meta,
121 size_t n,
122 const void* src,
123 void* dst) {
124 if (meta.copy()) {
125 EnforceMetaCopyOK();
126 meta.copy()(src, dst, n);
127 } else {
128 CopyBytesFromCPU(n * meta.itemsize(), src, dst);
129 }
130 }
131
133 const caffe2::TypeMeta meta,
134 size_t n,
135 const void* src,
136 void* dst) {
137 if (meta.copy()) {
138 EnforceMetaCopyOK();
139 meta.copy()(src, dst, n);
140 } else {
141 CopyBytesToCPU(n * meta.itemsize(), src, dst);
142 }
143 }
144};
145
146// Context constructor registry
148 ContextRegistry,
151 std::unique_ptr,
152 at::Device);
153
154#define REGISTER_CONTEXT(type, ...) \
155 C10_REGISTER_TYPED_CLASS(ContextRegistry, type, __VA_ARGS__)
156
157inline std::unique_ptr<at::BaseContext> CreateContext(
158 const at::Device& device) {
159 return at::ContextRegistry()->Create(device.type(), device);
160}
161
162} // namespace at
163
164namespace caffe2 {
165
166using at::BaseContext;
168} // namespace caffe2
#define AT_ASSERTM(cond,...)
Definition: Exception.h:481
#define TORCH_API
Definition: Export.h:98
Virtual interface for the Context class in Caffe2.
Definition: context_base.h:36
void CopySameDevice(size_t n, const T *src, T *dst)
Definition: context_base.h:72
virtual void SwitchToDevice(int)=0
void CopyItemsFromCPU(const caffe2::TypeMeta meta, size_t n, const void *src, void *dst)
Definition: context_base.h:119
virtual void WaitEvent(const caffe2::Event &ev)=0
virtual void FinishDeviceComputation()=0
virtual Device device() const =0
virtual void CopyBytesFromCPU(size_t nbytes, const void *src, void *dst)=0
virtual bool SupportsNonFundamentalTypes() const
Definition: context_base.h:97
void EnforceMetaCopyOK()
Definition: context_base.h:101
virtual void CopyBytesSameDevice(size_t nbytes, const void *src, void *dst)=0
void SwitchToDevice()
Definition: context_base.h:47
virtual ~BaseContext() noexcept
Definition: context_base.h:38
void CopyItemsSameDevice(const caffe2::TypeMeta meta, size_t n, const void *src, void *dst)
Definition: context_base.h:106
void CopyToCPU(size_t n, const T *src, T *dst)
Definition: context_base.h:90
virtual DeviceType device_type() const =0
virtual void Record(caffe2::Event *ev, const char *err_msg=nullptr) const =0
void CopyFromCPU(size_t n, const T *src, T *dst)
Definition: context_base.h:81
void CopyItemsToCPU(const caffe2::TypeMeta meta, size_t n, const void *src, void *dst)
Definition: context_base.h:132
virtual void CopyBytesToCPU(size_t nbytes, const void *src, void *dst)=0
TypeMeta is a thin class that allows us to store the type of a container such as a blob,...
Definition: typeid.h:325
size_t itemsize() const noexcept
Returns the size of the item.
Definition: typeid.h:384
Copy * copy() const noexcept
Returns the typed copy function pointer for individual iterms.
Definition: typeid.h:405
Distributions kernel adapted from THRandom.cpp The kernels try to follow std::random distributions si...
C10_DECLARE_TYPED_REGISTRY(ContextRegistry, at::DeviceType, at::BaseContext, std::unique_ptr, at::Device)
std::unique_ptr< at::BaseContext > CreateContext(const at::Device &device)
Definition: context_base.h:157
DeviceType
Definition: DeviceType.h:15
Copyright (c) 2016-present, Facebook, Inc.
Definition: blob.h:13
float T
Definition: cc_bmm_bg_op.h:11
The common world dst
The common world src
'torch.classes.cuda.Event' Event(bool enable_timing=False, bool blocking=False, bool interprocess=False)
Definition: cuda.py:168
Represents a a compute device on which a tensor is located.
Definition: Device.h:30
A backend-generic movable, not copyable, not thread-safe event.
Definition: Event.h:40
is_fundamental<T> is true_type iff the lambda type T is a fundamental type (that is,...
Definition: TypeTraits.h:131