MCPcopy Create free account
hub / github.com/pytorch/pytorch / split_module

Function split_module

torch/fx/passes/split_module.py:41–489  ·  view source on GitHub ↗

Creates subgraphs out of main graph Args: m (GraphModule): Graph module to split root_m (torch.nn.Module): root nn module. Not currently used. Included because the root nn module is usually transformed via torch.fx._symbolic_trace.symbolic_trace (see

(
    m: GraphModule,
    root_m: torch.nn.Module,
    split_callback: Callable[[Node], int],
    qualname_map: Optional[Dict[str, str]] = None,
    keep_original_order: Optional[bool] = False,
)

Source from the content-addressed store, hash-verified

39# Creates subgraphs out of main graph
40@compatibility(is_backward_compatible=True)
41def split_module(
42 m: GraphModule,
43 root_m: torch.nn.Module,
44 split_callback: Callable[[Node], int],
45 qualname_map: Optional[Dict[str, str]] = None,
46 keep_original_order: Optional[bool] = False,
47):
48 """
49 Creates subgraphs out of main graph
50
51 Args:
52 m (GraphModule): Graph module to split
53 root_m (torch.nn.Module): root nn module. Not currently used. Included
54 because the root nn module is usually transformed via
55 torch.fx._symbolic_trace.symbolic_trace (see example below)
56 split_callback (Callable[[Node], int]): Callable function
57 that maps a given Node instance to a numeric partition identifier.
58 split_module will use this function as the policy for which operations
59 appear in which partitions in the output Module.
60 qualname_map: Optional[Dict[str, str]]: optional output parameter that returns a
61 mapping from new target names in the module after split to old target
62 names in the original module.
63 keep_original_order: Optional[bool]: keep the original order of the GraphModule
64 or use the Topological order of the new constructed GraphModule
65
66
67 Returns:
68 GraphModule: the module after split.
69
70 Example:
71
72 This is a sample setup:
73
74 import torch
75 from torch.fx.symbolic_trace import symbolic_trace
76 from torch.fx.graph_module import GraphModule
77 from torch.fx.node import Node
78 from torch.fx.passes.split_module import split_module
79
80 class MyModule(torch.nn.Module):
81 def __init__(self):
82 super().__init__()
83 self.param = torch.nn.Parameter(torch.rand(3, 4))
84 self.linear = torch.nn.Linear(4, 5)
85
86 def forward(self, x, y):
87 z = self.linear(x + self.param).clamp(min=0.0, max=1.0)
88 w = self.linear(y).clamp(min=0.0, max=1.0)
89 return z + w
90
91 # symbolically trace model
92 my_module = MyModule()
93 my_module_traced = symbolic_trace(my_module)
94
95 # random mod partitioning
96 partition_counter = 0
97 NPARTITIONS = 3
98

Calls 15

call_moduleMethod · 0.95
outputMethod · 0.95
isinstanceFunction · 0.85
listFunction · 0.85
reversedFunction · 0.85
construct_graphFunction · 0.85
iterFunction · 0.85
map_argMethod · 0.80
allFunction · 0.50
addMethod · 0.45

Used in the wild real call sites across dependent graphs

searching dependent graphs…