# What the mcg DECODE costs in the mainloop, and which form was chosen (#1861)
#
#   for S in 27 32 36; do I8_ONLY_BITS=4 I8_MCG_INT_CONTROL=1 \
#     python3 bench_codebook.py --rows 1024 4096 8192 --shape $S --bits 4; done
#   for K in 6 8;      do I8_ONLY_BITS=$K I8_MCG_INT_CONTROL=1 \
#     python3 bench_codebook.py --rows 4096 --shape 27 --bits $K; done
#
# 206 gpu0, RTX 4090, registry.arbi.work/arbi-serve:test-latest, k = n = 4096.
# Three codebooks as ARMS of the same mainloop over the SAME trellis bytes, one
# binary, interleaved palindromic rounds, within-round paired ratios pooled.
# The arms do not compute the same answer and are not meant to: this times
# instruction mixes.  Correctness is gate1_mcg / gate1_decode / gate2_mapping.
#
# THE FINDING.  The two mcg decode forms TIE on accuracy over the full
# 65536-index domain (rel-MSE 5.2668e-05 both; mcg_grid.py), so the choice was
# made here:
#
#   shape 27 K=4 M=8192   mcg fp16+fma  x1.0451     mcg integer  x2.2316
#   shape 32 K=4 M=8192   mcg fp16+fma  x1.0262     mcg integer  x2.1241
#   shape 36 K=4 M=8192   mcg fp16+fma  x1.0082     mcg integer  x1.5385
#   shape 27 K=6 M=4096   mcg fp16+fma  x1.0463     mcg integer  x1.9854
#   shape 27 K=8 M=4096   mcg fp16+fma  x1.0489     mcg integer  x1.9809
#
# The integer form is 54-123% slower.  It takes each 16-bit half apart, so there
# is no half2 pairing and there is a variable shift per half -- and the cost is
# far beyond its instruction count, because it also fills the slack the shape
# had.  The fp16 form costs 0.8-4.9%.
#
# REGISTERS ARE NOT THE MECHANISM.  At shape 27 K=4: mul1 199, mcg 196, integer
# 201, and blocks/SM is 2 for all three.  The mcg premium is instruction cost.
#
# READ THE CEILING ROW.  NULL_DECODE is the same geometry with the decode
# DELETED -- wrong by construction, never served.  On shape 27 it says the whole
# decode is worth 8.3-8.5%, so mcg spends about half of what deleting the decode
# entirely would save.  On shapes 32 and 36 it says the decode is worth NOTHING
# (x1.00): mul1's decode is fully hidden there and mcg's is not quite, which is
# where the 1-3% comes from.
#
# THIS DOES NOT BEAR ON FOOTGUN 35 ("hidden in time is not free in watts").
# nvidia-smi is sampled once per ROW here, not per arm, so there is no per-arm
# power figure in this data.  Not chased.
#
# The `[state]` fields are clocks.sm, power.draw, temperature.gpu and the
# software power-cap throttle reason, sampled after the round loop.

=== shape 27 ===
[state] 210, 33.72, 41, Not Active

  shape 27 at K=4, per codebook, off the compiled kernel:
    mul1                         regs/thread=199  blocks/SM=2  smem=49152 B  local=0 B
    mcg                          regs/thread=196  blocks/SM=2  smem=49152 B  local=0 B
    mcg (integer-form control)   regs/thread=201  blocks/SM=2  smem=49152 B  local=0 B

  M=1024 k=4096 n=4096 shape=27 K=4   [state] 2625, 35.93, 41, Not Active
    mul1                                 82.0 us   418.99 TOPS  paired x1.0000 vs mul1
    mcg                                  84.7 us   405.84 TOPS  paired x1.0323 vs mul1
    mcg (integer-form control)          179.5 us   191.39 TOPS  paired x2.1883 vs mul1
    NULL_DECODE s40 (no decode at all)     75.9 us   452.53 TOPS  paired x0.9260 vs mul1

  M=4096 k=4096 n=4096 shape=27 K=4   [state] 2625, 35.93, 41, Not Active
    mul1                                298.9 us   459.78 TOPS  paired x1.0000 vs mul1
    mcg                                 311.7 us   440.92 TOPS  paired x1.0428 vs mul1
    mcg (integer-form control)          660.3 us   208.14 TOPS  paired x2.2051 vs mul1
    NULL_DECODE s40 (no decode at all)    276.6 us   496.95 TOPS  paired x0.9230 vs mul1

  M=8192 k=4096 n=4096 shape=27 K=4   [state] 2820, 98.72, 60, Not Active
    mul1                                593.3 us   463.34 TOPS  paired x1.0000 vs mul1
    mcg                                 618.3 us   444.55 TOPS  paired x1.0451 vs mul1
    mcg (integer-form control)         1333.6 us   206.12 TOPS  paired x2.2316 vs mul1
    NULL_DECODE s40 (no decode at all)    555.5 us   494.81 TOPS  paired x0.9300 vs mul1
