1 #include <c10/core/SymIntArrayRef.h>
2 #include <c10/core/TensorImpl.h>
3 #include <c10/core/impl/PyInterpreter.h>
4
5 namespace c10::impl {
6
7 struct NoopPyInterpreterVTable final : public PyInterpreterVTable {
namec10::impl::NoopPyInterpreterVTable8 std::string name() const override {
9 return "<unloaded interpreter>";
10 }
11
increfc10::impl::NoopPyInterpreterVTable12 void incref(PyObject* pyobj) const override {} // do nothing
13
decrefc10::impl::NoopPyInterpreterVTable14 void decref(PyObject* pyobj, bool has_pyobj_slot) const override {
15 } // do nothing
16
17 #define PANIC(m) \
18 TORCH_INTERNAL_ASSERT( \
19 0, \
20 "attempted to call " #m \
21 " on a Tensor with nontrivial PyObject after corresponding interpreter died")
22
detachc10::impl::NoopPyInterpreterVTable23 c10::intrusive_ptr<TensorImpl> detach(const TensorImpl* self) const override {
24 PANIC(detach);
25 }
26
dispatchc10::impl::NoopPyInterpreterVTable27 void dispatch(const c10::OperatorHandle& op, torch::jit::Stack* stack)
28 const override {
29 PANIC(dispatch);
30 }
31
reportErrorCallbackc10::impl::NoopPyInterpreterVTable32 void reportErrorCallback(PyObject* callback, DispatchKey key) const override {
33 PANIC(reportErrorCallback);
34 }
35
python_op_registration_trampolinec10::impl::NoopPyInterpreterVTable36 void python_op_registration_trampoline(
37 const c10::OperatorHandle& op,
38 c10::DispatchKey,
39 c10::DispatchKeySet keyset,
40 torch::jit::Stack* stack,
41 bool with_keyset,
42 bool with_op) const override {
43 PANIC(python_op_registration_trampoline);
44 }
45
throw_abstract_impl_not_imported_errorc10::impl::NoopPyInterpreterVTable46 void throw_abstract_impl_not_imported_error(
47 std::string opname,
48 const char* pymodule,
49 const char* context) const override {
50 PANIC(throw_abstract_impl_not_imported_error);
51 }
52
python_dispatcherc10::impl::NoopPyInterpreterVTable53 void python_dispatcher(
54 const c10::OperatorHandle& op,
55 c10::DispatchKeySet,
56 torch::jit::Stack* stack) const override {
57 PANIC(python_dispatcher);
58 }
59
is_contiguousc10::impl::NoopPyInterpreterVTable60 bool is_contiguous(const TensorImpl* self, at::MemoryFormat) const override {
61 PANIC(is_contiguous);
62 }
is_strides_likec10::impl::NoopPyInterpreterVTable63 bool is_strides_like(const TensorImpl* self, at::MemoryFormat)
64 const override {
65 PANIC(is_strides_like);
66 }
is_non_overlapping_and_densec10::impl::NoopPyInterpreterVTable67 bool is_non_overlapping_and_dense(const TensorImpl* self) const override {
68 PANIC(is_non_overlapping_and_dense);
69 }
devicec10::impl::NoopPyInterpreterVTable70 c10::Device device(const TensorImpl* self) const override {
71 PANIC(device);
72 }
dimc10::impl::NoopPyInterpreterVTable73 int64_t dim(const TensorImpl* self) const override {
74 PANIC(dim);
75 }
stridesc10::impl::NoopPyInterpreterVTable76 c10::IntArrayRef strides(const TensorImpl* self) const override {
77 PANIC(strides);
78 }
sizesc10::impl::NoopPyInterpreterVTable79 c10::IntArrayRef sizes(const TensorImpl* self) const override {
80 PANIC(sizes);
81 }
sym_sizesc10::impl::NoopPyInterpreterVTable82 c10::SymIntArrayRef sym_sizes(const TensorImpl* self) const override {
83 PANIC(sym_sizes);
84 }
layoutc10::impl::NoopPyInterpreterVTable85 c10::Layout layout(const TensorImpl* self) const override {
86 PANIC(layout);
87 }
numelc10::impl::NoopPyInterpreterVTable88 int64_t numel(const TensorImpl* self) const override {
89 PANIC(numel);
90 }
sym_numelc10::impl::NoopPyInterpreterVTable91 c10::SymInt sym_numel(const TensorImpl* self) const override {
92 PANIC(sym_numel);
93 }
sym_stridesc10::impl::NoopPyInterpreterVTable94 c10::SymIntArrayRef sym_strides(const TensorImpl* self) const override {
95 PANIC(sym_strides);
96 }
sym_storage_offsetc10::impl::NoopPyInterpreterVTable97 c10::SymInt sym_storage_offset(const TensorImpl* self) const override {
98 PANIC(sym_storage_offset);
99 }
100
101 // Just swallow the event, don't do anything
trace_gpu_event_creationc10::impl::NoopPyInterpreterVTable102 void trace_gpu_event_creation(c10::DeviceType device_type, uintptr_t event)
103 const override {}
trace_gpu_event_deletionc10::impl::NoopPyInterpreterVTable104 void trace_gpu_event_deletion(c10::DeviceType device_type, uintptr_t event)
105 const override {}
trace_gpu_event_recordc10::impl::NoopPyInterpreterVTable106 void trace_gpu_event_record(
107 c10::DeviceType device_type,
108 uintptr_t event,
109 uintptr_t stream) const override {}
trace_gpu_event_waitc10::impl::NoopPyInterpreterVTable110 void trace_gpu_event_wait(
111 c10::DeviceType device_type,
112 uintptr_t event,
113 uintptr_t stream) const override {}
trace_gpu_memory_allocationc10::impl::NoopPyInterpreterVTable114 void trace_gpu_memory_allocation(c10::DeviceType device_type, uintptr_t ptr)
115 const override {}
trace_gpu_memory_deallocationc10::impl::NoopPyInterpreterVTable116 void trace_gpu_memory_deallocation(c10::DeviceType device_type, uintptr_t ptr)
117 const override {}
trace_gpu_stream_creationc10::impl::NoopPyInterpreterVTable118 void trace_gpu_stream_creation(c10::DeviceType device_type, uintptr_t stream)
119 const override {}
trace_gpu_device_synchronizationc10::impl::NoopPyInterpreterVTable120 void trace_gpu_device_synchronization(
121 c10::DeviceType device_type) const override {}
trace_gpu_stream_synchronizationc10::impl::NoopPyInterpreterVTable122 void trace_gpu_stream_synchronization(
123 c10::DeviceType device_type,
124 uintptr_t stream) const override {}
trace_gpu_event_synchronizationc10::impl::NoopPyInterpreterVTable125 void trace_gpu_event_synchronization(
126 c10::DeviceType device_type,
127 uintptr_t event) const override {}
128
reset_backward_hooksc10::impl::NoopPyInterpreterVTable129 void reset_backward_hooks(const TensorImpl* self) const override {
130 PANIC(reset_backward_hooks);
131 };
132 };
133
134 // Construct this in Global scope instead of within `disarm`
135 // where it will be only initialized first time `disarm` is called.
136 // This increases the likelihood `noop_vtable` lives longer than
137 // any object that refers to it.
138
139 // If `noop_vtable` goes out of scope first, other objects will have dangling
140 // reference to it.
141 static NoopPyInterpreterVTable noop_vtable;
142
disarm()143 void PyInterpreter::disarm() noexcept {
144 vtable_ = &noop_vtable;
145 }
146
147 } // namespace c10::impl
148