CPU inference hangs due to forced torch.float16 usage
Environment OS: Pop!_OS (Linux) Device: Intel Core i7 laptop (CPU only, no CUDA)
Command:
uv run corridorkey_cli.py --device cpu generate-alphas Issue
The CLI exposes a --device cpu option, but there are several places where the code unconditionally forces torch.float16.
Initially, I received the warning:
Pipelines loaded with dtype=torch.float16 cannot run with cpu device.
It is not recommended to move them to cpu as running them will fail.
Please make sure to use an accelerator to run the pipeline in inference,
due to the lack of support for float16 operations on this device in PyTorch.
The process then remained at:
Inferencing Input: 0%
for a very long time.
Investigation
I found several unconditional FP16 conversions:
gvm_core/wrapper.py self.pipe = self.pipe.to(self.device, dtype=torch.float16)
and later:
batch.to(self.device, dtype=torch.float16) gvm_core/gvm/pipelines/pipeline_gvm.py self.vae.to(dtype=torch.float16)
These execute even when --device cpu is selected.
Temporary workaround
I modified the code so that CPU uses torch.float32 while GPU continues using torch.float16.
For example:
self.dtype = torch.float32 if self.device.type == "cpu" else torch.float16 self.pipe = self.pipe.to(self.device, dtype=self.dtype)
and used self.dtype instead of hardcoding torch.float16 elsewhere.
For the VAE I changed the dtype based on the current device instead of always forcing FP16.
After these changes the previous warning disappeared, the models loaded successfully, and inference progressed further.
Question
Is CPU inference officially supported?
If so, would it make sense for the code to automatically select torch.float32 whenever --device cpu is used instead of forcing torch.float16 in multiple locations?
Source: nikopueringer/CorridorKey