Skip to content

Commit b89a3b5

Browse files
jerryzh168facebook-github-bot
authored andcommitted
Remove StaticContext (pytorch#12547)
Summary: Pull Request resolved: pytorch#12547 Pull Request resolved: pytorch#12305 Remove StaticContext from context_base.h Reviewed By: dzhulgakov Differential Revision: D10073519 fbshipit-source-id: 350beec3c54365edef338318ce58229ccb825a98
1 parent c32839f commit b89a3b5

16 files changed

+7
-199
lines changed

aten/src/ATen/core/Allocator.h

+1
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ struct Allocator {
121121
}
122122
};
123123

124+
// Question: is this still needed?
124125
struct CAFFE2_API InefficientStdFunctionContext {
125126
std::unique_ptr<void, std::function<void(void*)>> ptr_;
126127
InefficientStdFunctionContext(

aten/src/ATen/core/TensorImpl.h

+4-16
Original file line numberDiff line numberDiff line change
@@ -411,17 +411,6 @@ struct CAFFE2_API TensorImpl : public c10::intrusive_ptr_target {
411411
return storage_.device();
412412
}
413413

414-
/**
415-
* The static context of a tensor intuitively represents the device
416-
* type of a tensor; e.g., a CPU tensor is associated with the
417-
* GetCPUStaticContext(). This method replaces the former Context template
418-
* parameter which was previously used to identify the device type
419-
* of a tensor.
420-
*/
421-
at::BaseStaticContext* GetStaticContext() const {
422-
return ::caffe2::get_static_context(device_type());
423-
}
424-
425414
/**
426415
* @brief Copies the data from a source tensor, with a contex provided to
427416
* carry out the underlying memcpy operation. This method respects
@@ -761,20 +750,19 @@ struct CAFFE2_API TensorImpl : public c10::intrusive_ptr_target {
761750
return storage_.data();
762751
}
763752
const at::Allocator* allocator = storage_.allocator();
764-
// TODO: Get rid of StaticContext
765753
AT_ENFORCE(
766754
allocator == nullptr,
767-
"Allocator is not used within Caffe2 functions, please use StaticContext instead.");
755+
"Allocator in storage_ is not used within Caffe2 functions. \
756+
we are using global function to get the allocator based on device \
757+
type.");
768758
allocator = caffe2::GetAllocator(storage_.device_type());
769759
if (meta.ctor()) {
770760
// For types that need placement new, we will call it, as well as
771761
// making sure that when the data is freed, it calls the right
772762
// destruction procedure.
773763
auto size = numel_;
774764
auto dtor = data_type_.dtor();
775-
auto data_ptr = allocator->allocate(
776-
numel_ * storage_.itemsize()); // Removing this can get rid of
777-
// InefficientStdFunctionContext
765+
auto data_ptr = allocator->allocate(numel_ * storage_.itemsize());
778766
storage_.set_data_ptr(PlacementDeleteContext::makeDataPtr(
779767
std::move(data_ptr), dtor, size, storage_.device()));
780768
data_type_.ctor()(storage_.data(), numel_);

aten/src/ATen/core/context_base.cpp

-15
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,5 @@ C10_DEFINE_TYPED_REGISTRY(
1414
namespace caffe2 {
1515

1616
// TODO: rename context.h -> context_cpu.h & context_base.h -> context.h
17-
StaticContextMap& GetStaticContexts() {
18-
static StaticContextMap static_contexts;
19-
return static_contexts;
20-
}
21-
22-
void set_static_context(at::DeviceType t, BaseStaticContext* ptr) {
23-
auto& static_contexts = GetStaticContexts();
24-
static_contexts[t] = ptr;
25-
}
26-
27-
BaseStaticContext* get_static_context(at::DeviceType t) {
28-
auto* ptr = GetStaticContexts()[t];
29-
AT_ASSERTM(ptr, "StaticContext for ", t, " is not registered yet.");
30-
return ptr;
31-
}
3217

3318
} // namespace caffe2

aten/src/ATen/core/context_base.h

-32
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,12 @@
1414

1515
namespace caffe2 {
1616
class Event;
17-
class DeviceOption;
1817

1918
} // namespace caffe2
2019
namespace at {
2120

2221
class BaseContext;
2322

24-
/* BaseStaticContext defines the interface for static context, which contains
25-
functions that are invoked statically before in Tensor class, e.g. New,
26-
We will merge this with Allocator later.
27-
*/
28-
class CAFFE2_API BaseStaticContext {
29-
public:
30-
virtual ~BaseStaticContext() noexcept {}
31-
32-
virtual DeviceType GetDeviceType() = 0;
33-
};
34-
3523
/**
3624
* Virtual interface for the Context class in Caffe2.
3725
*
@@ -44,8 +32,6 @@ class CAFFE2_API BaseContext {
4432
public:
4533
virtual ~BaseContext() noexcept {}
4634

47-
virtual BaseStaticContext* GetStaticContext() const = 0;
48-
4935
virtual Device device() const = 0;
5036

5137
/* Sorry for the naming, will get rid of this in future diff */
@@ -192,23 +178,5 @@ inline std::unique_ptr<at::BaseContext> CreateContext(
192178
namespace caffe2 {
193179

194180
using at::BaseContext;
195-
using at::BaseStaticContext;
196-
197-
using StaticContextMap = std::unordered_map<at::DeviceType, BaseStaticContext*>;
198-
CAFFE2_API StaticContextMap& GetStaticContexts();
199-
CAFFE2_API void set_static_context(at::DeviceType t, BaseStaticContext* ptr);
200-
CAFFE2_API BaseStaticContext* get_static_context(at::DeviceType t);
201-
202-
template <at::DeviceType t>
203-
struct StaticContextFunctionRegisterer {
204-
explicit StaticContextFunctionRegisterer(BaseStaticContext* ptr) {
205-
set_static_context(t, ptr);
206-
}
207-
};
208-
209-
#define REGISTER_STATIC_CONTEXT(t, f) \
210-
namespace { \
211-
static StaticContextFunctionRegisterer<t> g_static_context_##d(f); \
212-
}
213181

214182
} // namespace caffe2

caffe2/core/context.cc

-7
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,4 @@ uint32_t RandomNumberSeed() {
2828
kPrime2 * tv_sec + kPrime3 * tv_usec;
2929
}
3030

31-
BaseStaticContext* GetCPUStaticContext() {
32-
static CPUStaticContext context;
33-
return &context;
34-
}
35-
36-
REGISTER_STATIC_CONTEXT(CPU, GetCPUStaticContext());
37-
3831
} // namespace caffe2

caffe2/core/context.h

-20
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ C10_DECLARE_bool(caffe2_report_cpu_memory_usage);
2020

2121
namespace caffe2 {
2222

23-
CAFFE2_API BaseStaticContext* GetCPUStaticContext();
24-
2523
/**
2624
* A function to generate a random number seed that is unique in a best-effort
2725
* basis, using an ever-incrementing seed and the current time.
@@ -55,14 +53,6 @@ class CAFFE2_API CPUContext final : public BaseContext {
5553

5654
~CPUContext() noexcept override {}
5755

58-
BaseStaticContext* GetStaticContext() const override {
59-
return GetCPUStaticContext();
60-
}
61-
62-
static BaseStaticContext* StaticContext() {
63-
return GetCPUStaticContext();
64-
}
65-
6656
inline void SwitchToDevice(int /*stream_id*/) override {}
6757

6858
using BaseContext::SwitchToDevice;
@@ -187,16 +177,6 @@ inline void CPUContext::CopyBytes<CPUContext, CPUContext>(
187177
memcpy(dst, src, nbytes);
188178
}
189179

190-
// TODO(jerryzh): merge CPUStaticContext with Allocator
191-
class CAFFE2_API CPUStaticContext : public BaseStaticContext {
192-
public:
193-
194-
DeviceType GetDeviceType() override {
195-
return CPU;
196-
}
197-
198-
};
199-
200180
} // namespace caffe2
201181

202182
#endif // CAFFE2_CORE_CONTEXT_H_

caffe2/core/context_gpu.cu

-12
Original file line numberDiff line numberDiff line change
@@ -424,19 +424,7 @@ struct DefaultCUDAAllocator final : public at::Allocator {
424424
}
425425
};
426426

