Struct Node

Source
pub struct Node { /* private fields */ }
Expand description

A structure that stores a pointer to a computation graph node that corresponds to an operation.

Clone trait duplicates the pointer, not the underlying nodes.

PartialEq trait compares pointers, not the related nodes.

§Example

let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = scalar_type(BIT);
let n1 = g.input(t.clone()).unwrap();
let n2 = g.input(t).unwrap();
assert!(n1 != n2);
let n3 = n1.clone();
assert!(n1 == n3);

Implementations§

Source§

impl Node

Public methods which supposed to be imported in Python.

Source

pub fn get_graph(&self) -> Graph

Returns the parent graph that contains the node.

§Returns

Parent graph of the node

Source

pub fn get_node_dependencies(&self) -> Vec<Node>

Returns the dependency nodes that are used to compute the value in the current node.

§Returns

Vector of nodes used by the node to perform its operation

Source

pub fn get_graph_dependencies(&self) -> Vec<Graph>

Returns the dependency graphs that are used to compute the value in the current node.

These dependencies are non-empty only for Call and Iterate operations.

§Returns

Vector of graphs used by the node to perform its operation

Source

pub fn get_id(&self) -> u64

Returns the ID of the node.

A node ID is a serial number of a node between 0 and n-1 where n is the number of nodes in the parent graph. This number is equal to the number of nodes in the parent graph before this node was added to it.

§Returns

Node ID

Source

pub fn get_global_id(&self) -> (u64, u64)

Returns the pair of the parent graph ID and node ID

§Returns

(Graph ID, Node ID)

Source

pub fn get_operation(&self) -> Operation

Returns the operation associated with the node.

§Returns

Operation associated with the node

Source

pub fn get_type(&self) -> Result<Type>

Returns the type of the value computed by the node.

§Returns

Output type of the node operation

Source

pub fn set_name(&self, name: &str) -> Result<Node>

Applies Context::set_node_name to the parent context and this node. Returns the clone of this.

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

pub fn get_name(&self) -> Result<Option<String>>

Applies Context::get_node_name to the parent context and this 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!(n.get_name().unwrap(), Some("XOR".to_owned()));
Source

pub fn add(&self, b: Node) -> Result<Node>

Adds a node to the parent graph that adds elementwise the array or scalar associated with the node to an array or scalar of the same scalar type associated with another node.

Applies Graph::add to the parent graph, this node and the b node.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = scalar_type(BIT);
let n1 = g.input(t.clone()).unwrap();
let n2 = g.input(t).unwrap();
let n3 = n1.add(n2).unwrap();
Source

pub fn subtract(&self, b: Node) -> Result<Node>

Adds a node to the parent graph that subtracts elementwise the array or scalar of the same scalar type associated with another node from an array or scalar associated with the node.

Applies Graph::subtract to the parent graph, this node and the b node.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = scalar_type(BIT);
let n1 = g.input(t.clone()).unwrap();
let n2 = g.input(t).unwrap();
let n3 = n1.subtract(n2).unwrap();
Source

pub fn multiply(&self, b: Node) -> Result<Node>

Adds a node to the parent graph that multiplies elementwise the array or scalar associated with the node by an array or scalar of the same scalar type associated with another node.

Applies Graph::multiply to the parent graph, this node and the b node.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = scalar_type(BIT);
let n1 = g.input(t.clone()).unwrap();
let n2 = g.input(t).unwrap();
let n3 = n1.multiply(n2).unwrap();
Source

pub fn mixed_multiply(&self, b: Node) -> Result<Node>

Adds a node to the parent graph that multiplies elementwise the array or scalar associated with the node by a binary array or scalar associated with another node.

Applies Graph::mixed_multiply to the parent graph, this node and the b node.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = scalar_type(INT32);
let bit_t = scalar_type(BIT);
let n1 = g.input(t).unwrap();
let n2 = g.input(bit_t).unwrap();
let n3 = n1.mixed_multiply(n2).unwrap();
Source

pub fn dot(&self, b: Node) -> Result<Node>

Adds a node to the parent graph that computes the dot product of arrays or scalars associated with the node and another node.

Applies Graph::dot to the parent graph, this node and the b node.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![10], INT32);
let n1 = g.input(t.clone()).unwrap();
let n2 = g.input(t).unwrap();
let n3 = n1.dot(n2).unwrap();
Source

