pytorch/torch/csrc/dynamo/cache_entry.cpp
William Wen ae4e866bba [dynamo] refactor CacheEntry and ExtraState to eval_frame.c to C++ (#118438)
Part of implementing CacheEntry invalidation to fix https://github.com/pytorch/pytorch/issues/112090.

Changes:
- Move CacheEntry and ExtraState to C++
- Use pybind to control reference counting
- Use std::list instead of manually implementing a linked list

Pull Request resolved: https://github.com/pytorch/pytorch/pull/118438
Approved by: https://github.com/jansel
2024-02-06 20:48:11 +00:00

31 lines
793 B
C++

#include <torch/csrc/dynamo/cache_entry.h>
#include <torch/csrc/dynamo/debug_macros.h>
#include <torch/csrc/dynamo/extra_state.h>
CacheEntry::CacheEntry(const py::handle& guarded_code) {
this->check_fn = guarded_code.attr("check_fn");
this->code = guarded_code.attr("code");
}
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);
}
PyCodeObject* CacheEntry_get_code(CacheEntry* e) {
return (PyCodeObject*)e->code.ptr();
}
PyObject* CacheEntry_to_obj(CacheEntry* e) {
if (!e) {
return py::none().release().ptr();
}
return py::cast(e, py::return_value_policy::reference).release().ptr();
}