macOS: Blender doesn't utilize GPU for background rendering if Metal device type preference is set via script #114326

Closed
opened 2023-10-31 14:34:17 +01:00 by Ingo Clemens · 7 comments

System Information
Operating system: macOS-13.5.2-arm64-arm-64bit 64 Bits
Graphics card: Metal API Apple M1 Max 1.2

Blender Version
Broken: version: 4.0.0 Beta, branch: blender-v4.0-release, commit date: 2023-10-30 23:38, hash: b27ff093caa0
Worked: before 3.6.1

Short description of error
When the preference setting System > Cycles Render Devices > Metal is set via the python command
bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'METAL'
the background rendering doesn't use the GPU but only the CPU, which results in much slower render times.

The python command definitely sets the preferences (of course along with saving the prefs).

When rendering from within the open Blender session the preference setting is respected.

When setting the preference setting manually and then starting the background rendering, the GPU is used as expected.

Exact steps for others to reproduce the error

  1. Start Blender without any preferences, as it just has been downloaded.
  2. Execute:
import bpy
bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'METAL'
bpy.ops.wm.save_userpref()
  1. Close Blender.
  2. Open the Terminal application and run:
    /Applications/Blender.app/Contents/MacOS/Blender --background /PATH_TO_PROJECT_FOLDER/renderTest/test.blend -a

The rendering consists of two frames of which each should only take about half a minute or less (without kernel processing).

The project folder to test with is attached.

**System Information** Operating system: macOS-13.5.2-arm64-arm-64bit 64 Bits Graphics card: Metal API Apple M1 Max 1.2 **Blender Version** Broken: version: 4.0.0 Beta, branch: blender-v4.0-release, commit date: 2023-10-30 23:38, hash: `b27ff093caa0` Worked: before 3.6.1 **Short description of error** When the preference setting System > Cycles Render Devices > Metal is set via the python command `bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'METAL'` the background rendering doesn't use the GPU but only the CPU, which results in much slower render times. The python command definitely sets the preferences (of course along with saving the prefs). When rendering from within the open Blender session the preference setting is respected. When setting the preference setting manually and then starting the background rendering, the GPU is used as expected. **Exact steps for others to reproduce the error** 1. Start Blender without any preferences, as it just has been downloaded. 2. Execute: ``` import bpy bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'METAL' bpy.ops.wm.save_userpref() ``` 3. Close Blender. 4. Open the Terminal application and run: `/Applications/Blender.app/Contents/MacOS/Blender --background /PATH_TO_PROJECT_FOLDER/renderTest/test.blend -a` The rendering consists of two frames of which each should only take about half a minute or less (without kernel processing). The project folder to test with is attached.
Ingo Clemens added the
Status
Needs Triage
Type
Report
Priority
Normal
labels 2023-10-31 14:34:18 +01:00
Contributor

I can confirm the described behavior as of the latest 4.0.0 Beta build.

After verifying it using the exact steps originally described, I experimented a bit and found a slightly simpler way to demonstrates the issue. It doesn't require re-launching Blender or saving the preferences to disk.

  1. Launch Blender with the factory settings, but opening test.blend:

    /path/to/Blender --factory-startup /path/to/renderTest/test.blend
    
  2. Switch to the "Scripting" workspace, and in the Python terminal run:

    C.preferences.addons['cycles'].preferences.compute_device_type = 'METAL'
    
  3. In the main menu, click Render > Render Animation (or use the keyboard shortcut) to render the animation. Observe that the status line does not end with "(Using optimized kernels)", and the render takes significantly longer than it would if it were actually using the Metal backend. Close the render window.

  4. Open the Blender preferences, switch to the "System" tab (and observe that the device is indeed set to Metal), and then close the preferences again (without having changed or saved anything).

  5. Do Render > Render Animation again, and note that this time it uses Metal. (The status line ends in "(Using optimized kernels)", and assuming no kernels needed to be compiled, the render is much faster.)