=== shape 32 ===
[state] 2865, 94.97, 45, Not Active

  shape 32 at K=4, per codebook, off the compiled kernel:
    mul1                         regs/thread=122  blocks/SM=2  smem=24576 B  local=0 B
    mcg                          regs/thread=121  blocks/SM=2  smem=24576 B  local=0 B
    mcg (integer-form control)   regs/thread=125  blocks/SM=2  smem=24576 B  local=0 B

  M=1024 k=4096 n=4096 shape=32 K=4   [state] 2865, 94.97, 45, Not Active
    mul1                                 80.0 us   429.27 TOPS  paired x1.0000 vs mul1
    mcg                                  81.7 us   420.75 TOPS  paired x1.0225 vs mul1
    mcg (integer-form control)          169.0 us   203.26 TOPS  paired x2.1150 vs mul1
    NULL_DECODE s42 (no decode at all)     80.4 us   427.56 TOPS  paired x1.0042 vs mul1

  M=4096 k=4096 n=4096 shape=32 K=4   [state] 2625, 100.12, 56, Not Active
    mul1                                303.4 us   452.93 TOPS  paired x1.0000 vs mul1
    mcg                                 311.6 us   441.14 TOPS  paired x1.0262 vs mul1
    mcg (integer-form control)          635.6 us   216.23 TOPS  paired x2.0927 vs mul1
    NULL_DECODE s42 (no decode at all)    299.1 us   459.52 TOPS  paired x0.9853 vs mul1

  M=8192 k=4096 n=4096 shape=32 K=4   [state] 2625, 100.12, 56, Not Active
    mul1                                606.9 us   452.93 TOPS  paired x1.0000 vs mul1
    mcg                                 624.5 us   440.18 TOPS  paired x1.0262 vs mul1
    mcg (integer-form control)         1296.9 us   211.95 TOPS  paired x2.1241 vs mul1
    NULL_DECODE s42 (no decode at all)    612.7 us   448.64 TOPS  paired x1.0118 vs mul1
=== shape 36 ===
[state] 2865, 94.45, 46, Not Active

  shape 36 at K=4, per codebook, off the compiled kernel:
    mul1                         regs/thread=224  blocks/SM=2  smem=36864 B  local=0 B
    mcg                          regs/thread=223  blocks/SM=2  smem=36864 B  local=0 B
    mcg (integer-form control)   regs/thread=225  blocks/SM=2  smem=36864 B  local=0 B

  M=1024 k=4096 n=4096 shape=36 K=4   [state] 2625, 76.21, 44, Not Active
    mul1                                 87.0 us   394.76 TOPS  paired x1.0000 vs mul1
    mcg                                  87.0 us   395.15 TOPS  paired x0.9990 vs mul1
    mcg (integer-form control)          127.2 us   270.06 TOPS  paired x1.4618 vs mul1
    NULL_DECODE s41 (no decode at all)     87.3 us   393.61 TOPS  paired x1.0023 vs mul1

  M=4096 k=4096 n=4096 shape=36 K=4   [state] 2625, 76.21, 44, Not Active
    mul1                                313.1 us   438.98 TOPS  paired x1.0000 vs mul1
    mcg                                 315.2 us   436.01 TOPS  paired x1.0070 vs mul1
    mcg (integer-form control)          484.6 us   283.61 TOPS  paired x1.5441 vs mul1
    NULL_DECODE s41 (no decode at all)    315.3 us   435.89 TOPS  paired x1.0052 vs mul1

  M=8192 k=4096 n=4096 shape=36 K=4   [state] 2505, 156.76, 61, Active
    mul1                                628.9 us   437.07 TOPS  paired x1.0000 vs mul1
    mcg                                 634.2 us   433.43 TOPS  paired x1.0082 vs mul1
    mcg (integer-form control)          968.7 us   283.75 TOPS  paired x1.5385 vs mul1
    NULL_DECODE s41 (no decode at all)    629.1 us   436.95 TOPS  paired x0.9991 vs mul1
=== K=6 shape 27 ===
[state] 210, 35.92, 39, Not Active

  shape 27 at K=6, per codebook, off the compiled kernel:
    mul1                         regs/thread=209  blocks/SM=1  smem=57344 B  local=0 B
    mcg                          regs/thread=210  blocks/SM=1  smem=57344 B  local=0 B
    mcg (integer-form control)   regs/thread=218  blocks/SM=1  smem=57344 B  local=0 B

  M=4096 k=4096 n=4096 shape=27 K=6   [state] 2625, 46.30, 40, Not Active
    mul1                                415.2 us   331.04 TOPS  paired x1.0000 vs mul1
    mcg                                 434.2 us   316.50 TOPS  paired x1.0463 vs mul1
    mcg (integer-form control)          822.4 us   167.12 TOPS  paired x1.9854 vs mul1
    NULL_DECODE s40 (no decode at all)    378.6 us   363.00 TOPS  paired x0.9146 vs mul1
=== K=8 shape 27 ===
[state] 210, 36.48, 40, Not Active

  shape 27 at K=8, per codebook, off the compiled kernel:
    mul1                         regs/thread=199  blocks/SM=1  smem=65536 B  local=0 B
    mcg                          regs/thread=195  blocks/SM=1  smem=65536 B  local=0 B
    mcg (integer-form control)   regs/thread=196  blocks/SM=1  smem=65536 B  local=0 B

  M=4096 k=4096 n=4096 shape=27 K=8   [state] 2625, 41.09, 41, Not Active
    mul1                                413.1 us   332.72 TOPS  paired x1.0000 vs mul1
    mcg                                 433.2 us   317.29 TOPS  paired x1.0489 vs mul1
    mcg (integer-form control)          818.3 us   167.96 TOPS  paired x1.9809 vs mul1
    NULL_DECODE s40 (no decode at all)    378.6 us   363.05 TOPS  paired x0.9165 vs mul1
DONE4
