|
32 | 32 | import json |
33 | 33 | import logging |
34 | 34 | import os |
| 35 | +from pathlib import Path |
35 | 36 | from typing import Callable |
36 | 37 |
|
37 | 38 | import pdb # isort: split |
|
43 | 44 |
|
44 | 45 | from finn.builder.build_dataflow_config import DataflowBuildConfig, default_build_dataflow_steps |
45 | 46 | from finn.builder.build_dataflow_steps import build_dataflow_step_lookup |
46 | | -from finn.util.exception import UserException |
| 47 | +from finn.util.exception import FINNConfigurationError, FINNError, UserError |
47 | 48 |
|
48 | 49 |
|
49 | 50 | # adapted from https://stackoverflow.com/a/39215961 |
@@ -142,14 +143,19 @@ def resolve_build_steps(cfg: DataflowBuildConfig, partial: bool = True) -> list[ |
142 | 143 | def resolve_step_filename(step_name: str, cfg: DataflowBuildConfig, step_delta: int = 0): |
143 | 144 | step_names = list(map(lambda x: x.__name__, resolve_build_steps(cfg, partial=False))) |
144 | 145 | if step_name not in step_names: |
145 | | - raise UserException( |
| 146 | + raise FINNConfigurationError( |
146 | 147 | f"Cannot restart from step {step_name}.Step {step_name} for restarting not found." |
147 | 148 | ) |
148 | 149 | step_no = step_names.index(step_name) + step_delta |
149 | 150 | if step_no < 0 or step_no >= len(step_names): |
150 | | - raise RuntimeError("Invalid step+delta combination") |
| 151 | + raise FINNConfigurationError("Invalid step+delta combination") |
151 | 152 | filename = cfg.output_dir + "/intermediate_models/" |
152 | 153 | filename += "%s.onnx" % (step_names[step_no]) |
| 154 | + if not Path(filename).exists(): |
| 155 | + raise FINNConfigurationError( |
| 156 | + f"Expected model file at {filename} to start from step " |
| 157 | + f"{step_name}, but could not find it!" |
| 158 | + ) |
153 | 159 | return filename |
154 | 160 |
|
155 | 161 |
|
@@ -247,19 +253,27 @@ def build_dataflow_cfg(model_filename, cfg: DataflowBuildConfig): |
247 | 253 | os.makedirs(intermediate_model_dir) |
248 | 254 | model.save(os.path.join(intermediate_model_dir, chkpt_name)) |
249 | 255 | step_num += 1 |
250 | | - except UserException as ue: |
251 | | - console.print(f"[red]Error: {str(ue)}") |
252 | | - print("Build failed") |
253 | | - return -1 |
254 | 256 | except KeyboardInterrupt: |
255 | | - console.print("[red]Aborting...") |
| 257 | + log.error("KeyboardInterrupt detected. Aborting...") |
256 | 258 | print("Build failed") |
257 | 259 | return -1 |
258 | | - except: # noqa |
259 | | - # print exception info and traceback |
| 260 | + except (Exception, FINNError) as e: |
| 261 | + # Print full traceback if we are on debug log level |
| 262 | + # or encountered a non-user error |
| 263 | + print_full_traceback = True |
| 264 | + if issubclass(type(e), UserError) and log.level != logging.DEBUG: |
| 265 | + print_full_traceback = False |
| 266 | + |
260 | 267 | extype, value, tb = sys.exc_info() |
261 | | - console.print("[red]Internal Compiler Error:") |
262 | | - console.print_exception(show_locals=False) |
| 268 | + if print_full_traceback: |
| 269 | + # print exception info and traceback |
| 270 | + log.error("Internal compiler error:") |
| 271 | + console.print_exception(show_locals=False) |
| 272 | + else: |
| 273 | + log.error(f"FINN User Error: {e}") |
| 274 | + print("Build failed") |
| 275 | + return -1 # A user error shouldn't be need to be fixed using PDB |
| 276 | + |
263 | 277 | # start postmortem debug if configured |
264 | 278 | if cfg.enable_build_pdb_debug: |
265 | 279 | pdb.post_mortem(tb) |
|
0 commit comments