dace.frontend.python package

Submodules

dace.frontend.python.astutils module

Various AST parsing utilities for DaCe.

class dace.frontend.python.astutils.ASTFindReplace(repldict, trigger_names=None)

Bases: NodeTransformer

visit_Name(node)
visit_keyword(node)
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, field)

Modification of ast.NodeTransformer.generic_visit that only visits one field.

Parameters:
  • node (AST) – AST to visit.

  • field (str) – Field to visit.

Return type:

AST

generic_visit_filtered(node, filter=None)

Modification of ast.NodeTransformer.generic_visit that visits all fields without the set of filtered fields.

Parameters:
  • node (AST) – AST to visit.

  • filter (Optional[Set[str]]) – Set of strings of fields to skip.

class dace.frontend.python.astutils.AnnotateTopLevel

Bases: ExtNodeTransformer

visit_TopLevel(node)
class dace.frontend.python.astutils.ConstantExtractor(globals)

Bases: NodeTransformer

visit_Constant(node)
visit_Name(node)
visit_Num(node)
class dace.frontend.python.astutils.ExtNodeTransformer

Bases: 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: 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: Unparser

exception dace.frontend.python.astutils.NameFound

Bases: Exception

class dace.frontend.python.astutils.RemoveSubscripts(keywords)

Bases: NodeTransformer

visit_Subscript(node)
class dace.frontend.python.astutils.TaskletFreeSymbolVisitor(defined_syms)

Bases: 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)
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)

Copies an entire AST without copying the non-AST parts (e.g., constant values). A form of reduced deepcopy.

Parameters:

node (AST) – The tree to copy.

Return type:

AST

Returns:

The copied tree.

dace.frontend.python.astutils.create_constant(value, node=None)

Cross-Python-AST-version helper function that creates an AST constant node from a given value.

Parameters:
  • value (Any) – The value to create a constant from.

  • node (Optional[AST]) – An optional node to copy the source location information from.

Return type:

AST

Returns:

An AST node (ast.Constant after Python 3.8) that represents this value.

dace.frontend.python.astutils.escape_string(value)

Converts special Python characters in strings back to their parsable version (e.g., newline to \n)

dace.frontend.python.astutils.evalnode(node, gvars)

Tries to evaluate an AST node given only global variables.

Parameters:
  • node (AST) – The AST node/subtree to evaluate.

  • gvars (Dict[str, Any]) – A dictionary mapping names to variables.

Return type:

Any

Returns:

The result of evaluation, or raises SyntaxError on any failure to evaluate.

dace.frontend.python.astutils.function_to_ast(f)

Obtain the source code of a Python function and create an AST.

Parameters:

f – Python function.

Returns:

A 4-tuple of (AST, function filename, function line-number, source code as string).

dace.frontend.python.astutils.is_constant(node)

Returns True iff the AST node is a constant value

Return type:

bool

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.parse_function_arguments(node, argnames)

Parses function arguments (both positional and keyword) from a Call node, based on the function’s argument names. If an argument was not given, it will not be in the result.

Return type:

Dict[str, AST]

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.

Parameters:
  • node – The AST node to convert.

  • 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.cached_program module

Precompiled DaCe program/method cache.

class dace.frontend.python.cached_program.DaceProgramCache(evaluate, size=None)

Bases: object

add(key, sdfg, compiled_sdfg)

Adds a new entry to the program cache.

Return type:

None

clear()

Clears the program cache.

get(key)

Returns an existing entry if in the program cache, or raises KeyError otherwise.

Return type:

ProgramCacheEntry

has(key)

Returns True iff the given entry exists in the program cache.

Return type:

bool

make_key(argtypes, specified_args, closure_types, closure_constants, extra_constants=None)

Creates a program cache key from the given arguments.

Return type:

ProgramCacheKey

pop()

Remove the first entry from the cache.

Return type:

None

class dace.frontend.python.cached_program.LimitedSizeDict(*args, **kwds)

Bases: OrderedDict

class dace.frontend.python.cached_program.ProgramCacheEntry(sdfg, compiled_sdfg)

Bases: object

A value object representing a cache entry of a DaCe program. Contains the parsed SDFG and the compiled SDFG object.

