ml_switcheroo.core.rewriter.patcher =================================== .. py:module:: ml_switcheroo.core.rewriter.patcher .. autoapi-nested-parse:: 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 ------- .. autoapisummary:: ml_switcheroo.core.rewriter.patcher.PatchAction ml_switcheroo.core.rewriter.patcher.DeleteAction ml_switcheroo.core.rewriter.patcher.ReplaceAction ml_switcheroo.core.rewriter.patcher.GraphPatcher Module Contents --------------- .. py:class:: PatchAction Base class for patch instructions. .. py:attribute:: node_id :type: str .. py:class:: DeleteAction Bases: :py:obj:`PatchAction` Instruction to remove a node from the AST. Used for nodes that have been fused into others or pruned. .. py:class:: ReplaceAction Bases: :py:obj:`PatchAction` Instruction 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__`. .. attribute:: new_node The logical definition of the new component. .. attribute:: input_vars List of variable names to pass as arguments. .. attribute:: output_var The variable name to assign result to. .. attribute:: is_init If True, uses `emit_init` logic (assignments). If False, uses `emit_call` logic (executions). .. py:attribute:: new_node :type: ml_switcheroo.compiler.ir.LogicalNode .. py:attribute:: input_vars :type: List[str] :value: [] .. py:attribute:: output_var :type: str :value: 'x' .. py:attribute:: is_init :type: bool :value: False .. py:class:: GraphPatcher(plan: List[PatchAction], provenance: Dict[str, libcst.CSTNode], emitter: ml_switcheroo.compiler.backends.python_snippet.PythonSnippetEmitter) Bases: :py:obj:`libcst.CSTTransformer` LibCST 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. .. py:attribute:: plan .. py:attribute:: provenance .. py:attribute:: emitter .. py:method:: leave_Assign(original_node: libcst.Assign, updated_node: libcst.Assign) -> Union[libcst.Assign, libcst.SimpleStatementLine, libcst.RemovalSentinel] Intercepts Assignment statements (e.g. `self.conv = ...`, `y = func(x)`). .. py:method:: leave_Expr(original_node: libcst.Expr, updated_node: libcst.Expr) -> Union[libcst.Expr, libcst.SimpleStatementLine, libcst.RemovalSentinel] Intercepts Expression statements (e.g. `func(x)` without assignment). .. py:method:: leave_Call(original_node: libcst.Call, updated_node: libcst.Call) -> Union[libcst.Call, libcst.BaseExpression, libcst.RemovalSentinel] .. py:method:: leave_SimpleStatementLine(original_node: libcst.SimpleStatementLine, updated_node: libcst.SimpleStatementLine) -> Union[libcst.SimpleStatementLine, libcst.RemovalSentinel] Cleans up statements that became empty due to children deletion.