diff --git a/wiki/programming_language/python.md b/wiki/programming_language/python.md index 1dd41d8..f92163d 100644 --- a/wiki/programming_language/python.md +++ b/wiki/programming_language/python.md @@ -381,7 +381,7 @@ can be referenced. ### TensorFlow This section addresses the [TensorFlow module](https://www.tensorflow.org/). -Tensorflos is a machine learning resource which is often used for +TensorFlow is a machine learning resource which is often used for [neural networks](/wiki/neural_network.md). Apart from [package managers](/wiki/linux/package_manager.md) and @@ -390,6 +390,88 @@ on [the official website](https://www.tensorflow.org/install/source). This may especially be useful if specific configurations are needed such as vendor specific GPU support. +#### Setup TensorFlow with CUDA in a uv Project + +TensorFlow can install its required CUDA user-space libraries as optional dependencies on Linux. +The NVIDIA driver still has to be installed on the host system. + +```sh +nvidia-smi +uv add 'tensorflow[and-cuda]' +``` + +Verify whether TensorFlow was built with CUDA support and detects the GPU. + +```sh +uv run python -c 'import tensorflow as tf; print("CUDA build:", tf.test.is_built_with_cuda()); print(tf.config.list_physical_devices("GPU"))' +``` + +If `CUDA build` is `True` but no GPU is listed and TensorFlow reports that it cannot load GPU +libraries, the dynamic linker may not find the NVIDIA libraries installed inside the virtual +environment. +The following command temporarily adds all library directories from the installed `nvidia-*` +packages. + +```sh +SITE_PACKAGES=$(uv run python -c 'import site; print(site.getsitepackages()[0])') +CUDA_LIBS=$(find "$SITE_PACKAGES/nvidia" -type d -name lib -printf '%p:') + +LD_LIBRARY_PATH="${CUDA_LIBS}/usr/lib" uv run python -c \ + 'import tensorflow as tf; print(tf.config.list_physical_devices("GPU"))' +``` + +If the test succeeds, store the path in a machine-specific dotenv file. + +```sh +printf 'LD_LIBRARY_PATH=%s/usr/lib\n' "$CUDA_LIBS" > .env.cuda +printf '.env.cuda\n' >> .gitignore +``` + +Run the project with the file explicitly. + +```sh +uv run --env-file .env.cuda python