compiled_sdfg: CompiledSDFG
sdfg: SDFG
class dace.frontend.python.cached_program.ProgramCacheKey(arg_types, closure_types, closure_constants, specified_args)

Bases: object

A key object representing a single instance of a DaCe program.

arg_types: Dict[str, Data]
closure_constants: Dict[str, Any]
closure_types: Dict[str, Data]
specified_args: Set[str]

dace.frontend.python.common module

exception dace.frontend.python.common.DaceSyntaxError(visitor, node, message)

Bases: Exception

class dace.frontend.python.common.SDFGClosure

Bases: object

Represents a reduced closure of a parsed DaCe program. A dace.program’s closure is composed of its used constants, arrays, and other internal SDFG-convertible objects.

array_mapping: Dict[int, str]
call_tree_length()
Return type:

int

callbacks: Dict[str, Tuple[str, Callable[..., Any], bool]]
callstack: List[int]
closure_arrays: Dict[str, Tuple[str, Data, Callable[[], Any], bool]]
closure_constants: Dict[str, Any]
closure_sdfgs: Dict[int, Tuple[str, Union[SDFG, SDFGConvertible]]]
combine_nested_closures()
nested_closures: List[Tuple[str, SDFGClosure]]
print_call_tree(name, indent=0)
class dace.frontend.python.common.SDFGConvertible

Bases: object

A mixin that defines the interface to annotate SDFG-convertible objects.

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.

Parameters:
  • constant_args (Dict[str, Any]) – Arguments whose values are already resolved to compile-time values.

  • given_args (Set[str]) – Arguments that were given at call-time (used for determining which arguments with defaults were provided).

  • parent_closure (Optional[SDFGClosure]) – The parent SDFGClosure object (used for, e.g., recursion detection).

Return type:

SDFGClosure

Returns:

New SDFG closure object representing the convertible object.

class dace.frontend.python.common.StringLiteral(value)

Bases: object

A string literal found in a parsed DaCe program.

value: Union[str, bytes]
dace.frontend.python.common.inverse_dict_lookup(dict, value)

Finds the first key in a dictionary with the input value.

dace.frontend.python.interface module

Python interface for DaCe functions.

class dace.frontend.python.interface.MapGenerator(rng)

Bases: object

An SDFG map generator class that allows applying operators on it, used for syntactic sugar.

class dace.frontend.python.interface.MapMetaclass

Bases: type

Metaclass for map, to enable dace.map[0:N] syntax.

class dace.frontend.python.interface.TaskletMetaclass

Bases: type

Metaclass for tasklet, to enable with dace.tasklet: syntax.

class dace.frontend.python.interface.consume(stream, processing_elements=1, condition=None)

Bases: object

dace.frontend.python.interface.function(f, *args, auto_optimize=False, device=DeviceType.CPU, recreate_sdfg=True, regenerate_code=True, recompile=True, constant_functions=False, **kwargs)

Entry point to a data-centric program. For methods and classmethod``s, use ``@dace.method.

Parameters:
  • f (TypeVar(F, bound= Callable[..., Any])) – The function to define as the entry point.

  • auto_optimize – If True, applies automatic optimization heuristics on the generated DaCe program during compilation.

  • device – Transform the function to run on the target device.

  • recreate_sdfg (bool) – Whether to recreate the SDFG from the Python code. If False, the SDFG will be loaded from the cache (<build folder>/<program name>/program.sdfg) if it exists. Use this if you want to modify the SDFG after the first call to the function.

  • regenerate_code (bool) – Whether to regenerate the code from the SDFG. If False, the code in the build folder will be used if it exists. Use this if you want to modify the generated code without DaCe overriding it.

  • recompile (bool) – Whether to recompile the code. If False, the library in the build folder will be used if it exists, without recompiling it.

  • constant_functions – If True, assumes all external functions that do not depend on internal variables are constant. This will hardcode their return values into the resulting program.

Note:

If arguments are defined with type hints, the program can be compiled ahead-of-time with .compile().

Return type:

Callable[..., DaceProgram]

dace.frontend.python.interface.in_program()

Returns True if in a DaCe program parsing context. This function can be used to test whether the current code runs inside the @dace.program parser.

Return type:

bool

Returns:

