Blender allows the user to select “GPU Compute” as the rendering device even if the computer has no valid GPU device. It simply Greys out the drop down menu:
To a Python script, it then looks like a GPU compute device is in use:
Cycles will silently default to the CPU if the user selects GPU but doesn’t have one, but I don’t know how to detect this from a script.
I need a way to check from my Python script if an actual valid GPU device will be used for rendering or not.
If you look at the UI code that draws those dropdown boxes you will see that it calls a function named show_device_active(context). The method code itself looks like this:
The relevant part is the last line. You can replicate this in an own Add-on or script by calling context.preferences.addons[__package__].preferences.has_active_device()
I actually tried that already, but context.preferences.addons[__package__].preferences.has_active_device() seemed to always return False, even on my computer with an Nvidia GPU (note I had to change __package__ to "cycles" in that code.