#pragma once #include #include #include #include #include #define PYBIND11_SIMPLE_GIL_MANAGEMENT #include // Include some often-used cpp_wrapper headers, for precompiling. #include #include #include #include #include #include namespace py = pybind11; // NOLINT(misc-unused-alias-decls) class RAIIPyObject { public: RAIIPyObject() = default; // steals a reference to a PyObject RAIIPyObject(PyObject* obj) : obj_{obj} {} RAIIPyObject(const RAIIPyObject& other) : obj_{other.obj_} { Py_XINCREF(obj_); } RAIIPyObject(RAIIPyObject&& other) noexcept { // refcount doesn't change, and obj_ is currently nullptr std::swap(obj_, other.obj_); } ~RAIIPyObject() { Py_XDECREF(obj_); } RAIIPyObject& operator=(const RAIIPyObject& other) { if (this != &other) { Py_XDECREF(obj_); obj_ = other.obj_; Py_XINCREF(obj_); } return *this; } RAIIPyObject& operator=(RAIIPyObject&& other) noexcept { // refcount to the current object decreases, but refcount to other.obj_ is // the same Py_XDECREF(obj_); obj_ = std::exchange(other.obj_, nullptr); return *this; } operator bool() const noexcept { return obj_; } operator PyObject*() { return obj_; } PyObject* get() { return obj_; } private: PyObject* obj_{nullptr}; }; #include #include using namespace torch::aot_inductor; #include #include // Round up to the nearest multiple of 64 [[maybe_unused]] inline int64_t align(int64_t nbytes) { return (nbytes + 64 - 1) & -64; }