Files
pytorch/torch/csrc/dynamo/cache_entry.cpp
William Wen ab2294d828 [dynamo] fix _torchdynamo_orig_callable naming issues (#156901)
`_torchdynamo_orig_callable` was being used in two distinct places:
- to get the original user function from nested eval_frame.py decorators
- to get the original backend from nested convert_frame.py callbacks

We rename ~the first usage to `_torchdynamo_orig_fn`~ and the second to `_torchdynamo_orig_backend` in order to distinguish these cases.

UPDATE: seems like both internal and OSS users depend on `_torchdynamo_orig_callable`, but it only seems in the first context. We should thus keep the original name for the first case then.

Pull Request resolved: https://github.com/pytorch/pytorch/pull/156901
Approved by: https://github.com/StrongerXi, https://github.com/jansel
2025-07-02 09:53:55 +00:00

85 lines
2.8 KiB
C++

#include <torch/csrc/dynamo/cache_entry.h>
#include <torch/csrc/dynamo/guards.h>
#include <torch/csrc/dynamo/debug_macros.h>
#include <torch/csrc/dynamo/extra_state.h>
CacheEntry::CacheEntry(const py::handle& guarded_code, PyObject* backend)
: backend{py::cast<py::object>(get_backend(backend))} {
this->guard_manager = guarded_code.attr("guard_manager");
this->code = guarded_code.attr("code");
this->compile_id = guarded_code.attr("compile_id");
py::object trace_annotation = guarded_code.attr("trace_annotation");
const char* trace_annotation_str = PyUnicode_AsUTF8(trace_annotation.ptr());
if (trace_annotation) {
this->trace_annotation = std::string(trace_annotation_str);
} else {
this->trace_annotation = "Unknown";
}
this->root_mgr = torch::dynamo::convert_to_root_guard_manager(
this->guard_manager.attr("root"));
this->diff_guard_root_mgr = torch::dynamo::convert_to_root_guard_manager(
this->guard_manager.attr("diff_guard_root"));
}
C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED(
"-Wdeprecated-copy-with-user-provided-dtor")
C10_DIAGNOSTIC_PUSH_AND_IGNORED_IF_DEFINED("-Wdeprecated-copy-dtor")
// NOLINTNEXTLINE(bugprone-exception-escape)
CacheEntry::~CacheEntry() {
// prevent guard_manager from use-after-free when invalidating
this->guard_manager.attr("cache_entry") = py::none();
this->guard_manager.attr("extra_state") = py::none();
}
C10_DIAGNOSTIC_POP()
C10_DIAGNOSTIC_POP()
py::object CacheEntry::next() {
NULL_CHECK(this->_owner);
auto it = this->_owner_loc;
++it;
if (it == this->_owner->cache_entry_list.end()) {
return py::none();
}
return py::cast(*it, py::return_value_policy::reference);
}
void CacheEntry::invalidate(py::object deleted_guard_manager) {
// Keep the current pointer alive but make the fields as if no-op
this->guard_manager.attr("cache_entry") = py::none();
this->guard_manager.attr("extra_state") = py::none();
this->code = py::none();
this->guard_manager = std::move(deleted_guard_manager);
this->root_mgr = nullptr;
this->trace_annotation = "Invalidated";
this->backend = py::none();
}
void CacheEntry::update_diff_guard_root_manager() {
this->diff_guard_root_mgr = torch::dynamo::convert_to_root_guard_manager(
this->guard_manager.attr("diff_guard_root"));
}
PyCodeObject* CacheEntry_get_code(CacheEntry* e) {
return (PyCodeObject*)e->code.ptr();
}
const char* CacheEntry_get_trace_annotation(CacheEntry* e) {
return e->trace_annotation.c_str();
}
PyObject* CacheEntry_to_obj(CacheEntry* e) {
if (!e) {
return py::none().release().ptr();
}
return py::cast(e, py::return_value_policy::reference).release().ptr();
}
PyObject* get_backend(PyObject* callback) {
py::handle handle = py::handle(callback);
while (py::hasattr(handle, "_torchdynamo_orig_backend")) {
handle = handle.attr("_torchdynamo_orig_backend");
}
return handle.ptr();
}