Skip to content
Merged
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
39 changes: 39 additions & 0 deletions src/Devices.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,42 @@ function addressable_devices(client::XLA.Client=XLA.default_backend[])
ndevices = XLA.ClientNumAddressableDevices(client)
return [XLA.ClientGetAddressableDevice(client, i - 1) for i in 1:ndevices]
end

# https://github.com/jax-ml/jax/blob/152099ee0ef31119f16f4c2dac50d84fcb1575ef/jax/_src/hardware_utils.py#L19-L55
const _GOOGLE_PCI_VENDOR_ID = "0x1ae0"
const _TPU_PCI_DEVICE_IDS = (
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you link the source where this comes from?

# TPU v2, v3
"0x0027",
# No public name (plc)
"0x0056",
# TPU v4
"0x005e",
# TPU v5p
"0x0062",
# TPU v5e
"0x0063",
# TPU v6e
"0x006f",
)

function has_tpu()
Sys.islinux() || return false

devices_dir = "/sys/bus/pci/devices/"
isdir(devices_dir) || return false

try
for path in readdir(devices_dir; join=true, sort=false)
if trim(read(joinpath(path, "vendor"), String)) == _GOOGLE_PCI_VENDOR_ID &&
trim(read(joinpath(path, "device"), String)) in _TPU_PCI_DEVICE_IDS
return true
end
end
catch ex
@warn "failed to query PCI device information" maxlog = 1 exception = (
ex, catch_backtrace()
)
end

return false
end
2 changes: 1 addition & 1 deletion src/XLA.jl
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ function __init__()
end

@static if !Sys.isapple()
if isfile("/usr/lib/libtpu.so")
if Reactant.has_tpu()
dataset_dir = @get_scratch!("libtpu")
if !isfile(dataset_dir * "/libtpu.so")
Downloads.download(
Expand Down
Loading