microsoft / DirectML

DirectML is a high-performance, hardware-accelerated DirectX 12 library for machine learning. DirectML provides GPU acceleration for common machine learning tasks across a broad range of supported hardware and drivers, including all DirectX 12-capable GPUs from vendors such as AMD, Intel, NVIDIA, and Qualcomm.
MIT License
2.22k stars 297 forks source link

Using DirectML from C# #100

Open vishvanathan-k opened 3 years ago

vishvanathan-k commented 3 years ago

I know I could PInvoke the nuget package but how will I create a console app which uses DirectML and also will I be able to use a ML.NET with DirectML. Samples for this would be appreciated (or a gist). P.S: I am a beginner in ML.NET

jstoecker commented 3 years ago

This isn't something we support at the moment. It's possible, but you'd have to write your own bindings or wrapper/abstraction around DirectML. Additionally, DirectML in total isolation isn't particularly useful since you need Direct3D 12 for allocating resources and submitting work.

DirectML & D3D12 are both low-level "nano-COM" style APIs that don't have out-of-the-box language projections like WinRT APIs. There are other higher-level APIs like Windows ML & ONNX Runtime that layer on top of DML & D3D, but if you want to program directly against DML/D3D you need to drop down to C++.

The most viable approach is to probably to write a higher-level library in C++ that handles allocation and submission, then create bindings to use that library in the language of your choice; this is how the PyDirectML bindings work.

vishvanathan-k commented 3 years ago

hmm...... any idea when it will be supported ? or is it possible to just convert PyDirectML to into C#

wchao1115 commented 3 years ago

Have you looked at https://github.com/Microsoft/xlang?

We would love to know how DirectML API can be projected to other languages through xlang. We're open to contribution if you want to have a run with it.

PyDirectML is done manually and is specific to Python only today.

Axiverse commented 2 years ago

I've implemented DirectML bindings through https://github.com/amerkoleci/Vortice.Windows/tree/main/src/Vortice.DirectML with the HelloDirectML example as well. The example works and everything is technically implemented, but I'll be refining the API. Expect it to stabilize over the next few weeks. Give it a try, LMK if there are any issues.

The nuget package is here: https://www.nuget.org/packages/Vortice.DirectML/2.1.10-beta

You can see sample code here: https://github.com/amerkoleci/Vortice.Windows/blob/main/src/samples/HelloDirectML/DmlDevice.cs

BrainSlugs83 commented 5 months ago

DirectML & D3D12 are both low-level "nano-COM" style APIs that don't have out-of-the-box language projections like WinRT APIs. There are other higher-level APIs like Windows ML & ONNX Runtime that layer on top of DML & D3D, but if you want to program directly against DML/D3D you need to drop down to C++.

@jstoecker -- is Windows ML a different Runtime / API than Direct ML? -- Boy that's confusing. Does it have a .NET API?

At the end of the day, as a Windows App developer who abandoned C++ nearly 20 years ago, in favor of Microsoft's C#, I need guidance on how best to continue to do that with officially supported libraries and runtimes.

If Direct ML isn't Windows ML, it would be nice if the main page called that out, and gave us links of where Windows App developers should go to find this stuff, because it's not easily searchable or discoverable. And the MS Learn pages for the Windows Machine Learning Runtime sent me here. -- And even searching for Windows.AI.MachineLearning (as opposed to Microsoft.AI.MachineLearning) just links back to this page. So... any guidance on resources for this stuff would be really helpful.