safetensors icon indicating copy to clipboard operation
safetensors copied to clipboard

Pass device=cpu to torch.asarray() in get_tensor()

Open maxdebayser opened this issue 1 year ago • 1 comments

The code in get_tensor assumes that torch.asarray will always load the tensor in cpu, and then the tensor is copied to self.device in the "to" call at the end of the function. However, that assumption is not always correct. To make sure, device=cpu has to be passed to asarray.

Another option would be to set the final device since the beginning:

let device = match self.device {
    Device::Cpu     => format!("cpu"),
    Device::Cuda(n) => format!("cuda:{n}"),
    Device::Mps     => format!("mps"),
    Device::Npu(n)  => format!("npu:{n}"),
    Device::Xpu(n)  => format!("xpu:{n}"),
}.into_py(py);

let kwargs = [(intern!(py, "dtype"), torch_uint8),(intern!(py, "device"), device)].into_py_dict(py);

I'm not sure what the rationale is for loading first into CPU and then moving to self.device at the end.

Fixes issue #437

maxdebayser avatar Feb 08 '24 16:02 maxdebayser

@Narsil, I'm wondering of any of the maintainers could take a look at this. It's a simple issue, but it's blocking some of our users. Thanks!

maxdebayser avatar Feb 28 '24 18:02 maxdebayser

Any updates on this?

dtrifiro avatar Mar 25 '24 14:03 dtrifiro

No, I haven't heard back from the maintainers on this.

maxdebayser avatar Mar 25 '24 14:03 maxdebayser