Skip to content

Commit

Permalink
Track deprecated states and report alternatives (#1592)
Browse files Browse the repository at this point in the history
  • Loading branch information
rachitnigam authored Jul 9, 2023
1 parent 7c55d02 commit e60eb09
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 9 deletions.
2 changes: 0 additions & 2 deletions fud/fud/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,8 +385,6 @@ def construct_path(
log.debug(f"Inferred target state: {target}")

path = self.registry.make_path(source, target, through)
if path is None:
raise errors.NoPathFound(source, target, through)

# If the path doesn't execute anything, it is probably an error.
if len(path) == 0:
Expand Down
15 changes: 15 additions & 0 deletions fud/fud/errors.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,21 @@ class FudError(Exception):
"""


class DeprecatedState(FudError):
"""
The given state has been deprecated
"""

def __init__(self, stage, state, alt=None):
msg = (
f"Stage `{stage}' acts upon deprecated state `{state}'"
f". Use state `{alt}' instead in the stage definition"
if alt
else ""
)
super().__init__(msg)


class CycleLimitedReached(FudError):
"""
The cycle limit has been reached for simulation.
Expand Down
26 changes: 20 additions & 6 deletions fud/fud/registry.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
from typing import Optional, List, Tuple
from typing import List, Tuple

from collections import namedtuple
import networkx as nx # type: ignore

from fud import stages
from fud import stages, errors
from fud.errors import UndefinedState, MultiplePaths

# An edge in the state graph
Edge = namedtuple("Edge", ["dest", "stage"])
DEPRECATED_STATES = [("futil", "calyx")]


class Registry:
Expand All @@ -31,17 +32,25 @@ def get_states(self, stage: str) -> List[Tuple[str, str]]:
assert len(out) > 0, f"No state tranformation for {stage} found."
return out

@staticmethod
def _deprecate_check(stage_name, state):
for st, alt in DEPRECATED_STATES:
if state == st:
raise errors.DeprecatedState(stage_name, state, alt)

def register(self, stage):
"""
Defines a new stage named `stage` that converts programs from `src` to
`tar`
"""

# Error if the stage is attempting to register deprecated states.
Registry._deprecate_check(stage.name, stage.src_state)
Registry._deprecate_check(stage.name, stage.target_state)

self.graph.add_edge(stage.src_state, stage.target_state, stage=stage)

def make_path(
self, start: str, dest: str, through=[]
) -> Optional[List[stages.Stage]]:
def make_path(self, start: str, dest: str, through=[]) -> List[stages.Stage]:
"""
Compute a path from `start` to `dest` that contains all stages
mentioned in `through`.
Expand Down Expand Up @@ -102,7 +111,7 @@ def make_path(
if len(stage_paths) > 1:
raise MultiplePaths(start, dest, self.paths_str(all_paths))
elif len(stage_paths) == 0:
return None
raise errors.NoPathFound(start, dest, through)
else:
return stage_paths[0]

Expand All @@ -124,6 +133,11 @@ def paths_str(self, paths):
p.append(path_str)
return "\n".join(p)

def all_from(self, from_st):
"""
Returns all the transformations from a particular state
"""

def __str__(self):
stages = {}
transforms = []
Expand Down
2 changes: 1 addition & 1 deletion fud/fud/stages/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,7 +338,7 @@ def also_do_path(
given path.
@returns The output generated by this the branch of computation
"""
assert len(path) > 0, "Path is empty"
assert path is not None and len(path) > 0, "Path is empty"
first = path[0]
# The first stage uses the input to this computation graph
out = self.also_do(input, first, config)
Expand Down

0 comments on commit e60eb09

Please sign in to comment.