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

Conv3D is not supported on MPS #2044

Open
ryanaltair opened this issue Nov 24, 2023 · 33 comments
Open

Conv3D is not supported on MPS #2044

ryanaltair opened this issue Nov 24, 2023 · 33 comments

Comments

@ryanaltair
Copy link

ryanaltair commented Nov 24, 2023

failed to run SVD(stable video diffusion) on macOS

anyone can help?

Conv3D support on macOS

Error occurred when executing KSampler:  Conv3D is not supported on MPS  File "/Users/ryanaltair/github/ai/ComfyUI/execution.py", line 153, in recursive_execute output_data, output_ui = get_output_data(obj, input_data_all) File 
@Theodor800
Copy link

Did you find solution? I have same problem....

@enzyme69
Copy link

Same here, needs solution thanks~

@groovybits
Copy link

This may help pytorch/pytorch#114183 and gets me past an error that looks similar to yours.

@Theodor800
Copy link

I also found that but don’t know what to do with it. 🫣 Some tip for a noobie…?

@ivanfioravanti
Copy link

@groovybits it works but then there are other errors like [MPSNDArray initWithDevice:descriptor:] Error: total bytes of NDArray > 2**32

MPS is alpha not beta 😢

@jwooldridge234
Copy link

@Theodor800 I installed that pull request with: pip install git+https://github.com/pytorch/pytorch@refs/pull/114183/head. Takes A WHILE.

I get this error now on the KSampler:

`Error occurred when executing KSampler:

unsupported operand type(s) for *=: 'int' and 'NoneType'

File "/Users/jackwooldridge/StableDiffusion/ComfyUI/execution.py", line 153, in recursive_execute
output_data, output_ui = get_output_data(obj, input_data_all)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/execution.py", line 83, in get_output_data
return_values = map_node_over_list(obj, input_data_all, obj.FUNCTION, allow_interrupt=True)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/execution.py", line 76, in map_node_over_list
results.append(getattr(obj, func)(**slice_dict(input_data_all, i)))
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/nodes.py", line 1286, in sample
return common_ksampler(model, seed, steps, cfg, sampler_name, scheduler, positive, negative, latent_image, denoise=denoise)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/nodes.py", line 1256, in common_ksampler
samples = comfy.sample.sample(model, noise, steps, cfg, sampler_name, scheduler, positive, negative, latent_image,
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/sample.py", line 100, in sample
samples = sampler.sample(noise, positive_copy, negative_copy, cfg=cfg, latent_image=latent_image, start_step=start_step, last_step=last_step, force_full_denoise=force_full_denoise, denoise_mask=noise_mask, sigmas=sigmas, callback=callback, disable_pbar=disable_pbar, seed=seed)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/custom_nodes/ComfyUI_smZNodes/init.py", line 129, in KSampler_sample
return _KSampler_sample(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/samplers.py", line 711, in sample
return sample(self.model, noise, positive, negative, cfg, self.device, sampler, sigmas, self.model_options, latent_image=latent_image, denoise_mask=denoise_mask, callback=callback, disable_pbar=disable_pbar, seed=seed)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/custom_nodes/ComfyUI_smZNodes/init.py", line 138, in sample
return _sample(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/samplers.py", line 617, in sample
samples = sampler.sample(model_wrap, sigmas, extra_args, callback, noise, latent_image, denoise_mask, disable_pbar)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/samplers.py", line 556, in sample
samples = self.sampler_function(model_k, noise, sigmas, extra_args=extra_args, callback=k_callback, disable=disable_pbar, **self.extra_options)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/torch/utils/_contextlib.py", line 115, in decorate_context
return func(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/k_diffusion/sampling.py", line 580, in sample_dpmpp_2m
denoised = model(x, sigmas[i] * s_in, **extra_args)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/samplers.py", line 277, in forward
out = self.inner_model(x, sigma, cond=cond, uncond=uncond, cond_scale=cond_scale, model_options=model_options, seed=seed)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1520, in call_impl
return forward_call(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/samplers.py", line 267, in forward
return self.apply_model(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/custom_nodes/ComfyUI_smZNodes/smZNodes.py", line 892, in apply_model
out = super().apply_model(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/samplers.py", line 264, in apply_model
out = sampling_function(self.inner_model, x, timestep, uncond, cond, cond_scale, model_options=model_options, seed=seed)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/samplers.py", line 252, in sampling_function
cond, uncond = calc_cond_uncond_batch(model, cond, uncond, x, timestep, model_options)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/samplers.py", line 230, in calc_cond_uncond_batch
output = model.apply_model(input_x, timestep
, **c).chunk(batch_chunks)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/custom_nodes/ComfyUI_smZNodes/modules/sd_hijack_utils.py", line 17, in
setattr(resolved_obj, func_path[-1], lambda *args, **kwargs: self(*args, **kwargs))
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/custom_nodes/ComfyUI_smZNodes/modules/sd_hijack_utils.py", line 28, in call
return self.__orig_func(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/model_base.py", line 73, in apply_model
model_output = self.diffusion_model(xc, t, context=context, control=control, transformer_options=transformer_options, **extra_conds).float()
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/custom_nodes/FreeU_Advanced/nodes.py", line 173, in __temp__forward
h = forward_timestep_embed(module, h, emb, context, transformer_options)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/ldm/modules/diffusionmodules/openaimodel.py", line 38, in forward_timestep_embed
x = layer(x, emb, num_video_frames, image_only_indicator)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1511, in _wrapped_call_impl
return self._call_impl(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/torch/nn/modules/module.py", line 1520, in _call_impl
return forward_call(*args, **kwargs)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/comfy/ldm/modules/diffusionmodules/openaimodel.py", line 337, in forward
x_mix = rearrange(x, "(b t) c h w -> b c t h w", t=num_video_frames)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/einops/einops.py", line 591, in rearrange
return reduce(tensor, pattern, reduction="rearrange", **axes_lengths)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/einops/einops.py", line 523, in reduce
return _apply_recipe(
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/einops/einops.py", line 239, in _apply_recipe
_result = _reconstruct_from_shape_uncached(recipe, backend.shape(tensor), axes_lengths)
File "/Users/jackwooldridge/StableDiffusion/ComfyUI/venv/lib/python3.9/site-packages/einops/einops.py", line 179, in _reconstruct_from_shape_uncached
known_product *= axes_lengths[axis]`

