|  | 
|  | 1 | +# Visualize a Model with Highlighted QDQ Clusters and Partitions | 
|  | 2 | + | 
|  | 3 | +The [visualization_utils.py](/executorch/devtools/visualization/visualization_utils.py) contains the function | 
|  | 4 | +`visualize_with_clusters()` which takes an `ExportedProgram` and visualizes it using the `ModelExplorer` utility.   | 
|  | 5 | +It groups QDQ clusters and individual partitions together to improve readability. | 
|  | 6 | + | 
|  | 7 | +--- | 
|  | 8 | + | 
|  | 9 | +## Installation | 
|  | 10 | + | 
|  | 11 | +To install the `ModelExplorer` and its dependencies, run: | 
|  | 12 | + | 
|  | 13 | +```bash | 
|  | 14 | +$ executorch/devtools/install_requirements.sh | 
|  | 15 | +``` | 
|  | 16 | + | 
|  | 17 | +--- | 
|  | 18 | + | 
|  | 19 | +## Usage | 
|  | 20 | + | 
|  | 21 | +There are two main use cases for the visualization: | 
|  | 22 | + | 
|  | 23 | +### 1. Launching the `ModelExplorer` and Visualizing the Model Immediately | 
|  | 24 | + | 
|  | 25 | +Call: | 
|  | 26 | + | 
|  | 27 | +```python | 
|  | 28 | +visualize_with_clusters(exported_program) | 
|  | 29 | +``` | 
|  | 30 | + | 
|  | 31 | +This starts a `ModelExplorer` server and opens a browser tab with the visualization. | 
|  | 32 | + | 
|  | 33 | +By default, each call starts a new server instance and opens a new browser tab.   | 
|  | 34 | +To reuse an existing server, set the `reuse_server` parameter to `True`. | 
|  | 35 | + | 
|  | 36 | +Starting the server is **blocking**, so the rest of your script will not run. | 
|  | 37 | + | 
|  | 38 | +### 2. Storing a Serialized Graph and Visualizing Later (Non-blocking) | 
|  | 39 | + | 
|  | 40 | +To save the visualization to a JSON file, call: | 
|  | 41 | + | 
|  | 42 | +```python | 
|  | 43 | +visualize_with_clusters(exported_program, "my_model.json") | 
|  | 44 | +``` | 
|  | 45 | + | 
|  | 46 | +This just saves the visualization in the file, and it does **not** start the `ModelExplorer` server. You can then open | 
|  | 47 | +the file in the `ModelExplorer` GUI at any point. To launch the server, run: | 
|  | 48 | + | 
|  | 49 | +```bash | 
|  | 50 | +$ model-explorer | 
|  | 51 | +``` | 
|  | 52 | + | 
|  | 53 | +A browser tab should open automatically.   | 
|  | 54 | +Click **Select from your computer**, choose the JSON file, and then click **View selected models** to display the graph. | 
|  | 55 | + | 
|  | 56 | +--- | 
|  | 57 | + | 
|  | 58 | +## Styling the Graph | 
|  | 59 | + | 
|  | 60 | +`visualize_with_clusters()` supports custom grouping of nodes into QDQ clusters and partitions. | 
|  | 61 | + | 
|  | 62 | +You can pass the following optional parameters: | 
|  | 63 | + | 
|  | 64 | +- `get_node_partition_name` | 
|  | 65 | +- `get_node_qdq_cluster_name` | 
|  | 66 | + | 
|  | 67 | +These are functions that take a node and return a string identifying the partition or cluster it belongs to.   | 
|  | 68 | +Nodes with the same partition/cluster string will be grouped together and labeled accordingly in the visualization. | 
|  | 69 | + | 
|  | 70 | +### Custom Styles in `ModelExplorer` | 
|  | 71 | + | 
|  | 72 | +To customize the appearance of nodes: | 
|  | 73 | + | 
|  | 74 | +1. Click the **palette icon** in the top-right corner of the `ModelExplorer` interface. | 
|  | 75 | +2. Click **Import rules**. | 
|  | 76 | +3. Select | 
|  | 77 | +   the [cluster_highlight_style.json](/executorch/devtools/visualization/model_explorer_styles/cluster_highlight_style.json) | 
|  | 78 | +   file to apply predefined styles that highlight each partition in a different color. | 
0 commit comments