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

Creates an empty computation graph in this context.

Returns

New computation graph

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

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

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

Returns the vector of graphs contained in this context in order of creation.

Returns

Vector of the graphs in this context

Does nothing if the context is finalized; otherwise returns a runtime error.

Returns

Runtime error if this context is not finalized

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

Returns

Main graph of the context

Returns the number of graphs contained in this context.

Returns

Number of the graphs in this context

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

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

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

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

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

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

Returns the name of a node.

Arguments

node - node

Returns

Name of a node

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(), "XOR".to_owned());

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

Trait Implementations

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

Performs copy-assignment from source. Read more

Formats the value using the given formatter. Read more

Deserialize this value from the given Serde deserializer. Read more

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

This method tests for !=.

Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations

Blanket Implementations

Gets the TypeId of self. Read more

Immutably borrows from an owned value. Read more

Mutably borrows from an owned value. Read more

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

Returns the argument unchanged.

Calls U::from(self).

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

The resulting type after obtaining ownership.

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

🔬 This is a nightly-only experimental API. (toowned_clone_into)

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

The type returned in the event of a conversion error.

Performs the conversion.

The type returned in the event of a conversion error.

Performs the conversion.