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

Torch("supported devices include CPU, CUDA and HPU, however got MPS") #609

Closed
LeeeSe opened this issue Jan 15, 2023 · 4 comments
Closed

Comments

@LeeeSe
Copy link

LeeeSe commented Jan 15, 2023

torch-version: 1.13.1
OS: macOS 13.1
Chip: M1

use tch::{kind, Tensor};

fn main() {
    let model =
        tch::CModule::load_on_device("model/yolov5s.torchscript", tch::Device::Mps).unwrap();
}
@LaurentMazare
Copy link
Owner

I think the issue is actually on the libtorch side, see pytorch/pytorch#88820
A workaround seems to be to load the model to the cpu and then move it to mps.

@LeeeSe
Copy link
Author

LeeeSe commented Jan 16, 2023

Thanks, the problem was solved by the following

fn load_model_to_gpu() {
    let mut vs = tch::nn::VarStore::new(tch::Device::Cpu);
    vs.load("model/yolov5s.torchscript").unwrap();
    println!("vs.device {:?}", vs.device());
    vs.set_device(tch::Device::Mps);
    println!("vs.device now {:?}", vs.device());
}

@LeeeSe LeeeSe closed this as completed Jan 16, 2023
@guillaume-be
Copy link
Contributor

@LaurentMazare the workaround seems to work for users with access to the VarStore, but causes some frictions when the application does not expose it anymore (e.g. higher level model APIs), see guillaume-be/rust-bert#311

Since this seems to be the only way to load weights on MPS currently, would you be open to a pull request making this the default behaviour when loading weights (i,e. if the VarStore device is Device::Mps, change it to Device::Cpu, load the weights and then change back to Device::Mps)?

This hopefully gets resolved eventually and the workaround could then be removed from the load method.

@LaurentMazare
Copy link
Owner

@guillaume-be sure, that sounds like a reasonable temporary workaround so feel free to mint a PR or let me know if you don't have the time and I'll do it, probably good to have a comment that points at this issue and mention that we should revisit.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants