mlx-examples icon indicating copy to clipboard operation
mlx-examples copied to clipboard

M2 Ultra 192 GB fails to run while M3 Max 128GB can run

Open alphrc opened this issue 2 years ago • 4 comments

Guys, I was fine-tuning the Qwen/Qwen1.5-32B-Chat model on M2 Ultra 192GB Mac Studio. At the end of the 1000th iteration, I encountered the below error, which I had never seen while running the exact same code on another M3 Max 128GB MBP.

...
Iter 980: Train loss 1.169, Learning Rate 1.000e-05, It/sec 0.124, Tokens/sec 178.563, Trained Tokens 1740144, Peak mem 127.703 GB
Iter 990: Train loss 1.390, Learning Rate 1.000e-05, It/sec 0.109, Tokens/sec 175.406, Trained Tokens 1756298, Peak mem 127.703 GB
libc++abi: terminating due to uncaught exception of type std::runtime_error: [METAL] Command buffer execution failed: Internal Error (0000000e:Internal Error)
zsh: abort      mlx_lm.lora --model Qwen/Qwen1.5-32B-Chat --train --data dataset/chat --iters
(lilm) rainchong@Mac-Studio Dev % /Library/Developer/CommandLineTools/Library/Frameworks/Python3.framework/Versions/3.9/lib/python3.9/multiprocessing/resource_tracker.py:216: UserWarning: resource_tracker: There appear to be 1 leaked semaphore objects to clean up at shutdown
  warnings.warn('resource_tracker: There appear to be %d '

The model is running on FP16, and the training set size is about 50000. All the entries have <2048 tokens.

I can train the same model in same precision with same dataset smoothly on M3 Max 128GB, so I don't think it would be a problem of insufficient memory.

Also, I can't even fuse the model on the Mac Studio with adapter trained from the MBP.

Please help!!!

alphrc avatar Jul 04 '24 04:07 alphrc

Yes this is a known issue with <= M2. There is no simple fix unfortunately as it's a system level problem. The internal error signifies that the GPU kernel timed out. One way to fix that is to decrease the size of the operations:

  • decreasing the batch size
  • decreasing the sequence length
  • decrease the model size

Another possible fix but I can't be sure is to decrease the number of operations per command buffer. You can do that like so:

MLX_MAX_OPS_PER_BUFFER=1 <lora command here>

awni avatar Jul 05 '24 13:07 awni

I'm going to close this as a dup of https://github.com/ml-explore/mlx/issues/1231 but we'll keep looking for options to fix this and let you know if we figure anything more useful out.

awni avatar Jul 05 '24 13:07 awni

Thank you so much!!

alphrc avatar Jul 05 '24 15:07 alphrc

tl;dr; this is a driver issue specific to the M2 Ultra that apple would need to fix. We could mitigate this with a per-queue encoder creation lock.

** proposed fix ** Add a std::mutex to DeviceStream and lock it around both commit() and computeCommandEncoder(). This only serializes within a single queue. Cost: minimal, since streams are already single-threaded by design.

** full report ** The following evidence collectively establishes that the root cause is inside the AGXMetalG14X driver bundle, not in the calling application:

  1. THE CRASH STACK NAMES A G14X-SPECIFIC CLASS

    The faulting method is: -[AGXG14XFamilyCommandBuffer tryCoalescingPreviousComputeCommandEncoderWithConfig:nextEncoderClass:]

    The "AGXG14XFamily" prefix identifies this as a G14-architecture-specific subclass of MTLCommandBuffer, implemented inside the AGXMetalG14X driver bundle. This is not generic Metal framework code — it is driver-family- specific logic that only executes on G14-architecture GPUs (A15, M2).

  2. THE CRASH DOES NOT OCCUR ON M3 MAX (G15X DRIVER)

    The identical application (MLX 0.31.0), running the identical workload (quantized LLM inference), with the identical Metal API call sequence, does NOT crash on M3 Max. The M3 Max uses the AGXMetalG15X driver bundle with its own AGXG15XFamilyCommandBuffer class.

    Same caller code + same API usage + different driver = different behavior. The only variable is which driver bundle implements the command buffer.

  3. THE FAULTING METHOD IS A DRIVER-INTERNAL OPTIMIZATION

    "tryCoalescingPreviousComputeCommandEncoderWithConfig:" is not part of the public Metal API. It is an internal driver optimization that attempts to merge consecutive compute command encoders for performance. This coalescing logic:

    • Is implemented per-GPU-family in each driver bundle
    • Is not documented in the Metal API
    • Cannot be controlled, disabled, or influenced by the caller
    • Has a G14X-specific implementation that differs from G15X

    The G15X driver either: a) Implements coalescing with proper synchronization, or b) Does not perform cross-buffer coalescing in the same code path, or c) Does not hold references to previously committed buffer state

  4. THE PAC FAILURE CONFIRMS USE-AFTER-FREE INSIDE THE DRIVER

    The pointer authentication failure (address 0x0065c62f691dadd0 with PAC stripping to 0x0000462f691dadd0) proves that:

    • The pointer was originally valid (it passed PAC signing)
    • The object has since been freed (PAC verification fails)
    • The freed object was a driver-internal Objective-C object (reached via -[NSObject isMemberOfClass:] inside the coalescing method)

    The caller (MLX) does not hold references to any internal encoder or driver objects. The stale pointer is maintained entirely within the driver's own bookkeeping structures.

  5. THE CALLER FOLLOWS THE DOCUMENTED METAL LIFECYCLE

    The calling code performs a standard, single-threaded sequence:

    1. Create command buffer from queue
    2. Create compute command encoder
    3. Encode dispatches
    4. End encoding
    5. Commit command buffer
    6. Create new command buffer from same queue
    7. Create new compute command encoder → CRASH

    Steps 1-7 are executed sequentially on a single thread. There is no concurrent Metal API usage from the caller. The race is between step 7 (caller thread) and Metal's own internal submission thread processing the buffer committed in step 5. The caller has no mechanism to prevent or avoid this race — it would require either:

    • Calling waitUntilScheduled() after every commit (undocumented requirement, significant performance impact), or
    • The driver properly synchronizing its internal state

CONCLUSION

The crash occurs in a G14X-family-specific Objective-C method, in a G14X-specific code path, referencing driver-internal state that the caller cannot access or control. The identical API usage is stable on G15X hardware. This is a synchronization defect in the AGXMetalG14X driver's encoder coalescing logic.

Specific fix location: /System/Library/Extensions/AGXMetalG14X.bundle/Contents/MacOS/AGXMetalG14X Class: AGXG14XFamilyCommandBuffer Method: tryCoalescingPreviousComputeCommandEncoderWithConfig:nextEncoderClass:

MillaFleurs avatar Mar 09 '26 20:03 MillaFleurs