Skip to content

Metal backend: compile metal with precise and safe flags on#16598

Merged
manuelcandales merged 2 commits intomainfrom
manuel/parakeet-metal-precise
Jan 15, 2026
Merged

Metal backend: compile metal with precise and safe flags on#16598
manuelcandales merged 2 commits intomainfrom
manuel/parakeet-metal-precise

Conversation

@manuelcandales
Copy link
Copy Markdown
Contributor

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 MTLCompileOptions when running on macOS 15.0, iOS 18.0, or newer, to ensure safer and more precise math operations during shader compilation:

  • mathMode set to MTLMathModeSafe (equivalent to -fmetal-math-mode=safe)
  • mathFloatingPointFunctions set to MTLMathFloatingPointFunctionsPrecise (equivalent to -fmetal-math-fp32-functions=precise)

Copilot AI review requested due to automatic review settings January 14, 2026 21:02
@pytorch-bot
Copy link
Copy Markdown

pytorch-bot bot commented Jan 14, 2026

🔗 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 Failure

As of commit c860695 with merge base 27b778d (image):

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.

@manuelcandales manuelcandales added the release notes: none Do not include this in the release notes label Jan 14, 2026
@malfet
Copy link
Copy Markdown
Contributor

malfet commented Jan 14, 2026

@meta-cla meta-cla bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Jan 14, 2026
Copy link
Copy Markdown
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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];
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
library_ = [device newLibraryWithSource:sourceString options:options error:&error];
library_ = [device newLibraryWithSource:sourceString options:options error:&error];
[options release];

Copilot uses AI. Check for mistakes.

library_ = [device newLibraryWithSource:sourceString options:nil error:&error];
MTLCompileOptions* options = [[MTLCompileOptions alloc] init];
if (@available(macOS 15.0, iOS 18.0, *)) {
Copy link

Copilot AI Jan 14, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Suggested change
if (@available(macOS 15.0, iOS 18.0, *)) {
if (@available(macOS 15.0, iOS 18.0, tvOS 18.0, watchOS 11.0, *)) {

Copilot uses AI. Check for mistakes.
NSError* error = nil;

library_ = [device newLibraryWithSource:sourceString options:nil error:&error];
MTLCompileOptions* options = [[MTLCompileOptions alloc] init];
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit

Suggested change
MTLCompileOptions* options = [[MTLCompileOptions alloc] init];
MTLCompileOptions* options = [MTLCompileOptions new];

@manuelcandales manuelcandales merged commit a0ba28e into main Jan 15, 2026
170 of 173 checks passed
@manuelcandales manuelcandales deleted the manuel/parakeet-metal-precise branch January 15, 2026 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. release notes: none Do not include this in the release notes

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants