Skip to content

Commit 2fbf444

Browse files
authored
[Inductor model breakage] Revert upstream commits in library loading (#2722)
These changes from upstream result in a breakage when loading external library ``` 61170: calling init: /opt/venv/lib/python3.12/site-packages/torchvision/_C.so 61170: terminate called after throwing an instance of 'std::bad_alloc' what(): std::bad_alloc Fatal Python error: Aborted Current thread 0x00007f229fb36080 (most recent call first): File "/usr/lib/python3.12/ctypes/__init__.py", line 379 in __init__ File "/pytorch/torch/_ops.py", line 1488 in load_library File "/opt/venv/lib/python3.12/site-packages/torchvision/extension.py", line 34 in <module> File "<frozen importlib._bootstrap>", line 488 in _call_with_frames_removed File "<frozen importlib._bootstrap_external>", line 995 in exec_module File "<frozen importlib._bootstrap>", line 935 in _load_unlocked File "<frozen importlib._bootstrap>", line 1331 in _find_and_load_unlocked File "<frozen importlib._bootstrap>", line 1360 in _find_and_load File "/opt/venv/lib/python3.12/site-packages/torchvision/__init__.py", line 9 in <module> ``` This was already reverted in rocm/7.1_internal_testing, need to investigate whether upstream needs a fix
1 parent fa57f9c commit 2fbf444

File tree

10 files changed

+0
-437
lines changed

10 files changed

+0
-437
lines changed

aten/src/ATen/core/boxing/KernelFunction.h

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@
66
#include <c10/core/DispatchKeySet.h>
77
#include <c10/util/TypeList.h>
88
#include <c10/util/intrusive_ptr.h>
9-
#include <atomic>
10-
#include <memory>
119
#include <type_traits>
1210

1311
namespace c10 {
@@ -19,9 +17,6 @@ class OperatorHandle;
1917
struct OperatorKernel;
2018
class KernelFunction;
2119

22-
class KernelToken;
23-
class SafeKernelFunction;
24-
2520
template <typename T>
2621
using has_symint = std::disjunction<
2722
std::is_same<c10::SymInt, T>,
@@ -95,12 +90,6 @@ class TORCH_API KernelFunction final {
9590
BoxedKernel::BoxedKernelFunction_withDispatchKeys;
9691

9792
KernelFunction();
98-
~KernelFunction();
99-
100-
KernelFunction(const KernelFunction& other);
101-
KernelFunction& operator=(const KernelFunction& other);
102-
103-
KernelFunction(KernelFunction&&) noexcept = default;
10493

10594
// Fast path for dispatch to allow not touching the boxed kernel in
10695
// the common case where unboxed is available.
@@ -273,9 +262,6 @@ class TORCH_API KernelFunction final {
273262
// For testing internal invariants only
274263
bool _equalsBoxedAndUnboxed(const KernelFunction&) const;
275264

276-
// Register a token to be invalidated when this KernelFunction is destroyed
277-
void registerToken(std::weak_ptr<KernelToken> token) const;
278-
279265
private:
280266
explicit KernelFunction(
281267
std::unique_ptr<OperatorKernel> functor,
@@ -290,50 +276,6 @@ class TORCH_API KernelFunction final {
290276
BoxedKernel boxed_kernel_func_;
291277
void* unboxed_kernel_func_;
292278
void* sym_unboxed_kernel_func_;
293-
// List of tokens that need to be invalidated when this KernelFunction is
294-
// destroyed (lazy allocation to save memory when empty)
295-
mutable std::unique_ptr<std::vector<std::weak_ptr<KernelToken>>> tokens_;
296-
};
297-
298-
// Token held by SafeKernelFunction that gets invalidated when KernelFunction is
299-
// destroyed
300-
class KernelToken {
301-
public:
302-
bool isValid() const;
303-
void invalidate();
304-
305-
private:
306-
std::atomic<bool> invalid_{false};
307-
};
308-
309-
class SafeKernelFunction {
310-
public:
311-
SafeKernelFunction(
312-
const KernelFunction* kernel,
313-
std::string debug,
314-
std::shared_ptr<OperatorHandle> opHandle);
315-
316-
// Safe callBoxed - checks token validity first
317-
void callBoxed(
318-
const OperatorHandle& opHandle,
319-
DispatchKeySet dispatchKeySet,
320-
Stack* stack) const;
321-
322-
// Get debug information
323-
const std::string& debug() const {
324-
return debug_;
325-
}
326-
327-
// Get the OpHandle that lives on this SafeKernelFunction
328-
const OperatorHandle& opHandle() const {
329-
return *opHandle_;
330-
}
331-
332-
private:
333-
KernelFunction kernel_;
334-
std::shared_ptr<KernelToken> token_;
335-
std::string debug_;
336-
std::shared_ptr<OperatorHandle> opHandle_;
337279
};
338280

339281
} // namespace c10

aten/src/ATen/core/boxing/KernelFunction_impl.h

Lines changed: 0 additions & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -24,36 +24,6 @@ inline KernelFunction::KernelFunction()
2424
unboxed_kernel_func_(nullptr),
2525
sym_unboxed_kernel_func_(nullptr) {}
2626

27-
inline KernelFunction::~KernelFunction() {
28-
if (tokens_) {
29-
for (auto& weak_token : *tokens_) {
30-
if (auto token = weak_token.lock()) {
31-
token->invalidate();
32-
}
33-
}
34-
}
35-
}
36-
37-
inline KernelFunction::KernelFunction(const KernelFunction& other)
38-
: boxed_kernel_func_(other.boxed_kernel_func_),
39-
unboxed_kernel_func_(other.unboxed_kernel_func_),
40-
sym_unboxed_kernel_func_(other.sym_unboxed_kernel_func_) {
41-
// tokens_ is intentionally not copied as we only care about invalidating
42-
// tokens if the original KernelFunction is destroyed
43-
}
44-
45-
inline KernelFunction& KernelFunction::operator=(const KernelFunction& other) {
46-
if (this != &other) {
47-
boxed_kernel_func_ = other.boxed_kernel_func_;
48-
unboxed_kernel_func_ = other.unboxed_kernel_func_;
49-
sym_unboxed_kernel_func_ = other.sym_unboxed_kernel_func_;
50-
51-
// tokens_ is intentionally not copied as we only care about invalidating
52-
// tokens if the original KernelFunction is destroyed
53-
}
54-
return *this;
55-
}
56-
5727
inline KernelFunction::KernelFunction(
5828
std::unique_ptr<OperatorKernel> functor,
5929
InternalBoxedKernelFunction* boxed_kernel_func,
@@ -187,14 +157,6 @@ C10_ALWAYS_INLINE Return KernelFunction::call(
187157
std::forward<Args>(args)...);
188158
}
189159

190-
inline void KernelFunction::registerToken(
191-
std::weak_ptr<KernelToken> token) const {
192-
if (!tokens_) {
193-
tokens_ = std::make_unique<std::vector<std::weak_ptr<KernelToken>>>();
194-
}
195-
tokens_->push_back(std::move(token));
196-
}
197-
198160
inline KernelFunction KernelFunction::makeFromBoxedKernel(
199161
BoxedKernel boxed_fn) {
200162
return KernelFunction(
@@ -355,38 +317,4 @@ KernelFunction::makeFromUnboxedLambda(Lambda&& lambda) {
355317
std::forward<Lambda>(lambda)));
356318
}
357319

358-
inline bool KernelToken::isValid() const {
359-
return !invalid_.load(std::memory_order_acquire);
360-
}
361-
362-
inline void KernelToken::invalidate() {
363-
invalid_.store(true, std::memory_order_release);
364-
}
365-
366-
inline SafeKernelFunction::SafeKernelFunction(
367-
const KernelFunction* kernel,
368-
std::string debug,
369-
std::shared_ptr<OperatorHandle> opHandle)
370-
: kernel_(kernel ? *kernel : KernelFunction()),
371-
token_(std::make_shared<KernelToken>()),
372-
debug_(std::move(debug)),
373-
opHandle_(std::move(opHandle)) {
374-
// Register the token with the original kernel so it gets invalidated when the
375-
// kernel is destroyed
376-
if (kernel) {
377-
kernel->registerToken(token_);
378-
}
379-
}
380-
381-
inline void SafeKernelFunction::callBoxed(
382-
const OperatorHandle& opHandle,
383-
DispatchKeySet dispatchKeySet,
384-
Stack* stack) const {
385-
TORCH_CHECK(
386-
token_ && token_->isValid(),
387-
"SafeKernelFunction has been invalidated ",
388-
debug_);
389-
kernel_.callBoxed(opHandle, dispatchKeySet, stack);
390-
}
391-
392320
} // namespace c10

aten/src/ATen/core/dispatch/Dispatcher.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -487,10 +487,6 @@ class TORCH_API OperatorHandle {
487487
return operatorDef_->op.hasComputedKernelForDispatchKey(k);
488488
}
489489

490-
SafeKernelFunction getComputedKernelForDispatchKey(DispatchKey k) const {
491-
return operatorDef_->op.getComputedKernelForDispatchKey(k);
492-
}
493-
494490
std::string dumpComputedTable() const {
495491
return operatorDef_->op.dumpComputedTable();
496492
}

aten/src/ATen/core/dispatch/OperatorEntry.cpp

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -315,42 +315,6 @@ const AnnotatedKernel* OperatorEntry::getKernelForDispatchKey(DispatchKey dispat
315315
return nullptr;
316316
}
317317

318-
SafeKernelFunction OperatorEntry::getComputedKernelForDispatchKey(
319-
DispatchKey k) const {
320-
TORCH_CHECK(
321-
!isAliasDispatchKey(k),
322-
"Alias keys do not have runtime kernel registrations.");
323-
const auto dispatch_ix = getDispatchTableIndexForDispatchKey(k);
324-
TORCH_CHECK(
325-
dispatchTable_[dispatch_ix].isValid(),
326-
"no kernel for ",
327-
k,
328-
" for ",
329-
name_);
330-
331-
// Get the KernelFunction object from kernels_ to pass to SafeKernelFunction
332-
333-
// The KernelFunction object in dispatchTable_ is a copy of the KernelFunction
334-
// in the AnnotatedKernel in kernels_. A KernelFunction is only truly
335-
// deregistered when the kernel is removed from kernels_. However, the
336-
// KernelFunction in dispatchTable_ might be removed before it is deregistered
337-
// (when a newer kernel is registered). Therefore, here we want to return a
338-
// SafeKernelFunction that is backed by the original KernelFunction in
339-
// kernels_, so that we only invalidate it when the kernel is deregistered.
340-
auto [annotatedKernel, _] =
341-
computeDispatchTableEntryWithDebug(c10::Dispatcher::singleton(), k);
342-
343-
// Use findSchemaOrThrow to get OpHandle for the OperatorEntry
344-
auto& dispatcher = c10::Dispatcher::singleton();
345-
auto opHandle = dispatcher.findSchemaOrThrow(
346-
name_.name.c_str(), name_.overload_name.c_str());
347-
348-
return SafeKernelFunction(
349-
&annotatedKernel.kernel,
350-
annotatedKernel.debug,
351-
std::make_shared<OperatorHandle>(opHandle));
352-
}
353-
354318
const std::vector<at::Tag>& OperatorEntry::getTags() const {
355319
#if defined C10_MOBILE
356320
TORCH_CHECK(false, "tags are not saved for Mobile");

aten/src/ATen/core/dispatch/OperatorEntry.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -217,8 +217,6 @@ class TORCH_API OperatorEntry final {
217217
const KernelFunction& kernelForDispatchKey(DispatchKey k) const;
218218
// Returns true if the "computed table" has an entry for a particular key.
219219
bool hasComputedKernelForDispatchKey(DispatchKey k) const;
220-
// Returns a KernelFunction corresponding to the kernel in dispatchTable
221-
SafeKernelFunction getComputedKernelForDispatchKey(DispatchKey k) const;
222220
// Returns all the operator tags added at the time of registration
223221
const std::vector<at::Tag>& getTags() const;
224222
void setReportErrorCallback_(std::unique_ptr<c10::SafePyObject> callback);

docs/source/library.md

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,6 @@ via PyTorch's C++ operator registration APIs).
5656
.. autofunction:: infer_schema
5757
.. autoclass:: torch._library.custom_ops.CustomOpDef
5858
:members: set_kernel_enabled
59-
.. autofunction:: get_kernel
6059
```
6160

6261
## Low-level APIs

0 commit comments

Comments
 (0)