True if in a DaCe program parsing context, or False otherwise.

dace.frontend.python.interface.inline(expression)

Explicitly annotates that an expression should be evaluated and inlined during parsing.

Parameters:

expression – The expression to evaluate.

Note:

Only use with stateless and compile-time evaluateable expressions!

class dace.frontend.python.interface.map

Bases: object

A Map is representation of parallel execution, containing an integer set (Python range) for which its contents are run concurrently. Written in Python as a loop with the following syntax: for i, j in dace.map[1:20, 0:N]:.

dace.frontend.python.interface.method(f, *args, auto_optimize=False, device=DeviceType.CPU, recreate_sdfg=True, regenerate_code=True, recompile=True, constant_functions=False, **kwargs)

Entry point to a data-centric program that is a method or a classmethod.

Parameters:
  • f (TypeVar(F, bound= Callable[..., Any])) – The method to define as the entry point.

  • auto_optimize – If True, applies automatic optimization heuristics on the generated DaCe program during compilation.

  • device – Transform the function to run on the target device.

  • recreate_sdfg (bool) – Whether to recreate the SDFG from the Python code. If False, the SDFG will be loaded from the cache (<build folder>/<program name>/program.sdfg) if it exists. Use this if you want to modify the SDFG after the first call to the function.

  • regenerate_code (bool) – Whether to regenerate the code from the SDFG. If False, the code in the build folder will be used if it exists. Use this if you want to modify the generated code without DaCe overriding it.

  • recompile (bool) – Whether to recompile the code. If False, the library in the build folder will be used if it exists, without recompiling it.

  • constant_functions – If True, assumes all external functions that do not depend on internal variables are constant. This will hardcode their return values into the resulting program.

Note:

If arguments are defined with type hints, the program can be compiled ahead-of-time with .compile().

Return type:

DaceProgram

dace.frontend.python.interface.nounroll(generator)

Explicitly annotates that a loop should not be unrolled during parsing.

Parameters:

generator – The original generator to loop over.

dace.frontend.python.interface.program(f, *args, auto_optimize=False, device=DeviceType.CPU, recreate_sdfg=True, regenerate_code=True, recompile=True, constant_functions=False, **kwargs)

Entry point to a data-centric program. For methods and classmethod``s, use ``@dace.method.

Parameters:
  • f (TypeVar(F, bound= Callable[..., Any])) – The function to define as the entry point.

  • auto_optimize – If True, applies automatic optimization heuristics on the generated DaCe program during compilation.

  • device – Transform the function to run on the target device.

  • recreate_sdfg (bool) – Whether to recreate the SDFG from the Python code. If False, the SDFG will be loaded from the cache (<build folder>/<program name>/program.sdfg) if it exists. Use this if you want to modify the SDFG after the first call to the function.

  • regenerate_code (bool) – Whether to regenerate the code from the SDFG. If False, the code in the build folder will be used if it exists. Use this if you want to modify the generated code without DaCe overriding it.

  • recompile (bool) – Whether to recompile the code. If False, the library in the build folder will be used if it exists, without recompiling it.

  • constant_functions – If True, assumes all external functions that do not depend on internal variables are constant. This will hardcode their return values into the resulting program.

Note:

If arguments are defined with type hints, the program can be compiled ahead-of-time with .compile().

Return type:

Callable[..., DaceProgram]

class dace.frontend.python.interface.tasklet(language=Language.Python, side_effects=False)

Bases: object

A general procedure that cannot access any memory apart from incoming and outgoing memlets. Memlets use the shift operator, an example of a tasklet is:

with dace.tasklet:
    a << A[i, j]  # Memlet going from A to a
    b = a + 5
    b >> B[i, j]  # Memlet going out of the tasklet to B

The DaCe framework cannot analyze these tasklets for optimization.

dace.frontend.python.interface.unroll(generator)

Explicitly annotates that a loop should be unrolled during parsing.

Parameters:

generator – The original generator to loop over.

Note:

Only use with stateless and compile-time evaluateable loops!

dace.frontend.python.memlet_parser module

class dace.frontend.python.memlet_parser.MemletExpr(name, accesses, wcr, subset, new_axes, arrdims)

Bases: object

