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]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[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.

✅ No Failures

As of commit d37e7ef with merge base 6e0c9f6 (image):
💚 Looks good so far! There are no failures yet. 💚

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

[ghstack-poisoned]
[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; 

Copy link
Contributor Author

Choose a reason for hiding this comment

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

With commitAndContinue we commit and continue reusing the buffer, until we flush. So, we don't release it after commit.

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think retaining ensures that the objects remain valid for the lifetime of the ETMetalKernelFunction instance, preventing them from being deallocated elsewhere. Without the retains cps_ and func_ could become dangling pointers.
Similar patter followed in PyTorch here

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;

Copy link
Contributor Author

Choose a reason for hiding this comment

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

No, we need to release them, because we own them now.
Same pattern followed in PyTorch here

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?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

left over of debugging, deleted.

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?

[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
@manuelcandales manuelcandales added the release notes: none Do not include this in the release notes label Oct 13, 2025
manuelcandales added a commit to manuelcandales/executorch-1 that referenced this pull request Oct 14, 2025
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-source-id: ea4fbb5
ghstack-comment-id: 3392300041
Pull-Request: pytorch#15020
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
Base automatically changed from gh/manuelcandales/139/head to main October 15, 2025 02:58
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
[ghstack-poisoned]
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.

2 participants