Skip to content
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

feat: add the tpu support to torch backend #28569

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions ivy/functional/backends/torch/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,7 @@


# devices
valid_devices = ("cpu", "gpu")

invalid_devices = ("tpu",)


valid_devices = ("cpu", "gpu", "tpu")
# native data types
native_int8 = torch.int8
native_int16 = torch.int16
Expand Down
10 changes: 10 additions & 0 deletions ivy/functional/backends/torch/device.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import importlib
import torch
import torch_xla.core.xla_model as xm
from typing import Optional, Union
from torch.profiler import ProfilerActivity
from torch.profiler import profile
Expand All @@ -33,6 +34,8 @@ def dev(
dv = dv.type
elif hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return torch.device(dv.replace("gpu", "mps"))
elif xm.xla_device().type == "xla":
return torch.device(dv.replace("tpu", "xla"))
return torch.device(dv.replace("gpu", "cuda"))
return as_ivy_dev(dv)

Expand Down Expand Up @@ -64,6 +67,11 @@ def as_ivy_dev(device: torch.device, /):
dev_type.replace("mps", "gpu")
+ (":" + (str(dev_idx) if dev_idx is not None else "0"))
)
elif dev_type == "xla":
return ivy.Device(
dev_type.replace("xla", "tpu")
+ (":" + str((dev_idx) if dev_idx is not None else "0"))
)
return ivy.Device(
dev_type.replace("cuda", "gpu")
+ (":" + (str(dev_idx) if dev_idx is not None else "0"))
Expand All @@ -78,6 +86,8 @@ def as_native_dev(
return device
if hasattr(torch.backends, "mps") and torch.backends.mps.is_available():
return torch.device(ivy.Device(device).replace("gpu", "mps"))
elif xm.xla_device().type == "xla":
return torch.device(ivy.Device(device).replace("tpu", "xla"))
return torch.device(ivy.Device(device).replace("gpu", "cuda"))


Expand Down
Loading