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§

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

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

This method 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 Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

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

source§

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

Mutably borrows from an owned value. Read more
source§

impl<Q, K> Equivalent<K> for Qwhere 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 Twhere 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> Serialize for Twhere T: Serialize + ?Sized,

source§

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

source§

impl<T> ToOwned for Twhere T: Clone,

§

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 Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

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

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

§

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 Twhere U: TryFrom<T>,

§

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

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

§

fn vzip(self) -> V

source§

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