From c5dfd1ce812d5da9ff5509348bb21b1d368660f2 Mon Sep 17 00:00:00 2001 From: Cassie Breviu <46505951+cassiebreviu@users.noreply.github.com> Date: Fri, 17 Mar 2023 19:14:11 -0500 Subject: [PATCH] Add csharp dml sample code to EP doc and Stable Diff Repo (#15103) --- .../DirectML-ExecutionProvider.md | 12 ++++++++++++ docs/tutorials/csharp/stable-diffusion-csharp.md | 7 ++++++- 2 files changed, 18 insertions(+), 1 deletion(-) diff --git a/docs/execution-providers/DirectML-ExecutionProvider.md b/docs/execution-providers/DirectML-ExecutionProvider.md index ddd147c103..8f36cbaa90 100644 --- a/docs/execution-providers/DirectML-ExecutionProvider.md +++ b/docs/execution-providers/DirectML-ExecutionProvider.md @@ -67,6 +67,7 @@ When using the [C API](../get-started/with-c.md) with a DML-enabled build of onn Creates a DirectML Execution Provider which executes on the hardware adapter with the given `device_id`, also known as the adapter index. The device ID corresponds to the enumeration order of hardware adapters as given by [IDXGIFactory::EnumAdapters](https://docs.microsoft.com/windows/win32/api/dxgi/nf-dxgi-idxgifactory-enumadapters). A `device_id` of 0 always corresponds to the default adapter, which is typically the primary display GPU installed on the system. Beware that in systems with multiple GPU's, the primary display (GPU 0) is often not the most performant one, particularly on laptops with dual adapters where battery lifetime is preferred over performance. So you can double check in Task Manager's performance tab to see which GPU is which. A negative `device_id` is invalid. +Example for C API: ```c OrtStatus* OrtSessionOptionsAppendExecutionProvider_DML( _In_ OrtSessionOptions* options, @@ -74,6 +75,16 @@ OrtStatus* OrtSessionOptionsAppendExecutionProvider_DML( ); ``` +Example for C# API: + +Install the Nuget Package [Microsoft.ML.OnnxRuntime.DirectML](https://www.nuget.org/packages/Microsoft.ML.OnnxRuntime.DirectML/1.14.1) and use the following code to enable the DirectML EP: + +```csharp +SessionOptions sessionOptions = newSessionOptions(); +sessionOptions.GraphOptimizationLevel = GraphOptimizationLevel.ORT_ENABLE_ALL; +sessionOptions.AppendExecutionProvider_DML(0); +``` + ### `OrtSessionOptionsAppendExecutionProviderEx_DML` function {: .no_toc } @@ -87,6 +98,7 @@ OrtStatus* OrtSessionOptionsAppendExecutionProviderEx_DML( ); ``` + ## Configuration Options The DirectML execution provider does not support the use of memory pattern optimizations or parallel execution in onnxruntime. When supplying session options during InferenceSession creation, these options must be disabled or an error will be returned. diff --git a/docs/tutorials/csharp/stable-diffusion-csharp.md b/docs/tutorials/csharp/stable-diffusion-csharp.md index 8a4e048c8d..594b5596bb 100644 --- a/docs/tutorials/csharp/stable-diffusion-csharp.md +++ b/docs/tutorials/csharp/stable-diffusion-csharp.md @@ -34,7 +34,12 @@ This tutorial can be run locally or in the cloud by leveraging Azure Machine Lea To run locally: - [Visual Studio](https://visualstudio.microsoft.com/downloads/) or [VS Code](https://code.visualstudio.com/Download) -- A GPU enabled machine with CUDA EP Configured. This was built on a GTX 3070 and it has not been tested on anything smaller. Follow [this tutorial to configure CUDA and cuDNN for GPU with ONNX Runtime and C# on Windows 11](https://onnxruntime.ai/docs/tutorials/csharp/csharp-gpu.html) + +- A GPU enabled machine with CUDA or DirectML on Windows + - Configure CUDA EP. Follow [this tutorial to configure CUDA and cuDNN for GPU with ONNX Runtime and C# on Windows 11](https://onnxruntime.ai/docs/tutorials/csharp/csharp-gpu.html) + - Windows comes with DirectML support. No additional configuration is needed. Be sure to clone the [`direct-ML-EP`](https://github.com/cassiebreviu/StableDiffusion/tree/direct-ML-EP) branch of this repo if you choose this option. + - This was built on a GTX 3070 and it has not been tested on anything smaller. + To run in the cloud with Azure Machine Learning: