Struct ciphercore_base::graphs::Context
source · [−]pub struct Context { /* private fields */ }
Expand description
A structure that stores a pointer to a computation context that contains related computation graphs.
Context is a basic object to create computation graphs, arrange data flow between them and keep necessary information about them that is used for optimization, secure compilation and evaluation.
Context should have a main graph and be finalized in order to evaluate any of its graphs.
Clone trait duplicates the pointer, not the underlying context.
PartialEq trait compares pointers, not the related contexts.
Example
let context = || -> Result<Context> {
let context = create_context()?;
let graph = context.create_graph()?.set_name("main")?;
graph
.input(scalar_type(INT32))?
.set_name("a")?
.add(graph
.input(scalar_type(INT32))?
.set_name("b")?)?
.set_as_output()?;
graph.finalize()?.set_as_main()?;
context.finalize()?;
Ok(context)
}().unwrap();
let result = || -> Result<i32> {
let g = context.retrieve_graph("main")?;
let result = random_evaluate(
g.clone(),
g.prepare_input_values(
hashmap!{
"a" => Value::from_scalar(123, INT32)?,
"b" => Value::from_scalar(654, INT32)?,
},
)?,
)?;
let result = result.to_i32(INT32)?;
Ok(result)
}().unwrap();
assert_eq!(result, 777);
Implementations
sourceimpl Context
impl Context
sourcepub fn create_graph(&self) -> Result<Graph>
pub fn create_graph(&self) -> Result<Graph>
sourcepub fn finalize(&self) -> Result<Context>
pub fn finalize(&self) -> Result<Context>
Finalizes the context if all its graphs are finalized and the main graph is set.
After finalization the context can’t be changed.
Returns
Finalized context
Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![3, 2], INT32);
let vec_t = vector_type(4, t);
let n1 = g.input(vec_t).unwrap();
let n2 = g.vector_to_array(n1).unwrap();
n2.set_as_output().unwrap();
g.finalize().unwrap();
c.set_main_graph(g).unwrap();
c.finalize().unwrap();
sourcepub fn set_main_graph(&self, graph: Graph) -> Result<Context>
pub fn set_main_graph(&self, graph: Graph) -> Result<Context>
Promotes a graph to the main one in this context.
Arguments
graph
- graph
Returns
This context
Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![3, 2], INT32);
let n = g.input(t).unwrap();
n.set_as_output().unwrap();
g.finalize().unwrap();
c.set_main_graph(g).unwrap();
sourcepub fn get_graphs(&self) -> Vec<Graph>
pub fn get_graphs(&self) -> Vec<Graph>
Returns the vector of graphs contained in this context in order of creation.
Returns
Vector of the graphs in this context
sourcepub fn check_finalized(&self) -> Result<()>
pub fn check_finalized(&self) -> Result<()>
Does nothing if the context is finalized; otherwise returns a runtime error.
Returns
Runtime error if this context is not finalized
sourcepub fn get_main_graph(&self) -> Result<Graph>
pub fn get_main_graph(&self) -> Result<Graph>
sourcepub fn get_num_graphs(&self) -> u64
pub fn get_num_graphs(&self) -> u64
Returns the number of graphs contained in this context.
Returns
Number of the graphs in this context
sourcepub fn get_graph_by_id(&self, id: u64) -> Result<Graph>
pub fn get_graph_by_id(&self, id: u64) -> Result<Graph>
sourceimpl Context
impl Context
sourcepub fn set_graph_name(&self, graph: Graph, name: &str) -> Result<Context>
pub fn set_graph_name(&self, graph: Graph, name: &str) -> Result<Context>
sourcepub fn get_graph_name(&self, graph: Graph) -> Result<String>
pub fn get_graph_name(&self, graph: Graph) -> Result<String>
sourcepub fn retrieve_graph(&self, name: &str) -> Result<Graph>
pub fn retrieve_graph(&self, name: &str) -> Result<Graph>
Returns the graph with a given name in this context.
Arguments
name
- graph name
Returns
Graph with a given name
Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let n = g.input(scalar_type(BIT)).unwrap();
g.set_name("input_graph").unwrap();
assert!(g == c.retrieve_graph("input_graph").unwrap());
sourcepub fn set_node_name(&self, node: Node, name: &str) -> Result<Context>
pub fn set_node_name(&self, node: Node, name: &str) -> Result<Context>
sourcepub fn get_node_name(&self, node: Node) -> Result<String>
pub fn get_node_name(&self, node: Node) -> Result<String>
sourcepub fn retrieve_node(&self, graph: Graph, name: &str) -> Result<Node>
pub fn retrieve_node(&self, graph: Graph, name: &str) -> Result<Node>
Returns the node with a given name in a given graph.
Arguments
graph
- graphname
- node name
Returns
Node with a given name
Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let n = g.input(scalar_type(BIT)).unwrap();
n.set_name("input_node").unwrap();
assert!(n == c.retrieve_node(g, "input_node").unwrap());
Trait Implementations
sourceimpl<'de> Deserialize<'de> for Context
impl<'de> Deserialize<'de> for Context
sourcefn deserialize<D>(deserializer: D) -> Result<Context, D::Error> where
D: Deserializer<'de>,
fn deserialize<D>(deserializer: D) -> Result<Context, D::Error> where
D: Deserializer<'de>,
Deserialize this value from the given Serde deserializer. Read more
impl Eq for Context
Auto Trait Implementations
impl !RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
Blanket Implementations
sourceimpl<T> BorrowMut<T> for T where
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
const: unstable · sourcefn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more
sourceimpl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
impl<Q, K> Equivalent<K> for Q where
Q: Eq + ?Sized,
K: Borrow<Q> + ?Sized,
sourcefn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
Compare self to key
and return true
if they are equal.
sourceimpl<T> Serialize for T where
T: Serialize + ?Sized,
impl<T> Serialize for T where
T: Serialize + ?Sized,
fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<Ok, Error>
sourceimpl<T> ToOwned for T where
T: Clone,
impl<T> ToOwned for T where
T: Clone,
type Owned = T
type Owned = T
The resulting type after obtaining ownership.
sourcefn clone_into(&self, target: &mut T)
fn clone_into(&self, target: &mut T)
toowned_clone_into
)Uses borrowed data to replace owned data, usually by cloning. Read more