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

Experimental DirectML support via torch-directml and onnxruntime-directml #1702

Open
wants to merge 17 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
8e55334
Enable DirectML acceleration and improve device handling
kazssym Feb 18, 2024
26d2ed9
Refine device handling for PyTorch 2.0+ and device type check
kazssym Feb 18, 2024
4502bc4
Support privateuseone device for PyTorch model export
kazssym Feb 25, 2024
99c92b6
Enable DML device support for PyTorch models in Optimum
kazssym Feb 25, 2024
08253df
Remove redundant DML device handling in optimum_cli.py
kazssym Feb 25, 2024
6bf9b39
Merge branch 'huggingface:main' into feature/directml
kazssym Feb 27, 2024
107879e
Add DML-specific dependencies to `setup.py`
kazssym Feb 28, 2024
b90b059
Merge remote-tracking branch 'upstream/main' into feature/directml
kazssym Feb 28, 2024
5231c8f
Merge branch 'feature/directml' of https://github.com/kazssym/optimum…
kazssym Feb 28, 2024
6134c8c
Merge branch 'huggingface:main' into feature/directml
kazssym Mar 17, 2024
19c1db4
Merge remote-tracking branch 'origin/main' into feature/directml
kazssym Jan 12, 2025
5253846
Merge remote-tracking branch 'upstream/main' into feature/directml
kazssym Jan 17, 2025
ae49e73
Enhance package availability check to support multiple distribution n…
kazssym Jan 19, 2025
e84dadb
Add torch-directml to exporters-directml requirements
kazssym Jan 19, 2025
e6d15fe
Merge remote-tracking branch 'upstream/main' into feature/directml
kazssym Feb 2, 2025
66ed0ff
Add support for onnxruntime-directml in import utilities
kazssym Feb 2, 2025
471f803
Merge remote-tracking branch 'upstream/main' into feature/directml
kazssym Feb 16, 2025
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions optimum/exporters/onnx/convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

import copy
import gc
import importlib
import multiprocessing as mp
import os
import traceback
Expand Down Expand Up @@ -532,15 +533,19 @@ def export_pytorch(
# Check that inputs match, and order them properly
dummy_inputs = config.generate_dummy_inputs(framework="pt", **input_shapes)

device = torch.device(device)
if device == "dml" and importlib.util.find_spec("torch_directml"):
torch_directml = importlib.import_module("torch_directml")
device = torch_directml.device()
else:
device = torch.device(device)

def remap(value):
if isinstance(value, torch.Tensor):
value = value.to(device)

return value

if device.type == "cuda" and torch.cuda.is_available():
if device.type == "cuda" and torch.cuda.is_available() or device.type == "privateuseone":
model.to(device)
dummy_inputs = tree_map(remap, dummy_inputs)

Expand Down
8 changes: 6 additions & 2 deletions optimum/exporters/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -2276,12 +2276,16 @@ def get_model_from_task(
kwargs["torch_dtype"] = torch_dtype

if isinstance(device, str):
device = torch.device(device)
if device == "dml" and importlib.util.find_spec("torch_directml"):
torch_directml = importlib.import_module("torch_directml")
device = torch_directml.device()
else:
device = torch.device(device)
elif device is None:
device = torch.device("cpu")

# TODO : fix EulerDiscreteScheduler loading to enable for SD models
if version.parse(torch.__version__) >= version.parse("2.0") and library_name != "diffusers":
if version.parse(torch.__version__) >= version.parse("2.0") and library_name != "diffusers" and device.type != "privateuseone":
with device:
# Initialize directly in the requested device, to save allocation time. Especially useful for large
# models to initialize on cuda device.
Expand Down
9 changes: 7 additions & 2 deletions optimum/onnxruntime/io_binding/io_binding_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import importlib
import logging
import traceback
from typing import TYPE_CHECKING
Expand Down Expand Up @@ -145,8 +146,12 @@ def to_pytorch_via_dlpack(ort_value: OrtValue) -> torch.Tensor:
@staticmethod
def get_device_index(device):
if isinstance(device, str):
# could be 'cuda:0', 'cuda:1', or 'cpu'. with cpu, set index=0
device = torch.device(device)
if device == "dml" and importlib.util.find_spec("torch_directml"):
torch_directml = importlib.import_module("torch_directml")
device = torch_directml.device()
else:
# could be 'cuda:0', 'cuda:1', or 'cpu'. with cpu, set index=0
device = torch.device(device)
elif isinstance(device, int):
return device
return 0 if device.index is None else device.index
Expand Down
2 changes: 2 additions & 0 deletions optimum/utils/import_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,8 @@ def _is_package_available(
"onnxruntime-migraphx",
"ort-migraphx-nightly",
"ort-rocm-nightly",
# For DirectML
"onnxruntime-directml",
],
)
_tf_available, _tf_version = _is_package_available(
Expand Down
16 changes: 16 additions & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,15 @@
"protobuf>=3.20.1",
"transformers>=4.36,<4.49.0",
],
"onnxruntime-directml": [
"onnx",
"onnxruntime-directml>=1.11.0",
"datasets>=1.2.1",
"evaluate",
"protobuf>=3.20.1",
"accelerate", # ORTTrainer requires it.
"transformers>=4.36,<4.48.0",
],
"exporters": [
"onnx",
"onnxruntime",
Expand All @@ -81,6 +90,13 @@
"timm",
"transformers>=4.36,<4.49.0",
],
"exporters-directml": [
"torch-directml",
"onnx",
"onnxruntime-directml",
"timm",
"transformers>=4.36,<4.48.0",
],
"exporters-tf": [
"tensorflow>=2.4,<=2.12.1",
"tf2onnx",
Expand Down