Skip to content

Commit 12ff513

Browse files
authored
[CuTeDSL] Add a render function hook to allow render layout natively (#3135)
* [CuTeDSL] Add a render function hook to allow render layout natively Signed-off-by: Kaining Zhong <kainingz@nvidia.com> * nit Signed-off-by: Kaining Zhong <kainingz@nvidia.com> --------- Signed-off-by: Kaining Zhong <kainingz@nvidia.com>
1 parent d4b4b49 commit 12ff513

1 file changed

Lines changed: 60 additions & 26 deletions

File tree

python/CuTeDSL/cutlass/utils/print_latex.py

Lines changed: 60 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
# and related documentation outside the scope permitted by the EULA
1010
# is strictly prohibited.
1111

12-
from typing import Callable, Union
12+
from typing import Callable, Optional, Union
1313

1414
from ..cute import (
1515
Layout,
@@ -59,14 +59,19 @@ def tikz_color_tv(tid: int, vid: int) -> str:
5959

6060

6161
def print_latex(
62-
x: Union[Layout, ComposedLayout], *, color: Callable = tikz_color_bwx8
62+
x: Union[Layout, ComposedLayout],
63+
*,
64+
color: Callable = tikz_color_bwx8,
65+
render_func: Optional[Callable[[str], None]] = None,
6366
) -> None:
6467
"""
6568
Prints a layout.
6669
:param x: A layout
6770
:type x: Union[Layout, ComposedLayout]
6871
:param color: A function that returns TiKZ colors
6972
:type color: Callable
73+
:param render_func: An user provided function to render the latex output, which only includes tikz picture section. If None, it will print to stdout.
74+
:type render_func: Optional[Callable]
7075
"""
7176

7277
if not is_static(x):
@@ -79,11 +84,20 @@ def print_latex(
7984
else:
8085
layout = x
8186

82-
print("%% Layout: {}", layout)
83-
print("\\documentclass[convert]{standalone}")
84-
print("\\usepackage{tikz}")
85-
print("\\begin{document}")
86-
print(
87+
latex_output = []
88+
89+
def print_or_append(*args):
90+
if render_func is not None:
91+
latex_output.append(" ".join(str(arg) for arg in args))
92+
else:
93+
print(*args)
94+
95+
if render_func is None:
96+
print_or_append("%% Layout: {}", layout)
97+
print_or_append("\\documentclass[convert]{standalone}")
98+
print_or_append("\\usepackage{tikz}")
99+
print_or_append("\\begin{document}")
100+
print_or_append(
87101
"\\begin{tikzpicture}[x={(0cm,-1cm)},y={(1cm,0cm)},every node/.style={minimum size=1cm, outer sep=0pt}]"
88102
)
89103

@@ -92,27 +106,32 @@ def print_latex(
92106
for m in range(M):
93107
for n in range(N):
94108
idx = layout((m, n))
95-
print("\\node[fill=")
96-
print(color(idx))
97-
print("] at (%d,%d) {%d};\n" % (m, n, idx))
98-
print(
109+
print_or_append("\\node[fill=")
110+
print_or_append(color(idx))
111+
print_or_append("] at (%d,%d) {%d};\n" % (m, n, idx))
112+
print_or_append(
99113
"\\draw[color=black,thick,shift={(-0.5,-0.5)}] (0,0) grid (%d,%d);\n\n" % (M, N)
100114
)
101115
for m in range(M):
102-
print("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n" % (m, -1, m))
116+
print_or_append("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n" % (m, -1, m))
103117
for n in range(N):
104-
print("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n" % (-1, n, n))
118+
print_or_append("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n" % (-1, n, n))
105119

106120
## Footer
107-
print("\\end{tikzpicture}")
108-
print("\\end{document}")
121+
print_or_append("\\end{tikzpicture}")
122+
if render_func is None:
123+
print_or_append("\\end{document}")
124+
125+
if render_func is not None:
126+
render_func(" ".join(latex_output))
109127

110128

111129
def print_latex_tv(
112130
layout_tv: Union[Layout, ComposedLayout],
113131
tile_mn: Union[IntTuple, Layout],
114132
*,
115133
color: Callable = tikz_color_tv,
134+
render_func: Optional[Callable[[str], None]] = None,
116135
) -> None:
117136
"""
118137
Prints a tv layout for a tile M N. Everything must be static.
@@ -122,17 +141,28 @@ def print_latex_tv(
122141
:type tile_mn: Union[IntTuple, Layout]
123142
:param color: A function that returns TiKZ colors
124143
:type color: Callable
144+
:param render_func: An user provided function to render the latex output, which only includes tikz picture section. If None, it will print to stdout.
145+
:type render_func: Optional[Callable]
125146
"""
126147
if not is_static(layout_tv) or not is_static(tile_mn):
127148
raise ValueError("Layout tv and tile_mn must be static")
128149
if rank(layout_tv) != 2:
129150
raise ValueError("Require layout_tv to be rank 2")
130151

131-
print("%% Layout TV: {}", layout_tv)
132-
print("\\documentclass[convert]{standalone}")
133-
print("\\usepackage{tikz}")
134-
print("\\begin{document}")
135-
print(
152+
latex_output = []
153+
154+
def print_or_append(*args):
155+
if render_func is not None:
156+
latex_output.append(" ".join(str(arg) for arg in args))
157+
else:
158+
print(*args)
159+
160+
if render_func is None:
161+
print_or_append("%% Layout TV: {}", layout_tv)
162+
print_or_append("\\documentclass[convert]{standalone}")
163+
print_or_append("\\usepackage{tikz}")
164+
print_or_append("\\begin{document}")
165+
print_or_append(
136166
"\\begin{tikzpicture}[x={(0cm,-1cm)},y={(1cm,0cm)},every node/.style={minimum size=1cm, outer sep=0pt}]\n"
137167
)
138168

@@ -149,19 +179,23 @@ def print_latex_tv(
149179
n = (idx // tile_mn.stride[1]) % tile_mn.shape[1] # type: ignore[operator, union-attr, index]
150180
if not filled[m][n]:
151181
filled[m][n] = True
152-
print(
182+
print_or_append(
153183
"\\node[fill=%s] at (%d,%d) {\\shortstack{T%d \\\\ V%d}};\n"
154184
% (color(tid, vid), m, n, tid, vid)
155185
)
156186

157-
print(
187+
print_or_append(
158188
"\\draw[color=black,thick,shift={(-0.5,-0.5)}] (0,0) grid (%d,%d);\n\n" % (M, N)
159189
)
160190
for m in range(M):
161-
print("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n" % (m, -1, m))
191+
print_or_append("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n" % (m, -1, m))
162192
for n in range(N):
163-
print("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n" % (-1, n, n))
193+
print_or_append("\\node at (%d,%d) {\\Large{\\texttt{%d}}};\n" % (-1, n, n))
164194

165195
## Footer
166-
print("\\end{tikzpicture}")
167-
print("\\end{document}")
196+
print_or_append("\\end{tikzpicture}")
197+
if render_func is None:
198+
print_or_append("\\end{document}")
199+
200+
if render_func is not None:
201+
render_func(" ".join(latex_output))

0 commit comments

Comments
 (0)