pub fn matmul(&self, b: Node) -> Result<Node>

Adds a node to the parent graph that computes the matrix product of two arrays associated with the node and another node.

Applies Graph::matmul to the parent graph, this node and the b node.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t1 = array_type(vec![2, 3], INT32);
let t2 = array_type(vec![3, 2], INT32);
let n1 = g.input(t1).unwrap();
let n2 = g.input(t2).unwrap();
let n3 = n1.matmul(n2).unwrap();
Source

pub fn join( &self, b: Node, t: JoinType, headers: HashMap<String, String>, ) -> Result<Node>

Adds a node that computes a join of a given type on two named tuples along given key headers. More detailed documentation can be found in Graph::join.

Applies Graph::join to the parent graph, this node and the b node.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t1n = array_type(vec![100], BIT);
let t11 = array_type(vec![100], INT32);
let t12 = array_type(vec![100, 128], BIT);
let t13 = array_type(vec![100], INT64);
let t2n = array_type(vec![50], BIT);
let t21 = array_type(vec![50], INT32);
let t22 = array_type(vec![50, 128], BIT);
let t23 = array_type(vec![50], UINT8);
let t1 = named_tuple_type(vec![
    (NULL_HEADER.to_owned(), t1n),
    ("ID".to_owned(), t11),
    ("Occupation".to_owned(), t12),
    ("Revenue".to_owned(), t13),
]);
let t2 = named_tuple_type(vec![
    (NULL_HEADER.to_owned(), t2n),
    ("ID".to_owned(), t21),
    ("Job".to_owned(), t22),
    ("Age".to_owned(), t23),
]);
let n1 = g.input(t1).unwrap();
let n2 = g.input(t2).unwrap();
let n3 = n1.join(n2, JoinType::Inner, HashMap::from([
    ("ID".to_owned(), "ID".to_owned()),
    ("Occupation".to_owned(), "Job".to_owned()),
])).unwrap();
Source

pub fn join_with_column_masks( &self, b: Node, t: JoinType, headers: HashMap<String, String>, ) -> Result<Node>

Adds a node that computes a join of a given type on two named tuples along given key headers. More detailed documentation can be found in Graph::join_with_column_masks.

Applies Graph::join_with_column_masks to the parent graph, this node and the b node.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t1n = array_type(vec![100], BIT);
let t11 = tuple_type(vec![array_type(vec![100], BIT), array_type(vec![100], INT32)]);
let t12 = tuple_type(vec![array_type(vec![100], BIT), array_type(vec![100, 128], BIT)]);
let t13 = tuple_type(vec![array_type(vec![100], BIT), array_type(vec![100], INT64)]);
let t2n = array_type(vec![50], BIT);
let t21 = tuple_type(vec![array_type(vec![50], BIT), array_type(vec![50], INT32)]);
let t22 = tuple_type(vec![array_type(vec![50], BIT), array_type(vec![50, 128], BIT)]);
let t23 = tuple_type(vec![array_type(vec![50], BIT), array_type(vec![50], UINT8)]);
let t1 = named_tuple_type(vec![
    (NULL_HEADER.to_owned(), t1n),
    ("ID".to_owned(), t11),
    ("Occupation".to_owned(), t12),
    ("Revenue".to_owned(), t13),
]);
let t2 = named_tuple_type(vec![
    (NULL_HEADER.to_owned(), t2n),
    ("ID".to_owned(), t21),
    ("Job".to_owned(), t22),
    ("Age".to_owned(), t23),
]);
let n1 = g.input(t1).unwrap();
let n2 = g.input(t2).unwrap();
let n3 = n1.join_with_column_masks(n2, JoinType::Inner, HashMap::from([
    ("ID".to_owned(), "ID".to_owned()),
    ("Occupation".to_owned(), "Job".to_owned()),
])).unwrap();
Source

pub fn sort(&self, key: String) -> Result<Node>

Adds a node that sorts a table given as named tuple according to the column given by the key argument. The key column must be a 2-d BIT array of shape [n, b], interpreted as bitstrings of length b. Other columns in the named tuple must be arrays of arbitrary type and shape, as long as they share the first dimension: [n, …]. Bitstrings are sorted lexicographically, and the sorting algorithm is stable: preserving relative order of entries in other arrays where the corresponding key entries match.