@Theodor800
Copy link

@jwooldridge234 Thank you. Have to try that soon. I had also that operand type error, but you'll solve that by uninstalling FreeU_Advanced node.

@jwooldridge234
Copy link

@Theodor800 Thanks- that solved that error. Now just crashes on render (running the first img2vid workflow from the examples):

Requested to load SVD_img2vid Loading 1 new model 0%| | 0/20 [00:00<?, ?it/s]/Users/jackwooldridge/miniconda3/lib/python3.9/multiprocessing/resource_tracker.py:216: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown warnings.warn('resource_tracker: There appear to be %d ' zsh: segmentation fault python main.py

@Theodor800
Copy link

@jwooldridge234 Errors after errors everyday. 😀 I hope you find solution for that one. I got mine to work now. So thank you very much! And thanks @groovybits!

@LucasSte
Copy link

This may help pytorch/pytorch#114183 and gets me past an error that looks similar to yours.

I'm working on that PR. I found an issue in the Conv3D implementation and I need to debug it and write tests for it before making the PR as ready for review.

@gravityrail
Copy link

gravityrail commented Nov 28, 2023

Try something like:

pip install git+https://github.com/pytorch/pytorch@refs/pull/114183/head git+https://github.com/pytorch/vision git+https://github.com/pytorch/audio

At least until @LucasSte 's PR is merged.

@MayorDapper
Copy link

Error occurred when executing KSampler:

Conv3D is not supported on MPS

Hi All, anyone able to solve this one please.

@thekannanmca
Copy link

Please help on this issue

@MayorDapper
Copy link

MayorDapper commented Nov 29, 2023

I dont know if this helps but I just found it https://github.com/cocktailpeanut/mac-svd-install

@thekannanmca
Copy link

I tried pip install git+https://github.com/pytorch/pytorch@refs/pull/114183/head git+https://github.com/pytorch/vision git+https://github.com/pytorch/audio

Its taking almost 2 hours and showing below command

Preparing metadata (pyproject.toml) ... |

@thekannanmca
Copy link

Finally end up with below error.

Resolved https://github.com/pytorch/audio to commit ccd78ffba3208b4a9f5face4bd76e942ddc2d4a3
Running command git submodule update --init --recursive -q
Installing build dependencies ... done
Getting requirements to build wheel ... error
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> [17 lines of output]
Traceback (most recent call last):
File "/opt/anaconda3/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 353, in
main()
File "/opt/anaconda3/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 335, in main
json_out['return_val'] = hook(**hook_input['kwargs'])
File "/opt/anaconda3/lib/python3.9/site-packages/pip/_vendor/pyproject_hooks/_in_process/_in_process.py", line 118, in get_requires_for_build_wheel
return hook(config_settings)
File "/private/tmp/pip-build-env-v4hijz0j/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 325, in get_requires_for_build_wheel
return self._get_build_requires(config_settings, requirements=['wheel'])
File "/private/tmp/pip-build-env-v4hijz0j/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 295, in _get_build_requires
self.run_setup()
File "/private/tmp/pip-build-env-v4hijz0j/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 480, in run_setup
super(_BuildMetaLegacyBackend, self).run_setup(setup_script=setup_script)
File "/private/tmp/pip-build-env-v4hijz0j/overlay/lib/python3.9/site-packages/setuptools/build_meta.py", line 311, in run_setup
exec(code, locals())
File "", line 9, in
ModuleNotFoundError: No module named 'torch'
[end of output]

note: This error originates from a subprocess, and is likely not a problem with pip.
error: subprocess-exited-with-error

× Getting requirements to build wheel did not run successfully.
│ exit code: 1
╰─> See above for output.

note: This error originates from a subprocess, and is likely not a problem with pip.

@MayorDapper
Copy link

Pytorch issue. I had similar error and I couldnt even run comfyUI anymore after trying it so had to uninstall torch and re-install. Comfy works now but still cannot get SVD to run. Probably best to wait till its resolved.

@soederpop
Copy link

@thekannanmca i'm also experiencing the same exact error. please let me know if you figure out a workaround

@LucasSte
Copy link

LucasSte commented Nov 30, 2023

A workaround would be using the code from my PR (pytorch/pytorch#114183). You would need to clone the repository, fetch my PR's changes and build PyTorch from scratch. This is a bit time consuming.

git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
git submodule update --init --recursive
git fetch origin pull/114183/head:conv-3d-mps
git checkout conv-3d-mps
python3 setup.py develop

@mastroent
Copy link

The core issue encountered was that the Conv3D operation is currently not supported on Apple's Metal Performance Shaders (MPS) through PyTorch. To utilize Conv3D with MPS, we must await an update from PyTorch that extends support for this functionality. In the interim, I devised a workaround to circumvent this limitation. The modification was made in the model_management.py file within the ComfyUI directory:

The original function get_torch_device() contained logic to determine and return the appropriate compute device based on the current environment's capabilities:

def get_torch_device():
global directml_enabled
global cpu_state
if directml_enabled:
global directml_device
return directml_device
if cpu_state == CPUState.MPS:
return torch.device("mps")
if cpu_state == CPUState.CPU:
return torch.device("cpu")
else:
if is_intel_xpu():
return torch.device("xpu")
else:
return torch.device(torch.cuda.current_device())
This was simplified to exclusively direct PyTorch to utilize the CPU for all operations:

def get_torch_device():
return torch.device("cpu")

By applying this change, the Conv3D operation and other computations are forced to run on the CPU. While this resolves the immediate compatibility issue, it's important to note that computation will be slower due to the CPU's comparative performance limitations. The model will function correctly, albeit with increased processing times for generating the output.

This solution is a temporary measure until PyTorch introduces MPS support for the Conv3D operation, after which full hardware acceleration can be leveraged for improved efficiency.

@YAY-3M-TA3
Copy link

A workaround would be using the code from my PR (pytorch/pytorch#114183). You would need to clone the repository, fetch my PR's changes and build PyTorch from scratch. This is a bit time consuming.

git clone --recursive https://github.com/pytorch/pytorch
cd pytorch
git submodule update --init --recursive
git fetch origin pull/114183/head:conv-3d-mps
git checkout conv-3d-mps
python3 setup.py develop

I'm getting a segmentation fault 11 once it gets to the ksampler (Running on a mac m2 with 24G). I also saw that someone running on an M1 Ultra studio has the same issue. Stability-AI/StableSwarmUI#185 (comment).

@mynameisbill
Copy link

@jwooldridge234 I have the same error that you had last week.

UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
warnings.warn('resource_tracker: There appear to be %d '

Did you ever find a solution?

@jwooldridge234
Copy link

@mynameisbill Afraid not- going to wait until pytorch gets patched to get this working locally.

@thekannanmca
Copy link

As of now I am using AnimatedDiff, I am hoping for the best for this issue to be resolved

@yovizzle
Copy link

yovizzle commented Dec 16, 2023

Bumping as this appears to have been fixed in pytorch #pytorch/pytorch#114183

@yondonfu
Copy link

FWIW I just tried running SVD using ComfyUI with the latest PyTorch nightly that includes Conv3D support on a 16gb M1.

I am no longer getting the Conv3D not supported error, but I am running out of memory:

RuntimeError: MPS backend out of memory (MPS allocated: 8.39 GB, other allocations: 9.40 GB, max allowed: 18.13 GB). Tried to allocate 420.00 MB on private pool. Use PYTORCH_MPS_HIGH_WATERMARK_RATIO=0.0 to disable upper limit for memory allocations (may cause system failure).

Prompt executed in 89.71 seconds

I ran ComfyUI using python main.py --force-fp16 and was using the svd-fp16.safetensors checkpoint from https://huggingface.co/becausecurious/stable-video-diffusion-img2vid-fp16.

@yyahav
Copy link

yyahav commented Dec 26, 2023

Running ComfyUI with SVD XT on MBP M1 32GB.

Installed pytorch versions:
torch 2.2.0a0+gitc9950e1
torchaudio 0.13.1
torchvision 0.14.1

Using the --force-fp16 option, and the svd_xt.safetensors

Although it's the 25 frames model, I too get RuntimeError: MPS backend out of memory even while using euler_ancestral sampler and karras scheduler.

The only way I get it to work without errors is to configure the conditioning node to video_frames: 14 instead of 25.
At least it's working and I can learn to use it, until 25 frames will fit into 32GB...
Still, it's painfully slow:
100%|███████████████████████████████████████████| 20/20 [22:52<00:00, 68.60s/it]

@KarthikDani
Copy link

What about Macbook M1 Air with 8GB RAM?

@yyahav
Copy link

yyahav commented Jan 23, 2024

@KarthikDani haven't tried with the M1 Air 8GB, I believe there's not enough RAM, and with the pro it takes over 20 minutes so yeah...

@leoyuk
Copy link

leoyuk commented Feb 23, 2024

The core issue encountered was that the Conv3D operation is currently not supported on Apple's Metal Performance Shaders (MPS) through PyTorch. To utilize Conv3D with MPS, we must await an update from PyTorch that extends support for this functionality. In the interim, I devised a workaround to circumvent this limitation. The modification was made in the model_management.py file within the ComfyUI directory:

The original function get_torch_device() contained logic to determine and return the appropriate compute device based on the current environment's capabilities:

def get_torch_device(): global directml_enabled global cpu_state if directml_enabled: global directml_device return directml_device if cpu_state == CPUState.MPS: return torch.device("mps") if cpu_state == CPUState.CPU: return torch.device("cpu") else: if is_intel_xpu(): return torch.device("xpu") else: return torch.device(torch.cuda.current_device()) This was simplified to exclusively direct PyTorch to utilize the CPU for all operations:

def get_torch_device(): return torch.device("cpu")

By applying this change, the Conv3D operation and other computations are forced to run on the CPU. While this resolves the immediate compatibility issue, it's important to note that computation will be slower due to the CPU's comparative performance limitations. The model will function correctly, albeit with increased processing times for generating the output.

This solution is a temporary measure until PyTorch introduces MPS support for the Conv3D operation, after which full hardware acceleration can be leveraged for improved efficiency.

It works for me but Too slow to apply it...

@Kaputmaher
Copy link

I had the same error in Stable Diffusion Forge installed inside Pinokio.

It was solved by reinstalling Torch, i.e. doing the following:

Close Pinokio
cd /Users/myuser/pinokio/api/forge.git/app
source venv/bin/activate
pip uninstall torch torchvision torchaudio
pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu

By the way, both Automatic1111 and ComfyUI are too slow on Mac Silicon.
I feel that Forge now works on my M2 Max / 32 Gb faster than Colab PRO.

@gilberthello
Copy link

I had the same error in Stable Diffusion Forge installed inside Pinokio.

It was solved by reinstalling Torch, i.e. doing the following:

Close Pinokio cd /Users/myuser/pinokio/api/forge.git/app source venv/bin/activate pip uninstall torch torchvision torchaudio pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu

By the way, both Automatic1111 and ComfyUI are too slow on Mac Silicon. I feel that Forge now works on my M2 Max / 32 Gb faster than Colab PRO.

thx a lot, this approach really worked. after several hours trying. Tried on M3, python 3.11.7. 24/03/28

@wsyeasy
Copy link

wsyeasy commented Apr 16, 2024

I had the same error in Stable Diffusion Forge installed inside Pinokio.

It was solved by reinstalling Torch, i.e. doing the following:

Close Pinokio cd /Users/myuser/pinokio/api/forge.git/app source venv/bin/activate pip uninstall torch torchvision torchaudio pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cpu

By the way, both Automatic1111 and ComfyUI are too slow on Mac Silicon. I feel that Forge now works on my M2 Max / 32 Gb faster than Colab PRO.

Thx too. It works. but 'ConvTranspose 3D is not supported on MPS' has not fix in pre version

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