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