`--directml` crashes with a native DLL error when torch-directml is missing or incompatible

Open Beginner friendly
#17,695 0 comments 0 reactions 0 assignees View on GitHub

Maintainers usually reply within 1 day

Nobody has claimed this yet.

Assessment

Difficulty
2/5
Estimated time
1-3 hours
Newbie friendliness
75/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
python, pytorch
Domain
ai, backend

Research direction

Start in comfy/model_management.py at the torch_directml import and device selection described in the issue. Check how the default CUDA/CPU device is chosen and how import failures are handled. Run ComfyUI with --directml both with torch-directml unavailable and with a compatible installation; done means an import failure warns and falls back, while a working DirectML installation still selects DirectML.

Written by the indexing model from the issue text.

Description

Problem

Launching ComfyUI with --directml when torch-directml is missing or incompatible crashes the whole process with a native DLL exception instead of a clear Python error.

With an incompatible build installed (e.g. torch-directml 0.2.3.dev240715 built for torch 2.3.1, on torch 2.9.1):

File "comfy\model_management.py", line 114, in <module>
    import torch_directml
ImportError: DLL load failed while importing torch_directml_native: The specified procedure could not be found.
Windows fatal exception: code 0xc0000139

With the package removed entirely:

ModuleNotFoundError: No module named 'torch_directml'

In both cases there is no fallback, so the app exits with code 1.

Why this happens

Portable distributions commonly ship torch-directml pinned to an old torch version. When the user upgrades torch, a launcher may still pass --directml, and model_management.py imports the stale native module unconditionally.

Suggestion

Wrap the import in try/except ImportError and fall back to the default device (CUDA/CPU) with a warning, so the app stays launchable:

if args.directml is not None:
    logging.warning("WARNING: torch-directml barely works, ...")
    try:
        import torch_directml
    except ImportError:
        logging.warning("torch_directml is not installed, ignoring --directml and falling back to the default device.")
    else:
        directml_enabled = True
        device_index = args.directml
        ...

I verified this locally: with the fallback in place, main.py --directml prints the warning and starts normally on CUDA.

Environment

  • ComfyUI 0.45.2
  • Python 3.11.9, Windows 11
  • torch 2.9.1+cu128
  • torch-directml 0.2.3.dev240715 (built against torch 2.3.1)
Dominant language
Python
Stars
146k
Forks
17.4k
Avg merge
19h 44m
Merged PRs (30d)
178

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Comfy-Org/ComfyUI

All issues in Comfy-Org/ComfyUI

Similar issues

More Python issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.