Archives

Archives / 2020
  • Setup and use CUDA and TensorFlow in Windows Subsystem for Linux 2

    Table of contents

    • Install Windows preview
    • Install WSL 2 preview
    • Install Nvidia driver preview and CUDA toolkit
      • Run CUDA sample application
    • Install Docker and Nvidia container toolkit
      • Run CUDA containers
      • Troubleshoot
    • Run WSL + CUDA + Docker + Jupyter + TensorFlow
    • Encoding and decoding video with GPU in WSL?

    GPU support is the most requested feature in Windows Subsystem for Linux (WSL). It is available in WSL 2.0 through Windows Insiders program. And Nvidia CUDA is supported. The following diagram shows the WDDM model supporting CUDA user mode driver running inside Linux guest:

    In the Linux guest, the CUDA user mode library talks to dxgkrnl driver's /dev/dxg device using IoCtls wrapped with libdxcore API. The dxg requests then get forwarded to the Windows host system using VMBus where for those the host dxgkrnl driver makes calls to the KMD (Kernel Mode Driver) DDI handlers.

    So the popular Linux AI frameworks like TensorFlow, PyTorch, etc. can work with WSL with CUDA acceleration:

    The diagram shows Microsoft Windows GPU machines running on the NVIDIA hardware. For the software layers, it shows the Windows kernel, NVIDIA Windows driver, GPU virtualization, WSL2 environment (Linux kernel), NVIDIA CUDA, and other Linux AI frameworks and apps.

    This article walks through the installation of Windows, WSL, CUDA in WSL, and Docker in WSL.

    Install Windows preview

    First, you must enable “Optional diagnostic data”, otherwise Windows cannot join Windows Insiders.

    image

    Then, join Windows Insiders program with Microsoft account (an account can be created if you don’t have one: https://insider.windows.com/). The channel must be Dev:

    image

    Then run Windows Update. It will download the pre=release installer. Windows will restart and reinstall.

    Install WSL 2 preview

    In Windows, make sure the following Windows features are enabled:

    • WSL:
      dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
    • Virtual machine platform:
      dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

    Now restart Windows, then Windows will have WSL and the wsl command line tool. Run Windows Update again to get the latest WSL 2. When this is done, in the update history, it must show 4.19.121 or later:

    image

    Then manually install this patch: https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi. And then run the following command as administrator to update the kernel to the latest version:

    C:\WINDOWS\system32>wsl --update
    Checking for updates...
    Downloading updates...
    Installing updates...
    This change will take effect on the next full restart of WSL. To force a restart, please run 'wsl --shutdown'.
    Kernel version: 5.4.72

  • Update code font from Consolas to Cascadia Code with ligature

    A decade ago, I blogged that I switched my code font from Courier New to Consolas. They are both monospaced. The difference is:

    • Courier New is an old font introduced in Windows 3.1.
    • Consolas is introduced with Windows Vista/Office 2007/Visual Studio 2010. It always uses ClearType, which is designed for LCD screens and other flat panels which arrange their pixels in vertical stripes of red, green and blue.

    image