15 KiB
module concrete.fhe.representation.graph
Declaration of Graph class.
Global Variables
- P_ERROR_PER_ERROR_SIZE_CACHE
class Graph
Graph class, to represent computation graphs.
method __init__
__init__(
graph: MultiDiGraph,
input_nodes: Dict[int, Node],
output_nodes: Dict[int, Node],
is_direct: bool = False,
name: str = 'main'
)
method draw
draw(
horizontal: bool = False,
save_to: Optional[Path, str] = None,
show: bool = False
) → Path
Draw the graph.
That this function requires the python pygraphviz package which itself requires the installation of graphviz packages
(see https://pygraphviz.github.io/documentation/stable/install.html)
Args: horizontal (bool, default = False): whether to draw horizontally
save_to (Optional[Path], default = None): path to save the drawing a temporary file will be used if it's None
show (bool, default = False): whether to show the drawing using matplotlib
Returns: Path: path to the drawing
method evaluate
evaluate(
*args: Any,
p_error: Optional[float] = None
) → Dict[Node, Union[bool_, integer, floating, ndarray]]
Perform the computation Graph represents and get resulting values for all nodes.
Args: *args (List[Any]): inputs to the computation
p_error (Optional[float]): probability of error for table lookups
Returns: Dict[Node, Union[np.bool_, np.integer, np.floating, np.ndarray]]: nodes and their values during computation
method format
format(
maximum_constant_length: int = 25,
highlighted_nodes: Optional[Dict[Node, List[str]]] = None,
highlighted_result: Optional[List[str]] = None,
show_types: bool = True,
show_bounds: bool = True,
show_tags: bool = True,
show_locations: bool = False,
show_assigned_bit_widths: bool = False
) → str
Get the textual representation of the Graph.
Args: maximum_constant_length (int, default = 25): maximum length of formatted constants
highlighted_nodes (Optional[Dict[Node, List[str]]], default = None): nodes to be highlighted and their corresponding messages
highlighted_result (Optional[List[str]], default = None): messages corresponding to highlighted return line
show_types (bool, default = True): whether to show types of nodes
show_bounds (bool, default = True): whether to show bounds of nodes
show_tags (bool, default = True): whether to show tags of nodes
show_locations (bool, default = False): whether to show line information of nodes
show_assigned_bit_widths (bool, default = False) whether to show assigned bit width of nodes instead of their original bit width
Returns:
str: textual representation of the Graph
method format_bit_width_assignments
format_bit_width_assignments() → str
Get the textual representation of bit width assignments of the graph.
Returns: str: textual representation of bit width assignments of the graph
method format_bit_width_constraints
format_bit_width_constraints() → str
Get the textual representation of bit width constraints of the graph.
Returns: str: textual representation of bit width constraints of the graph
method integer_range
integer_range(
tag_filter: Optional[str, List[str], Pattern] = None,
operation_filter: Optional[str, List[str], Pattern] = None,
is_encrypted_filter: Optional[bool] = None,
custom_filter: Optional[Callable[[Node], bool]] = None
) → Optional[Tuple[int, int]]
Get integer range of the graph.
Only nodes after filtering will be used to calculate the result.
Args: tag_filter (Optional[Union[str, List[str], re.Pattern]], default = None): filter for tags
operation_filter (Optional[Union[str, List[str], re.Pattern]], default = None): filter for operations
is_encrypted_filter (Optional[bool], default = None) filter for encryption status
custom_filter (Optional[CallableNode], bool, default = None): flexible filter
Returns: Optional[Tuple[int, int]]: minimum and maximum integer value observed during inputset evaluation if there are no integer nodes matching the query, result is None
method maximum_integer_bit_width
maximum_integer_bit_width(
tag_filter: Optional[str, List[str], Pattern] = None,
operation_filter: Optional[str, List[str], Pattern] = None,
is_encrypted_filter: Optional[bool] = None,
custom_filter: Optional[Callable[[Node], bool]] = None,
assigned_bit_width: bool = False
) → int
Get maximum integer bit-width within the graph.
Only nodes after filtering will be used to calculate the result.
Args: tag_filter (Optional[Union[str, List[str], re.Pattern]], default = None): filter for tags
operation_filter (Optional[Union[str, List[str], re.Pattern]], default = None): filter for operations
is_encrypted_filter (Optional[bool], default = None): filter for encryption status
custom_filter (Optional[CallableNode], bool, default = None): flexible filter
assigned_bit_width (Optional[bool], default = None): whether to query on assigned bit-widths
Returns: int: maximum integer bit-width within the graph if there are no integer nodes matching the query, result is -1
method measure_bounds
measure_bounds(
inputset: Union[Iterable[Any], Iterable[Tuple[Any, ]]]
) → Dict[Node, Dict[str, Union[integer, floating]]]
Evaluate the Graph using an inputset and measure bounds.
inputset is either an iterable of anything for a single parameter
or
an iterable of tuples of anything (of rank number of parameters) for multiple parameters
e.g.,
.. code-block:: python
inputset = [1, 3, 5, 2, 4] def f(x): ...
inputset = [(1, 2), (2, 4), (3, 1), (2, 2)] def g(x, y): ...
Args: inputset (Union[Iterable[Any], Iterable[Tuple[Any, ...]]]): inputset to use
Returns:
Dict[Node, Dict[str, Union[np.integer, np.floating]]]: bounds of each node in the Graph
method ordered_inputs
ordered_inputs() → List[Node]
Get the input nodes of the Graph, ordered by their indices.
Returns: List[Node]: ordered input nodes
method ordered_outputs
ordered_outputs() → List[Node]
Get the output nodes of the Graph, ordered by their indices.
Returns: List[Node]: ordered output nodes
method ordered_preds_of
ordered_preds_of(node: Node) → List[Node]
Get predecessors of node, ordered by their indices.
Args: node (Node): node whose predecessors are requested
Returns:
List[Node]: ordered predecessors of node.
method prune_useless_nodes
prune_useless_nodes()
Remove unreachable nodes from the graph.
method query_nodes
query_nodes(
tag_filter: Optional[str, List[str], Pattern] = None,
operation_filter: Optional[str, List[str], Pattern] = None,
is_encrypted_filter: Optional[bool] = None,
custom_filter: Optional[Callable[[Node], bool]] = None,
ordered: bool = False
) → List[Node]
Query nodes within the graph.
Filters work like so: str -> nodes without exact match is skipped List[str] -> nodes without exact match with one of the strings in the list is skipped re.Pattern -> nodes without pattern match is skipped
Args: tag_filter (Optional[Union[str, List[str], re.Pattern]], default = None): filter for tags
operation_filter (Optional[Union[str, List[str], re.Pattern]], default = None): filter for operations
is_encrypted_filter (Optional[bool], default = None) filter for encryption status
custom_filter (Optional[CallableNode], bool, default = None): flexible filter
ordered (bool) whether to apply topological sorting before filtering nodes
Returns: List[Node]: filtered nodes
method update_with_bounds
update_with_bounds(bounds: Dict[Node, Dict[str, Union[integer, floating]]])
Update ValueDescriptions within the Graph according to measured bounds.
Args:
bounds (Dict[Node, Dict[str, Union[np.integer, np.floating]]]): bounds of each node in the Graph
class GraphProcessor
GraphProcessor base class, to define the API for a graph processing pipeline.
Process a single graph.
method apply
apply(graph: Graph)
Process the graph.
method error
error(graph: Graph, highlights: Mapping[Node, Union[str, List[str]]])
Fail processing with an error.
Args: graph (Graph): graph being processed
highlights (Mapping[Node, Union[str, List[str]]]): nodes to highlight along with messages
class MultiGraphProcessor
MultiGraphProcessor base class, to define the API for a multiple graph processing pipeline.
Processes multiple graphs at once.
method apply
apply(graph: Graph)
Process a single graph.
method apply_many
apply_many(graphs: Dict[str, Graph])
Process a dictionnary of graphs.
method error
error(graph: Graph, highlights: Mapping[Node, Union[str, List[str]]])
Fail processing with an error.
Args: graph (Graph): graph being processed
highlights (Mapping[Node, Union[str, List[str]]]): nodes to highlight along with messages