427-
at::Allocator* GetCUDAAllocator() {
428-
return GetAllocator(CUDA);
429-
}
430-
431427
static DefaultCUDAAllocator g_cuda_alloc;
432-
433428
REGISTER_ALLOCATOR(CUDA, &g_cuda_alloc);
434429

435-
BaseStaticContext* GetCUDAStaticContext() {
436-
static CUDAStaticContext context;
437-
return &context;
438-
}
439-
440-
REGISTER_STATIC_CONTEXT(CUDA, GetCUDAStaticContext());
441-
442430
} // namespace caffe2

caffe2/core/context_gpu.h

-17
Original file line numberDiff line numberDiff line change
@@ -135,8 +135,6 @@ class CAFFE2_CUDA_API ThreadLocalCUDAObjects {
135135
#endif // CAFFE2_USE_CUDNN
136136
};
137137

138-
CAFFE2_CUDA_API BaseStaticContext* GetCUDAStaticContext();
139-
140138
class CAFFE2_CUDA_API CUDAContext final : public BaseContext {
141139
public:
142140
// The default cuda context constructor.
@@ -152,14 +150,6 @@ class CAFFE2_CUDA_API CUDAContext final : public BaseContext {
152150
FinishDeviceComputation();
153151
}
154152

155-
BaseStaticContext* GetStaticContext() const override {
156-
return GetCUDAStaticContext();
157-
}
158-
159-
static BaseStaticContext* StaticContext() {
160-
return GetCUDAStaticContext();
161-
}
162-
163153
inline void SwitchToDevice(int stream_id) override {
164154
set_stream_id(stream_id);
165155
CaffeCudaSetDevice(gpu_id_);
@@ -389,13 +379,6 @@ struct CAFFE2_CUDA_API PinnedCPUAllocator final : public at::Allocator {
389379
DefaultCPUAllocator baseAllocator_;
390380
};
391381

392-
class CAFFE2_CUDA_API CUDAStaticContext final : public BaseStaticContext {
393-
public:
394-
DeviceType GetDeviceType() override {
395-
return CUDA;
396-
}
397-
};
398-
399382
using TensorCUDA = Tensor;
400383

401384
} // namespace caffe2

caffe2/core/hip/context_hip.cc

-7
Original file line numberDiff line numberDiff line change
@@ -430,11 +430,4 @@ struct DefaultHIPAllocator final : public at::Allocator {
430430
static DefaultHIPAllocator g_hip_alloc;
431431
REGISTER_ALLOCATOR(HIP, &g_hip_alloc);
432432

433-
BaseStaticContext* GetHIPStaticContext() {
434-
static HIPStaticContext context;
435-
return &context;
436-
}
437-
438-
REGISTER_STATIC_CONTEXT(HIP, GetHIPStaticContext());
439-
440433
} // namespace caffe2

caffe2/core/hip/context_hip.h

-17
Original file line numberDiff line numberDiff line change
@@ -120,8 +120,6 @@ class ThreadLocalHIPObjects {
120120
vector<miopenHandle_t> miopen_handles_[CAFFE2_COMPILE_TIME_MAX_HIP_GPUS];
121121
};
122122

123-
BaseStaticContext* GetHIPStaticContext();
124-
125123
class HIPContext final : public BaseContext {
126124
public:
127125
// The default HIP context constructor.
@@ -137,14 +135,6 @@ class HIPContext final : public BaseContext {
137135
FinishDeviceComputation();
138136
}
139137

140-
BaseStaticContext* GetStaticContext() const override {
141-
return GetHIPStaticContext();
142-
}
143-
144-
static BaseStaticContext* StaticContext() {
145-
return GetHIPStaticContext();
146-
}
147-
148138
inline void SwitchToDevice(int stream_id) override {
149139
set_stream_id(stream_id);
150140
CaffeHipSetDevice(gpu_id_);
@@ -378,13 +368,6 @@ struct PinnedCPUAllocator final : public at::Allocator {
378368
DefaultCPUAllocator baseAllocator_;
379369
};
380370

381-
class HIPStaticContext final : public BaseStaticContext {
382-
public:
383-
DeviceType GetDeviceType() override {
384-
return HIP;
385-
}
386-
};
387-
388371
typedef Tensor TensorHIP;
389372

390373
} // namespace caffe2

caffe2/core/tensor.h

-4
Original file line numberDiff line numberDiff line change
@@ -115,10 +115,6 @@ class CAFFE2_API Tensor final {
115115
return x;
116116
}
117117

118-
BaseStaticContext* GetStaticContext() const {
119-
return impl_.get()->GetStaticContext();
120-
}
121-
122118
DeviceType GetDeviceType() const {
123119
return impl_->device_type();
124120
}

caffe2/ideep/utils/ideep_context.h

-18
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
namespace caffe2 {
1010

11-
BaseStaticContext* GetIDEEPStaticContext();
12-
1311
class IDEEPContext final : public BaseContext {
1412
public:
1513
typedef std::mt19937 rand_gen_type;
@@ -25,14 +23,6 @@ class IDEEPContext final : public BaseContext {
2523

2624
~IDEEPContext() noexcept override {}
2725

28-
BaseStaticContext* GetStaticContext() const override {
29-
return GetIDEEPStaticContext();
30-
}
31-
32-
static BaseStaticContext* StaticContext() {
33-
return GetIDEEPStaticContext();
34-
}
35-
3626
inline void SwitchToDevice(int /*stream_id*/) {}
3727
using BaseContext::SwitchToDevice;
3828

@@ -178,12 +168,4 @@ inline void IDEEPContext::CopyBytes<IDEEPContext, CPUContext>(
178168
memcpy(dst, src, nbytes);
179169
}
180170

181-
class IDEEPStaticContext : public BaseStaticContext {
182-
public:
183-
DeviceType GetDeviceType() override {
184-
return IDEEP;
185-
}
186-
187-
};
188-
189171
} // namespace caffe2

caffe2/ideep/utils/ideep_register.cc

-7
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,4 @@ REGISTER_EVENT_ERROR_MESSAGE_FUNCTION(IDEEP, EventErrorMessageCPU);
3030
REGISTER_EVENT_SET_FINISHED_FUNCTION(IDEEP, EventSetFinishedCPU);
3131
REGISTER_EVENT_RESET_FUNCTION(IDEEP, EventResetCPU);
3232

33-
C10_EXPORT BaseStaticContext* GetIDEEPStaticContext() {
34-
static IDEEPStaticContext context;
35-
return &context;
36-
}
37-
38-
REGISTER_STATIC_CONTEXT(IDEEP, GetIDEEPStaticContext());
39-
4033
} // namespace caffe2

caffe2/mkl/utils/mkl_context.cc

-7
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,4 @@ REGISTER_EVENT_ERROR_MESSAGE_FUNCTION(MKLDNN, EventErrorMessageCPU);
2323
REGISTER_EVENT_SET_FINISHED_FUNCTION(MKLDNN, EventSetFinishedCPU);
2424
REGISTER_EVENT_RESET_FUNCTION(MKLDNN, EventResetCPU);
2525

26-
BaseStaticContext* GetMKLStaticContext() {
27-
static MKLStaticContext context;
28-
return &context;
29-
}
30-
31-
REGISTER_STATIC_CONTEXT(MKLDNN, GetMKLStaticContext());
32-
3326
} // namespace caffe2

0 commit comments

Comments
 (0)