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

Method format

test/profiler/test_profiler_tree.py:107–148  ·  view source on GitHub ↗
(cls, profiler, indent: int = 0)

Source from the content-addressed store, hash-verified

105
106 @classmethod
107 def format(cls, profiler, indent: int = 0):
108
109 def flatten(nodes, depth=0, out=None):
110 if out is None:
111 out = []
112
113 for node in nodes:
114 cls.validate_node(node)
115 name = cls.fmt_name(node.name)
116 prune_level = PRUNE_FUNCTIONS.get(name.strip(), None)
117 if prune_level is None:
118 out.append((depth, name))
119 flatten(node.children, depth + 1, out)
120 elif prune_level == KEEP_NAME_AND_ELLIPSES:
121 out.append((depth, name))
122 if node.children:
123 out.append((depth + 1, "..."))
124 elif prune_level == KEEP_ELLIPSES:
125 out.append((depth, "..."))
126 else:
127 assert prune_level == PRUNE_ALL
128
129 return out
130
131 flat_nodes = flatten(profiler.kineto_results.experimental_event_tree())
132
133 # Profiler inserts a `cudaDeviceSynchronize` at the end of profiling.
134 # and may also insert 'Context Sync' CUDA synchronization event.
135 if flat_nodes and flat_nodes[-2][1] == "cudaDeviceSynchronize":
136 flat_nodes = flat_nodes[:-2]
137
138 if flat_nodes and flat_nodes[-1][1] == "cudaDeviceSynchronize":
139 flat_nodes = flat_nodes[:-1]
140
141 # Profiler inserts a `hipDeviceSynchronize` at the end of profiling.
142 if flat_nodes and flat_nodes[-1][1] == "hipDeviceSynchronize":
143 flat_nodes = flat_nodes[:-1]
144
145 min_depth = min([d + 1 for d, name in flat_nodes if "begin_unit_test_marker" in name] or [0])
146 return textwrap.indent(
147 "\n".join([f"{' ' * (d - min_depth)}{name.rstrip()}" for d, name in flat_nodes if d >= min_depth]),
148 " " * indent)
149
150 @staticmethod
151 def fmt_name(name: str) -> str:

Calls 4

flattenFunction · 0.50
minFunction · 0.50
indentMethod · 0.45
joinMethod · 0.45

Tested by

no test coverage detected