Skip to content

Commit fc8bfff

Browse files
committed
Added base FINNError and changed errors during start step resolution
1 parent 693e9cd commit fc8bfff

File tree

2 files changed

+43
-14
lines changed

2 files changed

+43
-14
lines changed

src/finn/builder/build_dataflow.py

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import json
3333
import logging
3434
import os
35+
from pathlib import Path
3536
from typing import Callable
3637

3738
import pdb # isort: split
@@ -43,7 +44,7 @@
4344

4445
from finn.builder.build_dataflow_config import DataflowBuildConfig, default_build_dataflow_steps
4546
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
4748

4849

4950
# adapted from https://stackoverflow.com/a/39215961
@@ -142,14 +143,19 @@ def resolve_build_steps(cfg: DataflowBuildConfig, partial: bool = True) -> list[
142143
def resolve_step_filename(step_name: str, cfg: DataflowBuildConfig, step_delta: int = 0):
143144
step_names = list(map(lambda x: x.__name__, resolve_build_steps(cfg, partial=False)))
144145
if step_name not in step_names:
145-
raise UserException(
146+
raise FINNConfigurationError(
146147
f"Cannot restart from step {step_name}.Step {step_name} for restarting not found."
147148
)
148149
step_no = step_names.index(step_name) + step_delta
149150
if step_no < 0 or step_no >= len(step_names):
150-
raise RuntimeError("Invalid step+delta combination")
151+
raise FINNConfigurationError("Invalid step+delta combination")
151152
filename = cfg.output_dir + "/intermediate_models/"
152153
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+
)
153159
return filename
154160

155161

@@ -247,19 +253,27 @@ def build_dataflow_cfg(model_filename, cfg: DataflowBuildConfig):
247253
os.makedirs(intermediate_model_dir)
248254
model.save(os.path.join(intermediate_model_dir, chkpt_name))
249255
step_num += 1
250-
except UserException as ue:
251-
console.print(f"[red]Error: {str(ue)}")
252-
print("Build failed")
253-
return -1
254256
except KeyboardInterrupt:
255-
console.print("[red]Aborting...")
257+
log.error("KeyboardInterrupt detected. Aborting...")
256258
print("Build failed")
257259
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+
260267
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+
263277
# start postmortem debug if configured
264278
if cfg.enable_build_pdb_debug:
265279
pdb.post_mortem(tb)

src/finn/util/exception.py

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
1-
class UserException(Exception):
1+
class FINNError(Exception):
2+
"""Base-class for FINN exceptions. Useful to differentiate exceptions while catching."""
3+
4+
def __init__(self, *args: object) -> None:
5+
super().__init__(*args)
6+
7+
8+
class UserError(FINNError):
29
"""Custom exception class which should be used to
310
print errors without stacktraces if debug is disabled."""
411

5-
pass
12+
def __init__(self, *args: object) -> None:
13+
super().__init__(*args)
14+
15+
16+
class FINNConfigurationError(FINNError):
17+
"""Error emitted when FINN is configured incorrectly"""
18+
19+
def __init__(self, *args: object) -> None:
20+
super().__init__(*args)

0 commit comments

Comments
 (0)