Skip to content

Fix MPS encoder lifecycle in relu-metal-cpp example#316

Draft
robtaylor wants to merge 1 commit intohuggingface:mainfrom
robtaylor:fix-relu-metal-cpp-lifecycle
Draft

Fix MPS encoder lifecycle in relu-metal-cpp example#316
robtaylor wants to merge 1 commit intohuggingface:mainfrom
robtaylor:fix-relu-metal-cpp-lifecycle

Conversation

@robtaylor
Copy link

Summary

  • Add C bridge functions (getMPSCommandEncoder, mpsSynchronize, mpsDispatchSync) to metallib_loader.mm so the C++ metal-cpp example properly integrates with PyTorch's MPS stream encoder lifecycle
  • Update relu.cpp to use the bridge functions instead of creating encoders directly, which bypasses PyTorch's kernel coalescing and crashes on sequential kernel calls

This is the metal-cpp companion to #308 (which fixes the ObjC++ path). The same MPS encoder lifecycle issue affects the C++ example — direct [commandBuffer computeCommandEncoder] usage crashes when multiple kernels are dispatched sequentially.

Test plan

  • Verified on macOS 14/15/26 via ChipFlow CI (macos-*-xlarge runners with Metal GPU)
  • Build and run relu-metal-cpp example on MPS device

Co-developed-by: Claude Code (claude-opus-4-6)

Add C bridge functions (getMPSCommandEncoder, mpsSynchronize,
mpsDispatchSync) to metallib_loader.mm so the C++ metal-cpp example
can properly integrate with PyTorch's MPS stream encoder lifecycle
without needing ObjC++ code in the main kernel file.

Co-developed-by: Claude Code v2.1.50 (claude-opus-4-6)
@robtaylor robtaylor force-pushed the fix-relu-metal-cpp-lifecycle branch from 2b1cafd to 8bca140 Compare March 9, 2026 14:51
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant