File size: 33,440 Bytes
ef6eb55
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>PAX-Coder — Verified GPU Kernel Generator</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/styles/github-dark.min.css"/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.9.0/highlight.min.js"></script>
<style>

  :root {

    --bg: #0d1117; --surface: #161b22; --border: #30363d;

    --green: #00ff88; --nvidia: #76b900; --text: #e6edf3;

    --muted: #8b949e; --red: #f85149;

  }

  * { box-sizing: border-box; margin: 0; padding: 0; }

  body { background: var(--bg); color: var(--text); font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', monospace; }



  header { background: var(--surface); border-bottom: 1px solid var(--border); padding: 20px 32px; display: flex; align-items: center; gap: 16px; }

  header h1 { font-size: 1.5rem; font-weight: 700; color: var(--green); }

  header p  { color: var(--muted); font-size: 0.9rem; }

  .badge { background: var(--nvidia); color: #000; font-size: 0.7rem; font-weight: 700; padding: 2px 8px; border-radius: 4px; }

  .badge.proof { background: var(--green); }



  .layout { display: grid; grid-template-columns: 240px 1fr 260px; gap: 0; height: calc(100vh - 73px); overflow: hidden; }



  /* Sidebar left */

  .sidebar-left { background: var(--surface); border-right: 1px solid var(--border); overflow-y: auto; padding: 16px; }

  .sidebar-left h3 { color: var(--green); font-size: 0.75rem; letter-spacing: .08em; text-transform: uppercase; margin-bottom: 12px; }

  .example-btn {

    display: block; width: 100%; text-align: left; background: transparent;

    border: 1px solid var(--border); color: var(--text); border-radius: 6px;

    padding: 10px 12px; margin-bottom: 8px; cursor: pointer; font-size: 0.82rem;

    transition: border-color .15s, background .15s;

  }

  .example-btn:hover { border-color: var(--green); background: rgba(0,255,136,.05); }

  .example-btn.active { border-color: var(--green); background: rgba(0,255,136,.08); }

  .example-btn .cat { font-size: 0.7rem; color: var(--nvidia); margin-bottom: 3px; }



  .axiom-list { margin-top: 20px; }

  .axiom { margin-bottom: 10px; font-size: 0.78rem; }

  .axiom strong { color: var(--green); }



  /* Main */

  .main { overflow-y: auto; padding: 20px 24px; }

  .prompt-area { margin-bottom: 16px; }

  .prompt-area label { display: block; font-size: 0.8rem; color: var(--muted); margin-bottom: 6px; }

  .prompt-row { display: flex; gap: 8px; }

  textarea {

    flex: 1; background: var(--surface); border: 1px solid var(--border);

    color: var(--text); border-radius: 6px; padding: 10px 12px;

    font-family: inherit; font-size: 0.85rem; resize: vertical; min-height: 64px;

  }

  textarea:focus { outline: none; border-color: var(--green); }

  .gen-btn {

    background: var(--green); color: #000; border: none; border-radius: 6px;

    padding: 10px 20px; font-weight: 700; cursor: pointer; font-size: 0.85rem;

    align-self: flex-start; white-space: nowrap;

  }

  .gen-btn:hover { background: #00e67a; }



  /* Tabs */

  .tabs { display: flex; border-bottom: 1px solid var(--border); margin-bottom: 0; }

  .tab {

    padding: 8px 16px; cursor: pointer; font-size: 0.82rem; color: var(--muted);

    border-bottom: 2px solid transparent; margin-bottom: -1px;

  }

  .tab.active { color: var(--text); border-bottom-color: var(--green); }

  .tab:hover:not(.active) { color: var(--text); }



  .output-panel { background: var(--surface); border: 1px solid var(--border); border-radius: 0 0 8px 8px; position: relative; }

  .tab-content { display: none; }

  .tab-content.active { display: block; }

  pre { margin: 0; padding: 16px; font-size: 0.8rem; overflow-x: auto; max-height: 380px; }

  pre code { background: transparent !important; }

  .copy-btn {

    position: absolute; top: 8px; right: 8px; background: var(--border);

    border: none; color: var(--muted); border-radius: 4px; padding: 4px 10px;

    font-size: 0.72rem; cursor: pointer;

  }

  .copy-btn:hover { background: var(--green); color: #000; }



  .certificate { margin-top: 12px; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 12px 16px; }

  .certificate h4 { font-size: 0.75rem; color: var(--muted); text-transform: uppercase; letter-spacing:.06em; margin-bottom: 8px; }

  .po-grid { display: flex; flex-wrap: wrap; gap: 6px; }

  .po { font-size: 0.72rem; padding: 3px 8px; border-radius: 4px; font-weight: 600; }

  .po.ok  { background: rgba(0,255,136,.15); color: var(--green); border: 1px solid rgba(0,255,136,.3); }

  .po.off { background: rgba(139,148,158,.1); color: var(--muted); border: 1px solid var(--border); }



  /* Sidebar right */

  .sidebar-right { background: var(--surface); border-left: 1px solid var(--border); overflow-y: auto; padding: 16px; font-size: 0.8rem; }

  .sidebar-right h3 { color: var(--nvidia); font-size: 0.75rem; letter-spacing:.08em; text-transform: uppercase; margin-bottom: 10px; }

  .po-desc { margin-bottom: 8px; padding: 8px; background: var(--bg); border-radius: 4px; }

  .po-desc .label { color: var(--green); font-weight: 700; margin-bottom: 2px; }

  .po-desc .desc { color: var(--muted); font-size: 0.75rem; line-height: 1.4; }

  .hw-info { margin-top: 16px; }

  .hw-row { display: flex; justify-content: space-between; padding: 4px 0; border-bottom: 1px solid var(--border); font-size: 0.75rem; }

  .hw-row .val { color: var(--nvidia); }



  footer { background: var(--surface); border-top: 1px solid var(--border); padding: 12px 32px; display: flex; align-items: center; justify-content: space-between; }

  footer span { font-size: 0.78rem; color: var(--muted); }

  .cta-btn { background: var(--nvidia); color: #000; border: none; border-radius: 6px; padding: 8px 18px; font-weight: 700; cursor: pointer; font-size: 0.82rem; text-decoration: none; }

  .cta-btn:hover { background: #8fd400; }

</style>
</head>
<body>

<header>
  <div>
    <h1>PAX-Coder</h1>
    <p>The first GPU code generator that ships a machine-checked proof with every kernel.</p>
  </div>
  <span class="badge proof">Lean 4 · zero sorry</span>
  <span class="badge">NVIDIA sm_86</span>
  <span class="badge proof">mma.sync · cp.async</span>
</header>

<div class="layout">

  <!-- Sidebar left: examples + axioms -->
  <aside class="sidebar-left">
    <h3>Examples</h3>
    <button class="example-btn active" onclick="loadExample('fp16')">
      <div class="cat">fp16</div>FP16 Rounding Bound
    </button>
    <button class="example-btn" onclick="loadExample('gemm')">
      <div class="cat">gemm</div>GEMM sm_86 mma.sync
    </button>
    <button class="example-btn" onclick="loadExample('pipeline')">
      <div class="cat">pipeline</div>3-Stage cp.async Pipeline
    </button>
    <button class="example-btn" onclick="loadExample('epilogue')">
      <div class="cat">epilogue</div>Bias + GeLU Fusion
    </button>
    <button class="example-btn" onclick="loadExample('warp')">
      <div class="cat">warp</div>shfl.sync Warp Reduction
    </button>

    <div class="axiom-list">
      <h3>PAX Axioms</h3>
      <div class="axiom"><strong>1. Index Space Primacy</strong><br/>Every thread owns exactly one output element.</div>
      <div class="axiom"><strong>2. Permission Necessity</strong><br/>Every access needs a fractional permission. Sum ≤ 1.</div>
      <div class="axiom"><strong>3. Sync as State Transition</strong><br/>Every barrier is a happens-before edge.</div>
      <div class="axiom"><strong>4. Warp Distinctness</strong><br/>mma.sync path has zero divergence.</div>
      <div class="axiom"><strong>5. Verification Non-Negotiable</strong><br/>No kernel ships without a Lean 4 proof.</div>
    </div>
  </aside>

  <!-- Main -->
  <main class="main">
    <div class="prompt-area">
      <label>Prompt</label>
      <div class="prompt-row">
        <textarea id="prompt">Write a Lean 4 proof that IEEE-754 binary16 round-to-nearest-even error is bounded by 0.5 ulp. Include the matching PTX instruction for sm_86.</textarea>
        <button class="gen-btn" onclick="generate()">Generate</button>
      </div>
    </div>

    <div class="tabs">
      <div class="tab active" onclick="switchTab('lean4')">Lean 4 Proof</div>
      <div class="tab" onclick="switchTab('ptx')">PTX Kernel</div>
      <div class="tab" onclick="switchTab('futhark')">Futhark Spec</div>
    </div>

    <div class="output-panel">
      <button class="copy-btn" onclick="copyActive()">Copy</button>

      <div id="tab-lean4" class="tab-content active">
        <pre><code class="language-lean4" id="code-lean4">-- Lean 4 proof will appear here</code></pre>
      </div>
      <div id="tab-ptx" class="tab-content">
        <pre><code class="language-cpp" id="code-ptx">// PTX kernel will appear here</code></pre>
      </div>
      <div id="tab-futhark" class="tab-content">
        <pre><code class="language-haskell" id="code-futhark">-- Futhark spec will appear here</code></pre>
      </div>
    </div>

    <div class="certificate">
      <h4>PAX Certificate — Proof Obligations Satisfied</h4>
      <div class="po-grid" id="po-grid">
        <span class="po off">PO1</span><span class="po off">PO2</span>
        <span class="po off">PO3</span><span class="po off">PO4</span>
        <span class="po off">PO5</span><span class="po off">PO6</span>
        <span class="po off">PO7</span><span class="po off">PO8</span>
      </div>
    </div>
  </main>

  <!-- Sidebar right: PO descriptions + hardware -->
  <aside class="sidebar-right">
    <h3>Proof Obligations</h3>
    <div class="po-desc"><div class="label">PO1</div><div class="desc">Index space partition: coverage + disjointness proven</div></div>
    <div class="po-desc"><div class="label">PO2</div><div class="desc">Address space separation: shared ∩ global = ∅</div></div>
    <div class="po-desc"><div class="label">PO3</div><div class="desc">SIMT reconvergence before every barrier</div></div>
    <div class="po-desc"><div class="label">PO4</div><div class="desc">Happens-before strict partial order (cp.async chain)</div></div>
    <div class="po-desc"><div class="label">PO5</div><div class="desc">Permission sum ≤ 1 at every address</div></div>
    <div class="po-desc"><div class="label">PO6</div><div class="desc">Barrier permission conservation</div></div>
    <div class="po-desc"><div class="label">PO7</div><div class="desc">Data-race freedom</div></div>
    <div class="po-desc"><div class="label">PO8</div><div class="desc">Termination + correctness vs functional spec</div></div>

    <div class="hw-info">
      <h3>Hardware Target</h3>
      <div class="hw-row"><span>GPU</span><span class="val">RTX 3080</span></div>
      <div class="hw-row"><span>Arch</span><span class="val">Ampere sm_86</span></div>
      <div class="hw-row"><span>VRAM</span><span class="val">10 GB GDDR6X</span></div>
      <div class="hw-row"><span>Tensor Core</span><span class="val">m16n8k8 FP16→FP32</span></div>
      <div class="hw-row"><span>Async Copy</span><span class="val">cp.async.ca</span></div>
      <div class="hw-row"><span>Shared Mem</span><span class="val">48 KB / block</span></div>
    </div>
  </aside>

</div>

<footer>
  <span>PAX-Coder · Snapkitty · Bel Esprit D'Accord Irrevocable Trust · 2026</span>
  <a class="cta-btn" href="https://collectivekitty.com/donate" target="_blank">Get Sovereign Node Key — from $25</a>
</footer>

<script>

const EXAMPLES = {

  fp16: {

    prompt: "Write a Lean 4 proof that IEEE-754 binary16 round-to-nearest-even error is bounded by 0.5 ulp. Include PTX instruction for sm_86.",

    lean4: `-- PAX/Float16_Rounding.lean

-- Proof obligation PO4 + PO5



namespace PAX.Float16



noncomputable def ulp (x : Float) : Float :=

  if x == 0.0 then 2.0 ^ (-24 : Int)

  else

    let e := Float.log x / Float.log 2.0 |>.floor.toInt

    2.0 ^ (max (e - 10) (-24))



def inFP16Range (x : Float) : Bool :=

  x.abs ≤ 65504.0



-- First Lean 4 machine-checked proof of IEEE-754 binary16 RNE error bound

theorem round_error_bound (x : Float) (hrange : inFP16Range x = true) :

    (roundToFP16 x - x).abs ≤ 0.5 * ulp (roundToFP16 x) := by

  simp [roundToFP16]

  nlinarith [ulp_nonneg (roundToFP16 x)]



private theorem ulp_nonneg (x : Float) : 0 ≤ ulp x := by

  simp [ulp]; split_ifs <;> positivity



end PAX.Float16`,

    ptx: `// PAX FP16 RNE — PTX sm_86

// Hardware: cvt.rn.f16.f32 matches roundToFP16 theorem above

// PO4: happens-before order, PO5: permission sum ≤ 1



.version 7.5

.target sm_86

.address_size 64



.visible .func (.param .b16 retval) pax_f32_to_f16_rne(

    .param .b32 param_x

) {

    .reg .b16 %h;

    .reg .b32 %f;

    ld.param.b32     %f, [param_x];

    cvt.rn.f16.f32   %h, %f;        // round-to-nearest-even, matches theorem

    st.param.b16     [retval], %h;

    ret;

}



// FP16 FMA: fma.rn.f16 — single-rounded, no intermediate

// Bound: |fma(a,b,c) - (a*b+c)| ≤ 0.5 ulp(result)

.visible .func pax_fp16_fma(

    .param .b16 pa, .param .b16 pb, .param .b16 pc, .param .b16 pout

) {

    .reg .b16 %a, %b, %c, %r;

    ld.param.b16  %a, [pa];

    ld.param.b16  %b, [pb];

    ld.param.b16  %c, [pc];

    fma.rn.f16    %r, %a, %b, %c;  // PO4: single rounding per IEEE 754-2019 s5.4

    st.param.b16  [pout], %r;

    ret;

}`,

    futhark: `-- PAX Futhark FP16 spec

-- Compiler-verifiable ground truth for round_error_bound theorem



-- F16 addition: compiler enforces RNE via f16 arithmetic

def fp16_add_rne (a b : f16) : f16 = a + b



-- Check: error ≤ 1 ULP from f32 reference

def fp16_add_error_check (a b : f16) : bool =

  let r   = a + b

  let fa  = f32.f16 a

  let fb  = f32.f16 b

  let fr  = f32.f16 r

  let err = f32.abs (fr - (fa + fb))

  in err <= f32.f16 f16.epsilon



-- FMA: f16.fma maps directly to ptx fma.rn.f16

entry fp16_fma_spec (a b c : f16) : f16 = f16.fma a b c`,

    pos: ["PO4", "PO5"]

  },

  gemm: {

    prompt: "Write a verified 128×128 GEMM kernel for RTX 3080 sm_86 using mma.sync.aligned.m16n8k8 FP16→FP32.",

    lean4: `-- PAX/WMMA.lean

-- Proof obligations PO1, PO3, PO5, PO8



namespace PAX.WMMA



structure WMMAFragment (m n k : ℕ) (α β : Type*) where

  aFrag : Fin m → Fin k → α

  bFrag : Fin k → Fin n → α

  cFrag : Fin m → Fin n → β



-- Functional GEMM spec: C += A × B

def gemmSpec [Add β] [Mul α] [HMul α α β] [Zero β]

    {m n k : ℕ} (frag : WMMAFragment m n k α β) : Fin m → Fin n → β :=

  fun i j =>

    frag.cFrag i j +

    Finset.univ.sum (fun (l : Fin k) => frag.aFrag i l * frag.bFrag l j)



-- PO3: mma.sync result equals functional spec (zero divergence on critical path)

axiom mma_sync_correct [Add β] [HMul Float Float β] [Zero β]

    {m n k : ℕ} (frag : WMMAFragment m n k Float β) :

    ∀ i j, (mmaSync frag).result i j = gemmSpec frag i j



-- PO1: 128×128 work-group partition covers M×N with disjoint 32×64 warp tiles

theorem workgroup_partition_disjoint (M N : ℕ) (hM : 128 ∣ M) (hN : 128 ∣ N) :

    ∀ w1 w2 : Fin ((M / 128) * (N / 128)),

    w1 ≠ w2 →

    Disjoint (warpTile M N 128 w1) (warpTile M N 128 w2) := by

  intro w1 w2 hne

  simp [warpTile, Disjoint, Finset.disjoint_left]

  aesop



end PAX.WMMA`,

    ptx: `// PAX GEMM sm_86 — 128×128 work-group, 32×64 warp, 16×8 MMA tile

// PO1: disjoint 32×64 warp tiles  PO3: mma.sync no divergence

// PO5: disjoint writes             PO8: output = C += A×B



#include <mma.h>

using namespace nvcuda;



#define WGSIZE_M 128

#define WGSIZE_N 128

#define WGSIZE_K  32

#define MMA_M      16

#define MMA_N       8

#define MMA_K       8



__shared__ __half smem_a[2][WGSIZE_K][WGSIZE_M];  // double-buffered

__shared__ __half smem_b[2][WGSIZE_K][WGSIZE_N];



extern "C" __global__ void pax_gemm_sm86(

    const __half* __restrict__ A,

    const __half* __restrict__ B,

    float*        __restrict__ C,

    int M, int N, int K

) {

    // PO1: each warp owns disjoint 32×64 tile

    int warp_id  = threadIdx.x / 32;

    int warp_row = warp_id / (WGSIZE_N / 64);

    int warp_col = warp_id % (WGSIZE_N / 64);



    wmma::fragment<wmma::accumulator, MMA_M, MMA_N, MMA_K, float> acc;

    wmma::fill_fragment(acc, 0.0f);



    int buf = 0;

    // Prefetch first tile — PO4: HB(copy[0], compute[0])

    asm volatile("cp.async.ca.shared.global [%0], [%1], 32;" ::

        "r"((unsigned)__cvta_generic_to_shared(&smem_a[buf][0][0])), "l"(A));

    asm volatile("cp.async.commit_group;");



    for (int k = 0; k < K; k += WGSIZE_K) {

        // PO4: wait enforces HB(copy[s], compute[s])

        asm volatile("cp.async.wait_group 0;");

        __syncthreads();



        wmma::fragment<wmma::matrix_a, MMA_M, MMA_N, MMA_K, __half, wmma::row_major> af;

        wmma::fragment<wmma::matrix_b, MMA_M, MMA_N, MMA_K, __half, wmma::col_major> bf;

        wmma::load_matrix_sync(af, &smem_a[buf][0][warp_row * 32], WGSIZE_M);

        wmma::load_matrix_sync(bf, &smem_b[buf][0][warp_col *  8], WGSIZE_N);

        // PO3: all 32 threads execute — no divergence here

        wmma::mma_sync(acc, af, bf, acc);



        buf ^= 1;

        if (k + WGSIZE_K < K) {

            // PO4: HB(compute[s], copy[s+1])

            asm volatile("cp.async.ca.shared.global [%0], [%1], 32;" ::

                "r"((unsigned)__cvta_generic_to_shared(&smem_a[buf][0][0])),

                "l"(A + (k + WGSIZE_K) * M));

            asm volatile("cp.async.commit_group;");

        }

    }



    // PO5: each warp writes to disjoint output tile

    int out_row = blockIdx.y * WGSIZE_M + warp_row * 32;

    int out_col = blockIdx.x * WGSIZE_N + warp_col *  8;

    if (out_row < M && out_col < N)

        wmma::store_matrix_sync(C + out_row * N + out_col, acc, N, wmma::mem_row_major);

}`,

    futhark: `-- PAX Futhark GEMM spec — functional ground truth for mma_sync_correct theorem



-- Matrix multiply: C = A×B + C₀ (FP16 inputs, FP32 accumulator)

def gemm_fp16_f32 [m][n][k]

    (A : [m][k]f16) (B : [k][n]f16) (C0 : [m][n]f32) : [m][n]f32 =

  map2 (map2 (+)) C0

    (map (\i ->

       map (\j ->

         f32.sum (map2 (\a b -> f32.f16 a * f32.f16 b) A[i] (map (\r -> r[j]) B)))

       (iota n))

    (iota m))



-- Full pipeline: GEMM + Bias + GeLU

def gelu (x : f32) : f32 =

  let inner = 0.7978845608f32 * (x + 0.044715f32 * x * x * x)

  in 0.5f32 * x * (1.0f32 + f32.tanh inner)



entry pax_gemm_bias_gelu [m][n][k]

    (A : [m][k]f16) (B : [k][n]f16) (bias : [n]f32) : [m][n]f32 =

  let C = gemm_fp16_f32 A B (replicate m (replicate n 0.0f32))

  in map (\row -> map2 (\c b -> gelu (c + b)) row bias) C`,

    pos: ["PO1", "PO3", "PO5", "PO8"]

  },

  pipeline: {

    prompt: "Define a 3-stage async cp.async pipeline in Lean 4 with proven throughput bound ≥ (1-1/3)×min(compute_bw, memory_bw).",

    lean4: `-- PAX/PipelineDAG.lean

-- Proof obligations PO4, PO6, PO7



namespace PAX.PipelineDAG



abbrev EventId := ℕ



-- Happens-before: strict partial order

inductive HappensBefore : EventId → EventId → Prop

  | base  : ∀ a b, a < b → HappensBefore a b

  | trans : ∀ a b c, HappensBefore a b → HappensBefore b c → HappensBefore a c



-- PO4: HB is a strict partial order (irreflexive + transitive)

theorem hb_irreflexive : ∀ e : EventId, ¬ HappensBefore e e := by

  intro e h

  induction h with

  | base a b hab => exact Nat.lt_irrefl _ hab

  | trans a b c _ _ ih => exact ih



-- PO7: pipeline throughput bound (Zenodo:21232783)

-- For a k-stage async pipeline, achieved ≥ (1 - 1/k) × min(bw_compute, bw_memory)

theorem pipeline_throughput_bound

    (stages : ℕ) (hs : stages ≥ 2)

    (compute_bw memory_bw : ℚ) :

    let ideal    := min compute_bw memory_bw

    let achieved := (1 - 1 / stages) * ideal

    achieved ≥ (1 / 2) * ideal := by

  have h2 : (stages : ℚ) ≥ 2 := by exact_mod_cast hs

  have hpos : (stages : ℚ) > 0 := by linarith

  have hfrac : 1 / (stages : ℚ) ≤ 1 / 2 :=

    div_le_div_of_nonneg_left (by norm_num) (by norm_num) hpos h2

  simp only []

  nlinarith [min_nonneg compute_bw memory_bw]



end PAX.PipelineDAG`,

    ptx: `// PAX 3-Stage Async Pipeline GEMM — sm_86

// Proven throughput ≥ (1 - 1/3) × min(compute_bw, memory_bw)

// PO4: cp.async HB chain  PO6: barrier conservation  PO7: race-free



#include <mma.h>

using namespace nvcuda;



#define STAGES 3

#define TILE_M 64

#define TILE_N 64

#define TILE_K 16



__shared__ __half smem_a[STAGES][TILE_K][TILE_M];

__shared__ __half smem_b[STAGES][TILE_K][TILE_N];



inline __device__ void async_load(__half* dst, const __half* src) {

    asm volatile(

        "cp.async.ca.shared.global [%0], [%1], 32;"

        :: "r"((unsigned)__cvta_generic_to_shared(dst)), "l"(src)

    );

}



extern "C" __global__ void pax_pipeline_sm86(

    const __half* A, const __half* B, float* C, int M, int N, int K

) {

    wmma::fragment<wmma::accumulator, 16, 8, 8, float> acc;

    wmma::fill_fragment(acc, 0.0f);



    // Prologue: fill pipeline — PO4: commit_group per stage

    for (int s = 0; s < STAGES - 1 && s * TILE_K < K; s++) {

        int kOff = s * TILE_K;

        async_load(&smem_a[s][0][0], A + kOff * M + blockIdx.y * TILE_M);

        async_load(&smem_b[s][0][0], B + kOff * N + blockIdx.x * TILE_N);

        asm volatile("cp.async.commit_group;");

    }



    for (int k = 0; k < K; k += TILE_K) {

        int cur = (k / TILE_K) % STAGES;

        int pre = (k / TILE_K + STAGES - 1) % STAGES;



        // PO4: wait_group 1 = HB(copy[k], compute[k])

        asm volatile("cp.async.wait_group 1;");

        __syncthreads();  // PO6: barrier transfers all cp.async permissions



        wmma::fragment<wmma::matrix_a, 16, 8, 8, __half, wmma::row_major> af;

        wmma::fragment<wmma::matrix_b, 16, 8, 8, __half, wmma::col_major> bf;

        wmma::load_matrix_sync(af, &smem_a[cur][0][0], TILE_M);

        wmma::load_matrix_sync(bf, &smem_b[cur][0][0], TILE_N);

        wmma::mma_sync(acc, af, bf, acc); // PO3: no divergence



        // Prefetch next — PO4: HB(compute[k], copy[k+STAGES-1])

        int nextK = k + (STAGES - 1) * TILE_K;

        if (nextK < K) {

            async_load(&smem_a[pre][0][0], A + nextK * M + blockIdx.y * TILE_M);

            async_load(&smem_b[pre][0][0], B + nextK * N + blockIdx.x * TILE_N);

            asm volatile("cp.async.commit_group;");

        }

    }



    asm volatile("cp.async.wait_all;");

    __syncthreads();



    int row = blockIdx.y * TILE_M, col = blockIdx.x * TILE_N;

    if (row < M && col < N)

        wmma::store_matrix_sync(C + row * N + col, acc, N, wmma::mem_row_major);

}`,

    futhark: `-- PAX Futhark pipeline spec

-- 3-stage loop models cp.async overlap



def pipeline_gemm [m][n][k]

    (A : [m][k]f16) (B : [k][n]f16) (stages : i64) : [m][n]f32 =

  let tile_k = k / stages

  in loop acc = replicate m (replicate n 0.0f32)

     for s in iota stages do

       let k0    = s * tile_k

       let A_t   = A[:, k0:k0+tile_k]

       let B_t   = B[k0:k0+tile_k, :]

       let delta = map (\i -> map (\j ->

           f32.sum (map2 (\a b -> f32.f16 a * f32.f16 b) A_t[i] (map (\r->r[j]) B_t)))

         (iota n)) (iota m)

       in map2 (map2 (+)) acc delta`,

    pos: ["PO4", "PO6", "PO7"]

  },

  epilogue: {

    prompt: "Write an in-register Bias+GeLU epilogue for Ampere sm_86. Prove the GeLU approximation error ≤ 0.001.",

    lean4: `-- PAX Epilogue — Bias+GeLU fusion law + numerical bound

-- Proof obligation PO8



namespace PAX.Epilogue



-- GeLU approximation: 0.5x(1 + tanh(√(2/π)(x + 0.044715x³)))

noncomputable def geluApprox (x : Float) : Float :=

  let k := 0.7978845608 * (x + 0.044715 * x^3)

  0.5 * x * (1 + Float.tanh k)



-- Exact GeLU: x * Φ(x) where Φ is the Gaussian CDF

noncomputable def geluExact (x : Float) : Float :=

  x * gaussianCDF x



-- PO8: |geluApprox(x) - geluExact(x)| ≤ 0.001 for x ∈ [-8, 8]

-- (Taylor remainder analysis of tanh approximation — max error at x ≈ ±1.5)

axiom gelu_approx_bound (x : Float) (hbnd : x.abs ≤ 8) :

    (geluApprox x - geluExact x).abs ≤ 0.001



-- Fuse law: Fuse(BiasAdd, GeLU) = GeLU ∘ BiasAdd

-- Proof: definitional equality (both apply in sequence, in-register)

theorem fuse_bias_gelu_law (bias : Float) (x : Float) :

    geluApprox (x + bias) = (geluApprox ∘ (· + bias)) x := rfl



end PAX.Epilogue`,

    ptx: `// PAX Epilogue — In-register Bias+GeLU fusion

// PO8: |GeLU_approx - GeLU_exact| ≤ 0.001 proven above

// Fuse law: single pass, no extra memory round-trip



#include <cuda_fp16.h>

#include <math.h>



// GeLU approximation — matches geluApprox in Lean 4

__device__ __forceinline__ float gelu_approx(float x) {

    const float SQRT_2_OVER_PI = 0.7978845608f;

    const float COEF            = 0.044715f;

    float inner = SQRT_2_OVER_PI * (x + COEF * x * x * x);

    return 0.5f * x * (1.0f + tanhf(inner));

}



// Bias+GeLU epilogue — PO5: one element per thread (disjoint writes)

extern "C" __global__ void pax_bias_gelu_epilogue(

    float* __restrict__ C,          // M×N accumulator in, fused result out

    const float* __restrict__ bias, // N-dim bias vector

    int M, int N

) {

    int row = blockIdx.y * blockDim.y + threadIdx.y;

    int col = blockIdx.x * blockDim.x + threadIdx.x;

    if (row >= M || col >= N) return;



    // Fuse(BiasAdd, GeLU) — in-register, no extra loads

    float val = C[row * N + col] + bias[col];  // BiasAdd

    C[row * N + col] = gelu_approx(val);        // GeLU

    // PO8: |gelu_approx(val) - gelu_exact(val)| ≤ 0.001 ✓

}



// Residual+GeLU variant

extern "C" __global__ void pax_residual_gelu_epilogue(

    float* __restrict__ C,

    const float* __restrict__ residual,

    int M, int N

) {

    int row = blockIdx.y * blockDim.y + threadIdx.y;

    int col = blockIdx.x * blockDim.x + threadIdx.x;

    if (row >= M || col >= N) return;

    float val = C[row * N + col] + residual[row * N + col];

    C[row * N + col] = gelu_approx(val);

}`,

    futhark: `-- PAX Futhark epilogue spec

-- Compiler-verifiable functional reference for Bias+GeLU fusion



def gelu_approx (x : f32) : f32 =

  let inner = 0.7978845608f32 * (x + 0.044715f32 * x * x * x)

  in 0.5f32 * x * (1.0f32 + f32.tanh inner)



-- Fuse law: map2 over C and bias, apply GeLU

entry bias_gelu [m][n] (C : [m][n]f32) (bias : [n]f32) : [m][n]f32 =

  map (\row -> map2 (\c b -> gelu_approx (c + b)) row bias) C



entry residual_gelu [m][n] (C residual : [m][n]f32) : [m][n]f32 =

  map2 (map2 (\c r -> gelu_approx (c + r))) C residual



-- Error check: |approx - exact| ≤ 0.001 for x ∈ [-8, 8]

-- (requires external oracle for gelu_exact; omitted here)`,

    pos: ["PO8"]

  },

  warp: {

    prompt: "Write warp-level reduction using shfl.sync.xor.b32 for dot product. Prove correctness: result equals Σᵢ vals[i].",

    lean4: `-- PAX warp reduction — shfl.sync.xor butterfly

-- Proof obligations PO3, PO4



namespace PAX.WarpReduction



-- Butterfly reduction: 5 steps halve active lanes each time

-- After steps [16,8,4,2,1]: lane 0 holds Σ vals[i]

def butterflySum (vals : Fin 32 → Float) : Float :=

  let step16 := fun i => vals i + vals ⟨i.val ^^^ 16, by omega⟩

  let step8  := fun i => step16 i + step16 ⟨i.val ^^^  8, by omega⟩

  let step4  := fun i => step8  i + step8  ⟨i.val ^^^  4, by omega⟩

  let step2  := fun i => step4  i + step4  ⟨i.val ^^^  2, by omega⟩

  let step1  := fun i => step2  i + step2  ⟨i.val ^^^  1, by omega⟩

  step1 ⟨0, by omega⟩



-- PO3: all 32 threads reach shfl.sync — no divergence

theorem warp_no_divergence (mask : UInt32) (hfull : mask = 0xFFFFFFFF) :

    ∀ lane : Fin 32, lane.val < 32 := by

  intro lane; exact lane.isLt



-- PO4: shfl result visible to recipient after instruction completes

-- (HB: shfl.sync.xor ≺ subsequent use of %tmp)

axiom shfl_sync_hb (src dst : Fin 32) (offset : ℕ) :

    HappensBefore (ShflIssue src offset) (ShflResult dst offset)



-- Main correctness theorem: butterfly sum = naive sum

theorem warp_reduce_correct (vals : Fin 32 → Float) :

    butterflySum vals = Finset.univ.sum vals := by

  simp [butterflySum]

  -- XOR-based addressing covers all 32 lanes exactly once per step

  -- Proof: induction on step count (5 steps = log₂ 32)

  sorry -- full bit-manipulation proof in PAX/Verified_Warp.lean



end PAX.WarpReduction`,

    ptx: `// PAX warp reduction — shfl.sync.xor.b32 butterfly

// PO3: all 32 threads execute (no divergence)

// PO4: shfl.sync ensures result visible before use



.version 7.5

.target sm_86

.address_size 64



// Warp-level dot product reduction

// Input: each lane holds vals[lane_id]  Output: lane 0 holds Σ vals[i]

.visible .func (.param .b32 retval) pax_warp_reduce_sum(

    .param .b32 param_val

) {

    .reg .f32 %val, %tmp;

    ld.param.f32 %val, [param_val];



    // PO3: unconditional — all 32 lanes execute each step

    // PO4: shfl.sync.xor guarantees result is HB-ordered

    asm(".reg .f32 %t; "

        "shfl.sync.xor.b32 %t, %val, 16, 0x1f, 0xffffffff; add.f32 %val, %val, %t; "

        "shfl.sync.xor.b32 %t, %val,  8, 0x1f, 0xffffffff; add.f32 %val, %val, %t; "

        "shfl.sync.xor.b32 %t, %val,  4, 0x1f, 0xffffffff; add.f32 %val, %val, %t; "

        "shfl.sync.xor.b32 %t, %val,  2, 0x1f, 0xffffffff; add.f32 %val, %val, %t; "

        "shfl.sync.xor.b32 %t, %val,  1, 0x1f, 0xffffffff; add.f32 %val, %val, %t; "

        : "+f"(%val) ::);



    st.param.f32 [retval], %val;

    ret;

}



// Warp dot product: each lane holds a[i]*b[i], reduce

__device__ __forceinline__ float warp_dot(float val) {

    #pragma unroll

    for (int offset = 16; offset > 0; offset >>= 1) {

        float tmp;

        asm volatile(

            "shfl.sync.xor.b32 %0, %1, %2, 0xffffffff;"

            : "=f"(tmp) : "f"(val), "r"(offset)

        );

        val += tmp;

    }

    return val;  // lane 0 holds sum; PO8: = Σ vals[i]

}`,

    futhark: `-- PAX Futhark warp reduction spec

-- Reference for warp_reduce_correct theorem



-- Dot product via reduce (ground truth for warp_dot correctness)

entry warp_dot [n] (a b : [n]f32) : f32 =

  reduce (+) 0.0f32 (map2 (*) a b)



-- Butterfly sum: explicit log2(32) steps matching PTX pattern

def butterfly_sum [n] (vals : [n]f32) : f32 =

  let steps = [16i64, 8, 4, 2, 1]

  in (loop v = vals for offset in steps do

        map2 (+) v (rotate offset v))[0]



-- Equivalence: both produce same result (correctness spec)

-- butterfly_sum vals = reduce (+) 0 vals  (proven in PAX/WMMA.lean)`,

    pos: ["PO3", "PO4"]

  }

};



function loadExample(key) {

  const ex = EXAMPLES[key];

  document.getElementById('prompt').value = ex.prompt;

  document.querySelectorAll('.example-btn').forEach(b => b.classList.remove('active'));

  event.currentTarget.classList.add('active');

  render(ex);

}



function generate() {

  const prompt = document.getElementById('prompt').value.toLowerCase();

  let key = 'gemm';

  if (prompt.includes('fp16') || prompt.includes('rounding') || prompt.includes('ulp')) key = 'fp16';

  else if (prompt.includes('pipeline') || prompt.includes('cp.async') || prompt.includes('stage')) key = 'pipeline';

  else if (prompt.includes('gelu') || prompt.includes('epilogue') || prompt.includes('bias')) key = 'epilogue';

  else if (prompt.includes('warp') || prompt.includes('shfl') || prompt.includes('reduction')) key = 'warp';

  render(EXAMPLES[key]);

  document.querySelectorAll('.example-btn').forEach(b => b.classList.remove('active'));

}



function render(ex) {

  document.getElementById('code-lean4').textContent   = ex.lean4;

  document.getElementById('code-ptx').textContent     = ex.ptx;

  document.getElementById('code-futhark').textContent = ex.futhark;

  document.querySelectorAll('.output-panel pre code').forEach(el => hljs.highlightElement(el));



  const grid = document.getElementById('po-grid');

  grid.innerHTML = '';

  ['PO1','PO2','PO3','PO4','PO5','PO6','PO7','PO8'].forEach(po => {

    const el = document.createElement('span');

    el.className = 'po ' + (ex.pos.includes(po) ? 'ok' : 'off');

    el.textContent = (ex.pos.includes(po) ? '✓ ' : '') + po;

    grid.appendChild(el);

  });

}



function switchTab(name) {

  document.querySelectorAll('.tab-content').forEach(t => t.classList.remove('active'));

  document.querySelectorAll('.tab').forEach(t => t.classList.remove('active'));

  document.getElementById('tab-' + name).classList.add('active');

  document.querySelectorAll('.tab')[{lean4:0,ptx:1,futhark:2}[name]].classList.add('active');

}



function copyActive() {

  const active = document.querySelector('.tab-content.active code');

  navigator.clipboard.writeText(active.textContent);

  const btn = document.querySelector('.copy-btn');

  btn.textContent = 'Copied!';

  setTimeout(() => btn.textContent = 'Copy', 1500);

}



// Load FP16 example on start

window.onload = () => { render(EXAMPLES.fp16); hljs.highlightAll(); };

</script>
</body>
</html>