accesses: Union[Basic, SymExpr]
arrdims: Dict[int, str]
name: str
new_axes: List[int]
subset: Range
wcr: Optional[AST]
dace.frontend.python.memlet_parser.ParseMemlet(visitor, defined_arrays_and_symbols, node, parsed_slice=None)
Return type:

MemletExpr

dace.frontend.python.memlet_parser.inner_eval_ast(defined, node, additional_syms=None)
dace.frontend.python.memlet_parser.parse_memlet(visitor, src, dst, defined_arrays_and_symbols)
dace.frontend.python.memlet_parser.parse_memlet_subset(array, node, das, parsed_slice=None)

Parses an AST subset and returns access range, as well as new dimensions to add.

Parameters:
  • array (Data) – Accessed data descriptor (used for filling in missing data, e.g., negative indices or empty shapes).

  • node (Union[Name, Subscript]) – AST node representing whole array or subset thereof.

  • das (Dict[str, Any]) – Dictionary of defined arrays and symbols mapped to their values.

Return type:

Tuple[Range, List[int], List[int]]

Returns:

A 3-tuple of (subset, list of new axis indices, list of index-to-array-dimension correspondence).

dace.frontend.python.memlet_parser.pyexpr_to_symbolic(defined_arrays_and_symbols, expr_ast)

Converts a Python AST expression to a DaCe symbolic expression with error checks (raises SyntaxError on failure).

Parameters:
  • defined_arrays_and_symbols (Dict[str, Any]) – Defined arrays and symbols in the context of this expression.

  • expr_ast (AST) – The Python AST expression to convert.

Returns:

Symbolic expression.

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.

Parameters:
  • ndslice – Slice or list of slices (slice objects) to loop over.

  • internal_function – Function to call in loop.

  • args – Arguments to internal_function.

  • kwargs – Keyword arguments to internal_function.

Returns:

N-dimensional loop index generator.

dace.frontend.python.ndloop.ndrange(slice_list)

Generator that creates an N-dimensional for loop in Python.

Parameters:

slice_list (Union[Tuple[slice], slice]) – Slice or list of slices (as tuples or slice objects) 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.

class dace.frontend.python.newast.ProgramVisitor(name, filename, line_offset, col_offset, global_vars, constants, scope_arrays, scope_vars, map_symbols=None, annotated_types=None, closure=None, nested=False, tmp_idx=0, simplify=None)

Bases: ExtNodeVisitor

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

create_callback(node, create_graph=True)
property defined
classmethod increment_progress(number=1)

Adds a number of parsed SDFGs to the progress bar (whether visible or not).

make_slice(arrname, rng)
parse_program(program, is_tasklet=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()

Returns the number of parsed SDFGs so far within this run.

Return type:

int

start_time: float = 0
visit(node)

Visit a node.

visit_AnnAssign(node)
visit_Assign(node)
visit_AsyncWith(node)
visit_Attribute(node)
visit_AugAssign(node)
visit_BinOp(node)
visit_BoolOp(node)
visit_Break(node)
visit_Bytes(node)
visit_Call(node, create_callbacks=False)
visit_Compare(node)
visit_Constant(node)
visit_Continue(node)
visit_Dict(node)
visit_ExtSlice(node)
Return type:

Any

visit_For(node)
visit_FunctionDef(node)
visit_If(node)
visit_Index(node)
Return type:

Any

visit_Lambda(node)
visit_List(node)
visit_Name(node)
visit_NameConstant(node)
visit_NamedExpr(node)
visit_Num(node)
visit_Return(node)
visit_Set(node)
visit_Str(node)
visit_Subscript(node, inference=False)
visit_TopLevelExpr(node)
visit_Tuple(node)
visit_TypeAlias(node)
visit_UnaryOp(node)
visit_While(node)
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, state, filename, lang=None, location={}, nested=False, scope_arrays={}, scope_vars={}, variables={}, accesses={}, symbols={})

Bases: ExtNodeTransformer

A visitor that traverses a data-centric tasklet, removes memlet annotations and returns input and output memlets.

parse_tasklet(tasklet_ast, name=None)

Parses the AST of a tasklet and returns the tasklet node, as well as input and output memlets.

