ml_switcheroo.compiler.backends.python¶

Python Source Code Backend.

This module implements a Compiler Backend that synthesizes Python source code from the Logical Graph Internal Representation via LibCST.

Classes¶

ClassBodyReplacer

Transformer to swap __init__ and forward methods in a target class.

PythonBackend

Synthesizes a Python CST Module from a LogicalGraph.

Module Contents¶

class ml_switcheroo.compiler.backends.python.ClassBodyReplacer(target_class: str, new_init: libcst.FunctionDef, new_forward: libcst.FunctionDef)[source]¶

Bases: libcst.CSTTransformer

Transformer to swap __init__ and forward methods in a target class.

target_class¶
new_init¶
new_forward¶
found = False¶
leave_ClassDef(original_node: libcst.ClassDef, updated_node: libcst.ClassDef) → libcst.ClassDef[source]¶
class ml_switcheroo.compiler.backends.python.PythonBackend(framework: str = 'torch', semantics: Any = None)[source]¶

Bases: ml_switcheroo.compiler.backend.CompilerBackend

Synthesizes a Python CST Module from a LogicalGraph.

framework = 'torch'¶
compile(graph: ml_switcheroo.compiler.ir.LogicalGraph) → str[source]¶

Compiles the Logical Intermediate Representation (IR) into a target artifact.

Parameters:

graph (LogicalGraph) – The intermediate representation of the model structure.

Returns:

The compiled output (e.g., source code string, binary buffer, or AST).

Return type:

Any

generate(graph: ml_switcheroo.compiler.ir.LogicalGraph, class_name: str = 'SwitcherooNet', original_tree: libcst.Module | None = None) → str[source]¶