ml_switcheroo.core.rewriter.patcher¶
Graph Patcher for AST Surgery.
This module implements the execution phase of the Graph-Guided Rewriting pipeline.
It applies a set of topological mutations (Deletions, Replacements) generated
by the GraphDiffer to the concrete syntax tree (CST) of the source code.
It bridges the gap between the Abstract Logical Graph and the physical source code by using Provenance Tracking to locate the exact CST nodes corresponding to graph nodes, and the Snippet Emitter to generate valid replacement code.
Classes¶
Base class for patch instructions. |
|
Instruction to remove a node from the AST. |
|
Instruction to replace an existing node with a new code snippet. |
|
LibCST Transformer that applies graph optimizations to source code. |
Module Contents¶
- class ml_switcheroo.core.rewriter.patcher.PatchAction¶
Base class for patch instructions.
- node_id: str¶
- class ml_switcheroo.core.rewriter.patcher.DeleteAction¶
Bases:
PatchActionInstruction to remove a node from the AST. Used for nodes that have been fused into others or pruned.
- class ml_switcheroo.core.rewriter.patcher.ReplaceAction¶
Bases:
PatchActionInstruction to replace an existing node with a new code snippet.
Commonly used for: 1. Replacing a sequence anchor (e.g. Conv2d) with a Fused Block. 2. Swapping layer definitions in __init__.
- new_node¶
The logical definition of the new component.
- input_vars¶
List of variable names to pass as arguments.
- output_var¶
The variable name to assign result to.
- is_init¶
If True, uses emit_init logic (assignments). If False, uses emit_call logic (executions).
- new_node: ml_switcheroo.compiler.ir.LogicalNode¶
- input_vars: List[str] = []¶
- output_var: str = 'x'¶
- is_init: bool = False¶
- class ml_switcheroo.core.rewriter.patcher.GraphPatcher(plan: List[PatchAction], provenance: Dict[str, libcst.CSTNode], emitter: ml_switcheroo.compiler.backends.python_snippet.PythonSnippetEmitter)¶
Bases:
libcst.CSTTransformerLibCST Transformer that applies graph optimizations to source code.
It builds a reverse-lookup map of the Provenance Registry to identify CST nodes by object identity, then checks against the Transformation Plan during traversal to apply Deletes or Replacements.
- plan¶
- provenance¶
- emitter¶
- leave_Assign(original_node: libcst.Assign, updated_node: libcst.Assign) libcst.Assign | libcst.SimpleStatementLine | libcst.RemovalSentinel¶
Intercepts Assignment statements (e.g. self.conv = …, y = func(x)).
- leave_Expr(original_node: libcst.Expr, updated_node: libcst.Expr) libcst.Expr | libcst.SimpleStatementLine | libcst.RemovalSentinel¶
Intercepts Expression statements (e.g. func(x) without assignment).
- leave_Call(original_node: libcst.Call, updated_node: libcst.Call) libcst.Call | libcst.BaseExpression | libcst.RemovalSentinel¶
- leave_SimpleStatementLine(original_node: libcst.SimpleStatementLine, updated_node: libcst.SimpleStatementLine) libcst.SimpleStatementLine | libcst.RemovalSentinel¶
Cleans up statements that became empty due to children deletion.