Hello,
Finally, I was able to install nvidia on my debian bullseye amd 64, through the nvidia-driver
debian package (it also worked with the Nvidia installation script) and with and without secure boot.
1. Installing nvidia driver
With recent kernels, Secure boot impose to create / enroll a MOK key + sign the nvidia driver. This is explained in the Wiki Debian / Secure boot. Below, I provide two script I adapted / wrote to achieve the corresponding steps.
- (secure boot only) Create the following files:
/root/secure_boot/enroll.sh
#!/bin/sh
#https://wiki.debian.org/SecureBoot
echo "Creating MOK.priv and MOK.der"
openssl req -new -x509 -newkey rsa:2048 -keyout MOK.priv -outform DER -out MOK.der -days 36500 -subj "/CN=My Name/" -nodes
echo "Importing MOK.der, please enter your one-time password"
mokutil --import MOK.der
echo "Check that your key is listed below"
mokutil --list-new
echo "Now, reboot the machine. Then, enters MOK manager EFI utility"
echo "enroll MOK, continue, confirm, enter password, reboot>"
echo "Finally, then check it is loaded with dmesg | grep cert"
exit 0
/root/secure_boot/sign.sh
#!/bin/sh
PRIV=/root/secure_boot/MOK.priv
DER=/root/secure_boot/MOK.der
for filename in $PRIV $DER
do
(test -f $filename && echo "$filename found :-)") || (echo "$filename not found" && exit 1)
done
KBUILD_VER=$(uname -r | cut -d"." -f1,2)
echo "Kbuild version $KBUILD_VER"
cd /lib/modules/$(uname -r)/updates/dkms
for ko in $(ls -1 *.ko)
do
echo "Signing $ko"
/usr/lib/linux-kbuild-$KBUILD_VER/scripts/sign-file sha256 $PRIV $DER $ko
done
exit 0
- (secure boot only) Create MOK keys (
/root/secure_boot/MOK.der
and/root/secure_boot/MOK.der
) by running/root/secure_boot/enroll.sh
then enroll they key. This is required only once. - Install
nvidia-driver
:
apt update
apt install nvidia-driver
- (secure boot only) Sign the module by running
/root/secure_boot/sign.sh
. This is required whenever the kernel or the nvidia driver is updated. - In my case, I also have to create
/etc/X11/xorg.conf.d/nvidia.conf
(see previous messages):
Section "ServerLayout"
Identifier "layout"
Option "AllowNVIDIAGPUScreens"
EndSection
- Reboot. Then, ensure that nvidia driver is loaded.
lsmod | grep nvidia
nvidia-smi
2. Benchmark CPU vs GPU
This section presents steps to run a python3 script demonstrating the gap between CPU and GPU.
- Install Nvidia driver (see previous section).
- Install torch & CUDA requirements:
apt update
apt install nvidia-cuda-dev nvidia-cuda-toolkit python3 python3-pip
pip3 install torch
- Create and run the following python script:
#!/usr/bin/env python3
import functools, time
import torch
def timer(f):
"""Print the runtime of the decorated function"""
@functools.wraps(f)
def wrapper_timer(*args, **kwargs):
start_time = time.perf_counter()
value = f(*args, **kwargs)
end_time = time.perf_counter()
run_time = end_time - start_time
print(f"Finished {f.__name__!r} in {run_time:.4f} secs")
return value
return wrapper_timer
@timer
def cpu(x, y, num_times):
for _ in range(num_times):
z = torch.matmul(x, y)
@timer
def gpu(x, y, num_times):
for _ in range(num_times):
z = torch.matmul(x, y)
torch.cuda.init()
assert torch.cuda.is_available() # Fail if nvidia and CUDA dependencies are not installed
X = torch.randn(10, 300, 400)
Y = torch.randn(10, 400, 500)
X_cuda = X.cuda()
Y_cuda = Y.cuda()
N = 1000
cpu(X, Y, N)
gpu(X_cuda, Y_cuda, N)
Results: (note that matrices have to be large enough to make GPU usage worth)
Finished 'cpu' in 5.3031 secs
Finished 'gpu' in 0.0213 secs
Once again, I’d like to thank to @generix and @leigh123linux for their help.
Best regards,
mando