Visualize exported programs using the Model Explorer. The QDQ clusters and individual partitions are highlighted. To install the Model Explorer, run `devtools/install_requirements.sh`. To display a stored json file, first launch the Model Explorer server by running `model-explorer`,
(
exported_program: ExportedProgram,
json_file_name: str | None = None,
no_open_in_browser: bool = False,
reuse_server: bool = False,
get_node_partition_name: Callable[[Node], str | None] = lambda node: node.meta.get(
"delegation_tag", None
),
get_node_qdq_cluster_name: Callable[
[Node], str | None
] = lambda node: node.meta.get("cluster", None),
**kwargs,
)
| 176 | |
| 177 | |
| 178 | def visualize_with_clusters( |
| 179 | exported_program: ExportedProgram, |
| 180 | json_file_name: str | None = None, |
| 181 | no_open_in_browser: bool = False, |
| 182 | reuse_server: bool = False, |
| 183 | get_node_partition_name: Callable[[Node], str | None] = lambda node: node.meta.get( |
| 184 | "delegation_tag", None |
| 185 | ), |
| 186 | get_node_qdq_cluster_name: Callable[ |
| 187 | [Node], str | None |
| 188 | ] = lambda node: node.meta.get("cluster", None), |
| 189 | **kwargs, |
| 190 | ): |
| 191 | """Visualize exported programs using the Model Explorer. The QDQ clusters and individual partitions are highlighted. |
| 192 | |
| 193 | To install the Model Explorer, run `devtools/install_requirements.sh`. |
| 194 | To display a stored json file, first launch the Model Explorer server by running `model-explorer`, and then |
| 195 | use the GUI to open the json. |
| 196 | |
| 197 | NOTE: FireFox seems to have issues rendering the graphs. Other browsers seem to work well. |
| 198 | |
| 199 | :param exported_program: Program to visualize. |
| 200 | :param json_file_name: If not None, a JSON of the visualization will be stored in the provided file. The JSON can |
| 201 | then be opened in the Model Explorer GUI later. |
| 202 | If None, a Model Explorer instance will be launched with the model visualization. |
| 203 | :param no_open_in_browser: If `True`, a browser instance with the model explorer will NOT be launched, and only the |
| 204 | URI to the model explorer server with the visualization will be printed. |
| 205 | :param reuse_server: If True, an existing instance of the Model Explorer server will be used (if one exists). |
| 206 | Otherwise, a new instance at a separate port will start. |
| 207 | :param get_node_partition_name: Function which takes a `Node` and returns a string with the name of the partition |
| 208 | the `Node` belongs to, or `None` if it has no partition. |
| 209 | :param get_node_qdq_cluster_name: Function which takes a `Node` and returns a string with the name of the QDQ |
| 210 | cluster the `Node` belongs to, or `None` if it has no cluster. |
| 211 | :param kwargs: Additional kwargs for the `visualize_from_config()` function. |
| 212 | """ |
| 213 | |
| 214 | cur_config = config() |
| 215 | |
| 216 | # Create a Model Explorer graph from the `exported_program`. |
| 217 | adapter = PytorchExportedProgramAdapterImpl( |
| 218 | exported_program, consts.DEFAULT_SETTINGS |
| 219 | ) |
| 220 | graphs = adapter.convert() |
| 221 | |
| 222 | nodes = list(exported_program.graph.nodes) |
| 223 | explorer_nodes = graphs["graphs"][0].nodes |
| 224 | |
| 225 | # Highlight QDQ clusters and individual partitions. |
| 226 | known_partition_names = [] |
| 227 | for explorer_node, node in zip(explorer_nodes, nodes, strict=True): |
| 228 | # Generate the `namespace` for the node, which will determine node grouping in the visualizer. |
| 229 | # The character "/" is used as a divider when a node has multiple namespaces. |
| 230 | namespace = "" |
| 231 | |
| 232 | if (partition_name := get_node_partition_name(node)) is not None: |
| 233 | # If the nodes are tagged by the partitioner, highlight the tagged groups. |
| 234 | |
| 235 | # Create a custom naming for the partitions ("partition <i>" where `i` = 0, 1, 2, ...). |