Struct 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.

§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.

Source

pub fn create_graph(&self) -> Result<Graph>

Creates an empty computation graph in this context.

§Returns

New computation graph

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
Source

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();
Source

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();
Source

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

Source

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

Source

pub fn get_main_graph(&self) -> Result<Graph>

Returns the main graph of the context if it is already set.

§Returns

Main graph of the context

Source

pub fn get_num_graphs(&self) -> u64

Returns the number of graphs contained in this context.

§Returns

Number of the graphs in this context

Source

pub fn get_graph_by_id(&self, id: u64) -> Result<Graph>

Returns the graph contained in this context with a given ID.

A graph ID is a serial number of a graph between 0 and n-1 where n is the number of graphs in this context.

§Arguments

id - ID of a graph

§Returns

Graph with the given ID

Source

pub fn get_node_by_global_id(&self, id: (u64, u64)) -> Result<Node>

Returns the node contained in this context with a given global ID.

The global ID of a node is a pair of the node ID and the ID of its parent graph.

§Arguments

id - tuple (graph ID, node ID)

§Returns

Node with the given global ID

Source

pub fn set_graph_name(&self, graph: Graph, name: &str) -> Result<Context>

Sets the name of a graph.

A given name should be unique.

§Arguments
  • graph - graph
  • name - name of the graph
§Returns

This context

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
g.set_name("relu").unwrap();
Source

pub fn get_graph_name(&self, graph: Graph) -> Result<String>

Returns the name of a graph.

§Arguments

graph - graph

§Returns

Name of a given graph

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
g.set_name("relu").unwrap();
assert_eq!(c.get_graph_name(g).unwrap(), "relu".to_owned());
Source

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());
Source

pub fn set_node_name(&self, node: Node, name: &str) -> Result<Context>

Sets the name of a node.

A given name should be unique.

§Arguments
  • node - node
  • name - name of a node
§Returns

This context

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = scalar_type(BIT);
let n = g.input(t).unwrap();
c.set_node_name(n, "XOR").unwrap();
Source

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()));
Source

pub fn retrieve_node(&self, graph: Graph, name: &str) -> Result<Node>

Returns the node with a given name in a given graph.

§Arguments
  • graph - graph
  • name - 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());
Source

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 Clone for Context

Source§

fn clone(&self) -> Self

Returns a new Context value with a copy of the pointer to a node.

1.0.0 · Source§

const fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Context

Source§

fn deserialize<D>(deserializer: D) -> Result<Context, D::Error>
where D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Context

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for Context

Source§

fn eq(&self, other: &Self) -> bool

Tests whether self and other contexts are equal via comparison of their respective pointers.

§Arguments

other - another Context value

§Returns

true if self and other are equal, false otherwise

1.0.0 · Source§

const fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for Context

Source§

fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for Context

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> Serialize for T
where T: Serialize + ?Sized,

Source§

fn erased_serialize(&self, serializer: &mut dyn Serializer) -> Result<(), Error>

Source§

fn do_erased_serialize( &self, serializer: &mut dyn Serializer, ) -> Result<(), ErrorImpl>

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,