Parameters:
  • tasklet_ast (Union[FunctionDef, With, For]) – The Tasklet’s Python AST to parse.

  • name (Optional[str]) – Optional name to use as prefix for tasklet.

Returns:

3-tuple of (Tasklet node, input memlets, output memlets).

visit_Call(node)
Return type:

Any

visit_Name(node)
visit_TopLevelExpr(node)
visit_TopLevelStr(node)
dace.frontend.python.newast.add_indirection_subgraph(sdfg, graph, src, dst, memlet, local_name, pvisitor, output=False, with_wcr=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, preprocessed_ast, argtypes, constants, closure, simplify=None, save=True, progress=None)

Parses a @dace.program function into an SDFG.

Parameters:
  • src_ast – The AST of the Python program to parse.

  • visitor – A ProgramVisitor object returned from preprocess_dace_program.

  • closure (SDFGClosure) – An object that contains the @dace.program closure.

  • simplify (Optional[bool]) – If True, simplification pass will be performed.

  • save (bool) – If True, saves source mapping data for this SDFG.

  • progress (Optional[bool]) – If True, prints a progress bar of the parsing process. If None (default), prints after 5 seconds of parsing. If False, never prints progress.

Return type:

SDFG

Returns:

A 2-tuple of SDFG and its reduced (used) closure.

dace.frontend.python.newast.specifies_datatype(func, 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, recreate_sdfg=True, regenerate_code=True, recompile=True, method=False)

Bases: SDFGConvertible

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

auto_optimize(sdfg, symbols=None)

Invoke automatic optimization heuristics on internal program.

Return type:

SDFG

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.

Parameters:
  • constant_args – Arguments whose values are already resolved to compile-time values.

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

Returns the program’s hash (cache key) given the arguments and the program’s closure.

Parameters:
  • args – Arguments that the SDFG will be called with.

  • kwargs – Keyword arguments that the SDFG will be called with.

Return type:

ProgramCacheKey

Returns:

A hashable program cache key object.

load_precompiled_sdfg(path, *args, **kwargs)

Loads an external compiled SDFG object that will be invoked when the function is called.

Parameters:
  • path (str) – Path to SDFG build folder (e.g., “.dacecache/program”). Path has to include program.sdfg and the binary shared object under the build folder.

  • args – Optional compile-time arguments.

  • kwargs – Optional compile-time keyword arguments.

Return type:

None

load_sdfg(path, *args, **kwargs)

Loads an external SDFG that will be used when the function is called.

Parameters:
  • path (str) – Path to SDFG file.

  • args – Optional compile-time arguments.

  • kwargs – Optional compile-time keyword arguments.

property methodobj: Any
property name: str

Returns a unique name for this program.

to_sdfg(*args, simplify=None, save=False, validate=False, use_cache=False, **kwargs)

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.

Return type:

SDFG

Returns:

An SDFG object that can be transformed, saved, or called.

validate: bool

Whether to validate on code generation

dace.frontend.python.parser.infer_symbols_from_datadescriptor(sdfg, args, exclude=None)

Infers the values of SDFG symbols (not given as arguments) from the shapes and strides of input arguments (e.g., arrays).

Parameters:
  • sdfg (SDFG) – The SDFG that is being called.

  • args (Dict[str, Any]) – A dictionary mapping from current argument names to their values. This may also include symbols.

  • exclude (Optional[Set[str]]) – An optional set of symbols to ignore on inference.

Return type:

Dict[str, Any]

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

class dace.frontend.python.preprocessing.ArrayClosureResolver(closure)

Bases: NodeVisitor

visit_Name(node)
class dace.frontend.python.preprocessing.AugAssignExpander

Bases: NodeTransformer

visit_AugAssign(node)
Return type:

Assign

class dace.frontend.python.preprocessing.CallTreeResolver(closure, globals)

Bases: NodeVisitor

visit_Call(node)
class dace.frontend.python.preprocessing.ConditionalCodeResolver(globals)

Bases: NodeTransformer

Replaces if conditions by their bodies if can be evaluated at compile time.

visit_If(node)
Return type:

Any

visit_IfExp(node)
Return type:

Any

visit_Name(node)
class dace.frontend.python.preprocessing.ContextManagerInliner(globals, filename, closure_resolver)

Bases: NodeTransformer, ASTHelperMixin

