Skip to content

llmcompressor.pipelines.sequential.ast_utils.name_analyzer

Classes:

  • NameAnalyzer

    Determines the unbound, assigned, and conditionally assigned names associated with

NameAnalyzer

NameAnalyzer(omit: set[str])

Bases: NodeVisitor

Determines the unbound, assigned, and conditionally assigned names associated with a piece of code. This information is used to determine the arguments and return values of the wrapper function

For example, for the following piece of code

b = a + 1
if some_condition:
    c = 5

a is unbound, meaning that it must be an input of wrapper function b is assigned, meaning that it must be an output of the wrapper function c is conditionally assigned, meaning that it must be an output of the wrapper function, and might be an input iff c already exists in the namespace

Note that names which are assigned to before being read are not considered unbound

a = 2  # no longer unbound
b = a + 1

Methods:

  • analyze

    Analyzes the use of names in the given piece of code

Source code in llmcompressor/pipelines/sequential/ast_utils/name_analyzer.py
def __init__(self, omit: set[str]):
    self._omit = builtins.__dict__.keys() | omit

analyze

analyze(node: AST) -> tuple[set[str], set[str], set[str]]

Analyzes the use of names in the given piece of code

Parameters:

  • node

    (AST) –

    code to analyze return: tuple of unbound names, assigned names, and conditionally assigned names

Source code in llmcompressor/pipelines/sequential/ast_utils/name_analyzer.py
def analyze(self, node: ast.AST) -> tuple[set[str], set[str], set[str]]:
    """
    Analyzes the use of names in the given piece of code

    :param node: code to analyze
    return: tuple of unbound names, assigned names, and conditionally assigned names
    """
    self._unbound = set()
    self._assigned = set()
    self._conditionally_assigned = set()
    self.visit(node)

    return self._unbound, self._assigned, self._conditionally_assigned