llmcompressor.pipelines.sequential.ast_utils
Modules:
Classes:
-
AutoWrapper–Automatically wraps untracable code according to the following patterns:
-
ControlFlowAnalyzer–Used to determine if a piece of code can be wrapped into a function. Includes any
-
NameAnalyzer–Determines the unbound, assigned, and conditionally assigned names associated with
AutoWrapper
Bases: NodeTransformer
Automatically wraps untracable code according to the following patterns:
The following patterns are automatically wrapped 1. If statements whose conditions cannot be statically evaluated 2. Ignored functions (_update_causal_mask) 3. Starred tuple unpacking 4. Starred argument unpacking
See also: https://github.com/vllm-project/llm-compressor/pull/1411
Methods:
-
auto_wrap–Modify ast by automatically wrapping any untraceable code segments. Segments to
-
visit_Call–Wrap any functions which use (4) variadic arguments or (2) match the ignore list
-
visit_Delete–Remove any deleted names from
self._local_names, -
visit_FunctionDef–Remove decorators which prevent forward function recompilation
-
visit_If–Attempt to statically evaluate the condition of the
ifstatement. If the -
visit_Name–Add any new names in
self._local_names, -
visit_Tuple–(3) Wrap any tuples which use starred unpacking
Source code in llmcompressor/pipelines/sequential/ast_utils/auto_wrapper.py
auto_wrap
Modify ast by automatically wrapping any untraceable code segments. Segments to wrap are determined through analysis of the code and basic pattern matching
Parameters:
-
(treeModule) –module containing a definition to an original forward function
Returns:
-
Module–module with added wrapper function definitions and function calls
Source code in llmcompressor/pipelines/sequential/ast_utils/auto_wrapper.py
visit_Call
Wrap any functions which use (4) variadic arguments or (2) match the ignore list
Source code in llmcompressor/pipelines/sequential/ast_utils/auto_wrapper.py
visit_Delete
Remove any deleted names from self._local_names, which are used to determine function wrapper arguments
Source code in llmcompressor/pipelines/sequential/ast_utils/auto_wrapper.py
visit_FunctionDef
Remove decorators which prevent forward function recompilation For example, add_start_docstrings_to_model_forward
Because _wrapper_fn_defs are appended after visit finishes, this function will not affect wrapper functions
Parameters:
-
(nodeFunctionDef) –function definition whose decorators will be stripped
Returns:
-
FunctionDef–function definition without decorators
Source code in llmcompressor/pipelines/sequential/ast_utils/auto_wrapper.py
visit_If
Attempt to statically evaluate the condition of the if statement. If the condition can not be statically evaluated (1), then attmept to wrap the if statement
Parameters:
-
(nodeIf) –ifstatement which may be wrapped
Returns:
-
If | Assign–if the
ifstatement cannot be statically evaluated, return theifstatement with the condition replaced byTrueorFalse. Otherwise, return a wrapper function call
Source code in llmcompressor/pipelines/sequential/ast_utils/auto_wrapper.py
visit_Name
Add any new names in self._local_names, which are used to determine function wrapper arguments
Source code in llmcompressor/pipelines/sequential/ast_utils/auto_wrapper.py
visit_Tuple
(3) Wrap any tuples which use starred unpacking
Source code in llmcompressor/pipelines/sequential/ast_utils/auto_wrapper.py
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:
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
Returns False if a node contains control statements that are not in their proper control flow context
Parameters:
-
(nodeAST) –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
NameAnalyzer
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
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
Methods:
-
analyze–Analyzes the use of names in the given piece of code
Source code in llmcompressor/pipelines/sequential/ast_utils/name_analyzer.py
analyze
Analyzes the use of names in the given piece of code
Parameters:
-
(nodeAST) –code to analyze return: tuple of unbound names, assigned names, and conditionally assigned names