Since exceptions are disabled in dace programs, AST with with statements can be replaced with appropriate calls to the __enter__/__exit__ calls in the right places, i.e., at the end of the body or when the context is left due to a return statement, or top-level break/continue statements.

visit_AsyncFor(node)
visit_AsyncWith(node)
visit_Break(node)
visit_Continue(node)
visit_For(node)
visit_Return(node)
visit_While(node)
visit_With(node)
exception dace.frontend.python.preprocessing.DaceRecursionError(fid)

Bases: Exception

Exception that indicates a recursion in a data-centric parsed context. The exception includes the id of the topmost function as a stopping condition for parsing.

class dace.frontend.python.preprocessing.DeadCodeEliminator

Bases: NodeTransformer

Removes any code within scope after return/break/continue/raise.

generic_visit(node)

Called if no explicit visitor function exists for a node.

class dace.frontend.python.preprocessing.DisallowedAssignmentChecker(filename)

Bases: NodeVisitor

Tests a pre-processed program for disallowed assignments to compile-time constants, and raises a DaceSyntaxError exception if one is found.

visit_AnnAssign(node)
visit_Assign(node)
visit_AugAssign(node)
visit_Call(node)
visit_NamedExpr(node)
class dace.frontend.python.preprocessing.ExpressionInliner(globals, filename, closure_resolver)

Bases: NodeTransformer

Replaces dace.inline() expressions by their bodies if they can be compile-time evaluated.

visit_Call(node)
Return type:

Any

class dace.frontend.python.preprocessing.GlobalResolver(globals, resolve_functions=False, default_args=None)

Bases: ExtNodeTransformer, ASTHelperMixin

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

generic_visit(node)

Called if no explicit visitor function exists for a node.

global_value_to_node(value, parent_node, qualname, recurse=False, detect_callables=False, keep_object=False)
property globals
visit_Assert(node)
Return type:

Any

visit_AsyncFunctionDef(node)
Return type:

Any

visit_Attribute(node)
Return type:

Any

visit_AugAssign(node)
visit_Call(node)
Return type:

Any

visit_For(node)
visit_FunctionDef(node)
Return type:

Any

visit_JoinedStr(node)
Return type:

Any

visit_Lambda(node)
Return type:

Any

visit_Name(node)
visit_Raise(node)
Return type:

Any

visit_Subscript(node)
Return type:

Any

visit_TopLevelExpr(node)
visit_keyword(node)
class dace.frontend.python.preprocessing.LoopUnroller(globals, filename, closure_resolver)

Bases: NodeTransformer

Replaces loops by their unrolled bodies if generator can be evaluated at compile time and one of the following conditions apply:

  1. dace.unroll was explicitly called

  2. looping over compile-time constant tuples/lists/dictionaries

  3. generator is one of the predetermined “stateless generators”

  4. any generator with compile-time size that is lower than the “unroll_threshold” configuration

STATELESS_GENERATORS = [<class 'enumerate'>, <class 'zip'>, <class 'reversed'>, <method 'values' of 'dict' objects>, <method 'keys' of 'dict' objects>, <method 'items' of 'dict' objects>]
THRESHOLD_GENERATORS = [<class 'range'>]
visit_AsyncFor(node)
Return type:

Any

visit_For(node)
Return type:

Any

class dace.frontend.python.preprocessing.MPIResolver(globals)

Bases: NodeTransformer

Resolves mpi4py-related constants, e.g., mpi4py.MPI.COMM_WORLD.

visit(node)

Visit a node.

visit_Attribute(node)
Return type:

Attribute

visit_Name(node)
Return type:

Union[Name, Attribute]

class dace.frontend.python.preprocessing.ModuleResolver(modules, always_replace=False)

Bases: NodeTransformer

visit_Attribute(node)
visit_Call(node)
Return type:

Any

class dace.frontend.python.preprocessing.ModuloConverter

Bases: NodeTransformer

Converts a % b expressions to (a + b) % b for C/C++ compatibility.

visit_BinOp(node)
Return type:

BinOp

class dace.frontend.python.preprocessing.PreprocessedAST(filename, src_line, src, preprocessed_ast, program_globals)

Bases: object

