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

Add support for xla devices #494

Merged
merged 2 commits into from
Jul 30, 2024
Merged
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
14 changes: 14 additions & 0 deletions bindings/python/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,7 @@ enum Device {
Mps,
Npu(usize),
Xpu(usize),
Xla(usize),
}

impl<'source> FromPyObject<'source> for Device {
Expand All @@ -256,6 +257,7 @@ impl<'source> FromPyObject<'source> for Device {
"mps" => Ok(Device::Mps),
"npu" => Ok(Device::Npu(0)),
"xpu" => Ok(Device::Xpu(0)),
"xla" => Ok(Device::Xla(0)),
name if name.starts_with("cuda:") => {
let tokens: Vec<_> = name.split(':').collect();
if tokens.len() == 2 {
Expand Down Expand Up @@ -289,6 +291,17 @@ impl<'source> FromPyObject<'source> for Device {
)))
}
}
name if name.starts_with("xla:") => {
let tokens: Vec<_> = name.split(':').collect();
if tokens.len() == 2 {
let device: usize = tokens[1].parse()?;
Ok(Device::Xla(device))
} else {
Err(SafetensorError::new_err(format!(
"device {name} is invalid"
)))
}
}
name => Err(SafetensorError::new_err(format!(
"device {name} is invalid"
))),
Expand All @@ -309,6 +322,7 @@ impl IntoPy<PyObject> for Device {
Device::Mps => "mps".into_py(py),
Device::Npu(n) => format!("npu:{n}").into_py(py),
Device::Xpu(n) => format!("xpu:{n}").into_py(py),
Device::Xla(n) => format!("xla:{n}").into_py(py),
}
}
}
Expand Down