|
| 1 | +# Copyright 2025 Arm Limited and/or its affiliates. |
| 2 | +# |
| 3 | +# This source code is licensed under the BSD-style license found in the |
| 4 | +# LICENSE file in the root directory of this source tree. |
| 5 | + |
| 6 | + |
| 7 | +import subprocess |
| 8 | +import time |
| 9 | + |
| 10 | +from executorch.exir import EdgeProgramManager, ExecutorchProgramManager |
| 11 | +from model_explorer import config, consts, visualize_from_config # type: ignore |
| 12 | +from torch.export.exported_program import ExportedProgram |
| 13 | + |
| 14 | + |
| 15 | +class SingletonModelExplorerServer: |
| 16 | + """Singleton context manager for starting a model-explorer server. |
| 17 | + If multiple ModelExplorerServer contexts are nested, a single |
| 18 | + server is still used. |
| 19 | + """ |
| 20 | + |
| 21 | + server: None | subprocess.Popen = None |
| 22 | + num_open: int = 0 |
| 23 | + wait_after_start = 2.0 |
| 24 | + |
| 25 | + def __init__(self, open_in_browser: bool = True, port: int | None = None): |
| 26 | + if SingletonModelExplorerServer.server is None: |
| 27 | + command = ["model-explorer"] |
| 28 | + if not open_in_browser: |
| 29 | + command.append("--no_open_in_browser") |
| 30 | + if port is not None: |
| 31 | + command.append("--port") |
| 32 | + command.append(str(port)) |
| 33 | + SingletonModelExplorerServer.server = subprocess.Popen(command) |
| 34 | + |
| 35 | + def __enter__(self): |
| 36 | + SingletonModelExplorerServer.num_open = ( |
| 37 | + SingletonModelExplorerServer.num_open + 1 |
| 38 | + ) |
| 39 | + time.sleep(SingletonModelExplorerServer.wait_after_start) |
| 40 | + return self |
| 41 | + |
| 42 | + def __exit__(self, type, value, traceback): |
| 43 | + SingletonModelExplorerServer.num_open = ( |
| 44 | + SingletonModelExplorerServer.num_open - 1 |
| 45 | + ) |
| 46 | + if SingletonModelExplorerServer.num_open == 0: |
| 47 | + if SingletonModelExplorerServer.server is not None: |
| 48 | + SingletonModelExplorerServer.server.kill() |
| 49 | + try: |
| 50 | + SingletonModelExplorerServer.server.wait( |
| 51 | + SingletonModelExplorerServer.wait_after_start |
| 52 | + ) |
| 53 | + except subprocess.TimeoutExpired: |
| 54 | + SingletonModelExplorerServer.server.terminate() |
| 55 | + SingletonModelExplorerServer.server = None |
| 56 | + |
| 57 | + |
| 58 | +class ModelExplorerServer: |
| 59 | + """Context manager for starting a model-explorer server.""" |
| 60 | + |
| 61 | + wait_after_start = 2.0 |
| 62 | + |
| 63 | + def __init__(self, open_in_browser: bool = True, port: int | None = None): |
| 64 | + command = ["model-explorer"] |
| 65 | + if not open_in_browser: |
| 66 | + command.append("--no_open_in_browser") |
| 67 | + if port is not None: |
| 68 | + command.append("--port") |
| 69 | + command.append(str(port)) |
| 70 | + self.server = subprocess.Popen(command) |
| 71 | + |
| 72 | + def __enter__(self): |
| 73 | + time.sleep(self.wait_after_start) |
| 74 | + |
| 75 | + def __exit__(self, type, value, traceback): |
| 76 | + self.server.kill() |
| 77 | + try: |
| 78 | + self.server.wait(self.wait_after_start) |
| 79 | + except subprocess.TimeoutExpired: |
| 80 | + self.server.terminate() |
| 81 | + |
| 82 | + |
| 83 | +def _get_exported_program( |
| 84 | + visualizable: ExportedProgram | EdgeProgramManager | ExecutorchProgramManager, |
| 85 | +) -> ExportedProgram: |
| 86 | + if isinstance(visualizable, ExportedProgram): |
| 87 | + return visualizable |
| 88 | + if isinstance(visualizable, (EdgeProgramManager, ExecutorchProgramManager)): |
| 89 | + return visualizable.exported_program() |
| 90 | + raise RuntimeError(f"Cannot get ExportedProgram from {visualizable}") |
| 91 | + |
| 92 | + |
| 93 | +def visualize( |
| 94 | + visualizable: ExportedProgram | EdgeProgramManager | ExecutorchProgramManager, |
| 95 | + reuse_server: bool = True, |
| 96 | + no_open_in_browser: bool = False, |
| 97 | + **kwargs, |
| 98 | +): |
| 99 | + """Wraps the visualize_from_config call from model_explorer. |
| 100 | + For convenicence, figures out how to find the exported_program |
| 101 | + from EdgeProgramManager and ExecutorchProgramManager for you. |
| 102 | +
|
| 103 | + See https://github.com/google-ai-edge/model-explorer/wiki/4.-API-Guide#visualize-pytorch-models |
| 104 | + for full documentation. |
| 105 | + """ |
| 106 | + cur_config = config() |
| 107 | + settings = consts.DEFAULT_SETTINGS |
| 108 | + cur_config.add_model_from_pytorch( |
| 109 | + "Executorch", |
| 110 | + exported_program=_get_exported_program(visualizable), |
| 111 | + settings=settings, |
| 112 | + ) |
| 113 | + if reuse_server: |
| 114 | + cur_config.set_reuse_server() |
| 115 | + visualize_from_config( |
| 116 | + cur_config, |
| 117 | + no_open_in_browser=no_open_in_browser, |
| 118 | + **kwargs, |
| 119 | + ) |
0 commit comments