-
Notifications
You must be signed in to change notification settings - Fork 527
Migrate runtime.xla_device in favor of core.xla_model.xla_device #9200
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
Draft
ghpvnist
wants to merge
3
commits into
pytorch:master
Choose a base branch
from
ghpvnist:7831
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,14 +15,14 @@ import torch | |
import torch_xla | ||
import torch_xla.core.xla_model as xm | ||
|
||
t = torch.randn(2, 2, device=xm.xla_device()) | ||
t = torch.randn(2, 2, device=torch_xla.device()) | ||
print(t.device) | ||
print(t) | ||
``` | ||
|
||
This code should look familiar. PyTorch/XLA uses the same interface as regular | ||
PyTorch with a few additions. Importing `torch_xla` initializes PyTorch/XLA, and | ||
`xm.xla_device()` returns the current XLA device. This may be a CPU or TPU | ||
`torch_xla.device()` returns the current XLA device. This may be a CPU or TPU | ||
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. can this be further simplified to simply describing that users get a devicea via |
||
depending on your environment. | ||
|
||
## XLA Tensors are PyTorch Tensors | ||
|
@@ -32,8 +32,8 @@ PyTorch operations can be performed on XLA tensors just like CPU or CUDA tensors | |
For example, XLA tensors can be added together: | ||
|
||
```python | ||
t0 = torch.randn(2, 2, device=xm.xla_device()) | ||
t1 = torch.randn(2, 2, device=xm.xla_device()) | ||
t0 = torch.randn(2, 2, device=torch_xla.device()) | ||
t1 = torch.randn(2, 2, device=torch_xla.device()) | ||
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. can this be |
||
print(t0 + t1) | ||
``` | ||
|
||
|
@@ -46,8 +46,8 @@ print(t0.mm(t1)) | |
Or used with neural network modules: | ||
|
||
```python | ||
l_in = torch.randn(10, device=xm.xla_device()) | ||
linear = torch.nn.Linear(10, 20).to(xm.xla_device()) | ||
l_in = torch.randn(10, device=torch_xla.device()) | ||
linear = torch.nn.Linear(10, 20).to(torch_xla.device()) | ||
l_out = linear(l_in) | ||
print(l_out) | ||
``` | ||
|
@@ -56,7 +56,7 @@ Like other device types, XLA tensors only work with other XLA tensors on the | |
same device. So code like | ||
|
||
```python | ||
l_in = torch.randn(10, device=xm.xla_device()) | ||
l_in = torch.randn(10, device=torch_xla.device()) | ||
linear = torch.nn.Linear(10, 20) | ||
l_out = linear(l_in) | ||
print(l_out) | ||
|
@@ -109,10 +109,10 @@ class MNIST(nn.Module): | |
batch_size = 128 | ||
train_loader = xu.SampleGenerator( | ||
data=(torch.zeros(batch_size, 1, 28, 28), | ||
torch.zeros(batch_size, dtype=torch.int64)), | ||
torch.zeros(batch_size, dtype=torch.int64)), | ||
sample_count=60000 // batch_size // xr.world_size()) | ||
|
||
device = xm.xla_device() # Get the XLA device (TPU). | ||
device = torch_xla.device() # Get the XLA device (TPU). | ||
model = MNIST().train().to(device) # Create a model and move it to the device. | ||
loss_fn = nn.NLLLoss() | ||
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5) | ||
|
@@ -169,7 +169,7 @@ def _mp_fn(index): | |
index: Index of the process. | ||
""" | ||
|
||
device = xm.xla_device() # Get the device assigned to this process. | ||
device = torch_xla.device() # Get the device assigned to this process. | ||
# Wrap the loader for multi-device. | ||
mp_device_loader = pl.MpDeviceLoader(train_loader, device) | ||
|
||
|
@@ -197,7 +197,7 @@ single device snippet. Let's go over then one by one. | |
- `torch_xla.launch()` | ||
- Creates the processes that each run an XLA device. | ||
- This function is a wrapper of multithreading spawn to allow user run the script with torchrun command line also. Each process will only be able to access the device assigned to the current process. For example on a TPU v4-8, there will be 4 processes being spawn up and each process will own a TPU device. | ||
- Note that if you print the `xm.xla_device()` on each process you will see `xla:0` on all devices. This is because each process can only see one device. This does not mean multi-process is not functioning. The only exeption is with PJRT runtime on TPU v2 and TPU v3 since there will be `#devices/2` processes and each process will have 2 threads (check this [doc](https://github.com/pytorch/xla/blob/master/docs/pjrt.md#tpus-v2v3-vs-v4) for more details). | ||
- Note that if you print the `torch_xla.device()` on each process you will see `xla:0` on all devices. This is because each process can only see one device. This does not mean multi-process is not functioning. The only exeption is with PJRT runtime on TPU v2 and TPU v3 since there will be `#devices/2` processes and each process will have 2 threads (check this [doc](https://github.com/pytorch/xla/blob/master/docs/pjrt.md#tpus-v2v3-vs-v4) for more details). | ||
- `MpDeviceLoader` | ||
- Loads the training data onto each device. | ||
- `MpDeviceLoader` can wrap on a torch dataloader. It can preload the data to the device and overlap the dataloading with device execution to improve the performance. | ||
|
@@ -290,7 +290,7 @@ import torch | |
import torch_xla | ||
import torch_xla.core.xla_model as xm | ||
|
||
device = xm.xla_device() | ||
device = torch_xla.device() | ||
|
||
t0 = torch.randn(2, 2, device=device) | ||
t1 = torch.randn(2, 2, device=device) | ||
|
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
can this be changed to
device="xla"
?