-
Notifications
You must be signed in to change notification settings - Fork 635
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve decorator to turn function into PyTorch operator
ghstack-source-id: 97ad2a9bfd84fb0a0a50b270e65c05a09b4ea5a7 Pull Request resolved: fairinternal/xformers#857 __original_commit__ = fairinternal/xformers@c530810
- Loading branch information
Showing
5 changed files
with
93 additions
and
26 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
#include <pybind11/pybind11.h> | ||
|
||
// Must come first to load TORCH_VERSION_FOO. | ||
#include <torch/torch.h> | ||
|
||
#if TORCH_VERSION_MAJOR > 1 || \ | ||
(TORCH_VERSION_MAJOR == 1 && TORCH_VERSION_MINOR >= 13) | ||
#include <torch/csrc/distributed/c10d/ProcessGroup.hpp> | ||
#else | ||
#include <c10d/ProcessGroup.hpp> | ||
#endif | ||
|
||
#include <torch/csrc/jit/python/pybind_utils.h> | ||
|
||
namespace py = pybind11; | ||
|
||
namespace { | ||
|
||
// Starting with PyTorch 2.2, we will be able to do boxing/unboxing in Python. | ||
// See https://github.com/pytorch/pytorch/pull/111997. | ||
// In the meantime, we had to implement the conversions in C++ ourselves. | ||
|
||
py::object box_process_group( | ||
c10::intrusive_ptr<c10d::ProcessGroup> process_group) { | ||
return torch::jit::toPyObject(c10::IValue(process_group)); | ||
} | ||
|
||
c10::intrusive_ptr<c10d::ProcessGroup> unbox_process_group( | ||
const py::object& obj) { | ||
return torch::jit::toIValue( | ||
obj, | ||
c10::getCustomClassType<c10::intrusive_ptr<c10d::ProcessGroup>>()) | ||
.toCustomClass<c10d::ProcessGroup>(); | ||
} | ||
|
||
} // namespace | ||
|
||
PYBIND11_MODULE(TORCH_EXTENSION_NAME, module) { | ||
module.def("box_process_group", &box_process_group); | ||
module.def("unbox_process_group", &unbox_process_group); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters