Getting Started with TensorFlow-GPU and TouchDesigner For Advanced

Hello TouchDesigners, and welcome aboard the TensorFlow hype train! Choo choo!
One of our users recently contacted us and was wondering how to run TensorFlow-GPU, with Anaconda, in TouchDesigner. Running TensorFlow without GPU support was fairly easy, but we did run into issues getting GPU support working. While it would take an incredible amount of time and resources to test all of TensorFlow, or other use cases where TensorFlow is a dependency of another library, we hope that this tutorial can be used as a starting point. We invite users to report instabilities and crashes in the comments below when encountered and reproducible.
As a first disclaimer (and you’ll see many of those in that tutorial) this article is meant for advanced Python programmers, with a basic knowledge of Anaconda and an understanding of library versions, compatibilities, and the issues that can come with it. If you are familiar with AI libraries and custom development using those, you should be good to go. Chances are you had issues in the past and figured those out. This tutorial is for Windows users only.

Computer specs

For this tutorial, the machine used is setup with:
  • CPU: Intel i7-7700K
  • GPU: RTX 3080 (Driver version 512.95)
  • RAM: 16 GB
  • Windows 11 Pro Version 22H2 22621.1

Versions, and why they are so important

For this setup to succeed, you have to make sure that the versions you are using for your Anaconda environment, Tensorflow, CUDA and cuDNN match what is used in the current TouchDesigner build. If you do not follow this rule strictly, don’t be surprised if you encounter issues.
Let’s start first with versions of the current stable build of TouchDesigner, at the time of writing:
  • Build version: 2022.24200
  • TouchDesigner Python version: 3.9.5
  • TouchDesigner CUDA version: 11.2
  • TouchDesigner cuDNN version: 8.1.1
Did you write that down? Only if you wrote it down can you get a ticket and board the train. Fasten your seatbelt and let’s get going!
What we want is to create an anaconda environment that matches the TouchDesigner versions, but also install Tensorflow, with GPU support, and have Tensorflow use the matching CUDA and cuDNN versions. We are unlucky because the official / recommended install process for TensorFlow is fully relying on Anaconda and what is distributed through conda-forge... Which doesn’t match the versions TouchDesigner is using at the time of writing.
To summarize versions, what we will go with is:
  • Anaconda: 4.13.0
  • Environment Python version: 3.9.5
  • TouchDesigner: 2022.24200
  • CUDA: 11.2
  • cuDNN: 8.1.1
  • Tensorflow (details here): 2.9.0 for Python 3.7-3.10, w/ cuDNN 8.1 and CUDA 11.2
If you follow the previous link, you can assume that Tensorflow 2.5.0 to 2.9.0 should be compatible with our latest TouchDesigner build. We will go with the latest Tensorflow version, 2.9.0.
Note: 2.4.0 and lower are likely to be avoided since its cuDNN versions and CUDA versions are lower than what we are using in TouchDesigner.
UPDATE Jan 4th 2023: Make sure that you are using pip install tensorflow<2.11 . TensorFlow 2.11 dropped GPU support on Windows (native).
At the time of writing, it is not recommended to install Tensorflow out of conda-forge as it is often out of date. That’s what the official documentation states. pip should be used instead (but within your Anaconda environment). While the cudatoolkit is available on conda-forge, the cuDNN versions available do not match what we currently use in TouchDesigner: I repeat, do not install cuDNN using Anaconda, and do not follow the official / recommended Tensorflow install process from Tensorflow’s website.

Getting started

The order of those steps does have an impact on the installation. Especially when it comes to Anaconda, Tensorflow, and GPU support. Don’t try to speed through all those steps and take your time to read through everything.

Get CUDA, cuDNN and install.

You want to get CUDA from the Nvidia Archive:
You want to get cuDNN from the Nvidia Archive:
This version of cuDNN above, because it is matching with TouchDesigner’s version. Don’t get mistaken by the Nvidia archive page stating for other versions, CUDA 11.X.
Once everything is downloaded, start the CUDA install.
Note that all you need from the CUDA installer are the runtime libraries. Do a custom install, and select only those.
Keep the default install path if you don’t want to get confused by what’s to come in the tutorial.
It should be C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2.
Close the installer once everything is done.
Open your cuDNN archive. Inside is a cuda folder, and inside this cuda folder are 3 folders: bin, include and lib.
In a new explorer window, go to your CUDA installation folder, it should be C:\Program Files\NVIDIA GPU Computing Toolkit\CUDA\v11.2.
From your cuDNN archive, drag n drop (or copy paste) the folders bin, include, libto the CUDA install folder. If it asks you to replace some files, do it.
Go to your system environment variables and add the CUDA bin folder to the path.
At this point, if you had any cmd prompt opened, including Aconda’s Prompt, you should close it and reopen it.

Setup your Anaconda environment

If you are not familiar enough with Anaconda or need a refresher, you should give a complete read to this article we wrote.
I’ll consider you familiar with Anaconda and loading an Anaconda environment for the next steps.
In a new Anacon Prompt, create a new Anaconda environment with python 3.9.5. We’ll name our environment td-tfgpu.
conda create -n td-tfgpu python=3.9.5
Go in the environment, using conda activate td-tfgpu.
Now, you can use pip in that environment to install Tensorflow. To do so, type pip install tensorflow.

UPDATE Jan 4th 2023: Make sure that you are using pip install tensorflow<2.11 . TensorFlow 2.11 dropped GPU support on Windows (native).

It will download TensorFlow, about 4-500MBs, dependencies, and install.
If you don’t run into any issues, go into interactive mode typing python, press enter.
We will confirm that TensorFlow is installed.
Type import tensorflow as tf, press enter.

If you don’t get any error, type tf.__version__, press enter. At the time of writing, it should return 2.9.1.
Finally, an important bit, let’s confirm that we have GPU access.
Type tf.config.list_physical_devices('GPU'), press enter.
If it returns one or more devices, then you are good to go and you installed TensorFlow properly in your Anaconda environment.

Load your Anaconda environment in TouchDesigner

We will use the script from the Anaconda article.
Open a new project in TouchDesigner 2022.24200.
Create an Execute DAT, toggle on the Startparameter, and copy and paste the onStart script.
In the script you just copy pasted, there is a little issue caused by our online code formatter (apologies). Make sure that the line with if sys.version_info… reads >=.
Edit the variables at the top, user and condaEnv. Type your Windows username for user, and type td-tfgpu for the condaEnv.
Save your file. Restart your project.
Open a textport using alt+t.
Type import tensorflow as tf, press enter.
It might freeze for a few seconds while loading everything for the first time.
Type tf.__version__, press enter. It should print 2.9.1
Type tf.config.list_physical_devices('GPU'), press enter.
If it returns one or more devices, then you are good to go and you are ready to use TensorFlow-GPU in TouchDesigner.
We hope that this tutorial can help you get started and we hope to see some of the work you can achieve using Tensorlfow-GPU with TouchDesigner.
If you encounter any instabilities or crashes and that they are reproducible following this tutorial, please let us know in the comments below.
Experience level