-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathviz.py
More file actions
1443 lines (1210 loc) · 55.6 KB
/
viz.py
File metadata and controls
1443 lines (1210 loc) · 55.6 KB
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
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
# MIT License
#
# Copyright (c) 2026 Meta Platforms, Inc. and affiliates.
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
"""Visualization Examples Based on CuTe C++ Documentation.
This file demonstrates the visualization capabilities of the layouts library,
following examples from NVIDIA's CuTe (CUDA Templates) documentation.
Run this script after installing the package:
pip install -e ".[viz]"
python3 examples/viz.py
Output will be saved to ./examples_output/ directory.
Reference: https://github.com/NVIDIA/cutlass/blob/main/media/docs/cute/
"""
from pathlib import Path
import sys
# Prefer the local repo sources when running this script from a checkout.
# An installed `tensor-layouts` distribution is still required for package metadata.
REPO_ROOT = Path(__file__).resolve().parents[1]
SRC_DIR = REPO_ROOT / "src"
if str(SRC_DIR) not in sys.path:
sys.path.insert(0, str(SRC_DIR))
from tensor_layouts import *
from tensor_layouts.atoms_nv import *
from tensor_layouts.atoms_amd import *
from tensor_layouts.viz import *
def setup_output_dir(name: str = "examples_output") -> Path:
"""Create output directory for examples."""
output = Path(name)
output.mkdir(parents=True, exist_ok=True)
return output
# =============================================================================
# Section 1: Output Formats (SVG, PNG, PDF)
# =============================================================================
def example_output_formats(output: Path):
"""Demonstrate SVG, PNG, and PDF output formats.
The visualization library supports three output formats:
- SVG: Vector format, ideal for documentation and web
- PNG: Raster format, good for presentations and embedding
- PDF: Print-ready format, ideal for papers and reports
The format is determined by the file extension.
Coloring is controlled by color_layout, which is itself a Layout:
- color_layout=None: color by cell value (default)
- color_layout=Layout((r,c), (1, 0)): color by logical row
- color_layout=Layout((r,c), (0, 1)): color by logical column
- color_layout=Layout(1, 0): uniform color (no variation)
For ordinary 2D layouts, displayed cell (row, col) is colored by
evaluating color_layout at that same logical coordinate.
"""
print("\n" + "=" * 60)
print("1. Output Formats (SVG, PNG, PDF) and Coloring")
print("=" * 60)
# Create a simple layout to demonstrate all formats
layout = Layout((4, 8), (8, 1))
# SVG output - vector format (default, best for most uses)
draw_layout(layout, output / "format_example.svg", title="(4,8):(8,1)")
print(f"✓ SVG: format_example.svg (vector, scalable)")
# PNG output - raster format with configurable DPI
draw_layout(layout, output / "format_example.png", title="(4,8):(8,1)", dpi=150)
print(f"✓ PNG: format_example.png (raster, 150 dpi)")
# PDF output - print-ready format
draw_layout(layout, output / "format_example.pdf", title="(4,8):(8,1)")
print(f"✓ PDF: format_example.pdf (print-ready)")
# Demonstrate color_layout options
layout_8x8 = Layout((8, 8), (8, 1))
# Color by value (default) - same value = same color
draw_layout(layout_8x8, output / "color_by_value.svg", title="color_layout=None (by value)")
print(f"✓ Color by value: color_by_value.svg")
# Color by column - darker across columns (cute-viz style)
draw_layout(
layout_8x8,
output / "color_by_col.svg",
title="color_layout=(8,8):(0,1) (by column)",
color_layout=Layout((8, 8), (0, 1)),
)
print(f"✓ Color by column: color_by_col.svg")
# Color by row - darker down rows
draw_layout(
layout_8x8,
output / "color_by_row.svg",
title="color_layout=(8,8):(1,0) (by row)",
color_layout=Layout((8, 8), (1, 0)),
)
print(f"✓ Color by row: color_by_row.svg")
# Uniform color - no variation
draw_layout(
layout_8x8,
output / "color_uniform.svg",
title="color_layout=1:0 (uniform)",
color_layout=Layout(1, 0),
)
print(f"✓ Uniform color: color_uniform.svg")
# Rainbow colors with different color_layout
draw_layout(
layout_8x8,
output / "color_by_col_rainbow.svg",
title="colorize=True, by column",
colorize=True,
color_layout=Layout((8, 8), (0, 1)),
)
print(f"✓ Rainbow by column: color_by_col_rainbow.svg")
# color_by shorthand — equivalent to the manual color_layout above
draw_layout(
layout_8x8, output / "color_by_row_shorthand.svg", title='color_by="row"', color_by="row"
)
draw_layout(
layout_8x8,
output / "color_by_col_shorthand.svg",
title='color_by="column"',
color_by="column",
)
print(f"✓ color_by shorthand: color_by_row_shorthand.svg, color_by_col_shorthand.svg")
# Swizzle comparison showing row-group coloring (reveals permutation effect)
base = Layout((8, 8), (8, 1))
sw = Swizzle(3, 0, 3)
draw_swizzle(base, sw, output / "swizzle_example.svg")
draw_swizzle(base, sw, output / "swizzle_example_color.svg", colorize=True)
print(f"✓ Swizzle with row-group coloring: swizzle_example.svg, swizzle_example_color.svg")
# =============================================================================
# Section 2: 1D Layouts
# =============================================================================
def example_1d_layouts(output: Path):
"""1D contiguous and strided layouts.
CuTe Reference: Layout Algebra - 1D layouts
A 1D layout maps indices to memory locations. With stride=1, elements
are contiguous. With stride>1, elements are strided.
"""
print("\n" + "=" * 60)
print("2. 1D Layouts")
print("=" * 60)
# Contiguous 1D layout: 8 elements, stride 1
layout_1d_contiguous = Layout(8, 1)
draw_layout(layout_1d_contiguous, output / "1d_contiguous.svg", title="1D Contiguous: 8:1")
print(f"✓ 1D Contiguous: 8:1")
print(f" Maps index i → offset i (e.g., 3 → 3)")
# Strided 1D layout: 8 elements, stride 2
layout_1d_strided = Layout(8, 2)
draw_layout(layout_1d_strided, output / "1d_strided.svg", title="1D Strided: 8:2")
print(f"✓ 1D Strided: 8:2")
print(f" Maps index i → offset 2*i (e.g., 3 → 6)")
# Strided 1D layout: 4 elements, stride 4
layout_1d_stride4 = Layout(4, 4)
draw_layout(layout_1d_stride4, output / "1d_stride4.svg", title="1D Stride-4: 4:4")
print(f"✓ 1D Stride-4: 4:4")
print(f" Maps index i → offset 4*i (e.g., 2 → 8)")
# Transpose: render 1D layout as a column vector instead of a row
draw_layout(
layout_1d_contiguous,
output / "1d_contiguous_col.svg",
title="1D Column: 8:1 (transpose=True)",
transpose=True,
)
print(f"✓ 1D Column (transpose=True): renders as N×1 instead of 1×N")
# =============================================================================
# Section 3: 2D Layouts
# =============================================================================
def example_2d_layouts(output: Path):
"""2D row-major and column-major layouts.
CuTe Reference: Layout Algebra - 2D layouts
Row-major: consecutive elements in same row are contiguous
Column-major: consecutive elements in same column are contiguous
"""
print("\n" + "=" * 60)
print("3. 2D Layouts")
print("=" * 60)
# Row-major 4x3: shape (4 rows, 3 cols), stride (3, 1)
# Row i, Col j → offset = i*3 + j
row_major_4x3 = Layout((4, 3), (3, 1))
draw_layout(row_major_4x3, output / "2d_row_major_4x3.svg", title="Row-Major 4×3: (4,3):(3,1)")
print(f"✓ Row-Major 4×3: (4,3):(3,1)")
print(f" offset(i,j) = i*3 + j*1")
# Column-major 4x3: shape (4 rows, 3 cols), stride (1, 4)
# Row i, Col j → offset = i*1 + j*4
col_major_4x3 = Layout((4, 3), (1, 4))
draw_layout(col_major_4x3, output / "2d_col_major_4x3.svg", title="Col-Major 4×3: (4,3):(1,4)")
print(f"✓ Col-Major 4×3: (4,3):(1,4)")
print(f" offset(i,j) = i*1 + j*4")
# 8x8 Row-major: shape (8 rows, 8 cols), stride (8, 1)
# This is the common layout for matrix operations
row_major_8x8 = Layout((8, 8), (8, 1))
draw_layout(row_major_8x8, output / "2d_row_major_8x8.svg", title="Row-Major 8×8: (8,8):(8,1)")
draw_layout(
row_major_8x8,
output / "2d_row_major_8x8_color.svg",
title="Row-Major 8×8: (8,8):(8,1)",
colorize=True,
)
print(f"✓ Row-Major 8×8: (8,8):(8,1) [grayscale and colorized]")
print(f" offset(i,j) = i*8 + j*1")
# 8x8 Column-major: shape (8 rows, 8 cols), stride (1, 8)
col_major_8x8 = Layout((8, 8), (1, 8))
draw_layout(col_major_8x8, output / "2d_col_major_8x8.svg", title="Col-Major 8×8: (8,8):(1,8)")
draw_layout(
col_major_8x8,
output / "2d_col_major_8x8_color.svg",
title="Col-Major 8×8: (8,8):(1,8)",
colorize=True,
)
print(f"✓ Col-Major 8×8: (8,8):(1,8) [grayscale and colorized]")
print(f" offset(i,j) = i*1 + j*8")
# =============================================================================
# Section 4: Hierarchical Layouts
# =============================================================================
def example_hierarchical_layouts(output: Path):
"""Hierarchical (nested) layouts - flattened and nested views.
CuTe Reference: Layout Algebra - Hierarchical Layouts
Hierarchical layouts have nested shapes/strides. They're used to represent
tiled access patterns where the outer level selects tiles and inner level
selects within tiles.
"""
print("\n" + "=" * 60)
print("4. Hierarchical Layouts")
print("=" * 60)
# =========================================================================
# Example 1: 2×2 tiles in a 3×4 grid (cute-viz example)
# Shape: ((2, 3), (2, 4)) = ((inner_row, outer_row), (inner_col, outer_col))
# Total: 6 rows × 8 columns = 48 elements
# =========================================================================
print("\n --- 2×2 Tiles in 3×4 Grid ---")
hier_2x2_3x4 = Layout(((2, 3), (2, 4)), ((1, 6), (2, 12)))
# Show the mapping for first tile
print(f" Layout: ((2,3), (2,4)) : ((1,6), (2,12))")
print(f" Shape: 2×2 tiles arranged in 3×4 grid = 6×8 total")
print(f" Mapping examples:")
for i in range(2):
for j in range(2):
idx = hier_2x2_3x4((i, 0), (j, 0))
print(f" (({i},0),({j},0)) → {idx}")
# Flat view (default)
draw_layout(
hier_2x2_3x4,
output / "hier_2x2_3x4_flat.svg",
title=f"Flat: {hier_2x2_3x4}",
flatten_hierarchical=True,
)
print(f"✓ Flat view: hier_2x2_3x4_flat.svg")
# Nested pedagogical view:
# - each cell shows row=... (nested row coordinate)
# - each cell shows col=... (nested column coordinate)
# - each cell shows offset=... (resulting offset)
# - axes stay simple (R0, R1, ... / C0, C1, ...)
draw_layout(
hier_2x2_3x4,
output / "hier_2x2_3x4_nested.svg",
title=f"Nested: {hier_2x2_3x4}",
flatten_hierarchical=False,
label_hierarchy_levels=True,
)
print(f"✓ Nested view: hier_2x2_3x4_nested.svg")
# =========================================================================
# Example 2: 2×2 tiles in 2×2 grid (4×4 total) — the project logo layout
# =========================================================================
print("\n --- 2×2 Tiles in 2×2 Grid (Logo Layout) ---")
logo_layout = Layout(((2, 2), (2, 2)), ((1, 4), (2, 8)))
# This is the layout shown in the project logo (docs/images/logo.svg).
# It's a 4×4 Morton (Z-order) layout: blocked_product of 2×2 Z-tiles.
# Inner 2×2 tiles use stride (1, 2) (column-major within each tile),
# and the 2×2 outer grid uses stride (4, 8) to place tiles.
#
# Tile (0,0): 0 2 Tile (0,1): 8 10
# 1 3 9 11
#
# Tile (1,0): 4 6 Tile (1,1): 12 14
# 5 7 13 15
draw_layout(
logo_layout,
output / "hier_2x2_tiles_flat.svg",
title=f"Flat: {logo_layout}",
flatten_hierarchical=True,
)
draw_layout(
logo_layout,
output / "hier_2x2_tiles_nested.svg",
title=f"Nested: {logo_layout}",
flatten_hierarchical=False,
label_hierarchy_levels=True,
)
print(f"✓ Hierarchical 2×2 in 2×2 (logo layout): {logo_layout}")
print(
f" Nested view is pedagogical: row=... / col=... show nested coordinates, offset=... shows mapping"
)
# =========================================================================
# Example 3: 3-level asymmetric hierarchy with per-level axis labels
# =========================================================================
print("\n --- 3-Level Asymmetric Hierarchy ---")
hier_3level = Layout(
((2, 3, 2), (3, 2, 2)),
((1, 2, 6), (12, 36, 72)),
)
draw_layout(
hier_3level,
output / "hier_3level_asymmetric_flat.svg",
title=f"Flat: {hier_3level}",
flatten_hierarchical=True,
)
draw_layout(
hier_3level,
output / "hier_3level_asymmetric_nested.svg",
title=f"Nested: {hier_3level}",
flatten_hierarchical=False,
label_hierarchy_levels=True,
)
print("✓ 3-level asymmetric hierarchy")
print(" Row shape = (2,3,2), Col shape = (3,2,2)")
print(" Nested view labels hierarchy levels at tile/block granularity")
print(" and uses matching colors for boundary lines and level labels.")
print(" Axis labels use row[k]=... / col[k]=... to match cell notation.")
print(" Output: hier_3level_asymmetric_nested.svg")
# =========================================================================
# Example 4: 4-level asymmetric hierarchy with larger finest-level tiles
# =========================================================================
print("\n --- 4-Level Asymmetric Hierarchy (Small Cells) ---")
hier_4level = Layout(
((3, 2, 2, 2), (4, 2, 2, 2)),
((1, 3, 6, 12), (24, 96, 192, 384)),
)
draw_layout(
hier_4level,
output / "hier_4level_asymmetric_flat.svg",
title=f"Flat: {hier_4level}",
flatten_hierarchical=True,
)
for ext in ("svg", "pdf", "png"):
draw_layout(
hier_4level,
output / f"hier_4level_asymmetric_nested.{ext}",
title=f"Nested: {hier_4level}",
flatten_hierarchical=False,
label_hierarchy_levels=True,
dpi=300 if ext == "png" else 150,
)
print("✓ 4-level asymmetric hierarchy")
print(" Row shape = (3,2,2,2), Col shape = (4,2,2,2)")
print(" This example makes cells much smaller, so it is useful for checking")
print(" whether hierarchy-level labels and colored boundaries remain readable.")
print(" Output: hier_4level_asymmetric_nested.{svg,pdf,png}")
# Flatten the hierarchical layout (algebra operation)
flat_layout = flatten(logo_layout)
draw_layout(flat_layout, output / "hier_flattened.svg", title=f"flatten(): {flat_layout}")
print(f"✓ Flattened (algebra): {flat_layout}")
# Coalesce to merge contiguous dimensions
coal_layout = coalesce(logo_layout)
draw_layout(coal_layout, output / "hier_coalesced.svg", title=f"coalesce(): {coal_layout}")
print(f"✓ Coalesced: {coal_layout}")
# =========================================================================
# Cell Label Modes
#
# cell_labels controls what text appears inside each cell:
# True — default (offset in flat mode, row/col/offset in nested)
# "offset" — just the offset number (useful with hierarchy boundaries)
# False — no text (colored grid + boundaries only)
# list/tuple — custom labels indexed by offset value
# =========================================================================
print("\n --- Cell Label Modes ---")
demo = Layout(((2, 2), (2, 2)), ((1, 4), (2, 8)))
# Flat mode: default labels (offset numbers) vs no labels
draw_layout(
demo,
output / "cell_labels_flat_default.svg",
title="Flat: cell_labels=True (default)",
colorize=True,
flatten_hierarchical=True,
)
draw_layout(
demo,
output / "cell_labels_flat_none.svg",
title="Flat: cell_labels=False",
colorize=True,
flatten_hierarchical=True,
cell_labels=False,
)
print(f"✓ Flat mode: default vs cell_labels=False")
# Flat mode: custom labels (alphabet)
import string
labels = list(string.ascii_uppercase[: size(demo)])
draw_layout(
demo,
output / "cell_labels_flat_custom.svg",
title="Flat: cell_labels=['A','B',...]",
colorize=True,
flatten_hierarchical=True,
cell_labels=labels,
)
print(f"✓ Flat mode: cell_labels={labels}")
# Hierarchical mode: full detail (default)
draw_layout(
demo,
output / "cell_labels_hier_default.svg",
title="Nested: cell_labels=True (default)",
colorize=True,
flatten_hierarchical=False,
label_hierarchy_levels=True,
)
# Hierarchical mode: offset only — keeps boundaries + axis labels,
# replaces verbose row/col/offset with a single number
draw_layout(
demo,
output / "cell_labels_hier_offset.svg",
title='Nested: cell_labels="offset"',
colorize=True,
flatten_hierarchical=False,
label_hierarchy_levels=True,
cell_labels="offset",
)
# Hierarchical mode: no text at all
draw_layout(
demo,
output / "cell_labels_hier_none.svg",
title="Nested: cell_labels=False",
colorize=True,
flatten_hierarchical=False,
label_hierarchy_levels=True,
cell_labels=False,
)
# Hierarchical mode: custom labels
draw_layout(
demo,
output / "cell_labels_hier_custom.svg",
title="Nested: cell_labels=['A','B',...]",
colorize=True,
flatten_hierarchical=False,
label_hierarchy_levels=True,
cell_labels=labels,
)
print(f'✓ Nested mode: default / "offset" / False / custom labels')
# =========================================================================
# Examples from "Cute Layout Representation and Algebra" by Cris Cecka
# =========================================================================
print("\n --- From Cecka, 'CuTe Layout Representation and Algebra' ---")
# (4,8):(1,4) — column-major 4x8
cecka_1 = Layout((4, 8), (1, 4))
draw_layout(cecka_1, output / "cecka_4x8_col.svg", title="(4,8):(1,4)")
print(f"✓ (4,8):(1,4) — column-major 4×8")
# (4,8):(8,1) — row-major 4x8
cecka_2 = Layout((4, 8), (8, 1))
draw_layout(cecka_2, output / "cecka_4x8_row.svg", title="(4,8):(8,1)")
print(f"✓ (4,8):(8,1) — row-major 4×8")
# (4,8):(1,5) — non-injective layout (stride 5 with shape 8 wraps)
cecka_3 = Layout((4, 8), (1, 5))
draw_layout(cecka_3, output / "cecka_4x8_s1_s5.svg", title="(4,8):(1,5)")
print(f"✓ (4,8):(1,5) — non-injective (surjective) layout")
# (4,(4,2)):(4,(1,16)) — hierarchical column dimension
# Nested rendering explicitly shows how the hierarchical column coordinate
# maps to the final offset for each displayed cell.
cecka_4 = Layout((4, (4, 2)), (4, (1, 16)))
draw_layout(
cecka_4,
output / "cecka_hier_col.svg",
title="(4,(4,2)):(4,(1,16))",
flatten_hierarchical=False,
label_hierarchy_levels=True,
)
draw_layout(
cecka_4,
output / "cecka_hier_col_flat.svg",
title="(4,(4,2)):(4,(1,16))",
flatten_hierarchical=True,
)
print(f"✓ (4,(4,2)):(4,(1,16)) — hierarchical column")
# ((2,2),(4,2)):((1,8),(2,16)) — hierarchical in both modes
# This is a good example where explicit row=... / col=... labels help explain
# the two-level row and column structure.
cecka_5 = Layout(((2, 2), (4, 2)), ((1, 8), (2, 16)))
draw_layout(
cecka_5,
output / "cecka_hier_both.svg",
title="((2,2),(4,2)):((1,8),(2,16))",
flatten_hierarchical=False,
label_hierarchy_levels=True,
)
draw_layout(
cecka_5,
output / "cecka_hier_both_flat.svg",
title="((2,2),(4,2)):((1,8),(2,16))",
flatten_hierarchical=True,
)
print(f"✓ ((2,2),(4,2)):((1,8),(2,16)) — hierarchical both modes")
# ((2,2),(2,4)):((0,2),(0,4)) — zero-stride (broadcast) layout
# The pedagogical nested view is especially useful here because repeated
# offsets are easier to interpret when the source coordinates are explicit.
cecka_6 = Layout(((2, 2), (2, 4)), ((0, 2), (0, 4)))
draw_layout(
cecka_6,
output / "cecka_broadcast.svg",
title="((2,2),(2,4)):((0,2),(0,4))",
flatten_hierarchical=False,
label_hierarchy_levels=True,
)
draw_layout(
cecka_6,
output / "cecka_broadcast_flat.svg",
title="((2,2),(2,4)):((0,2),(0,4))",
flatten_hierarchical=True,
)
print(f"✓ ((2,2),(2,4)):((0,2),(0,4)) — broadcast (zero-stride) layout")
# Morton/Z-order layout using blocked_product (CuTe pattern)
# morton1 = 2x2 Z-order tile
# morton2 = blocked_product(morton1, morton1) -> 4x4
# morton3 = blocked_product(morton1, morton2) -> 8x8
morton1 = Layout((2, 2), (1, 2))
morton2 = blocked_product(morton1, morton1)
morton3 = blocked_product(morton1, morton2)
draw_layout(morton1, output / "hier_morton_2x2.svg", title=f"Morton 2×2: {morton1}")
draw_layout(morton2, output / "hier_morton_4x4.svg", title=f"Morton 4×4: {morton2}")
draw_layout(morton3, output / "hier_morton_8x8.svg", title=f"Morton 8×8: {morton3}")
draw_layout(
morton3, output / "hier_morton_8x8_color.svg", title=f"Morton 8×8: {morton3}", colorize=True
)
print(f"✓ Morton 2×2: {morton1}")
print(f"✓ Morton 4×4: {morton2}")
print(f"✓ Morton 8×8: {morton3}")
# Show nested mode access
# Mode 0 is the row dimension with shape (2, 2)
mode0 = mode(logo_layout, 0)
draw_layout(mode0, output / "hier_mode0.svg", title=f"Mode 0 (rows): {mode0}")
print(f"✓ Mode 0 (rows): {mode0}")
# Mode 1 is the column dimension with shape (2, 2)
mode1 = mode(logo_layout, 1)
draw_layout(mode1, output / "hier_mode1.svg", title=f"Mode 1 (cols): {mode1}")
print(f"✓ Mode 1 (cols): {mode1}")
# =============================================================================
# Section 5: Swizzled Layouts
# =============================================================================
def example_swizzled_layouts(output: Path):
"""Swizzled layouts for GPU shared memory bank conflict avoidance.
CuTe Reference: Swizzle Functions
Swizzle applies an XOR operation to indices, redistributing elements
across memory banks to avoid bank conflicts in GPU shared memory.
Swizzle(B, M, S):
- B (bits): number of bits to XOR
- M (base): starting bit position
- S (shift): offset for XOR source bits
Effect: bits at positions [M, M+B) are XORed with bits at [M+S, M+S+B)
Common patterns:
- Swizzle(B, 0, 3): Classic LDMATRIX patterns for 8×8 fp16 tiles
- Swizzle(B, 4, 3): GMMA/TMA patterns (SW32, SW64, SW128)
Using colorize=True makes the cell movement much clearer.
"""
print("\n" + "=" * 60)
print("5. Swizzled Layouts (colorized for clarity)")
print("=" * 60)
# =========================================================================
# Part A: Classic LDMATRIX swizzles - Swizzle(B, 0, 3) family
# Used for 8×8 fp16 tiles with LDMATRIX instruction
# =========================================================================
print("\n --- Classic LDMATRIX Swizzles: Swizzle(B, 0, 3) ---")
base_8x8 = Layout((8, 8), (8, 1))
# Swizzle(3, 0, 3) - 8-bank redistribution (most common)
sw_303 = Swizzle(3, 0, 3)
draw_swizzle(base_8x8, sw_303, output / "swizzle_8x8_303.svg", colorize=True)
print(f"✓ Swizzle(3,0,3) on 8×8: XOR bits [0,3) with [3,6) → 8-bank")
# Swizzle(2, 0, 3) - 4-bank redistribution
sw_203 = Swizzle(2, 0, 3)
draw_swizzle(base_8x8, sw_203, output / "swizzle_8x8_203.svg", colorize=True)
print(f"✓ Swizzle(2,0,3) on 8×8: XOR bits [0,2) with [3,5) → 4-bank")
# Swizzle(1, 0, 3) - 2-bank redistribution
sw_103 = Swizzle(1, 0, 3)
draw_swizzle(base_8x8, sw_103, output / "swizzle_8x8_103.svg", colorize=True)
print(f"✓ Swizzle(1,0,3) on 8×8: XOR bit 0 with bit 3 → 2-bank")
# Column-major variant
base_8x8_col = Layout((8, 8), (1, 8))
draw_swizzle(base_8x8_col, sw_303, output / "swizzle_8x8_col_303.svg", colorize=True)
print(f"✓ Swizzle(3,0,3) on 8×8 col-major")
# 16x8 variant (common for tensor core)
base_16x8 = Layout((16, 8), (8, 1))
draw_swizzle(base_16x8, sw_303, output / "swizzle_16x8_303.svg", colorize=True)
print(f"✓ Swizzle(3,0,3) on 16×8 row-major")
# =========================================================================
# Part B: GMMA/TMA swizzles - Swizzle(B, 4, 3) family (SM90+)
# Used for Tensor Memory Accelerator and GMMA operations
# SW32 = Swizzle(1,4,3), SW64 = Swizzle(2,4,3), SW128 = Swizzle(3,4,3)
#
# CuTe canonical shapes (from mma_traits_sm90_gmma.hpp):
# The base layout is defined in BITS as (N_bits, 8):(1, N_bits)
# For byte-level view, the canonical row-major shapes are:
# SW32: (8, 32):(32, 1) -- 32 bytes per row, 8 rows
# SW64: (8, 64):(64, 1) -- 64 bytes per row, 8 rows
# SW128: (8, 128):(128, 1) -- 128 bytes per row, 8 rows
# The column count must be 2^(base+bits) for the swizzle pattern
# to fully manifest as within-row element permutations.
# =========================================================================
print("\n --- GMMA/TMA Swizzles: Swizzle(B, 4, 3) ---")
# Swizzle(1, 4, 3) - SW32 (32-byte swizzle width)
# Canonical byte layout: 8 rows × 32 columns (32 bytes per row)
sw_143 = Swizzle(1, 4, 3)
base_8x32 = Layout((8, 32), (32, 1))
draw_swizzle(base_8x32, sw_143, output / "swizzle_8x32_143_SW32.svg", colorize=True)
print(f"✓ Swizzle(1,4,3) SW32 on 8×32: XOR bit 4 with bit 7")
# Swizzle(2, 4, 3) - SW64 (64-byte swizzle width)
# Canonical byte layout: 8 rows × 64 columns (64 bytes per row)
sw_243 = Swizzle(2, 4, 3)
base_8x64 = Layout((8, 64), (64, 1))
draw_swizzle(base_8x64, sw_243, output / "swizzle_8x64_243_SW64.svg", colorize=True)
print(f"✓ Swizzle(2,4,3) SW64 on 8×64: XOR bits [4,6) with [7,9)")
# Swizzle(3, 4, 3) - SW128 (128-byte swizzle width, maximum bandwidth)
# Canonical byte layout: 8 rows × 128 columns (128 bytes per row)
sw_343 = Swizzle(3, 4, 3)
base_8x128 = Layout((8, 128), (128, 1))
draw_swizzle(base_8x128, sw_343, output / "swizzle_8x128_343_SW128.svg", colorize=True)
print(f"✓ Swizzle(3,4,3) SW128 on 8×128: XOR bits [4,7) with [7,10)")
# =========================================================================
# Part C: No swizzle (identity) for comparison
# =========================================================================
print("\n --- No Swizzle (Identity) ---")
# Swizzle(0, M, S) is identity - no XOR applied
sw_043 = Swizzle(0, 4, 3)
draw_swizzle(base_8x128, sw_043, output / "swizzle_8x128_043_none.svg", colorize=True)
print(f"✓ Swizzle(0,4,3) on 8×128: Identity (no XOR)")
# =============================================================================
# Section 6: Thread-Value (TV) Layouts
# =============================================================================
def example_thread_value_layouts(output: Path):
"""Thread-Value (TV) layouts for GPU parallelism.
CuTe Reference: Thread-Value Layouts (MMA/Copy atoms)
TV layouts describe how data is distributed across threads and values
(registers per thread). Shape is (Threads, Values), showing which
thread owns which elements.
Each cell is labeled "TxVy" showing thread index x and value index y.
"""
print("\n" + "=" * 60)
print("6. Thread-Value (TV) Layouts")
print("=" * 60)
# Simple TV layout: 4 threads, 2 values each = 8 elements
# Thread 0: V0, V1; Thread 1: V0, V1; etc.
tv_4x2 = Layout((4, 2), (2, 1))
draw_tv_layout(
tv_4x2,
output / "tv_4threads_2values.svg",
title="TV: (4,2):(2,1) - 4 threads, 2 values each",
)
draw_tv_layout(
tv_4x2, output / "tv_4threads_2values_color.svg", title="TV: (4,2):(2,1)", colorize=True
)
print(f"✓ TV Layout 4×2: 4 threads, 2 values each")
print(f" Thread t owns values V0, V1 at offsets 2*t and 2*t+1")
# TV layout with interleaved threads
tv_4x2_col = Layout((4, 2), (1, 4))
draw_tv_layout(
tv_4x2_col,
output / "tv_4threads_2values_interleaved.svg",
title="TV interleaved: (4,2):(1,4)",
)
print(f"✓ TV Layout 4×2 interleaved: offsets t and t+4")
# 8x4 TV layout (smaller than full warp for clarity)
tv_8x4 = Layout((8, 4), (4, 1))
draw_tv_layout(tv_8x4, output / "tv_8x4.svg", title="TV: (8,4):(4,1) - 8 threads, 4 values")
draw_tv_layout(tv_8x4, output / "tv_8x4_color.svg", title="TV: (8,4):(4,1)", colorize=True)
print(f"✓ TV Layout 8×4: 8 threads, 4 values each")
# 8x8 TV layout (common for LDMATRIX)
tv_8x8 = Layout((8, 8), (8, 1))
draw_tv_layout(tv_8x8, output / "tv_8x8.svg", title="TV: (8,8):(8,1) - 8 threads, 8 values")
draw_tv_layout(tv_8x8, output / "tv_8x8_color.svg", title="TV: (8,8):(8,1)", colorize=True)
print(f"✓ TV Layout 8×8: 8 threads, 8 values each (LDMATRIX style)")
# Also show the regular layout view for comparison
draw_layout(
tv_8x8, output / "tv_8x8_offsets.svg", title="TV: (8,8):(8,1) - Memory offsets view"
)
print(f" (Also showing memory offset view for comparison)")
# =============================================================================
# Section 7: Copy Atom Traits (LDMATRIX, STMATRIX, TMA)
# =============================================================================
def example_copy_atoms(output: Path):
"""Copy atom TV layouts across GPU architectures.
Sources:
copy_traits_sm75.hpp — SM75 LDSM (ldmatrix)
copy_traits_sm90.hpp — SM90 STSM (stmatrix)
copy_traits_sm90_tma.hpp — SM90 TMA (Tensor Memory Accelerator)
mma_traits_sm90_gmma.hpp — SM90 GMMA shared memory layouts
Copy traits define Src and Dst layouts in *bit* coordinates.
upcast(layout, element_bits) converts to element-level TV layouts.
TMA atoms are single-threaded bulk transfers between global and shared
memory. Their interesting aspect is the shared memory swizzle patterns.
"""
print("\n" + "=" * 60)
print("7. Copy Atom Traits")
print("=" * 60)
element_bits = 16 # fp16
# =====================================================================
# SM75 LDMATRIX — ldmatrix.sync.aligned.m8n8.shared.b16
# =====================================================================
print("\n --- SM75 LDMATRIX (ldmatrix.sync.aligned) ---")
ldsm_atoms = [
SM75_U32x1_LDSM_N,
SM75_U32x4_LDSM_N,
SM75_U16x2_LDSM_T,
SM75_U16x4_LDSM_T,
SM75_U16x8_LDSM_T,
]
for atom in ldsm_atoms:
# draw_copy_atom handles upcast from bit to element coords automatically
draw_copy_atom(atom, element_bits=element_bits, filename=output / f"{atom.name}_copy.svg")
dst = upcast(atom.dst_layout_bits, element_bits)
n_thr = size(atom.thr_id)
n_val = size(mode(dst, 1))
print(f"✓ {atom.name} Dst: {dst} ({n_thr} thr × {n_val} val)")
# =====================================================================
# SM90 STMATRIX — stmatrix.sync.aligned.m8n8.shared.b16
# Inverse of SM75 LDMATRIX: STSM Src = LDSM Dst, STSM Dst = LDSM Src
# =====================================================================
print("\n --- SM90 STMATRIX (stmatrix.sync.aligned) ---")
stsm_atoms = [SM90_U32x4_STSM_N, SM90_U16x8_STSM_T]
for atom in stsm_atoms:
draw_copy_atom(atom, element_bits=element_bits, filename=output / f"{atom.name}_copy.svg")
print(f"✓ {atom.name} ({atom.ptx})")
# =====================================================================
# SM90 TMA — Tensor Memory Accelerator
# From copy_traits_sm90_tma.hpp
# TMA atoms are single-threaded (ThrID = Layout<_1>).
# The interesting aspect is the GMMA shared memory swizzle patterns
# that TMA writes into, defined in mma_traits_sm90_gmma.hpp.
# =====================================================================
print("\n --- SM90 TMA (Tensor Memory Accelerator) ---")
print(" TMA atoms: ThrID = Layout<_1> (single-threaded bulk transfer)")
print(" SM90_TMA_LOAD: global → shared (Layout<_1, NumBitsPerTMA>)")
print(" SM90_TMA_STORE: shared → global (Layout<_1, NumBitsPerTMA>)")
print(" SM90_TMA_LOAD_MULTICAST: with CTA multicast")
# TMA writes into swizzled shared memory. The canonical GMMA smem
# layouts are the interesting part to visualize.
# These come from mma_traits_sm90_gmma.hpp:
# Layout_K_SW128_Atom_Bits = Swizzle<3,4,3> ∘ (8, 1024):(1024, 1)
# For fp16: Swizzle<3,4,3> ∘ (8, 64):(64, 1) = 8 rows × 64 cols
print("\n TMA target: GMMA K-major SW128 smem layout (fp16):")
base_tma = Layout((8, 64), (64, 1))
draw_swizzle(base_tma, Swizzle(3, 4, 3), output / "SM90_TMA_GMMA_K_SW128.svg", colorize=True)
print(f"✓ SM90 TMA → GMMA K-major SW128: Swizzle(3,4,3) ∘ (8,64):(64,1)")
print("\n TMA target: GMMA M|N-major SW128 smem layout (fp16):")
base_tma_mn = Layout((64, 8), (1, 64))
draw_swizzle(
base_tma_mn, Swizzle(3, 4, 3), output / "SM90_TMA_GMMA_MN_SW128.svg", colorize=True
)
print(f"✓ SM90 TMA → GMMA M|N-major SW128: Swizzle(3,4,3) ∘ (64,8):(1,64)")
# =====================================================================
# LDMATRIX shared memory + swizzle (classic pattern)
# =====================================================================
print("\n --- LDMATRIX Shared Memory with Swizzle ---")
smem_8x8 = Layout((8, 8), (8, 1))
draw_swizzle(smem_8x8, Swizzle(3, 0, 3), output / "ldmatrix_smem_swizzle.svg", colorize=True)
print(f"✓ LDMATRIX shared memory with Swizzle(3,0,3)")
# =============================================================================
# Section 8: MMA Atom Traits
# =============================================================================
def _draw_mma_atom(atom, output: Path):
"""Draw A, B, C, and combined figures for one MMA atom."""
name = atom.name
M, N, K = atom.shape_mnk
thr = atom.thr_id
# For atoms with broadcast (stride-0), cosize < M*K. Use cosize-based
# grid dimensions so every cell maps to a thread — no '?' cells.
cs_a = cosize(atom.a_layout)
cs_b = cosize(atom.b_layout)
cs_c = cosize(atom.c_layout)
a_rows, a_cols = M, cs_a // M if cs_a % M == 0 else K
b_rows, b_cols = cs_b // N if cs_b % N == 0 else K, N
c_rows, c_cols = M, cs_c // M if cs_c % M == 0 else N
draw_tv_layout(
atom.a_layout,
output / f"{name}_A.svg",
title=f"{name} A ({a_rows}×{a_cols})",
colorize=True,
grid_shape=(a_rows, a_cols),
thr_id_layout=thr,
)
draw_tv_layout(
atom.b_layout,
output / f"{name}_B.svg",
title=f"{name} B ({b_rows}×{b_cols})",
colorize=True,
grid_shape=(b_rows, b_cols),
thr_id_layout=thr,
col_major=False,
)
draw_tv_layout(
atom.c_layout,
output / f"{name}_C.svg",
title=f"{name} C ({c_rows}×{c_cols})",
colorize=True,
grid_shape=(c_rows, c_cols),
thr_id_layout=thr,
)
draw_mma_layout(
atom.a_layout,
atom.b_layout,
atom.c_layout,
output / f"{name}_combined.svg",
tile_mnk=(a_rows, c_cols, a_cols),
main_title=name,
colorize=True,
thr_id_layout=thr,
)
n_thr = size(mode(atom.c_layout, 0))
n_val_a = size(mode(atom.a_layout, 1))
n_val_b = size(mode(atom.b_layout, 1))
n_val_c = size(mode(atom.c_layout, 1))
print(f"✓ {name}")
print(f" {atom.ptx}")
print(f" {n_thr} threads, A:{n_val_a} B:{n_val_b} C:{n_val_c} vals/thr")
def _draw_tiled_mma(atom, atom_layout, output: Path, tile_mnk=None):
"""Draw a TiledMMA: atom tiled across multiple quadpairs.
Equivalent to the C++ code:
TiledMMA mma = make_tiled_mma(atom, atom_layout, Tile<M,N,K>);
print_latex(mma);
Args:
atom: MMAAtom to tile
atom_layout: Layout describing atom arrangement in M×N space
E.g. Layout((2,2), (2,1)) for 2×2 n-major
output: Output directory
tile_mnk: Optional (M, N, K) final tile. If larger than the atom
arrangement, replicates across values.
"""
from tensor_layouts.layout_utils import tile_mma_grid
M_a, N_a, K_a = atom.shape_mnk
atom_shape = atom_layout.shape
if isinstance(atom_shape, int):
n_am, n_an = atom_shape, 1
else:
n_am, n_an = atom_shape
label = f"{atom.name}_{n_am}x{n_an}"
M, N, K = M_a * n_am, N_a * n_an, K_a
if tile_mnk is not None:
M, N, K = tile_mnk
label = f"{atom.name}_{n_am}x{n_an}_{M}x{N}x{K}"
# Compute tiled grids
c_grid, _ = tile_mma_grid(atom, atom_layout, "C", tile_mnk=tile_mnk)
a_grid, _ = tile_mma_grid(atom, atom_layout, "A", tile_mnk=tile_mnk)
b_grid, _ = tile_mma_grid(atom, atom_layout, "B", tile_mnk=tile_mnk)
draw_tiled_grid(c_grid, M, N, output / f"{label}_C.svg", title=f"{label} C ({M}×{N})")
draw_tiled_grid(a_grid, M, K, output / f"{label}_A.svg", title=f"{label} A ({M}×{K})")