Python AST and metadata of a preprocessed @dace.program/method, for use in parsing.

filename: str
preprocessed_ast: AST
program_globals: Dict[str, Any]
src: str
src_line: int
class dace.frontend.python.preprocessing.RewriteSympyEquality(globals)

Bases: NodeTransformer

Replaces symbolic equality checks by sympy.{Eq,Ne}. This is done because a test if x == 0 where x is a symbol would result in False, even in indeterminate cases.

visit_Compare(node)
Return type:

Any

visit_Constant(node)
visit_Num(node)
class dace.frontend.python.preprocessing.StructTransformer(gvars)

Bases: NodeTransformer

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

visit_Call(node)
dace.frontend.python.preprocessing.find_disallowed_statements(node)
dace.frontend.python.preprocessing.flatten_callback(func, node, global_vars)

Creates a version of the function that has only marshallable arguments and no keyword arguments. Arguments in callback matches the number of arguments used exactly. Used for creating callbacks from C to Python with keyword arguments or other Pythonic structures (such as literal lists).

dace.frontend.python.preprocessing.has_replacement(callobj, parent_object=None, node=None)

Returns True if the function/operator replacement repository has a registered replacement for the called function described by a live object.

Return type:

bool

dace.frontend.python.preprocessing.preprocess_dace_program(f, argtypes, global_vars, modules, resolve_functions=False, parent_closure=None, default_args=None)

Preprocesses a @dace.program and all its nested functions, returning a preprocessed AST object and the closure of the resulting SDFG.

Parameters:
  • f (Callable[..., Any]) – A Python function to parse.

  • argtypes (Dict[str, Data]) – An dictionary of (name, type) for the given function’s arguments, which may pertain to data nodes or symbols (scalars).

  • global_vars (Dict[str, Any]) – A dictionary of global variables in the closure of f.

  • modules (Dict[str, Any]) – A dictionary from an imported module name to the module itself.

  • constants – A dictionary from a name to a constant value.

  • resolve_functions (bool) – If True, treats all global functions defined outside of the program as returning constant values.

  • parent_closure (Optional[SDFGClosure]) – If not None, represents the closure of the parent of the currently processed function.

  • default_args (Optional[Set[str]]) – If not None, defines a list of unspecified default arguments.

Return type:

Tuple[PreprocessedAST, SDFGClosure]

Returns:

A 2-tuple of the AST and its reduced (used) closure.

dace.frontend.python.replacements module

dace.frontend.python.replacements.dot(pv, sdfg, state, op_a, op_b, op_out=None)
dace.frontend.python.replacements.eye(pv, sdfg, state, N, M=None, k=0, dtype=double)
dace.frontend.python.replacements.flat(pv, sdfg, state, arr, order=StringLiteral(value='C'))
Return type:

str

dace.frontend.python.replacements.implement_ufunc(visitor, ast_node, sdfg, state, ufunc_name, args, kwargs)

Implements a NumPy ufunc.

Parameters:
  • visitor (ProgramVisitor) – ProgramVisitor object handling the ufunc call

  • ast_node (Call) – AST node corresponding to the ufunc call

  • sdfg (SDFG) – SDFG object

  • state (SDFGState) – SDFG State object

  • ufunc_name (str) – Name of the ufunc

  • args (Sequence[Union[str, Number, Basic]]) – Positional arguments of the ufunc call

  • kwargs (Dict[str, Any]) – Keyword arguments of the ufunc call

Raises:

DaCeSyntaxError – When validation fails

Return type:

List[Optional[str]]

Returns:

List of output datanames

dace.frontend.python.replacements.implement_ufunc_accumulate(visitor, ast_node, sdfg, state, ufunc_name, args, kwargs)

Implements the ‘accumulate’ method of a NumPy ufunc.

Parameters:
  • visitor (ProgramVisitor) – ProgramVisitor object handling the ufunc call

  • ast_node (Call) – AST node corresponding to the ufunc call

  • sdfg (SDFG) – SDFG object

  • state (SDFGState) – SDFG State object

  • ufunc_name (str) – Name of the ufunc

  • args (Sequence[Union[str, Number, Basic]]) – Positional arguments of the ufunc call

  • kwargs (Dict[str, Any]) – Keyword arguments of the ufunc call

