llmcompressor.pipelines.sequential.helpers
Classes:
-
Subgraph–Dataclass specifying an executable subgraph of a model graph
Functions:
-
handle_sequential_oom–Catch ooms and suggest changing sequential targets
-
trace_subgraphs–Trace a model to produce subgraphs, where each sequential target belongs to exactly
SequentialTracer
Bases: HFTracer
Get a tracer specialized for the given model. The resulting tracer will not trace inside of sequential targets, nor any modules which are not call graph ancestors of sequential targets
Modules which run while a node is executed on meta tensors (e.g. inside wrapped
functions) are recorded on node.meta[INVOKED_SUBMODULES_META_KEY] so that
Subgraph.submodules can attribute them to a subgraph
Parameters:
-
ancestors(set[Module]) –modules which are ancestors of sequential targets
Methods:
-
call_module–Record
moduleif it is being executed on meta tensors bycreate_proxy -
create_proxy–Create a node and record the modules which run while HFTracer executes it on
Source code in src/llmcompressor/pipelines/sequential/helpers.py
call_module
Record module if it is being executed on meta tensors by create_proxy
(_disable_call_module is only set in that window). Names come from
submodule_paths rather than path_of_module, which would insert modules
constructed inside forward (e.g. nn.Softmax) as new root submodules
Source code in src/llmcompressor/pipelines/sequential/helpers.py
create_proxy
Create a node and record the modules which run while HFTracer executes it on
meta tensors, so Subgraph.submodules can see modules called inside wrapped
functions. call_module nodes are skipped because they already own their
descendants via recurse
Source code in src/llmcompressor/pipelines/sequential/helpers.py
Subgraph
dataclass
Subgraph(
graph: Graph,
input_names: set[str],
consumed_names: set[str],
_code: PythonCode | None = None,
)
Dataclass specifying an executable subgraph of a model graph
Parameters:
-
graph(Graph) –subgraph of model graph
-
input_names(set[str]) –argument names of the compiled forward function
-
consumed_names(set[str]) –argument names which are not used by any subsequent subgraphs and can therefore be deleted from the intermediates cache
Methods:
-
forward–Execute the operations within the subgraph
-
submodule_dict–Return subgraph modules keyed by their fully qualified model names.
forward
Execute the operations within the subgraph
Parameters:
-
\*args–argument inputs to subgraph forward function
-
\**kwargs–keyword inputs to subgraph forward function
Returns:
-
dict[str, Any]–
Source code in src/llmcompressor/pipelines/sequential/helpers.py
submodule_dict
Return subgraph modules keyed by their fully qualified model names.
Source code in src/llmcompressor/pipelines/sequential/helpers.py
collect_subgraph_modules
collect_subgraph_modules(
model: Module,
subgraphs: list[Subgraph],
recurse: bool = True,
) -> dict[str, Module]
Return the union of all modules owned by the traced subgraphs.
Source code in src/llmcompressor/pipelines/sequential/helpers.py
compress_subgraph_modules
Compress quantized modules owned by one subgraph.
Source code in src/llmcompressor/pipelines/sequential/helpers.py
decompress_subgraph_modules
Decompress quantized modules owned by one subgraph.
Source code in src/llmcompressor/pipelines/sequential/helpers.py
find_modules_outside_graph
Return modules that no node of a traced graph calls. These are the same modules as
find_modules_outside_subgraphs returns for any partition of the graph, so they
can be found before the graph is partitioned
Source code in src/llmcompressor/pipelines/sequential/helpers.py
find_modules_outside_subgraphs
find_modules_outside_subgraphs(
model: Module,
subgraphs: list[Subgraph],
recurse: bool = True,
) -> dict[str, Module]
Return modules that were traced but do not belong to any subgraph.
Source code in src/llmcompressor/pipelines/sequential/helpers.py
find_target_nodes
Find all nodes whose execution is equivalent to executing the target modules. Note that these nodes are guaranteed to be treated as leaf nodes by SequentialTracer
Parameters:
-
graph(GraphModule) –graph containing target nodes
-
targets(set[Module]) –modules whose nodes are being searched for
Returns:
-
set[Node]–set of all nodes which call the target modules
Source code in src/llmcompressor/pipelines/sequential/helpers.py
get_node_modules
Get the modules a node onloads when its subgraph runs: its roots (see
get_node_roots) and their submodules
Parameters:
-
model(Module) –model which owns the graph
-
node(Node) –node of the traced graph
Returns:
-
list[str]–module names
Source code in src/llmcompressor/pipelines/sequential/helpers.py
get_node_roots
Parameters:
-
node(Node) –node of a traced graph
Returns:
-
list[str]–names of the module a node calls and of the modules recorded as run inside it (see
INVOKED_SUBMODULES_META_KEY), not including their descendants
Source code in src/llmcompressor/pipelines/sequential/helpers.py
get_sequential_ancestors
Find modules which are call graph ancestors of the given sequential targets
Parameters:
-
model(Module) –model containing sequential targets
-
targets(set[Module]) –sequential targets to find ancestors of
Returns:
-
set[Module]–call graph ancestors of sequential targets
Source code in src/llmcompressor/pipelines/sequential/helpers.py
graph_is_well_formed
A graph is well formed if and only if
nodeA in NodeB.users <=> nodeB in Node.A.all_input_nodes
Parameters:
-
graph(Graph) –graph being checked
Returns:
-
bool–True if the graph is well formed, False otherwise
Source code in src/llmcompressor/pipelines/sequential/helpers.py
handle_sequential_oom
Catch ooms and suggest changing sequential targets
Source code in src/llmcompressor/pipelines/sequential/helpers.py
partition_graph
Convert each partition into a Subgraph. Each Subgraph returns a dictionary mapping
of output node names to their computed values. Note that the consumed_names
attribute of each Subgraph remains empty, to be later populated by
trace_consumed_names
Parameters:
-
model(Module) –model which owns the produced Subgraphs
-
partitions(list[list[Node]]) –list of partitions, where each partition is a list of nodes belonging to that partition
Returns:
-
list[Subgraph]–list of subgraphs in order of execution
Source code in src/llmcompressor/pipelines/sequential/helpers.py
partition_subgraphs
partition_subgraphs(
model: Module,
graph: GraphModule,
targets: set[Module],
targets_per_subgraph: int | str = 1,
dataloader: DataLoader | None = None,
modifiers: list[Modifier] | None = None,
dataset_args: DatasetArguments | None = None,
) -> list[Subgraph]
Partition a traced graph into subgraphs, where each sequential target belongs to exactly one subgraph and where executing each subgraph in order is equivalent to executing the original model
Parameters:
-
model(Module) –model which owns the produced Subgraphs
-
graph(GraphModule) –graph produced by
trace_graph -
targets(set[Module]) –sequential target modules produced by
trace_graph -
targets_per_subgraph(int | str, default:1) –number of targets to include per subgraph, or "auto" to group as many consecutive targets as fit in device memory
-
dataloader(DataLoader | None, default:None) –with "auto", the calibration data
-
modifiers(list[Modifier] | None, default:None) –with "auto", the modifiers which will calibrate the subgraphs
-
dataset_args(DatasetArguments | None, default:None) –with "auto", the arguments which set the memory budget
Returns:
-
list[Subgraph]–a list of Subgraphs in order of execution
Source code in src/llmcompressor/pipelines/sequential/helpers.py
populate_concrete_args
Creates concrete args which, unlike the equivalent function provided by transformers.utils.fx, creates default values for variadic arguments, which are needed by some models.
Parameters:
-
model(Module) –model being traced
-
sample_input(dict) –values used to symbolically trace the model. All arguments to the model.forward function which are not in the sample_input are considered concrete args
Returns:
-
dict–dictionary mapping concrete argument names to their default values
Source code in src/llmcompressor/pipelines/sequential/helpers.py
topological_partition
topological_partition(
graph: GraphModule,
targets: set[Module],
targets_per_subgraph: int | str = 1,
node_memory: dict[Node, int] | None = None,
max_memory: int | None = None,
) -> list[list[Node]]
Partition the graph into partitions such that each target belongs to exactly one
partition and executing each partition depends only on intermediate values produced
by executing the partitions before it.
Parameters:
-
graph(GraphModule) –graph being partitioned
-
targets(set[Module]) –target modules which will be assigned to disjoint partitions
-
targets_per_subgraph(int | str, default:1) –number of targets to include per subgraph, or "auto" to fill each partition until its nodes' memory would exceed
max_memory. A partition is only cut once it holds a target, so only the nodes before the first target or after the last one can end up in a partition without one -
node_memory(dict[Node, int] | None, default:None) –with "auto", the memory each node onloads and holds
-
max_memory(int | None, default:None) –with "auto", the memory available to each partition
Returns:
-
list[list[Node]]–list of partitions, where each partition is a list of nodes belonging to that partition
Source code in src/llmcompressor/pipelines/sequential/helpers.py
591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 | |
trace_consumed_names
Populate the consumed_names attribute of each Subgraph according to when inputs
are last used in order to vacate the intermediates cache and save memory
Parameters:
-
subgraphs(list[Subgraph]) –list of subgraphs with empty
consumed_namesattributes
Source code in src/llmcompressor/pipelines/sequential/helpers.py
trace_graph
trace_graph(
model: PreTrainedModel,
sample_input: dict[str, Any],
sequential_targets: list[str],
ignore: list[str],
eager_attention: bool = False,
) -> tuple[GraphModule, set[Module]]
Trace a model into a graph in which each sequential target is a leaf node. Tracing is the expensive step, so the returned graph can be partitioned more than once, for example with different numbers of targets per subgraph
Parameters:
-
model(PreTrainedModel) –model being traced
-
sample_input(dict[str, Any]) –inputs whose values will change during execution but whose len, bool, and contains values are assumed constant across batches
-
sequential_targets(list[str]) –list of patterns matching sequential targets. Detected attention modules are also used as targets when not contained by a requested target.
-
ignore(list[str]) –function and method names to skip during tracing
-
eager_attention(bool, default:False) –whether to force eager attention while tracing
Returns:
-
tuple[GraphModule, set[Module]]–the traced graph and the set of sequential target modules
Source code in src/llmcompressor/pipelines/sequential/helpers.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
trace_subgraphs
trace_subgraphs(
model: PreTrainedModel,
sample_input: dict[str, Any],
sequential_targets: list[str],
ignore: list[str],
targets_per_subgraph: int = 1,
eager_attention: bool = False,
) -> list[Subgraph]
Trace a model to produce subgraphs, where each sequential target belongs to exactly one subgraph and where executing each subgraph in order is equivalent to executing the original model
Parameters:
-
model(PreTrainedModel) –model being traced
-
sample_input(dict[str, Any]) –inputs whose values will change during execution but whose len, bool, and contains values are assumed constant across batches
-
sequential_targets(list[str]) –list of patterns matching sequential targets. Detected attention modules are also used as targets when not contained by a requested target.
-
ignore(list[str]) –function and method names to skip during tracing
-
targets_per_subgraph(int, default:1) –number of targets to include per subgraph
-
eager_attention(bool, default:False) –whether to force eager attention while tracing
Returns:
-
list[Subgraph]–a list of Subgraphs in order of execution