lllyasviel / sd-forge-layerdiffuse

[WIP] Layer Diffusion for WebUI (via Forge)
Apache License 2.0
3.79k stars 327 forks source link

Fix for Apple Silicon #121

Open shirecoding opened 1 month ago

shirecoding commented 1 month ago

the following fix is required for apple silicon, note the additional .float(), else the forge script will give this error

 File "/Applications/Data/Packages/Stable Diffusion WebUI Forge/extensions/sd-forge-layerdiffuse/lib_layerdiffusion/models.py", line 277, in estimate_augmented
        median = torch.median(result.cpu(), dim=0).values
    RuntimeError: "median_out" not implemented for 'Half'

in lib_layerdiffusion/models.py

if self.load_device == torch.device("mps"):
    '''
    In case that apple silicon devices would crash when calling torch.median() on tensors
    in gpu vram with dimensions higher than 4, we move it to cpu, call torch.median()
    and then move the result back to gpu.
    '''
    result_cpu = result.cpu().float()  # Convert to float32 on CPU
    median = torch.median(result_cpu, dim=0).values
    median = median.to(device=self.load_device, dtype=self.dtype)
else:
    median = torch.median(result, dim=0).values