elasticai.creator.ir_transforms#

Package Contents#

Classes#

Functions#

move_pattern_to_subimpls

Move all occurences of pattern into sub implementations.

build_sequential_pattern

API#

class elasticai.creator.ir_transforms.SequenceReorderer(old_order: collections.abc.Sequence[elasticai.creator.ir.Node], new_order: collections.abc.Sequence[elasticai.creator.ir.Node], node_constraint: elasticai.creator.ir_transforms.reorder.NodeConstraint[elasticai.creator.ir.Node, elasticai.creator.ir.Node])[source]#

Initialization

Use this to reorder the first occurence of a pattern as specified by the replacement.

has_changed() bool[source]#

Tells whether the graph has been changed by the last call to reorder.

reorder(impl: elasticai.creator.ir.Implementation) elasticai.creator.ir.Implementation[source]#

Reorder the first occurence of the pattern as specified by the replacement.

The resulting graph and its data are deep copies of the input graph and its data. The new node names are unique but chosen based on the replacement nodes and not based on the original node names. Use has_changed to check whether the graph has been changed by the last call to reorder if you want to keep reordering until you reach a fixed point.

Note

Will create a deep copy even if no changes were made. As such it is always safe to del original_graph after calling reorder.

class elasticai.creator.ir_transforms.NodeConstraint[source]#

Bases: typing.Protocol[elasticai.creator.ir_transforms.reorder.PNodeCon, elasticai.creator.ir_transforms.reorder.GNodeCon]

__call__(*, pattern_node: elasticai.creator.ir_transforms.reorder.PNodeCon, graph_node: elasticai.creator.ir_transforms.reorder.GNodeCon) bool[source]#
elasticai.creator.ir_transforms.move_pattern_to_subimpls(original: elasticai.creator.ir.Implementation, pattern: elasticai.creator.ir.Implementation[elasticai.creator.ir_transforms.move_to_submodules.PatternNode, elasticai.creator.ir.Edge], basename: str, replacement_data_fn: collections.abc.Callable[[elasticai.creator.ir.Implementation], dict[str, elasticai.creator.ir.Attribute]], extracted_data_fn: collections.abc.Callable[[elasticai.creator.ir.Implementation], dict[str, dict[str, elasticai.creator.ir.Attribute]]] = lambda _: ...) list[elasticai.creator.ir.Implementation][source]#

Move all occurences of pattern into sub implementations.

Each pattern match will be replaced by a single node with a name derived from the basename. For each match, we build a new implementation with the nodes and edges of the match.

Parameters:
  • original – the original implementation

  • pattern – Each node has a set of types that will be used as match constraints. Use “any” to match any type. The special node names "start" and "end" are considered interface nodes and will not be replaced.

  • basename – the base name for the new nodes

  • replacement_data_fn – a function that takes the matched implementation and returns a dictionary of attributes for the newly created node.

  • extracted_data_fn – a function that takes the matched implementation and returns a dictionary of node names and their data for the extracted implementation. Use the names from the pattern to identify nodes.

elasticai.creator.ir_transforms.build_sequential_pattern(*pattern: tuple[str, set[str]]) elasticai.creator.ir.Implementation[elasticai.creator.ir_transforms.move_to_submodules.PatternNode, elasticai.creator.ir.Edge][source]#
class elasticai.creator.ir_transforms.PatternNode(name: str, data: dict[str, elasticai.creator.ir.base.Attribute])[source]#

Bases: elasticai.creator.ir.Node

types: set[str]#

None