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.
§Rust crates
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§
Source§impl Context
Public methods which supposed to be imported in Python.
impl Context
Public methods which supposed to be imported in Python.
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>
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 get_node_name(&self, node: Node) -> Result<Option<String>>
pub fn get_node_name(&self, node: Node) -> Result<Option<String>>
Returns the name of a node.
§Arguments
node
- node
§Returns
Name of a node or None if it doesn’t have a name
§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = scalar_type(BIT);
let n = g.input(t).unwrap();
n.set_name("XOR").unwrap();
assert_eq!(c.get_node_name(n).unwrap(), Some("XOR".to_owned()));
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());
Sourcepub fn deep_equal(&self, context2: Context) -> bool
pub fn deep_equal(&self, context2: Context) -> bool
Check that two given contexts contain the same data, i.e. graphs, nodes, names, parameters.
Underlying structures that contain pointers (graphs, nodes) are compared by data they refer to.
§Arguments
context2
- context to compare
§Returns
true
if the given contexts contain the same content, otherwise false
Trait Implementations§
Source§impl<'de> Deserialize<'de> for Context
impl<'de> Deserialize<'de> for Context
Source§fn 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>,
impl Eq for Context
Auto Trait Implementations§
impl Freeze for Context
impl !RefUnwindSafe for Context
impl Send for Context
impl Sync for Context
impl Unpin for Context
impl !UnwindSafe for Context
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.