dace.frontend.python package¶
Submodules¶
dace.frontend.python.astnodes module¶
dace.frontend.python.astutils module¶
Various AST parsing utilities for DaCe.
-
class
dace.frontend.python.astutils.
ASTFindReplace
(repldict: Dict[str, str])¶ Bases:
ast.NodeTransformer
-
visit_Name
(node: _ast.Name)¶
-
visit_keyword
(node: _ast.keyword)¶
-
-
class
dace.frontend.python.astutils.
ASTHelperMixin
¶ Bases:
object
A mixin that adds useful helper functions for AST node transformers and visitors
-
generic_visit_field
(node: _ast.AST, field: str) → _ast.AST¶ Modification of ast.NodeTransformer.generic_visit that only visits one field. :param node: AST to visit. :param field: Field to visit.
-
generic_visit_filtered
(node: _ast.AST, filter: Optional[Set[str]] = None)¶ Modification of ast.NodeTransformer.generic_visit that visits all fields without the set of filtered fields. :param node: AST to visit. :param filter: Set of strings of fields to skip.
-
-
class
dace.frontend.python.astutils.
AnnotateTopLevel
¶ Bases:
dace.frontend.python.astutils.ExtNodeTransformer
-
visit_TopLevel
(node)¶
-
-
class
dace.frontend.python.astutils.
ConstantExtractor
(globals: Dict[str, Any])¶ Bases:
ast.NodeTransformer
-
visit_Constant
(node)¶
-
visit_Name
(node: _ast.Name)¶
-
visit_Num
(node: _ast.Num)¶
-
-
class
dace.frontend.python.astutils.
ExtNodeTransformer
¶ Bases:
ast.NodeTransformer
A NodeTransformer subclass that walks the abstract syntax tree and allows modification of nodes. As opposed to NodeTransformer, this class is capable of traversing over top-level expressions in bodies in order to discern DaCe statements from others.
-
generic_visit
(node)¶ Called if no explicit visitor function exists for a node.
-
visit_TopLevel
(node)¶
-
-
class
dace.frontend.python.astutils.
ExtNodeVisitor
¶ Bases:
ast.NodeVisitor
A NodeVisitor subclass that walks the abstract syntax tree. As opposed to NodeVisitor, this class is capable of traversing over top-level expressions in bodies in order to discern DaCe statements from others.
-
generic_visit
(node)¶ Called if no explicit visitor function exists for a node.
-
visit_TopLevel
(node)¶
-
-
class
dace.frontend.python.astutils.
ExtUnparser
(tree, file=<_io.TextIOWrapper name='<stdout>' mode='w' encoding='UTF-8'>)¶ Bases:
astunparse.unparser.Unparser
-
class
dace.frontend.python.astutils.
RemoveSubscripts
(keywords: Set[str])¶ Bases:
ast.NodeTransformer
-
visit_Subscript
(node: _ast.Subscript)¶
-
-
class
dace.frontend.python.astutils.
TaskletFreeSymbolVisitor
(defined_syms)¶ Bases:
ast.NodeVisitor
Simple Python AST visitor to find free symbols in a code, not including attributes and function calls.
-
visit_AnnAssign
(node)¶
-
visit_Attribute
(node)¶
-
visit_Call
(node: _ast.Call)¶
-
visit_Name
(node)¶
-
-
dace.frontend.python.astutils.
astrange_to_symrange
(astrange, arrays, arrname=None)¶ Converts an AST range (array, [(start, end, skip)]) to a symbolic math range, using the obtained array sizes and resolved symbols.
-
dace.frontend.python.astutils.
copy_tree
(node: _ast.AST) → _ast.AST¶ Copies an entire AST without copying the non-AST parts (e.g., constant values). A form of reduced deepcopy. :param node: The tree to copy. :return: The copied tree.
-
dace.frontend.python.astutils.
create_constant
(value: Any, node: Optional[_ast.AST] = None) → _ast.AST¶ Cross-Python-AST-version helper function that creates an AST constant node from a given value. :param value: The value to create a constant from. :param node: An optional node to copy the source location information from. :return: An AST node (
ast.Constant
after Python 3.8) that represents this value.
-
dace.frontend.python.astutils.
escape_string
(value: str)¶ Converts special Python characters in strings back to their parsable version (e.g., newline to `` ``)
-
dace.frontend.python.astutils.
evalnode
(node: _ast.AST, gvars: Dict[str, Any]) → Any¶ Tries to evaluate an AST node given only global variables. :param node: The AST node/subtree to evaluate. :param gvars: A dictionary mapping names to variables. :return: The result of evaluation, or raises
SyntaxError
on anyfailure to evaluate.
-
dace.frontend.python.astutils.
function_to_ast
(f)¶ Obtain the source code of a Python function and create an AST. :param f: Python function. :return: A 4-tuple of (AST, function filename, function line-number,
source code as string).
-
dace.frontend.python.astutils.
is_constant
(node: _ast.AST) → bool¶ Returns True iff the AST node is a constant value
-
dace.frontend.python.astutils.
negate_expr
(node)¶ Negates an AST expression by adding a Not AST node in front of it.
-
dace.frontend.python.astutils.
rname
(node)¶ Obtains names from different types of AST nodes.
-
dace.frontend.python.astutils.
slice_to_subscript
(arrname, range)¶ Converts a name and subset to a Python AST Subscript object.
-
dace.frontend.python.astutils.
subscript_to_ast_slice
(node, without_array=False)¶ Converts an AST subscript to slice on the form (<name>, [<3-tuples of AST nodes>]). If an ast.Name is passed, returns (name, None), implying the full range. :param node: The AST node to convert. :param without_array: If True, returns only the slice. Otherwise,
returns a 2-tuple of (array, range).
-
dace.frontend.python.astutils.
subscript_to_ast_slice_recursive
(node)¶ Converts an AST subscript to a slice in a recursive manner into nested subscripts. @see: subscript_to_ast_slice
-
dace.frontend.python.astutils.
subscript_to_slice
(node, arrays, without_array=False)¶ Converts an AST subscript to slice on the form (<name>, [<3-tuples of indices>]). If an ast.Name is passed, return (name, None), implying the full range.
-
dace.frontend.python.astutils.
unparse
(node)¶ Unparses an AST node to a Python string, chomping trailing newline.
dace.frontend.python.decorators module¶
dace.frontend.python.ndloop module¶
A single generator that creates an N-dimensional for loop in Python.
-
dace.frontend.python.ndloop.
NDLoop
(ndslice, internal_function, *args, **kwargs)¶ Wrapped generator that calls an internal function in an N-dimensional for-loop in Python. :param ndslice: Slice or list of slices (slice objects) to loop over. :param internal_function: Function to call in loop. :param *args: Arguments to internal_function. :param **kwargs: Keyword arguments to internal_function. :return: N-dimensional loop index generator.
-
dace.frontend.python.ndloop.
ndrange
(slice_list: Union[Tuple[slice], slice])¶ Generator that creates an N-dimensional for loop in Python. :param slice_list: Slice or list of slices (as tuples or `slice`s)
to loop over.Returns: N-dimensional loop index generator.
-
dace.frontend.python.ndloop.
slicetoxrange
(s)¶ Helper function that turns a slice into a range (for iteration).
dace.frontend.python.newast module¶
-
class
dace.frontend.python.newast.
AddTransientMethods
¶ Bases:
object
A management singleton for methods that add transient data to SDFGs.
-
static
get
(datatype)¶ Returns a method.
-
static
-
class
dace.frontend.python.newast.
ProgramVisitor
(name: str, filename: str, line_offset: int, col_offset: int, global_vars: Dict[str, Any], constants: Dict[str, Any], scope_arrays: Dict[str, dace.data.Data], scope_vars: Dict[str, str], map_symbols: Set[Union[str, dace.symbolic.symbol]] = None, annotated_types: Dict[str, dace.data.Data] = None, closure: dace.frontend.python.common.SDFGClosure = None, nested: bool = False, tmp_idx: int = 0, simplify: Optional[bool] = None)¶ Bases:
dace.frontend.python.astutils.ExtNodeVisitor
A visitor that traverses a data-centric Python program AST and constructs an SDFG.
-
create_callback
(node: _ast.Call, create_graph=True)¶
-
defined
¶
-
classmethod
increment_progress
(number=1)¶ Adds a number of parsed SDFGs to the progress bar (whether visible or not).
-
make_slice
(arrname: str, rng: dace.subsets.Range)¶
-
parse_program
(program: _ast.FunctionDef, is_tasklet: bool = False)¶ Parses a DaCe program or tasklet
- Arguments:
- program {ast.FunctionDef} – DaCe program or tasklet
- Keyword Arguments:
- is_tasklet {bool} – True, if program is tasklet (default: {False})
- Returns:
- Tuple[SDFG, Dict, Dict] – Parsed SDFG, its inputs and outputs
-
progress_bar
= None¶
-
classmethod
progress_count
() → int¶ Returns the number of parsed SDFGs so far within this run.
-
start_time
= 0¶
-
visit
(node: _ast.AST)¶ Visit a node.
-
visit_AnnAssign
(node: _ast.AnnAssign)¶
-
visit_Assign
(node: _ast.Assign)¶
-
visit_AsyncWith
(node)¶
-
visit_Attribute
(node: _ast.Attribute)¶
-
visit_AugAssign
(node: _ast.AugAssign)¶
-
visit_BinOp
(node: _ast.BinOp)¶
-
visit_BoolOp
(node: _ast.BoolOp)¶
-
visit_Break
(node: _ast.Break)¶
-
visit_Call
(node: _ast.Call, create_callbacks=False)¶
-
visit_Compare
(node: _ast.Compare)¶
-
visit_Constant
(node: _ast.Constant)¶
-
visit_Continue
(node: _ast.Continue)¶
-
visit_Dict
(node: _ast.Dict)¶
-
visit_ExtSlice
(node: _ast.ExtSlice) → Any¶
-
visit_For
(node: _ast.For)¶
-
visit_FunctionDef
(node: _ast.FunctionDef)¶
-
visit_If
(node: _ast.If)¶
-
visit_Index
(node: _ast.Index) → Any¶
-
visit_Lambda
(node: _ast.Lambda)¶
-
visit_List
(node: _ast.List)¶
-
visit_Name
(node: _ast.Name)¶
-
visit_NameConstant
(node: _ast.NameConstant)¶
-
visit_NamedExpr
(node)¶
-
visit_Num
(node: _ast.Num)¶
-
visit_Return
(node: _ast.Return)¶
-
visit_Set
(node: _ast.Set)¶
-
visit_Str
(node: _ast.Str)¶
-
visit_Subscript
(node: _ast.Subscript, inference: bool = False)¶
-
visit_TopLevelExpr
(node: _ast.Expr)¶
-
visit_Tuple
(node: _ast.Tuple)¶
-
visit_UnaryOp
(node: _ast.UnaryOp)¶
-
visit_While
(node: _ast.While)¶
-
visit_With
(node, is_async=False)¶
-
-
exception
dace.frontend.python.newast.
SkipCall
¶ Bases:
Exception
Exception used to skip calls to functions that cannot be parsed.
-
class
dace.frontend.python.newast.
TaskletTransformer
(visitor, defined, sdfg: dace.sdfg.sdfg.SDFG, state: dace.sdfg.state.SDFGState, filename: str, lang=None, location: dict = {}, nested: bool = False, scope_arrays: Dict[str, dace.data.Data] = {}, scope_vars: Dict[str, str] = {}, variables: Dict[str, str] = {}, accesses: Dict[Tuple[str, dace.subsets.Subset, str], str] = {}, symbols: Dict[str, dace.symbol] = {})¶ Bases:
dace.frontend.python.astutils.ExtNodeTransformer
A visitor that traverses a data-centric tasklet, removes memlet annotations and returns input and output memlets.
-
parse_tasklet
(tasklet_ast: Union[_ast.FunctionDef, _ast.With, _ast.For], name: Optional[str] = None)¶ Parses the AST of a tasklet and returns the tasklet node, as well as input and output memlets. :param tasklet_ast: The Tasklet’s Python AST to parse. :param name: Optional name to use as prefix for tasklet. :return: 3-tuple of (Tasklet node, input memlets, output memlets). @rtype: Tuple[Tasklet, Dict[str, Memlet], Dict[str, Memlet]]
-
visit_Call
(node: _ast.Call) → Any¶
-
visit_Name
(node: _ast.Name)¶
-
visit_TopLevelExpr
(node)¶
-
visit_TopLevelStr
(node: _ast.Str)¶
-
-
dace.frontend.python.newast.
add_indirection_subgraph
(sdfg: dace.sdfg.sdfg.SDFG, graph: dace.sdfg.state.SDFGState, src: dace.sdfg.nodes.Node, dst: dace.sdfg.nodes.Node, memlet: dace.memlet.Memlet, local_name: str, pvisitor: dace.frontend.python.newast.ProgramVisitor, output: bool = False, with_wcr: bool = False)¶ Replaces the specified edge in the specified graph with a subgraph that implements indirection without nested memlet subsets.
-
dace.frontend.python.newast.
parse_dace_program
(name: str, preprocessed_ast: _ast.AST, argtypes: Dict[str, dace.data.Data], constants: Dict[str, Any], closure: dace.frontend.python.common.SDFGClosure, simplify: Optional[bool] = None, save: bool = True, progress: Optional[bool] = None) → dace.sdfg.sdfg.SDFG¶ Parses a @dace.program function into an SDFG. :param src_ast: The AST of the Python program to parse. :param visitor: A ProgramVisitor object returned from
preprocess_dace_program
.Parameters: - closure – An object that contains the @dace.program closure.
- simplify – If True, simplification pass will be performed.
- save – If True, saves source mapping data for this SDFG.
- progress – If True, prints a progress bar of the parsing process. If None (default), prints after 5 seconds of parsing. If False, never prints progress.
Returns: A 2-tuple of SDFG and its reduced (used) closure.
-
dace.frontend.python.newast.
specifies_datatype
(func: Callable[[Any, dace.data.Data, Any], Tuple[str, dace.data.Data]], datatype=None)¶
-
dace.frontend.python.newast.
until
(val, substr)¶ Helper function that returns the substring of a string until a certain pattern.
dace.frontend.python.parser module¶
DaCe Python parsing functionality and entry point to Python frontend.
-
class
dace.frontend.python.parser.
DaceProgram
(f, args, kwargs, auto_optimize, device, constant_functions=False, method=False)¶ Bases:
dace.frontend.python.common.SDFGConvertible
A data-centric program object, obtained by decorating a function with
@dace.program
.-
closure_resolver
(constant_args, given_args, parent_closure=None)¶ Returns an SDFGClosure object representing the closure of the object to be converted to an SDFG. :param constant_args: Arguments whose values are already resolved to
compile-time values.Parameters: - given_args – Arguments that were given at call-time (used for determining which arguments with defaults were provided).
- parent_closure – The parent SDFGClosure object (used for, e.g., recursion detection).
Returns: New SDFG closure object representing the convertible object.
-
compile
(*args, simplify=None, save=False, **kwargs)¶ Convenience function that parses and compiles a DaCe program.
-
get_program_hash
(*args, **kwargs) → dace.frontend.python.cached_program.ProgramCacheKey¶ Returns the program’s hash (cache key) given the arguments and the program’s closure. :param args: Arguments that the SDFG will be called with. :param kwargs: Keyword arguments that the SDFG will be called with. :return: A hashable program cache key object.
-
load_precompiled_sdfg
(path: str, *args, **kwargs) → None¶ Loads an external compiled SDFG object that will be invoked when the function is called. :param path: Path to SDFG build folder (e.g., “.dacecache/program”).
Path has to includeprogram.sdfg
and the binary shared object under thebuild
folder.Parameters: - args – Optional compile-time arguments.
- kwargs – Optional compile-time keyword arguments.
-
load_sdfg
(path: str, *args, **kwargs)¶ Loads an external SDFG that will be used when the function is called. :param path: Path to SDFG file. :param args: Optional compile-time arguments. :param kwargs: Optional compile-time keyword arguments.
-
methodobj
¶
-
name
¶ Returns a unique name for this program.
-
to_sdfg
(*args, simplify=None, save=False, validate=False, use_cache=False, **kwargs) → dace.sdfg.sdfg.SDFG¶ Creates an SDFG from the DaCe function. If no type hints are provided on the function, example arrays/scalars (i.e., with the same shape and type) must be given to this method in order to construct a valid SDFG.
Parameters: - args – JIT (i.e., without type hints) argument examples.
- kwargs – JIT (i.e., without type hints) keyword argument examples.
- simplify – Whether to simplify the SDFG after parsing (default is None, which uses the .dace.conf setting)
- save – Whether to save the SDFG to a file after parsing
- validate – Whether to validate the parsed SDFG
- use_cache – If True, tries to find an already parsed SDFG in the local cache. Otherwise, re-parses SDFG.
Returns: An SDFG object that can be transformed, saved, or called.
-
validate
= None¶ Whether to validate on code generation
-
-
dace.frontend.python.parser.
infer_symbols_from_datadescriptor
(sdfg: dace.sdfg.sdfg.SDFG, args: Dict[str, Any], exclude: Optional[Set[str]] = None) → Dict[str, Any]¶ Infers the values of SDFG symbols (not given as arguments) from the shapes and strides of input arguments (e.g., arrays). :param sdfg: The SDFG that is being called. :param args: A dictionary mapping from current argument names to their
values. This may also include symbols.Parameters: exclude – An optional set of symbols to ignore on inference. Returns: A dictionary mapping from symbol names that are not in args
to their inferred values.Raises: ValueError – If symbol values are ambiguous.
dace.frontend.python.simulator module¶
dace.frontend.python.wrappers module¶
Types and wrappers used in DaCe’s Python frontend.
-
dace.frontend.python.wrappers.
define_local
(dimensions, dtype=float)¶ Defines a transient array in a DaCe program.
-
dace.frontend.python.wrappers.
define_local_scalar
(dtype=float)¶ Defines a transient scalar (array of size 1) in a DaCe program.
-
dace.frontend.python.wrappers.
define_stream
(dtype=float, buffer_size=1)¶ Defines a local stream in a DaCe program.
-
dace.frontend.python.wrappers.
define_streamarray
(dimensions, dtype=float, buffer_size=1)¶ Defines a local stream array in a DaCe program.
-
dace.frontend.python.wrappers.
ndarray
(shape, dtype=<class 'numpy.float64'>, *args, **kwargs)¶ Returns a numpy ndarray where all symbols have been evaluated to numbers and types are converted to numpy types.
-
dace.frontend.python.wrappers.
scalar
(dtype=float)¶ Convenience function that defines a scalar (array of size 1).