I can confirm the described behavior as of the latest 4.0.0 Beta build. After verifying it using the exact steps originally described, I experimented a bit and found a slightly simpler way to demonstrates the issue. It doesn't require re-launching Blender or saving the preferences to disk. 1. Launch Blender with the factory settings, but opening `test.blend`: ``` /path/to/Blender --factory-startup /path/to/renderTest/test.blend ``` 2. Switch to the "Scripting" workspace, and in the Python terminal run: ``` C.preferences.addons['cycles'].preferences.compute_device_type = 'METAL' ``` 3. In the main menu, click Render > Render Animation (or use the keyboard shortcut) to render the animation. Observe that the status line does _not_ end with "(Using optimized kernels)", and the render takes significantly longer than it would if it were actually using the Metal backend. Close the render window. 4. Open the Blender preferences, switch to the "System" tab (and observe that the device is indeed set to Metal), and then close the preferences again (without having changed or saved anything). 5. Do Render > Render Animation again, and note that this time it uses Metal. (The status line ends in "(Using optimized kernels)", and assuming no kernels needed to be compiled, the render is much faster.)

Setting the appropriate render devices from Python is a bit more involved. The full sequence is typically something like the following:

# Set the device type
C.preferences.addons['cycles'].preferences.compute_device_type = 'METAL'

# Refresh the list of devices
C.preferences.addons['cycles'].preferences.get_devices()

# Actually "use" the devices you want to...
# You can check .devices[...].type to find one that != "CPU" ... in this case I'm assuming the M1 GPU is device[1]
C.preferences.addons['cycles'].preferences.devices[1].use = True

So you must not only set the compute_device_type but you must also explicitly select the devices to "use".

Setting the appropriate render devices from Python is a bit more involved. The full sequence is typically something like the following: ``` # Set the device type C.preferences.addons['cycles'].preferences.compute_device_type = 'METAL' # Refresh the list of devices C.preferences.addons['cycles'].preferences.get_devices() # Actually "use" the devices you want to... # You can check .devices[...].type to find one that != "CPU" ... in this case I'm assuming the M1 GPU is device[1] C.preferences.addons['cycles'].preferences.devices[1].use = True ``` So you must not only set the `compute_device_type` but you must also explicitly select the devices to "use".
Contributor

@braverabbit If you want to insulate your code from device order changes/inconsistency, you could loop over all of the devices and set use automatically based on type:

for device in C.preferences.addons['cycles'].preferences.devices:
  device.use = (device.type != 'CPU')
@braverabbit If you want to insulate your code from device order changes/inconsistency, you could loop over all of the devices and set `use` automatically based on `type`: ``` for device in C.preferences.addons['cycles'].preferences.devices: device.use = (device.type != 'CPU') ```
Pratik Borhade added
Status
Needs Information from User
and removed
Status
Needs Triage
labels 2023-11-03 05:00:38 +01:00
Contributor

What about --cycles-device METAL? Does it work as expected?
https://docs.blender.org/manual/en/latest/advanced/command_line/render.html#cycles

What about `--cycles-device METAL`? Does it work as expected? https://docs.blender.org/manual/en/latest/advanced/command_line/render.html#cycles
Author

Thanks for the fast replies everyone.
I assumed that something else was missing, since it didn't work with a single preference switch. Thanks for finding it.

It appears that even only using:

C.preferences.addons['cycles'].preferences.compute_device_type = 'METAL'
C.preferences.addons['cycles'].preferences.get_devices()
bpy.ops.wm.save_userpref()

works also for background rendering. So far it wasn't necessary to also set use for the device. Might be saver though? I don't know.

Using the render flags --cycles-device METAL as in

Blender --background /PATH_TO_PROJECT_FOLDER/renderTest/test.blend -a -- --cycles-device METAL

also works as expected but of course is only temporary for the rendering and not changing any preference settings.

The interesting part is that only using the simple (and now obsolete) code mentioned in my first post

import bpy
bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'METAL'
bpy.ops.wm.save_userpref()

worked fine for several versions of Blender until it ceased to work without changing anything of the setup. Basically it stopped working mid project. On one machine about two weeks before it stopped working on another one. I am not sure which Blender version it was but probably 3.4 or 3.5.
Interesting how these things have a life of their own.