§Arguments
  • key - name of the field to sort on it, this array must be 2-d of type BIT.
§Returns

New sorted node

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let v1 = g.input(array_type(vec![20], INT32)).unwrap();
let v2 = g.input(array_type(vec![20, 10, 2], UINT64)).unwrap();
let k = g.input(array_type(vec![20, 32], BIT)).unwrap();
let a = g.create_named_tuple(vec![("key".to_string(), k), ("value1".to_string(), v1), ("value2".to_string(), v2)]).unwrap();
let a = a.sort("key".to_string()).unwrap();
Source

pub fn truncate(&self, scale: u128) -> Result<Node>

Adds a node to the parent graph that divides a scalar or each entry of the array associated with the node by a positive constant integer scale.

Applies Graph::add to the parent graph, this node and scale.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![2, 3], INT32);
let n1 = g.input(t).unwrap();
let n2 = n1.truncate(4).unwrap();
Source

pub fn sum(&self, axes: ArrayShape) -> Result<Node>

Adds a node to the parent graph that computes the sum of entries of the array associated with the node along given axes.

Applies Graph::sum to the parent graph, this node and axes.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![3, 2, 3], INT32);
let axes = vec![1, 0];
let n1 = g.input(t).unwrap();
let n2 = n1.sum(axes).unwrap();
Source

pub fn cum_sum(&self, axis: u64) -> Result<Node>

Adds a node to the parent graph that computes the cumulative sum of elements along a given axis.

Applies Graph::cum_sum to the parent graph, this node and axis.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![3, 2], INT32);
let n1 = g.input(t).unwrap();
let n2 = n1.cum_sum(1).unwrap();
Source

pub fn permute_axes(&self, axes: ArrayShape) -> Result<Node>

Adds a node to the parent graph that permutes the array associated with the node along given axes.

Applies Graph::permute_axes to the parent graph, this node and axes.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![3, 2, 3], INT32);
let axes = vec![1, 0, 2];
let n1 = g.input(t).unwrap();
let n2 = n1.permute_axes(axes).unwrap();
Source

pub fn get(&self, index: ArrayShape) -> Result<Node>

Adds a node to the parent graph that extracts a sub-array with a given index from the array associated with the node.

Applies Graph::get to the parent graph, this node and index.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![3, 2, 3], INT32);
let index = vec![2];
let n1 = g.input(t).unwrap();
let n2 = n1.get(index).unwrap();
Source

pub fn get_slice(&self, slice: Slice) -> Result<Node>

Adds a node that extracts a sub-array corresponding to a given slice from the array associated with the node.

Applies Graph::get_slice to the parent graph, this node and slice.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![3, 2, 3], INT32);
let slice = vec![SliceElement::Ellipsis, SliceElement::SubArray(None, None, Some(-2))];
let n1 = g.input(t).unwrap();
let n2 = n1.get_slice(slice).unwrap();
Source

pub fn reshape(&self, new_type: Type) -> Result<Node>

Adds a node to the parent graph that reshapes a value associated with the node to a given compatible type.

Applies Graph::reshape to the parent graph, this node and new_type.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let old_t = array_type(vec![3, 2, 3], INT32);
let new_t = array_type(vec![3,6], INT32);
let n1 = g.input(old_t).unwrap();
let n2 = n1.reshape(new_t).unwrap();
Source

pub fn a2b(&self) -> Result<Node>

Adds a node to the parent graph converting an integer array or scalar associated with the node to the binary form.

Applies Graph::a2b to the parent graph and this node.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![3, 2], INT32);
let n1 = g.input(t).unwrap();
let n2 = n1.a2b().unwrap();
Source

pub fn b2a(&self, scalar_type: ScalarType) -> Result<Node>

Adds a node to the parent graph converting a binary array associated with the node to an array of a given scalar type.

Applies Graph::b2a to the parent graph, this node and scalar_type.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![3, 32], BIT);
let n1 = g.input(t).unwrap();
let n2 = n1.b2a(INT32).unwrap();
Source

pub fn tuple_get(&self, index: u64) -> Result<Node>

Adds a node that extracts an element of a tuple associated with the node.

