-
Couldn't load subscription status.
- Fork 560
Attach callback to a Tensor's underlying buffer #7793
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
9 commits Select commit Hold shift + click to select a range
dd11e7e Attach callback to a Tensor's underlying buffer
will-cromar 20cb1d6 Wait for execution to complete in `PrepareToExit`
will-cromar 62cc8ba remove log
will-cromar 021fadd use combined `WaitDeviceOps`
will-cromar b27c744 unit test
will-cromar 031c1f8 docstring
will-cromar 4089718 fix unit test
will-cromar 65e9a99 handle SPMD
will-cromar c46b1c6 sanity-check data handle
will-cromar File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,34 @@ | ||
| import threading | ||
| | ||
| from absl.testing import absltest | ||
| import torch | ||
| import torch_xla | ||
| from torch_xla.experimental import callback | ||
| | ||
| | ||
| class TestExperimentalCallback(absltest.TestCase): | ||
| | ||
| @staticmethod | ||
| @torch_xla.compile | ||
| def executable(): | ||
| a, b = torch.randn((100, 100), device=torch_xla.device()), torch.randn( | ||
| (100, 100), device=torch_xla.device()) | ||
| return a @ b | ||
| | ||
| def test_callback(self): | ||
| event = threading.Event() | ||
| c = self.executable() | ||
| | ||
| def cb(tensor): | ||
| self.assertIs(c, tensor) | ||
will-cromar marked this conversation as resolved. Show resolved Hide resolved | ||
| # TODO: check that result is both assigned and completed | ||
| self.assertNotIn("Data Handle: None", | ||
| torch_xla._XLAC._get_xla_tensor_debug_info(tensor)) | ||
| event.set() | ||
| | ||
| callback.on_ready_callback(c, cb) | ||
| event.wait(3) | ||
| | ||
| | ||
| if __name__ == "__main__": | ||
| absltest.main() | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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 hidden or 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,17 @@ | ||
| from typing import Callable | ||
| import torch | ||
| import torch_xla | ||
| | ||
| | ||
| def on_ready_callback(tensor, callback: Callable[[torch.Tensor], None]): | ||
| """Installs callback on `tensor` to be called when underlying buffer is ready. | ||
| | ||
| Note: Since `callback` will need to re-acquire the GIL since it is a Python | ||
| callable. If the main thread is blocking on `callback` and holding the GIL, | ||
| this will result in a deadlock. | ||
| """ | ||
| | ||
| def _callback_wrapper(): | ||
| callback(tensor) | ||
| | ||
| torch_xla._XLAC._on_ready_callback(tensor, _callback_wrapper) |
Add this suggestion to a batch that can be applied as a single commit. This suggestion is invalid because no changes were made to the code. Suggestions cannot be applied while the pull request is closed. Suggestions cannot be applied while viewing a subset of changes. Only one suggestion per line can be applied in a batch. Add this suggestion to a batch that can be applied as a single commit. Applying suggestions on deleted lines is not supported. You must change the existing code in this line in order to create a valid suggestion. Outdated suggestions cannot be applied. This suggestion has been applied or marked resolved. Suggestions cannot be applied from pending reviews. Suggestions cannot be applied on multi-line comments. Suggestions cannot be applied while the pull request is queued to merge. Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
is it difficult to support the cases where
call_backtakes more than just the tensor value?call_backmight be a general python function thattensoris just one of the inputs.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You should
functools.partialin that case to add other arguments to the callable https://docs.python.org/3/library/functools.html#functools.partial