Metal backend: compile metal with precise and safe flags on#16598
Metal backend: compile metal with precise and safe flags on#16598manuelcandales merged 2 commits intomainfrom
Conversation
🔗 Helpful Links🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/16598
Note: Links to docs will display an error until the docs builds have been completed. ❌ 1 New Failure, 1 Pending, 1 Unrelated FailureAs of commit c860695 with merge base 27b778d ( NEW FAILURE - The following job has failed:
UNSTABLE - The following job is marked as unstable, possibly due to flakiness on trunk:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
Do you mind mentions that it mimics Pytorch behavior, see https://github.com/pytorch/pytorch/blob/e14a2f0e7e24b634ae1052cd1c70a8c53cab5f73/aten/src/ATen/native/mps/OperationUtils.mm#L873 |
There was a problem hiding this comment.
Pull request overview
This pull request enhances Metal shader compilation on macOS 15.0 and iOS 18.0 or newer by enabling safer and more precise math operations during shader compilation. The changes add compile-time options to improve numerical accuracy in Metal shaders used by the ExecutorTorch Metal backend.
Changes:
- Added MTLCompileOptions configuration in the shader library compilation process
- Applied MTLMathModeSafe and MTLMathFloatingPointFunctionsPrecise flags on newer OS versions
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| options.mathFloatingPointFunctions = MTLMathFloatingPointFunctionsPrecise; | ||
| } | ||
|
|
||
| library_ = [device newLibraryWithSource:sourceString options:options error:&error]; |
There was a problem hiding this comment.
Memory leak: MTLCompileOptions object is allocated but never released. Since this code uses manual reference counting (not ARC), the options object needs to be explicitly managed. Either call [options release] after line 243, or use [options autorelease] after line 237 to add it to the autorelease pool.
| library_ = [device newLibraryWithSource:sourceString options:options error:&error]; | |
| library_ = [device newLibraryWithSource:sourceString options:options error:&error]; | |
| [options release]; |
|
|
||
| library_ = [device newLibraryWithSource:sourceString options:nil error:&error]; | ||
| MTLCompileOptions* options = [[MTLCompileOptions alloc] init]; | ||
| if (@available(macOS 15.0, iOS 18.0, *)) { |
There was a problem hiding this comment.
The @available check should include tvOS 18.0 and watchOS 11.0 for consistency with other Apple platform availability checks in the codebase (see backends/apple/coreml/runtime/delegate/ETCoreMLModel.mm line 213 and backends/apple/mps/runtime/operations/OperationUtils.mm line 35). While Metal may primarily target macOS and iOS, including these platforms ensures consistency and future compatibility.
| if (@available(macOS 15.0, iOS 18.0, *)) { | |
| if (@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *)) { |
| NSError* error = nil; | ||
|
|
||
| library_ = [device newLibraryWithSource:sourceString options:nil error:&error]; | ||
| MTLCompileOptions* options = [[MTLCompileOptions alloc] init]; |
There was a problem hiding this comment.
Nit
| MTLCompileOptions* options = [[MTLCompileOptions alloc] init]; | |
| MTLCompileOptions* options = [MTLCompileOptions new]; |
This pull request updates the Metal shader compilation process to improve numerical accuracy and safety when compiling shaders on newer versions of macOS and iOS. The main change is the introduction of specific compile options for math operations when supported by the operating system.
In the compileLibrary() function, added
MTLCompileOptionswhen running on macOS 15.0, iOS 18.0, or newer, to ensure safer and more precise math operations during shader compilation:mathModeset toMTLMathModeSafe(equivalent to -fmetal-math-mode=safe)mathFloatingPointFunctionsset toMTLMathFloatingPointFunctionsPrecise(equivalent to -fmetal-math-fp32-functions=precise)