Applies Graph::tuple_get to the parent graph, this node and index.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t1 = array_type(vec![3, 2, 3], INT32);
let t2 = array_type(vec![2, 3], INT32);
let n1 = g.input(t1).unwrap();
let n2 = g.input(t2).unwrap();
let n3 = g.create_tuple(vec![n1, n2]).unwrap();
let n4 = n3.tuple_get(1).unwrap();
Source

pub fn named_tuple_get(&self, key: String) -> Result<Node>

Adds a node to the parent graph that extracts an element of a named tuple associated with the node.

Applies Graph::named_tuple_get to the parent graph, this node and the key string.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t1 = array_type(vec![3, 2, 3], INT32);
let t2 = array_type(vec![2, 3], INT32);
let n1 = g.input(t1).unwrap();
let n2 = g.input(t2).unwrap();
let n3 = g.create_named_tuple(vec![("node1".to_owned(), n1), ("node2".to_owned(), n2)]).unwrap();
let n4 = n3.named_tuple_get("node2".to_owned()).unwrap();
Source

pub fn vector_get(&self, index: Node) -> Result<Node>

Adds a node to the parent graph that extracts an element of a vector associated with the node.

Applies Graph::vector_get to the parent graph, this node and the index node.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![3, 2, 3], INT32);
let n1 = g.input(t.clone()).unwrap();
let n2 = g.input(t.clone()).unwrap();
let n3 = g.create_vector(t, vec![n1,n2]).unwrap();
let index = g.constant(scalar_type(UINT32), Value::from_scalar(0, UINT32).unwrap()).unwrap();
let n4 = n3.vector_get(index).unwrap();
Source

pub fn array_to_vector(&self) -> Result<Node>

Adds a node to the parent graph converting an array associated with the node to a vector.

Applies Graph::array_to_vector to the parent graph and this node.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![4, 3, 2], INT32);
let n1 = g.input(t).unwrap();
let n2 = g.array_to_vector(n1).unwrap();
let index = g.constant(scalar_type(UINT32), Value::from_scalar(0, UINT32).unwrap()).unwrap();
let n3 = n2.vector_get(index).unwrap();

assert!(n2.get_type().unwrap().is_vector());
assert_eq!(n3.get_type().unwrap().get_shape(), vec![3,2]);
Source

pub fn vector_to_array(&self) -> Result<Node>

Adds a node to the parent graph converting a vector associated with the node to an array.

Applies Graph::vector_to_array to the parent graph and this node.

§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 = n1.vector_to_array().unwrap();

assert!(n2.get_type().unwrap().is_array());
assert_eq!(n2.get_type().unwrap().get_shape(), vec![4, 3, 2]);
Source

pub fn gather(&self, indices: Node, axis: u64) -> Result<Node>

Adds a node to the parent graph converting a vector associated with the node to an array.

Applies Graph::gather to the parent graph and this node.

Source

pub fn repeat(&self, n: u64) -> Result<Node>

Adds a node that creates a vector with n copies of a value of this node.

Applies Graph::repeat to the parent graph, this node and n.

§Example
let c = create_context().unwrap();
let g = c.create_graph().unwrap();
let t = array_type(vec![3, 2, 3], INT32);
let n1 = g.input(t).unwrap();
let n2 = n1.repeat(10).unwrap();
Source

pub fn print(&self, message: String) -> Result<Node>

Adds an operation which logs the value of the node at runtime.

Source

pub fn set_as_output(&self) -> Result<Node>

Applies Graph::set_output_node to the parent graph and this node.

§Returns

This node

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

Trait Implementations§

Source§

impl Clone for Node

Source§

fn clone(&self) -> Self

Returns a new Node 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 Display for Node

Source§

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

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

impl Hash for Node

Source§

fn hash<H: Hasher>(&self, state: &mut H)

Hashes the node pointer.

§Arguments

state - state of a hash function that is changed after hashing the node

1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for Node

Source§

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

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

§Arguments

other - another Node value

§Returns

true if self and other are equal, false otherwise

1.0.0 · Source§

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 Eq for Node

Auto Trait Implementations§

§

impl Freeze for Node

§

impl !RefUnwindSafe for Node

§

impl Send for Node

§

impl Sync for Node

§

impl Unpin for Node

§

impl !UnwindSafe for Node

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