Repository navigation
Metal performance improvement - #1292
lightvector wants to merge 1 commit into
Conversation
…r transformer layout
|
Tested c9d129e against its base 81e9d43 on an Apple M5 Max (40-core GPU, 128 GB), macOS 27.2.1. Everything I ran passes, and the speedup on this machine is much larger than on M1/M4, for convnets as well. SpeedMedian of 3 interleaved base/PR runs, with the machine otherwise mostly idle. Values are base -> PR. Raw neural net evaluations per second (
Search visits per second (
The b18 PR result at 8 threads was noisy (1113, 917, 860 over the three runs). AccuracyFP16 against the PR's own FP32 (
These are close to your M1 values, except that tf3-b11c768 on 19x19 is higher (0.232x against 0.151x) and b28 is lower. Net versions may differ: I used tf3-b11c768-s12382M, tf2-b10c384-s2941M, b18c384nbt-s9996604416 and b28c512nbt-s8326494464 (and tf3-b10c512-s3203M above).
|
@ChinChangYang, or other metal devs/users @Slymaster @zhen-ke I would be interested in your thoughts on this branch and if it works for you.
This branch greatly improves the performance of transformers on Metal GPUs (MPSGraph, not ANE), improving speed by perhaps 30%-50% on the M1 and M4 that I obtained from AWS for testing for the tf3-b11 nets, and for convents improves it by maybe 2%-10%.
This PR was NOT tested on anything other than the M1 and M4 that I rented, so if you have other machines of your own to try it out, that would be great. My claude suggests this may include M2, M3, M5, Intel Macs, macOS versions other than 27.2, etc.
e.g.
Changes
GPU path now uses FP16
metalUseFP16(default true) previously affected only Neural Engine threads. Now GPU threads use FP16 or not based on it too.Transformer block layout was rearranged
Previously each attention and feed-forward block converted the activations between the convolution layout (NCHW) and a channels-last layout and back. Now a run of consecutive transformer blocks, including nested bottleneck blocks whose inner blocks are all transformers, converts once at the start and once at the end. Also:
requireExactNNLen), the mask multiplications and attention key mask are skipped.Speed
Baseline is current master, which already includes #1264, #1270 and #1274. I used AWS rented machines: an M1 (8-core GPU, 16 GB) and an M4 Max (40-core GPU), both on macOS 27.2.
Raw neural net evaluations per second (
katago benchmarknn, default masked path, one GPU server thread):M4 Max:
M1:
Search visits per second (
katago benchmark, 1600 visits, default config):Accuracy
Values are reported as a fraction of
testgpuerror's tolerance, i.e. 1.0x means failing and lower is better. Each value below is the one that had the worst margin on the 99th percentile.FP16 as of this PR, against FP32 (
testgpuerror -quick, M1) on two board sizes:For comparison, CUDA FP16 on b28c512nbt gives 0.105x on 19x19 and 0.131x on 9x9. So this is right in line with what CUDA gives. On M4 Max the numbers are also similar.
testbackendreferencealso passes on both machines. On large boards (building in cmake withUSE_BIGGER_BOARDS_EXPENSIVE, comparingkata-raw-nnFP16 against FP32): b18, b28, b40c256 and b60c320 on empty 37x37 and 50x50 boards, and on a 50x50 board randomly filled with 30% stones, at least didn't produce any non-finite outputs, although large errors were observed in some cases.Tried and not included