Use DirectML to train PyTorch machine learning models on a PC – InfoWorld

0
513
                                                                                            By <span class="fn" itemprop="name"><a rel="author" itemprop="url" href="/author/Simon-Bisson/"><span itemprop="name">Simon Bisson</span></a></span>,                                                               <span class="cn-container">                                                                     </span>                                                                  Columnist,                                                                                                                  <span itemprop="publisher" itemscope itemtype="http://schema.org/Organization"><span class="publisher" itemprop="name">InfoWorld</span><meta itemprop="url" content="https://www.infoworld.com" /><meta itemprop="logo" content="https://idge.staticworld.net/ifw/IFW_logo_social_300x300.png"/></span>                                                                 <span class="divider break">|</span>                                                                <span class="pub-date" itemprop="datePublished" content="2022-03-02T03:00-0800"><span class="format-date"></span>                                                                   </span>                             <br>Machine learning is an increasingly important tool for developers, providing a way to build applications that can deliver a wide range of prediction-based tasks. In the past you might have had to build a complex rules engine, using numeric techniques to deliver the required statistical models. Now you can work with a ML platform to build, train, and test models for your applications.<br>We call ML outputs <em>predictions</em>, but they can be anything. If you’re using computer vision, they can be <em>identified objects</em>. If you’re using a language model, they’re <em>intent</em> or <em>translations</em>. But whatever the output, it’s a statistically weighted response with a confidence level that can validate any returns.<br>There are two parts to working with machine learning. If you have a prebuilt model, you can run it from a cloud platform such as Azure ML, using a REST API to work with its predictions, or you can export it in the widely supported ONNX (Open Neural Network Exchange) format and run it on a PC using tools like WinML. That’s the easy part; the hard part is training and testing a model. That process needs a lot of validated and labeled data, as well as a significant amount of compute, either CPU or GPGPU (general-purpose GPU).<br>The logical place to train a new model is on a cloud-hosted platform, such as Azure’s Machine Learning studio. This can get expensive, requiring large virtual machines to host your models and a lot of storage for your training and test data. If you’re just learning how to build models or are creating a simple prototype with a relatively small set of training data, you’re more likely to want to use a PC.<br>A modern developer workstation has more than enough power for basic ML workloads. The machine I’m typing this on fits the bill: It has an 11th-generation Intel processor, 32GB of RAM, and a discrete Nvidia graphics card with support for GPGPU, via its own drivers or through Microsoft’s own DirectML APIs.<br>How do we set up a machine learning framework on a device like this? It can be complex, with incompatible drivers and setups that are targeted on Linux servers. <a href="https://devblogs.microsoft.com/windowsai/introducing-pytorch-directml-train-your-machine-learning-models-on-any-gpu/" rel="nofollow">Microsoft has been working to accelerate this</a>, providing a link between the popular PyTorch ML environment <a href="https://docs.microsoft.com/en-us/windows/ai/directml/gpu-pytorch-windows" rel="nofollow">and Windows’ GPU APIs, by using DirectML</a>. You’re not limited to Windows; you can also use WSL (Windows Subsystem for Linux), with the appropriate graphics drivers.<br>Using DirectML simplifies working with PyTorch, as it’s part of Microsoft’s DirectX graphics APIs. If your graphics card supports DirectML, you can use its GPU to deliver the parallel processing tasks at the heart of training a machine learning model, keeping the load on your development PC’s CPU to a minimum. Microsoft has been working with Windows GPU vendors, including Nvidia and AMD, to support training one of the more common PyTorch model types: convolutional neural networks.<br>A <a href="https://devblogs.microsoft.com/windowsai/pytorch-directml-preview-release-2/" rel="nofollow">second preview release of PyTorch-DirectML integration</a> rolled out recently, adding support for more versions of Python and support for working with multiple GPUs, allowing you to choose which GPU is being used. Integration is delivered by a new virtual device called DML. This meshes the DirectML APIs with PyTorch’s primitives, mapping calls in PyTorch to the native DirectML tools.<br>With PyTorch-DirectML, once a PyTorch tensor is called, it’s passed to the DirectML kernel. This calls into the DirectML back end, which constructs the GPGPU operators, allocates GPU memory, and sets up a queue to manage execution before passing training data and the operators to the GPU for training. It’s an approach that supports both Windows and WSL. <br>Trying it out is easy enough. The PyTorch-DirectML package is available from either <a href="https://github.com/microsoft/DirectML" rel="nofollow">GitHub</a> as part of the DirectML project or from <a href="https://pypi.org/project/pytorch-directml/" rel="nofollow">popular Python repositories like PyPl</a>. You can use familiar tools such as pip to add it to your Python environment, with only a single change to PyTorch Python code needed to run PyTorch through the DML virtual device.<br>Where things <a href="https://docs.microsoft.com/en-us/windows/ai/directml/gpu-pytorch-wsl" rel="nofollow">get interesting is with its WSL support</a>. This way you can build code that’s targeted at cloud-hosted Linux systems on your desktop. You’ll need a Windows 11 system to use the DirectML integration, using the WSLg (Windows Subsystem for Linux GUI) GUI-based system that adds tools to access the Windows graphics platform from the WSL environment. With WSL2 and WSLg installed, you next need to set up a virtual Python environment to host PyTorch.<br>Microsoft’s documentation is based around <a href="https://docs.conda.io/en/latest/miniconda.html" rel="nofollow">Miniconda Python</a> from the Anaconda team. It’s a stripped version of Anaconda that ships with <a href="https://docs.conda.io/en/latest/index.html" rel="nofollow">the conda package manager</a>, used by many Python numerical methods tools and often used by frameworks like PyTorch. Once installed, use the conda <code>create </code>and <code>activate </code>commands to set up a Python environment.<br>With that in place, install a set of required libraries before using pip to install the pytorch-directml package. This contains the DML virtual device and the supported PyTorch 1.8 release. Once installed in your Python virtual environment, you can start working with Pytorch tensors in the DML virtual device. The key to using DirectML is to use a <strong>to(“dml”)</strong> command to run on your GPU. For example, to create a simple tensor ready for use: <strong>tensor1 = torch.tensor([1]).to(“dml”)</strong><br>Microsoft has <a href="https://github.com/microsoft/DirectML/tree/master/PyTorch" rel="nofollow">a GitHub repository of samples</a> ready for use with DirectML, including the popular resnet50 image classification algorithm. Using a well-known algorithm like this makes it easier to benchmark using a developer PC to build and test your own machine learning models. By using Miniconda as the foundation of a Python development algorithm, there’s quick access to the tools you need to build and explore your algorithms, for example, working with Jupyter notebooks to share code with colleagues.<br>Not every PyTorch operator is supported in the current preview release. There’s a list of the operators you can use on GitHub, along with a <a href="https://github.com/microsoft/DirectML/wiki/PyTorch-DirectML-Operator-Roadmap" rel="nofollow">road map that shows what will be supported in the next milestone release</a>. The remaining 22 operators are marked as possibly being implemented in the future, so if you are bringing existing PyTorch code to DirectML, you should check if you have any dependencies on them.<br>The cloud is a powerful tool, but it’s important to remember that there’s plenty of power on our desktops. Tools such as PyTorch-DirectML take advantage of those often-ignored capabilities, allowing us to work wherever we want and giving access to those who can’t afford to use the cloud, for education as well as for product development. With access to common algorithms, it’s definitely a good way to start building and customizing machine learning models.<br>Author of InfoWorld's Enterprise Microsoft blog, Simon Bisson has worked in academic and telecoms research, been the CTO of a startup, run the technical side of UK Online, and done consultancy and technology strategy.<br>Copyright &copy; 2022 IDG Communications, Inc.<br><a href="/about/contactus.html#reprints">Copyright</a> &copy; 2022 IDG Communications, Inc.<br><br><a href="https://www.infoworld.com/article/3651930/use-directml-to-train-pytorch-machine-learning-models-on-a-pc.html">source</a>