zkmopro / mopro

Making client-side proving on mobile simple.
https://zkmopro.org
Apache License 2.0
118 stars 32 forks source link

Integrate metal msm in ios #156

Closed FoodChain1028 closed 3 months ago

FoodChain1028 commented 3 months ago

Integrating metal msm into research/gpu-exploration-app.

Things that also have to improve in this PR

Currently we encounter a problem that should be solved. When the instance size become larger (above $2^{10}$), GPU hang error happens.

Here's the error message: running MSM for $2^{16}$ scalars and $2^{16}$ points:

Init metal (GPU) state...
fopen failed for data file: errno = 2 (No such file or directory)
Errors found! Invalidating cache...
Done initializing metal (GPU) state in 1.008694042s
Encoding instance to GPU memory...
Done encoding data in 73.65875ms
Init buckets time: 15.534375ms
Execution of the command buffer was aborted due to an error during execution. Caused GPU Hang Error (00000003:kIOGPUCommandBufferCallbackErrorHang)
Accumulation and Reduction time: 254.047666ms
Final accumulation time: 2.59825ms
Average time to execute MSM with 65536 points and 65536 scalars in 1 iterations is: 273.486625ms
Encoding instance to GPU memory...
Done encoding data in 64.008334ms
Init buckets time: 4.517708ms
Execution of the command buffer was aborted due to an error during execution. Caused GPU Hang Error (00000003:kIOGPUCommandBufferCallbackErrorHang)
Accumulation and Reduction time: 254.630583ms
Execution of the command buffer was aborted due to an error during execution. Ignored (for causing prior/excessive GPU errors) (00000004:kIOGPUCommandBufferCallbackErrorSubmissionsIgnored)
Final accumulation time: 1.031334ms
Average time to execute MSM with 65536 points and 65536 scalars in 1 iterations is: 260.806292ms

According to this forum, the error might cause due to the task we are executing is too large.

Here's two recommendation to solve this:

  1. try to split the task up cross multiple sub-grid.
  2. the job might be too small(?) but I don't think this is in our case. For example if your threadgroupsPerGrid MTLSize that you pass to dispatchThreadgroups has zero in any of its dimensions and your image has some values in that dimension, your GPU will hang and give that error.

To run on iphone.

  1. compile metal for iphone via ./mopro-core/src/middleware/gpu_explorations/metal/compile_metal.sh ios (remember cd to the root dir)
  2. go into research/gpu-exploration-app and run it (follow the README.md instructions) and remember to switch the target in mopro-config.toml into device.

p.s. There's no metal compilation for simulator, so you should directly run this on your ios device. (either iphone or ipad)

cloudflare-workers-and-pages[bot] commented 3 months ago

Deploying mopro with  Cloudflare Pages  Cloudflare Pages

Latest commit: 2b92772
Status: ✅  Deploy successful!
Preview URL: https://418953de.mopro.pages.dev
Branch Preview URL: https://integrate-metal-msm-in-ios.mopro.pages.dev

View logs

doutv commented 3 months ago

Runtime error on iPad 15.2.

Seems that it requires iOS 17+. https://developer.apple.com/documentation/metal/mtllanguageversion/version3_1

Running MSM in algorithm: Metal Msm (our work)...
Vectors already generated
Init metal (GPU) state...
thread '<unnamed>' panicked at mopro-core/src/middleware/gpu_explorations/metal/msm.rs:65:67:
called `Result::unwrap()` on an `Err` value: PipelineError("Function initialize_buckets is using language version 3.1 which is incompatible with this OS.")
moven0831 commented 3 months ago

This PR will be moved with a newer PR for only iOS integration.