Skip to content

llmcompressor.pipelines.sequential.ast_utils.control_flow_analyzer

Classes:

  • ControlFlowAnalyzer

    Used to determine if a piece of code can be wrapped into a function. Includes any

ControlFlowAnalyzer

Bases: NodeVisitor

Used to determine if a piece of code can be wrapped into a function. Includes any code which includes return, continue, break, await, or yield without the proper context.

For example, this code can be wrapped:

while True:
    if some_condition:
        break

Whereas the inner code cannot be wrapped without the while context

def wrapped():
    if some_condition:
        break  # this control statement is now invalid

while True:
    wrapped()

Methods:

  • is_valid

    Returns False if a node contains control statements that are not in their

is_valid

is_valid(node: AST) -> bool

Returns False if a node contains control statements that are not in their proper control flow context

Parameters:

  • node

    (AST) –

    code to analyze

Returns:

  • bool

    True iff the code does not contain invalid control statements

Source code in llmcompressor/pipelines/sequential/ast_utils/control_flow_analyzer.py
def is_valid(self, node: ast.AST) -> bool:
    """
    Returns False if a node contains control statements that are not in their
    proper control flow context

    :param node: code to analyze
    :return: True iff the code does not contain invalid control statements
    """
    self._contexts = []
    self._is_valid = True
    self.visit(node)
    return self._is_valid