Skip to content

Conversation

manuelcandales
Copy link
Contributor

This commit introduces the foundational Metal backend runtime.

Key features:

  • ETMetalStream for managing Metal devices, command queues, buffers, and synchronization.
  • ETMetalShaderLibrary for compiling Metal shader source and caching pipeline states.
  • ETMetalKernelFunction for kernel argument binding, dispatching, and synchronization with stream-managed encoders.
  • Added global buffer management and pointer tracking between host and Metal buffers.
  • Added global stream management utilities and synchronization helpers

This provides the necessary runtime primitives for executing compute shaders and MPSGraph workloads.

[ghstack-poisoned]
Copy link

pytorch-bot bot commented Oct 10, 2025

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/pytorch/executorch/15020

Note: Links to docs will display an error until the docs builds have been completed.

❗ 1 Active SEVs

There are 1 currently active SEVs. If your PR is affected, please view them below:

❌ 3 New Failures, 1 Pending, 1 Unrelated Failure

As of commit 7e971b0 with merge base 896178e (image):

NEW FAILURES - The following jobs have failed:

FLAKY - The following job failed but was likely due to flakiness present on trunk:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

[ghstack-poisoned]
Copy link
Contributor

@mergennachin mergennachin left a comment

Choose a reason for hiding this comment

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

See inline

// Commit buffer and allow immediate reuse for better performance
[commandBuffer_ commit];
ET_LOG(Debug, "ETMetalStream::commitAndContinue: Committed buffer %p with continue", commandBuffer_);

Copy link
Contributor

Choose a reason for hiding this comment

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

Don't you need to release the buffer after commit?

      [commandBuffer_ release];
      commandBuffer_ = nil; 

Comment on lines +274 to +275
if (cps_) [cps_ retain];
if (func_) [func_ retain];
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do you need these retain lines? Aren't these already owned by the class?

Comment on lines +280 to +291
// Don't release encoder_ here - the stream owns it
// Only clean up our own references
if (cps_) {
[cps_ release];
cps_ = nil;
}
if (func_) {
[func_ release];
func_ = nil;
}

encoder_ = nil; // Clear reference without releasing
Copy link
Contributor

Choose a reason for hiding this comment

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

Just this?

cps_ = nil;
func_ = nil;
encoder_ =nil;

resultsDictionary:results
executionDescriptor:nil];

//synchronize(syncType);
Copy link
Contributor

Choose a reason for hiding this comment

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

Why commented out?

};

// =======================
// ETMetalShaderLibrary - ExecuTorch Metal shader library management
Copy link
Contributor

Choose a reason for hiding this comment

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

Add a bit more comments. ETMetalShaderLibrary

  • Compiles Metal Shading Language (MSL) source to MTLLibrary
  • Caches compiled pipeline states for reuse
  • Creates ETMetalKernelFunction lazily
  • Lifetime: Persistent (application lifetime)

};

// =======================
// ETMetalKernelFunction - ExecuTorch Metal kernel function execution
Copy link
Contributor

Choose a reason for hiding this comment

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

- Encodes kernel dispatches into command buffers
- Lifetime: Per-kernel-invocation

};

// =======================
// ETMetalStream - Metal command buffer and synchronization management
Copy link
Contributor

Choose a reason for hiding this comment

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

 ETMetalStream                                                  
   - Manages Metal device, command queue, and command buffers
   - Provides synchronization primitives (5 modes)               
   - Implements kernel coalescing for batch optimization      
   - Executes MPSGraph operations for AOTI-compiled models  

extern "C" {
#endif

// Memory management functions for Metal
Copy link
Contributor

Choose a reason for hiding this comment

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

Docblock about the memory management aspect of this design, such as buffer lifecycle, thread safety etc.


// C++ only - expose the Metal buffer mapping
#ifdef __OBJC__
extern std::unordered_map<void*, MTLBuffer_t> ptr_to_mtl_buffer;
Copy link
Contributor

Choose a reason for hiding this comment

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

do you need a lock and thread safety to access ptr_to_mtl_buffer?

Comment on lines +744 to +745
void ETMetalStream::copy(id<MTLBuffer> srcBuffer, id<MTLBuffer> dstBuffer, size_t length,
size_t srcOffset, size_t dstOffset, SyncType syncType) {
Copy link
Contributor

Choose a reason for hiding this comment

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

before copying check that offsets are within the buffer bounds

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.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants