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

MPS Load capabilities #623

Merged
merged 5 commits into from
Feb 18, 2023
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
40 changes: 29 additions & 11 deletions src/nn/var_store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,20 +185,38 @@ impl VarStore {
/// variables stored in the var-store is not changed, only the values
/// for these tensors are modified.
pub fn load<T: AsRef<std::path::Path>>(&mut self, path: T) -> Result<(), TchError> {
let named_tensors = self.named_tensors(&path)?;
let mut variables = self.variables_.lock().unwrap();
for (name, var) in variables.named_variables.iter_mut() {
match named_tensors.get(name) {
Some(src) => crate::no_grad(|| var.f_copy_(src).map_err(|e| e.path_context(name)))?,
None => {
return Err(TchError::TensorNameNotFound(
name.to_string(),
path.as_ref().to_string_lossy().into_owned(),
));
// Current workaround to allow loading in MPS device.
// On new libtorch releases check if direct loading becomes possible and revert
// See (https://github.com/LaurentMazare/tch-rs/issues/609#issuecomment-1427071598).
let is_mps = Device::Mps == self.device;
if is_mps {
self.set_device(Device::Cpu);
};
let load_tensors = || -> Result<(), TchError> {
let named_tensors = self.named_tensors(&path)?;
{
let mut variables = self.variables_.lock().unwrap();
for (name, var) in variables.named_variables.iter_mut() {
match named_tensors.get(name) {
Some(src) => {
crate::no_grad(|| var.f_copy_(src).map_err(|e| e.path_context(name)))?
}
None => {
return Err(TchError::TensorNameNotFound(
name.to_string(),
path.as_ref().to_string_lossy().into_owned(),
));
}
}
}
}
Ok(())
};
let load_tensors_result = load_tensors();
if is_mps {
self.set_device(Device::Mps);
}
Ok(())
load_tensors_result
}

/// Loads the var-store variable values from a stream.
Expand Down