Session

Algorithms for traversing, solving and evaluating computation graphs

paragraph.session.eager_mode()

Activate eager mode within a context manager.

In eager mode, the method Op.op is replaced with the direct invocation of the underlying method Op._run. In this mode, no variable is emitted, allowing to test a computation graph without ever calling session.evaluate.

paragraph.session.traverse_fw(output)

Returns a generator implementing a forward traversal of the computation subgraph leading to var.

The generator returned guarantees that every dependent variable occurs after all its dependencies upon iterating, whence the name forward traversal. When generated in this order, variables can be simply evaluated in turn: at each iteration, all dependencies of the current variable will have been evaluated already.

Parameters:output (Iterable[Variable]) – The variables whose dependencies should be traversed.
Yields:All dependencies of the output variables, each variable yielded occurring before the variables depending thereupon.
Raises:ValueError – If a cyclic dependency is detected in the graph.
Return type:Generator[Variable, None, None]
paragraph.session.evaluate(output, args, executor=None)

Evaluate the specified output variable.

The argument values provided through args should be:
  • of the type expected by the operations consuming the variable,
  • of type concurrent.futures.Future, in which case the result will be awaited by consuming ops. The result should be of the expected type.

Support of arguments values of type Variable will be dropped in version 2.0 and a DeprecationWarning will be issued. The same applies if any input variable required to evaluate the output is left uninitialized.

Parameters:
  • output (Iterable[Variable]) – The variables to evaluate.
  • args (Dict[Variable, Any]) – Initialization of the input variables, none of which should have dependencies.
  • executor (Optional[Executor]) – An instance of concurrent.futures.Executor, to which op evaluations are submitted. If None, the default, evaluation proceeds sequentially.
Return type:

List

Returns:

A list of values of the same size as output. The entry at index i is the computed value of output[i].

Raises:

ValueError – If a variable in args is not an input variable. In this case, the consistency of the results cannot be guaranteed.

paragraph.session.solve(output, args, executor=None)

Resolve the specified output variables.

The argument values provided through args should be:
  • of the type expected by the operations consuming the variable,
  • of type Variable, in which case it should evaluate to the above type,
  • of type concurrent.futures.Future, in which case the result will be awaited by consuming ops. The result should be of either above types.
Parameters:
  • output (Iterable[Variable]) – The variables to evaluate.
  • args (Dict[Variable, Any]) – Initialization of the input variables, none of which should have dependencies.
  • executor (Optional[Executor]) – An instance of concurrent.futures.Executor, to which op evaluations are submitted. If None, the default, evaluation proceeds sequentially.
Return type:

List

Returns:

A list of variables of the same size as output. The entry at index i is the resolved variable for output[i].

Raises:

ValueError – If a variable in args is not an input variable. In this case, the consistency of the results cannot be guaranteed.

paragraph.session.apply(output, args, iter_args, executor=None)

Iterate the evaluation of a set of output variables over input arguments.

This function accepts two types of arguments: args receives static arguments, using which a first evaluation of the output variables is executed; then, iter_args receives an iterable over input arguments, which are iterated over to resolve the output variables left unresolved after the first evaluation. The values of the output variables obtained after each iteration are then yielded. See evaluate() for the constraints bearing on the types of the values provided in both args and iter_args.

Parameters:
  • output (List[Variable]) – The variables to evaluate.
  • args (Dict[Variable, Any]) – A dictionary mapping input variables onto input values.
  • iter_args (Iterable[Dict[Variable, Any]]) – An iterable over dictionaries mapping input variables onto input values.
  • executor (Optional[Executor]) – An instance of concurrent.futures.Executor to which op evaluations are submitted. If None (the default), evaluation proceeds sequentially.
Yields:

A list of values of the same size as output. The entry at index i is the computed value of output[i].

Raises:

ValueError – If a dynamic argument assigns a value to a variable appearing in static arguments, as proceeding would produce inconsistent results.

Return type:

Generator[List[Any], None, None]

paragraph.session.traverse_bw(output)

Returns a generator implementing a backward traversal of var’s transitive dependencies.

This generator guarantees that a variable is yielded after all its usages.

Note

If var is in the boundary, the generator exits without yielding any variable.

Parameters:output (List[Variable]) – The variables whose transitive dependencies should be explored.
Yields:All dependencies of the output variables (stopping at the boundary), each variable is yielded after all variables depending thereupon.
Return type:Generator[Variable, None, None]
paragraph.session.solve_requirements(output_requirements)

Backward propagate requirements from the output variables to their transitive dependencies

Parameters:output_requirements (Dict[Variable, Requirement]) – the requirements to be fulfilled on output
Return type:Dict[Variable, Requirement]
Returns:A dictionary mapping all transitive dependencies of output onto their resolved requirement dictionaries