Raises:

DaCeSyntaxError – When validation fails

Return type:

List[Optional[str]]

Returns:

List of output datanames

dace.frontend.python.replacements.implement_ufunc_outer(visitor, ast_node, sdfg, state, ufunc_name, args, kwargs)

Implements the ‘outer’ method of a NumPy ufunc.

Parameters:
  • visitor (ProgramVisitor) – ProgramVisitor object handling the ufunc call

  • ast_node (Call) – AST node corresponding to the ufunc call

  • sdfg (SDFG) – SDFG object

  • state (SDFGState) – SDFG State object

  • ufunc_name (str) – Name of the ufunc

  • args (Sequence[Union[str, Number, Basic]]) – Positional arguments of the ufunc call

  • kwargs (Dict[str, Any]) – Keyword arguments of the ufunc call

Raises:

DaCeSyntaxError – When validation fails

Return type:

List[Optional[str]]

Returns:

List of output datanames

dace.frontend.python.replacements.implement_ufunc_reduce(visitor, ast_node, sdfg, state, ufunc_name, args, kwargs)

Implements the ‘reduce’ method of a NumPy ufunc.

Parameters:
  • visitor (ProgramVisitor) – ProgramVisitor object handling the ufunc call

  • ast_node (Call) – AST node corresponding to the ufunc call

  • sdfg (SDFG) – SDFG object

  • state (SDFGState) – SDFG State object

  • ufunc_name (str) – Name of the ufunc

  • args (Sequence[Union[str, Number, Basic]]) – Positional arguments of the ufunc call

  • kwargs (Dict[str, Any]) – Keyword arguments of the ufunc call

Raises:

DaCeSyntaxError – When validation fails

Return type:

List[Optional[str]]

Returns:

List of output datanames

dace.frontend.python.replacements.normalize_axes(axes, max_dim)

Normalize a list of axes by converting negative dimensions to positive.

Parameters:
  • dims – the list of dimensions, possibly containing negative ints.

  • max_dim (int) – the total amount of dimensions.

Return type:

List[int]

Returns:

a list of dimensions containing only positive ints.

dace.frontend.python.replacements.reshape(pv, sdfg, state, arr, newshape, order=StringLiteral(value='C'))
Return type:

str

dace.frontend.python.replacements.size(pv, sdfg, state, arr)
Return type:

Union[int, symbol]

dace.frontend.python.replacements.view(pv, sdfg, state, arr, dtype, type=None)
Return type:

str

dace.frontend.python.tasklet_runner module

A Python runner for DaCe tasklets. Used to execute with dace.tasklet statements.

class dace.frontend.python.tasklet_runner.TaskletRewriter

Bases: ExtNodeTransformer

Rewrites memlet expressions in tasklets such that the code can run in pure Python. This means placing all the reads before computations, writes after computations, handling write-conflict resolution, and dynamic memlets.

clear_statements()
rewrite_tasklet(node)
Return type:

Module

visit_Assign(node)
visit_Name(node)
visit_TopLevelExpr(node)
dace.frontend.python.tasklet_runner.get_tasklet_ast(stack_depth=2, frame=None)

Returns the AST of the contents of the statement that called this function.

Parameters:
  • stack_depth – If frame is None, how many levels up to go in the stack, default: 2 (with statement->``__enter__``->``get_tasklet_ast``).

  • frame – An optional argument specifying the frame, in order to avoid inspecting it twice.

Return type:

With

Returns:

The AST of the body of the statement, or a FileNotFoundError if the AST cannot be recovered.

dace.frontend.python.tasklet_runner.run_tasklet(tasklet_ast, filename, gvars, lvars)

Transforms and runs a tasklet given by its AST, filename, global, and local variables.

Parameters:
  • tasklet_ast (With) – AST of the “with dace.tasklet” statement.

  • filename (str) – A string representing the originating filename or code snippet (IPython, Jupyter) of the tasklet, used for debuggers to attach to.

  • gvars (Dict[str, Any]) – Globals defined at the calling point of the tasklet.

  • lvars (Dict[str, Any]) – Locals defined at the calling point of the tasklet.

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 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_array(dtype, shape)

Bases: Generic[T]

Stream array object in Python.

property shape

Module contents