Skip to content

Metal performance improvement - #1292

Open
lightvector wants to merge 1 commit into
masterfrom
metalopt
Open

lightvector wants to merge 1 commit into
masterfrom
metalopt

Conversation

@lightvector

Copy link
Copy Markdown
Owner

@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.

./katago testgpuerror -config gtp_example.cfg -model <net> -boardsize 19
./katago testgpuerror -config gtp_example.cfg -model <net> -boardsize 9
./katago testbackendreference -config gtp_example.cfg -model <net>
./katago benchmark -config gtp_example.cfg -model <net>

# Adding `-override-config metalPadBatch=false` to `testgpuerror` also checks that results agree across batch sizes, 
# since with padding every batch runs at the same size. 
# For a before/after speed comparison:
# `katago benchmarknn -config gtp_example.cfg -model <net> -batch-size WHATEVER_BATCH_SIZE` 
# run on this branch and on master. 

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.

  • Trunk runs in FP16.
  • Parts sensitive to precision stay in FP32: the sums inside global pooling and RMSNorm, the Mish activation, and the policy and value heads. Mish in FP16 measurably hurt accuracy on small boards (on b28c512nbt, 9x9 error went from about 0.09x to 0.45x of the testgpuerror limit), and computing it in FP32 didn't cost speed, I'm guessing due to this part of the code being bandwidth limited, rather than arithmetic limited.
  • Unlike the CUDA and OpenCL backends, this does not apply the scale8 transform (activations scaled down by 8 for FP16 headroom) for convnets (transformers don't scale8 regardless).

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:

  • Q, K and V and swiglu parts are computed by one matrix multiplication with concatenated weights
  • Attention scale is folded into the Q weights before FP16 conversion
  • RoPE is applied with precomputed tables, shared between Q and K when possible (true of all official nets).
  • When the board fills the whole net input (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:

Net batch 1 batchsize 8 batchsize 32
tf3-b11c768 84 -> 112 (+33%) 127 -> 180 (+42%) 144 -> 201 (+39%)
tf3-b10c512 131 -> 179 (+36%) 264 -> 367 (+39%) 294 -> 431 (+47%)
tf2-b10c384 232 -> 320 (+38%) 553 -> 778 (+41%) 671 -> 999 (+49%)
b18c384nbt 218 -> 222 (+2%) 553 -> 592 (+7%) 653 -> 710 (+9%)
b28c512nbt 104 -> 108 (+4%) 208 -> 226 (+9%) 250 -> 273 (+9%)

M1:

Net batch 1 batch 8 batch 16
tf3-b11c768 18.2 -> 24.7 (+36%) 20.0 -> 28.3 (+42%) 19.7 -> 28.4 (+44%)
tf3-b10c512 36.7 -> 49.6 (+35%) 39.5 -> 58.8 (+49%) 38.7 -> 58.9 (+52%)
tf2-b10c384 73.1 -> 100.5 (+37%) 90.7 -> 134.5 (+48%) 88.4 -> 136.2 (+54%)
b18c384nbt 58.1 -> 61.8 (+6%) 93.3 -> 98.2 (+5%) 96.0 -> 102.7 (+7%)
b28c512nbt 26.4 -> 28.3 (+7%) 35.9 -> 39.1 (+9%) 36.1 -> 39.5 (+9%)

Search visits per second (katago benchmark, 1600 visits, default config):

Machine, net 8 threads 16 threads
M4 Max, tf3-b11c768 139 -> 205 (+48%) 162 -> 231 (+43%)
M4 Max, b18c384nbt 445 -> 474 (+6%) 684 -> 727 (+6%)
M1, b18c384nbt 108 -> 121 (+12%) 119 -> 124 (+5%)

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:

Net 19x19 9x9
tf3-b11c768 0.151x 0.159x
tf2-b10c384 0.143x 0.181x
b18c384nbt 0.102x 0.117x
b28c512nbt 0.105x 0.089x

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.

testbackendreference also passes on both machines. On large boards (building in cmake with USE_BIGGER_BOARDS_EXPENSIVE, comparing kata-raw-nn FP16 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

  • MPSGraph's fused scaled-dot-product attention op (macOS 15+): no faster than the explicit version on M1 and M4 Max.
  • Caching compiled graph executables per batch size: no gain. The per-call CPU cost of encoding the graph comes from the number of kernels, not from looking up the compiled graph.
  • Two GPU server threads: modestly faster at equal work in flight in raw benchmarks, but slower in search together with the fixed batch size from Metal: hold the GPU batch size fixed to stop MPSGraph recompile stalls #1270.

@ChinChangYang

Copy link
Copy Markdown
Contributor

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.

Speed

Median of 3 interleaved base/PR runs, with the machine otherwise mostly idle. Values are base -> PR.

Raw neural net evaluations per second (benchmarknn, default masked path, one GPU server thread, 19x19):

Net batch 1 batch 8 batch 16 batch 32
tf3-b11c768 103 -> 229 (2.22x) 162 -> 576 (3.55x) 181 -> 624 (3.45x) 181 -> 626 (3.47x)
tf3-b10c512 114 -> 175 (1.53x) 339 -> 1039 (3.07x) 373 -> 1167 (3.13x) 382 -> 1257 (3.29x)
tf2-b10c384 111 -> 390 (3.52x) 694 -> 1786 (2.57x) 776 -> 2247 (2.90x) 822 -> 2559 (3.11x)
b18c384nbt 112 -> 118 (+5%) 628 -> 1132 (+80%) 690 -> 1726 (2.50x) 726 -> 1788 (2.46x)
b28c512nbt 119 -> 160 (+35%) 252 -> 571 (2.26x) 280 -> 709 (2.53x) 292 -> 746 (2.55x)

Search visits per second (benchmark, 1600 visits, default config):

Net 8 threads 16 threads
tf3-b11c768 186 -> 640 (3.44x) 202 -> 742 (3.67x)
b18c384nbt 546 -> 917 (+68%) 759 -> 1396 (+84%)

The b18 PR result at 8 threads was noisy (1113, 917, 860 over the three runs).

Accuracy

FP16 against the PR's own FP32 (testgpuerror -quick, fraction of limit, worst 99th percentile margin):

Net 19x19 9x9
tf3-b11c768 0.232x 0.181x
tf2-b10c384 0.146x 0.168x
b18c384nbt 0.102x 0.116x
b28c512nbt 0.066x 0.065x

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).

  • testbackendreference passes for those five nets.
  • With metalPadBatch=false on 19x19, the three transformer nets also pass (0.148x tf2-b10c384, 0.128x tf3-b10c512, 0.229x tf3-b11c768).

This branch has not been deployed

No deployments
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.

2 participants