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.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.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.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.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

Python decorators for DaCe functions.

dace.frontend.python.decorators.conditional(f, cond)

A decorator version of conditional execution, with an if-condition cond. :param cond: The condition of the branch.

dace.frontend.python.decorators.consume(f, stream, pes)

Consume is a scope, like Map, that creates parallel execution. Unlike Map, it creates a producer-consumer relationship between an input stream and the contents. The contents are run by the given number of processing elements, who will try to pop elements from the input stream until a given quiescence condition is reached. :param stream: The stream to pop from. :param pes: The number of processing elements to use.

dace.frontend.python.decorators.function(f: F, *args, auto_optimize=False, device=<DeviceType.CPU: 1>, **kwargs) → dace.frontend.python.parser.DaceProgram

DaCe program, entry point to a data-centric program.

dace.frontend.python.decorators.iterate(f, rng)

A decorator version of a for loop, with a range of rng. :param rng: The range of the for loop.

dace.frontend.python.decorators.loop(f, cond)

A decorator version of a while loop, with a looping condition cond. :param cond: The condition of the while loop.

dace.frontend.python.decorators.map(f, rng)

A Map is representation of parallel execution, containing an integer set (Python range) for which its contents are run concurrently. :param rng: The map’s range.

dace.frontend.python.decorators.program(f: F, *args, auto_optimize=False, device=<DeviceType.CPU: 1>, **kwargs) → dace.frontend.python.parser.DaceProgram

DaCe program, entry point to a data-centric program.

dace.frontend.python.decorators.tasklet(f)

A general procedure that cannot access any memory apart from incoming and outgoing memlets. The DaCe framework cannot analyze these tasklets for optimization.

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)

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.ndloop.tupletoxrange(s)

Helper function that turns a tuple 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.

class dace.frontend.python.newast.GlobalResolver(globals: Dict[str, Any])

Bases: ast.NodeTransformer

Resolves global constants and lambda expressions if not already defined in the given scope.

generic_visit(node: _ast.AST)

Called if no explicit visitor function exists for a node.

global_value_to_node(value, parent_node, recurse=False)
visit_Attribute(node: _ast.Attribute) → Any
visit_Name(node: _ast.Name)
visit_keyword(node: _ast.keyword)
class dace.frontend.python.newast.ModuleResolver(modules: Dict[str, str])

Bases: ast.NodeTransformer

visit_Attribute(node)
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, symbol]] = None, other_sdfgs: Dict[str, Union[dace.sdfg.sdfg.SDFG, DaceProgram]] = None, nested: bool = False, tmp_idx: int = 0, strict: Optional[bool] = None)

Bases: dace.frontend.python.astutils.ExtNodeVisitor

A visitor that traverses a data-centric Python program AST and constructs an SDFG.

defined
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
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)
visit_Compare(node: _ast.Compare)
visit_Constant(node: _ast.Constant)
visit_Continue(node: _ast.Continue)
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_Str(node: _ast.Str)
visit_Subscript(node: _ast.Subscript)
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)
class dace.frontend.python.newast.StructTransformer(gvars)

Bases: ast.NodeTransformer

A Python AST transformer that replaces `Call`s to create structs with the custom StructInitializer AST node.

visit_Call(node: _ast.Call)
class dace.frontend.python.newast.TaskletTransformer(defined, sdfg: dace.sdfg.sdfg.SDFG, state: dace.sdfg.state.SDFGState, filename: str, lang=<Language.Python: 1>, 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_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(f, name, argtypes, global_vars, modules, other_sdfgs, constants, strict=None)

Parses a @dace.program function into a _ProgramNode object. :param f: A Python function to parse. :param argtypes: An dictionary of (name, type) for the given

function’s arguments, which may pertain to data nodes or symbols (scalars).
Parameters:
  • global_vars – A dictionary of global variables in the closure of f.
  • modules – A dictionary from an imported module name to the module itself.
  • other_sdfgs – Other SDFG and DaceProgram objects in the context of this function.
  • constants – A dictionary from a name to a constant value.
  • strict – Whether to apply strict transformations after parsing nested dace programs.
Returns:

Hierarchical tree of astnodes._Node objects, where the top level node is an astnodes._ProgramNode.

@rtype: SDFG

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)

Bases: object

A data-centric program object, obtained by decorating a function with @dace.program.

compile(*args, strict=None, save=True)

Convenience function that parses and compiles a DaCe program.

generate_pdp(*compilation_args, strict=None)

Generates the parsed AST representation of a DaCe program. :param compilation_args: Various compilation arguments e.g., dtypes. :param strict: Whether to apply strict transforms when parsing nested dace programs. :return: A 2-tuple of (program, modules), where program is a

dace.astnodes._ProgramNode representing the parsed DaCe program, and modules is a dictionary mapping imported module names to their actual module names (for maintaining import aliases).
to_sdfg(*args, strict=None, save=True) → dace.sdfg.sdfg.SDFG

Parses the DaCe function into an SDFG.

dace.frontend.python.parser.infer_symbols_from_shapes(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 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.parser.parse_from_file(filename, *compilation_args)

Try to parse all DaCe programs in filename and return a list of obtained SDFGs. Raises exceptions in case of compilation errors. Also accepts optional compilation arguments containing types and symbol values.

dace.frontend.python.parser.parse_from_function(function, *compilation_args, strict=None, save=True)

Try to parse a DaceProgram object and return the dace.SDFG object that corresponds to it. :param function: DaceProgram object (obtained from the @dace.program

decorator).
Parameters:
  • compilation_args – Various compilation arguments e.g. dtypes.
  • strict – Whether to apply strict transformations or not (None uses configuration-defined value).
  • save – If True, saves the generated SDFG to _dacegraphs/program.sdfg after parsing.
Returns:

The generated SDFG object.

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).

class dace.frontend.python.wrappers.stream(dtype, shape)

Bases: object

Stream array object in Python. Mostly used in the Python SDFG simulator.

shape

Module contents