-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathvisitors_util.py
More file actions
406 lines (326 loc) · 11 KB
/
visitors_util.py
File metadata and controls
406 lines (326 loc) · 11 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
import ast
import os
import re
import sys
from typing import Dict, List, Optional, Set, Union
from pykokkos import kokkos_manager as km
from pykokkos.core import cppast
from pykokkos.core.keywords import Keywords
from pykokkos.interface import Layout, MemorySpace, Trait
def pretty_print(node):
print(ast.dump(node, indent=4))
allowed_types: Dict[str, str] = {
"int": "int",
"float": "float",
"double": "double",
"bool": "bool",
"TeamMember": f"Kokkos::TeamPolicy<{Keywords.DefaultExecSpace.value}>::member_type",
"cpp_auto": "auto"
}
# Maps from the DataType enum to cppast
view_dtypes: Dict[str, Union[cppast.BuiltinType, str]] = {
"int8": cppast.BuiltinType.INT8,
"int16": cppast.BuiltinType.INT16,
"int32": cppast.BuiltinType.INT32,
"int64": cppast.BuiltinType.INT64,
"uint8": cppast.BuiltinType.UINT8,
"uint16": cppast.BuiltinType.UINT16,
"uint32": cppast.BuiltinType.UINT32,
"uint64": cppast.BuiltinType.UINT64,
"float": cppast.BuiltinType.FLOAT,
"double": cppast.BuiltinType.DOUBLE,
"int": cppast.BuiltinType.INT32,
"real": Keywords.RealPrecision.value
}
op2str: Dict[type, str] = {
# Binary
ast.Add: "+",
ast.Sub: "-",
ast.Mult: "*",
ast.Mod: "%",
ast.LShift: "<<",
ast.RShift: ">>",
ast.BitAnd: "&",
ast.BitOr: "|",
ast.BitXor: "^", # Pow, Div, and FloorDiv handled separately
# Unary
ast.UAdd: "+",
ast.USub: "-",
ast.Not: "!",
ast.Invert: "~",
# Logical
ast.And: "&&",
ast.Or: "||",
# Comparisons
ast.Eq: "==",
ast.NotEq: "!=",
ast.Lt: "<",
ast.LtE: "<=",
ast.Gt: ">",
ast.GtE: ">=",
# ast.Is: "is",
# ast.IsNot: "is not",
# ast.In: "in",
# ast.NotIn: "not in",
}
# TODO: provide mapping to cmath versions
math_functions: Set = {
"acos",
"acosh",
"asin",
"asinh",
"atan",
"atan2",
"atanh",
"ceil",
"copysign",
"cos",
"cosh",
"degrees",
"exp",
"expm1",
"fabs",
"floor",
"fmax",
"fmin",
"fmod",
"hypot",
"isfinite",
"isinf",
"isnan",
"isqrt",
"log",
"log10",
"log1p",
"log2",
"modf",
"pow",
"radians",
"remainder",
"round",
"sin",
"sinh",
"sqrt",
"tan",
"tanh",
"trunc",
"nan",
}
math_special_functions: Set = {
"expint1",
"erfcx",
"cyl_bessel_j0",
"cyl_bessel_y0",
"cyl_bessel_i0",
"cyl_bessel_k0",
"cyl_bessel_j1",
"cyl_bessel_y1",
"cyl_bessel_i1",
"cyl_bessel_k1",
"cyl_bessel_h10",
"cyl_bessel_h11",
"cyl_bessel_h20",
"cyl_bessel_h21",
}
math_constants: Dict[str, str] = {
"e": "M_E",
"pi": "M_PI",
"tau": "2*M_PI",
"inf": "1.0/0.0",
"nan": "0.0/0.0",
}
def error(src, debug: bool, node, message) -> None:
if hasattr(node, "lineno"):
print(f"\n\033[31m\033[01mError on line {node.lineno} \033[0m: {message}")
else:
print(f"\n\033[31m\033[01mError\033[0m: {message}")
if debug:
print("DEBUG AST:")
pretty_print(node)
if hasattr(node, "lineno"):
print(src[0][node.lineno - src[1] - 1], end="")
err_len = node.end_col_offset - node.col_offset if node.end_col_offset else 1
print(" " * node.col_offset + "^" * err_len)
sys.exit("PyKokkos: Translation failed")
def generic_error(src, debug: bool, node) -> None:
error(src, debug, node, "Not supported for translation")
def get_op_str(op: ast.expr) -> str:
if type(op) not in op2str:
raise NotImplementedError
return op2str[type(op)]
def get_math_constant_str(constant: str) -> str:
if constant not in math_constants:
raise NotImplementedError
return math_constants[constant]
def get_allowed_type_str(python_type: str) -> str:
if python_type not in allowed_types:
raise NotImplementedError
return allowed_types[python_type]
def is_math_function(function: str) -> bool:
return function in math_functions
def is_math_special_function(function: str) -> bool:
return function in math_special_functions
def get_node_name(node: Union[ast.Attribute, ast.Name]) -> str:
name: str = ""
if isinstance(node, ast.Attribute):
# TODO: check fully qualified names
name = node.attr
else:
name = node.id
return name
def get_type(annotation: Union[ast.Attribute, ast.Name, ast.Subscript], pk_import: str) -> Optional[cppast.Type]:
if isinstance(annotation, ast.Attribute):
if annotation.value.id == pk_import:
type_name: str = get_node_name(annotation)
if type_name in view_dtypes:
return cppast.PrimitiveType(view_dtypes[type_name])
if type_name in allowed_types:
type_name = allowed_types[type_name]
return cppast.ClassType(type_name)
if isinstance(annotation, ast.Index):
# ast.Index has been deprecated since Python 3.9;
# this module attempts to shim around it.
# should convert to ast.Name:
annotation = annotation.value
if isinstance(annotation, ast.Name):
type_name: str = annotation.id
if type_name == "int":
return cppast.PrimitiveType(cppast.BuiltinType.INT)
if type_name == "float":
return cppast.PrimitiveType(cppast.BuiltinType.DOUBLE)
if type_name == "bool":
return cppast.PrimitiveType(cppast.BuiltinType.BOOL)
if type_name in allowed_types:
return cppast.ClassType(type_name)
if type_name == "List":
return None
if isinstance(annotation, ast.Subscript):
value: Union[ast.Name, ast.Attribute] = annotation.value
subscript: ast.Index = annotation.slice
id: str = ""
if isinstance(value, ast.Name):
id = value.id
elif isinstance(value, ast.Attribute):
id = value.value.id
if id == "List":
if sys.version_info.minor <= 8:
# In Python >= 3.9, ast.Index is deprecated
# (see # https://docs.python.org/3/whatsnew/3.9.html)
value = subscript.value
else:
value = subscript
member_type: cppast.Type = get_type(value, pk_import)
return member_type
if id == pk_import:
type_name: str = get_node_name(value)
if sys.version_info.minor <= 8:
# In Python >= 3.9, ast.Index is deprecated
# (see # https://docs.python.org/3/whatsnew/3.9.html)
dtype_node = subscript.value if isinstance(subscript, ast.Index) else subscript
else:
dtype_node: ast.Attribute = subscript
if type_name == "Acc":
return get_type(dtype_node, pk_import)
dtype: cppast.PrimitiveType = get_type(dtype_node, pk_import)
if dtype is None:
return None
view_type = cppast.ClassType(type_name)
view_type.add_template_param(dtype)
return view_type
return None
def parse_view_template_params(
view_type: cppast.ClassType,
rank: Optional[int] = None,
space: Optional[str] = None,
layout: Optional[str] = None,
real: Optional[str] = None,
) -> Dict[str, str]:
"""
Parse the template params of a view type node
:param view_type: the cppast representation of the view
:param rank: optionally provide the rank (used by subviews)
:param space: optionally provide a memory space
:param layout: optionally provide a layout
:param real: optionally provide the precision of pk.real dtypes
:returns: a dict with an entry for each template parameter
"""
py_type: str = view_type.typename
is_scratch_view: bool = py_type.startswith("ScratchView")
if rank is None:
rank = int(re.search(r'\d+', py_type).group())
if not 0 < rank < 8:
raise ValueError(f"View rank {rank} is not allowed")
params: Dict[str, str] = {}
# unmanaged views cannot have a layout
unmanaged: bool = False
if is_scratch_view:
unmanaged = True
template_params: List[cppast.Node] = view_type.template_params
s = cppast.Serializer()
for t in template_params:
parameter: str = s.serialize(t)
if parameter in ("int", "double", "float",
"int8_t", "int16_t", "int32_t", "int64_t",
"uint8_t", "uint16_t", "uint32_t", "uint64_t"):
datatype: str = parameter + "*" * rank
params["dtype"] = datatype
elif parameter == Keywords.RealPrecision.value:
real_dtype: str = Keywords.RealPrecision.value
if real is not None:
real_dtype = real
datatype: str = real_dtype + "*" * rank
params["dtype"] = datatype
elif parameter in Trait.__members__:
if parameter not in ("TraitDefault", "Managed", "Unmanaged"):
params["trait"] = f"Kokkos::MemoryTraits<Kokkos::{parameter}>"
elif parameter in Layout.__members__:
if parameter != "LayoutDefault":
params["layout"] = f"Kokkos::{parameter}"
elif parameter in MemorySpace.__members__:
space = f"Kokkos::{parameter}"
if "layout" not in params and not unmanaged:
if layout is not None:
params["layout"] = layout
else:
params["layout"] = f"{Keywords.DefaultExecSpace.value}::array_layout"
if space is not None:
if space == "Kokkos::HIPSpace":
space = "Kokkos::Experimental::HIPSpace"
params["space"] = space
elif is_scratch_view:
params["space"] = f"{Keywords.DefaultExecSpace.value}::scratch_memory_space"
else:
params["space"] = f"{Keywords.DefaultExecSpace.value}::memory_space"
if is_scratch_view:
params["trait"] = f"Kokkos::MemoryTraits<Kokkos::Unmanaged>"
return params
def cpp_view_type(
view_type: cppast.ClassType,
rank: Optional[int] = None,
space: Optional[str] = None,
layout: Optional[str] = None,
real: Optional[str] = None,
) -> str:
"""
Get the C++ type of a view
:param view_type: the cppast representation of the view
:param rank: optionally provide the rank (used by subviews)
:param space: optionally provide a memory space
:param layout: optionally provide a layout
:param real: optionally provide the precision of pk.real dtypes
:returns: string representation of C++ view type
"""
params = parse_view_template_params(view_type, rank, space, layout, real)
params_ordered: List[str] = []
params_ordered.append(params["dtype"])
if "layout" in params:
params_ordered.append(params["layout"])
if "space" in params:
params_ordered.append(params["space"])
if km.get_kokkos_version() >= 3.7:
params_ordered.append("Kokkos::Experimental::DefaultViewHooks")
if "trait" in params:
params_ordered.append(params["trait"])
cpp_type: str = "Kokkos::View<"
cpp_type += ",".join(params_ordered) + ">"
return cpp_type