Thanks for the fast replies everyone. I assumed that something else was missing, since it didn't work with a single preference switch. Thanks for finding it. It appears that even only using: ``` C.preferences.addons['cycles'].preferences.compute_device_type = 'METAL' C.preferences.addons['cycles'].preferences.get_devices() bpy.ops.wm.save_userpref() ``` works also for background rendering. So far it wasn't necessary to also set `use` for the device. Might be saver though? I don't know. Using the render flags `--cycles-device METAL` as in `Blender --background /PATH_TO_PROJECT_FOLDER/renderTest/test.blend -a -- --cycles-device METAL` also works as expected but of course is only temporary for the rendering and not changing any preference settings. The interesting part is that only using the simple (and now obsolete) code mentioned in my first post ``` import bpy bpy.context.preferences.addons['cycles'].preferences.compute_device_type = 'METAL' bpy.ops.wm.save_userpref() ``` worked fine for several versions of Blender until it ceased to work without changing anything of the setup. Basically it stopped working mid project. On one machine about two weeks before it stopped working on another one. I am not sure which Blender version it was but probably 3.4 or 3.5. Interesting how these things have a life of their own.
Contributor

So, it was a user issue, right? Case closed then :)

So, it was a user issue, right? Case closed then :)
Author

I suppose, sort of a user issue, along with Blender doing it's own thing. But yes, all good now.
Thanks.

I suppose, sort of a user issue, along with Blender doing it's own thing. But yes, all good now. Thanks.
Blender Bot added
Status
Archived
and removed
Status
Needs Information from User
labels 2023-11-03 11:49:44 +01:00
Sign in to join this conversation.
No Label
Interest
Alembic
Interest
Animation & Rigging
Interest
Asset Browser
Interest
Asset Browser Project Overview
Interest
Audio
Interest
Automated Testing
Interest
Blender Asset Bundle
Interest
BlendFile
Interest
Collada
Interest
Compatibility
Interest
Compositing
Interest
Core
Interest
Cycles
Interest
Dependency Graph
Interest
Development Management
Interest
EEVEE
Interest
EEVEE & Viewport
Interest
Freestyle
Interest
Geometry Nodes
Interest
Grease Pencil
Interest
ID Management
Interest
Images & Movies
Interest
Import Export
Interest
Line Art
Interest
Masking
Interest
Metal
Interest
Modeling
Interest
Modifiers
Interest
Motion Tracking
Interest
Nodes & Physics
Interest
OpenGL
Interest
Overlay
Interest
Overrides
Interest
Performance
Interest
Physics
Interest
Pipeline, Assets & IO
Interest
Platforms, Builds & Tests
Interest
Python API
Interest
Render & Cycles
Interest
Render Pipeline
Interest
Sculpt, Paint & Texture
Interest
Text Editor
Interest
Translations
Interest
Triaging
Interest
Undo
Interest
USD
Interest
User Interface
Interest
UV Editing
Interest
VFX & Video
Interest
Video Sequencer
Interest
Virtual Reality
Interest
Vulkan
Interest
Wayland
Interest
Workbench
Interest: X11
Legacy
Blender 2.8 Project
Legacy
Milestone 1: Basic, Local Asset Browser
Legacy
OpenGL Error
Meta
Good First Issue
Meta
Papercut
Meta
Retrospective
Meta
Security
Module
Animation & Rigging
Module
Core
Module
Development Management
Module
EEVEE & Viewport
Module
Grease Pencil
Module
Modeling
Module
Nodes & Physics
Module
Pipeline, Assets & IO
Module
Platforms, Builds & Tests
Module
Python API
Module
Render & Cycles
Module
Sculpt, Paint & Texture
Module
Triaging
Module
User Interface
Module
VFX & Video
Platform
FreeBSD
Platform
Linux
Platform
macOS
Platform
Windows
Priority
High
Priority
Low
Priority
Normal
Priority
Unbreak Now!
Status
Archived
Status
Confirmed
Status
Duplicate
Status
Needs Info from Developers
Status
Needs Information from User
Status
Needs Triage
Status
Resolved
Type
Bug
Type
Design
Type
Known Issue
Type
Patch
Type
Report
Type
To Do
No Milestone
No project
No Assignees
4 Participants
Notifications
Due Date
The due date is invalid or out of range. Please use the format 'yyyy-mm-dd'.

No due date set.

Dependencies

No dependencies set.

Reference: blender/blender#114326
No description provided.