-
Notifications
You must be signed in to change notification settings - Fork 561
[Fori_loop|While_loop] Placeholder lower torch.while_loop with python dispatch for simple addition test case #6532
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
ManfeiBai
merged 54 commits into
master
from
while_loop-lowering-with-simplecalculation-dispatch-in-torch
Feb 22, 2024
Merged
Changes from 49 commits
Commits
Show all changes
54 commits
Select commit
Hold shift + click to select a range
0a2cc00
Create fori_loop.py
ManfeiBai c4b4384
Update fori_loop.py
ManfeiBai 8ac0bb5
Update init_python_bindings.cpp
ManfeiBai 0a0f8c6
Update init_python_bindings.cpp
ManfeiBai c62ad7d
Update init_python_bindings.cpp
ManfeiBai f0dd53e
Update fori_loop.py
ManfeiBai dcf65fc
Update fori_loop.py
ManfeiBai a1ed583
Create test_fori_loop.py
ManfeiBai cea9062
Update test_fori_loop.py
ManfeiBai 7e1ffcb
Update test_fori_loop.py
ManfeiBai 6694c09
Update test_fori_loop.py
ManfeiBai caeeb50
Update test_fori_loop.py
ManfeiBai 6a4ad4f
use code from xb
ManfeiBai 76a1cfa
only xb test
ManfeiBai 694e797
check original version has used python dispatch or not
ManfeiBai fc4c8bb
check original version has used python dispatch or not again
ManfeiBai cf6aae9
add test script for xla
ManfeiBai 04a8eff
add test script for xla again
ManfeiBai 9c4ba74
test with while_loop_dense
ManfeiBai e377b77
change dispatchkey
ManfeiBai 27d66f8
re dispatch
ManfeiBai 82612f1
re dispatch again
ManfeiBai 0de6bae
check again
ManfeiBai 0977c54
check type
ManfeiBai e76484f
checkpoint to show body/cond hlo
ManfeiBai f5dea5d
correct xb
ManfeiBai a189666
add example code script
ManfeiBai 12e6886
add example code script again
ManfeiBai 202f1ef
only test tpu
ManfeiBai d6fd934
only test tpu
ManfeiBai 8b6df8e
add result value check
ManfeiBai 2db33ab
add result value check
ManfeiBai 6d3f3a4
clean code
ManfeiBai 700ed2a
try torchxla code
ManfeiBai 0f05d76
try torchxla code again
ManfeiBai d7e54c3
try torchxla code again again
ManfeiBai 1b11308
try torchxla code again again again
ManfeiBai fea71a6
add test on CPU/GPU tests
ManfeiBai 9a42faf
add test on TPU test trigger's
ManfeiBai 0d21e47
add test in TPU CI workflow
ManfeiBai 45c4433
Merge branch 'master' into while_loop-lowering-with-simplecalculation…
ManfeiBai 5050eb4
placeholder for xlacomputation
ManfeiBai 92b69f4
add test
ManfeiBai c4ed61a
modif test code
ManfeiBai 1c8395e
modify fori_loop
ManfeiBai c9e7b5f
format
ManfeiBai 02f738f
format
ManfeiBai d689acc
format
ManfeiBai 122d6e5
format
ManfeiBai e95ddc3
test log
ManfeiBai 589b1b0
test
ManfeiBai 02caa42
trigger test again
ManfeiBai ab5ae68
comment to explain
ManfeiBai 594280c
comment for explain
ManfeiBai 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
40 changes: 40 additions & 0 deletions
40
test/test_fori_loop_with_while_loop_simple_add_dispatch_in_torch.py
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,40 @@ | ||
| import os | ||
| import unittest | ||
| from typing import Callable, Dict, List | ||
|
|
||
| import torch | ||
| import torch_xla | ||
| import torch_xla.experimental.fori_loop | ||
| from torch._higher_order_ops.while_loop import while_loop | ||
| import torch_xla.core.xla_model as xm | ||
| import torch_xla.core.xla_builder as xb | ||
|
|
||
|
|
||
| def _fake_while_loop(cond_fn, body_fn, operands): | ||
| while cond_fn(*operands): | ||
| operands = body_fn(*operands) | ||
| return operands | ||
|
|
||
|
|
||
| class WhileLoopTest(unittest.TestCase): | ||
|
|
||
| def test_while_loop_tpu(self): | ||
|
|
||
| def cond_fn(x): | ||
| return x.sum() <= 10 | ||
|
|
||
| def body_fn(x): | ||
| return (x + 1,) | ||
|
|
||
| device = xm.xla_device() | ||
| x = torch.ones(1, dtype=torch.int, device=device) | ||
| res = while_loop(cond_fn, body_fn, (x,)) | ||
| print("while_loop result: ", res) | ||
| expected = torch.tensor(11, dtype=torch.int, device=device) | ||
| print("expected result: ", expected) | ||
| self.assertEqual(expected, res[0]) | ||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| test = unittest.main() | ||
| sys.exit(0 if test.result.wasSuccessful() else 1) | ||
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,42 @@ | ||
| import numpy as np | ||
| import torch | ||
| import torch_xla | ||
| import torch_xla.core.xla_builder as xb | ||
| import torch_xla.core.xla_model as xm | ||
| import torch_xla.utils.utils as xu | ||
| import torch_xla.core.xla_op_registry as xor | ||
|
|
||
| from torch._C import DispatchKey | ||
| from torch._ops import HigherOrderOperator | ||
| import torch._higher_order_ops.while_loop | ||
| from torch._higher_order_ops.while_loop import while_loop_op | ||
|
|
||
|
|
||
| @while_loop_op.py_impl(DispatchKey.XLA) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great to see that this is working! |
||
| def while_loop(cond_fn, body_fn, operands): | ||
| # cond_fn&body_fn: callable | ||
| # operands: (Tuple of possibly nested dict/list/tuple of tensors) | ||
| return _xla_while_loop(cond_fn, body_fn, operands) | ||
|
|
||
|
|
||
| def _xla_while_loop(cond_fn, body_fn, operands): | ||
|
|
||
| def op_fn(internal_x): | ||
| # TODO(manfei): replace cond_fn_placeholder and body_fn_placeholder after confirm xlacomputation could be in xla::while | ||
| def cond_fn_placeholder(counter, internal_x): | ||
| return counter < xb.Op.scalar(internal_x.builder(), 10, dtype=xb.Type.S32) | ||
|
|
||
| def body_fn_placeholder(counter, internal_x): | ||
| next_counter = counter + xb.Op.scalar( | ||
| counter.builder(), 1, dtype=xb.Type.S32) | ||
| internal_x = internal_x + xb.Op.scalar( | ||
| internal_x.builder(), 1, dtype=xb.Type.S32) | ||
| return xb.Op.tuple((next_counter, internal_x)) | ||
|
|
||
| zero = xb.Op.scalar(internal_x.builder(), 0, dtype=xb.Type.S32) | ||
| w = xb.Op.mkwhile((zero, internal_x), cond_fn_placeholder, | ||
| body_fn_placeholder) | ||
| return w.get_tuple_element(1) | ||
|
|
||
| op = xor.register('test_while', op_fn) | ||
| return xu.as_list(op(operands[0])) | ||
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.
Uh oh!
There was an error while loading. Please reload this page.