Skip to main content

Circuit

Struct Circuit 

Source
pub struct Circuit { /* private fields */ }

Implementations§

Source§

impl Circuit

Source

pub fn new(source_size: usize) -> Circuit

Source

pub fn source_size(&self) -> usize

Source

pub fn width(&self) -> usize

Source cells plus operations: the length of an evaluation’s values.

Source

pub fn operations(&self) -> &[Operation]

Source

pub fn operation(&self, wire: Wire) -> Option<&Operation>

Source

pub fn range(&self, wire: Wire) -> Range

Source

pub fn grid(&self, wire: Wire) -> Option<f64>

Source

pub fn is_exact_bit(&self, wire: Wire) -> bool

Source

pub fn constant_value(&self, wire: Wire) -> Option<f64>

The value a constant wire holds before rounding, e.g. Some(3.0) for constant(3.0).

Source

pub fn depth(&self, wire: Wire) -> u32

The build-time layer of a wire: 0 for a source cell.

Source

pub fn is_non_negative(&self, wire: Wire) -> bool

Source

pub fn assume_range(&mut self, wire: Wire, min: f64, max: f64)

Declare the range of a wire’s values, a promise from the caller. Panics when it narrows the range of a wire that operations already read, since they were built with the old range.

Source

pub fn assume_range_allowing_readers(&mut self, wire: Wire, min: f64, max: f64)

assume_range for a wire that operations already read. Those operations keep the range they were built with.

Source

pub fn assume_grid(&mut self, wire: Wire, step: f64)

Declare that every value of the wire is a multiple of step, a power of two.

Source

pub fn assume_grid_allowing_readers(&mut self, wire: Wire, step: f64)

Source

pub fn assume_bit(&mut self, wire: Wire)

Declare that the wire holds exactly 0 or 1. Narrows its range to 0..1.

Source

pub fn assume_one_hot(&mut self, members: &[Wire], allow_all_zero: bool)

Declare that members are exact bits of which exactly one is 1, or, with allow_all_zero, at most one. Narrows every member’s range to 0..1, panicking like assume_range when that changes a wire that operations already read; declare a group before the code that reads its members. Compilation reads every group, including one declared late.

Source

pub fn assume_one_hot_allowing_readers( &mut self, members: &[Wire], allow_all_zero: bool, )

assume_one_hot for members that operations already read.

Source

pub fn one_hot_groups(&self) -> &[OneHotGroup]

Source

pub fn validate_one_hot_groups(&self, values: &[f32]) -> Result<(), String>

Check evaluated values against every declared one-hot group: each member exactly 0 or 1, and exactly one of them 1 (or none, for a group that allows all zeros).

Source

pub fn mark_rounding_point(&mut self, wire: Wire)

Record an operation as a rounding point, e.g. each step of a round chain.

Source

pub fn rounding_points(&self) -> Vec<(Wire, RoundingPointKind)>

Every rounding point in wire order.

Source

pub fn source(&self, index: usize) -> Wire

Source

pub fn constant(&mut self, value: f64) -> Wire

A wire that always holds f(value). Ordinary affine readers fold it into their bias.

Source

pub fn affine(&mut self, form: impl Into<Lin>) -> Wire

A weighted sum, e.g. circuit.affine(width * 0.5 - 26.0). Small sums are copied into the operations that read them.

Source

pub fn round_affine(&mut self, form: impl Into<Lin>) -> Wire

A weighted sum that keeps its evaluation, term order and operand wires, including constants, so readers never copy its terms.

Source

pub fn reglu(&mut self, left: impl Into<Lin>, gate: impl Into<Lin>) -> Wire

left × max(0, gate).

Source

pub fn relu(&mut self, value: impl Into<Lin>) -> Wire

max(0, value).

Source

pub fn boolean(&mut self, score: Wire) -> Wire

Rounds a score within 0..1 to 0 below 0.5 and to 1 from 0.5 up.

Source

pub fn multiply(&mut self, left: Wire, right: Wire) -> Wire

left × right, one ReGLU when either side is declared nonnegative, three otherwise.

Source

pub fn select(&mut self, gate: Wire, off: Wire, on: Wire) -> Wire

gate × on + (1 - gate) × off. Both sides always compute.

Source

pub fn index_read( &mut self, index: Wire, candidates: &[Wire], out_of_range: OutOfRange, ) -> Wire

candidates[floor(index)] as one operation. The index needs a declared range strictly inside ±2^24 and must hold an integer.

Source

pub fn index_read_affine( &mut self, index: Lin, candidates: &[Wire], below: OutOfRange, above: OutOfRange, declared_index_range: Option<Range>, ) -> Wire

An index read with the index written as an affine form and one rule per side.

Source

pub fn validate_source_ranges(&self, source: &[f32]) -> Result<(), Error>

Check that every source cell is within its declared range.

Source

pub fn evaluate_reference(&self, source: &[f32]) -> Result<Vec<f32>, Error>

What the program computes: every wire’s value, one operation at a time in stored order. A compiled program must reproduce every live value.

Source

pub fn backpropagate_reference( &self, values: &[f32], output_gradients: &[f32], options: BackpropOptions, ) -> Result<Vec<f32>, Error>

Reverse-mode gradients of the intended real arithmetic: values from evaluate_reference, output_gradients one per wire. ReLUs get slope 0 at their corner; products and selects are differentiated as a whole.

Source§

impl Circuit

Source

pub fn inline_circuit( &mut self, source: &Circuit, source_wires: &[Wire], allow_existing_readers: bool, ) -> Vec<Wire>

Rebuild every operation of source in this circuit, with source cell k read from source_wires[k], and return where each of source’s wires landed, indexed by its wire. The operations go through this circuit’s builder, so an expression that already exists here is shared, e.g. a sum both circuits compute. Declared ranges, grids and exact bits of source’s cells land on the mapped wires, and rounding points, comparison bits and one-hot groups carry over. With allow_existing_readers, those declarations may narrow wires that operations here already read (see assume_range_allowing_readers).

Source§

impl Circuit

Source

pub fn compile(&self, roots: Option<&[Wire]>) -> TensorProgram

Compile the operations that roots need, or every operation when roots is None, with the default CompileOptions.

Source

pub fn compile_with( &self, roots: Option<&[Wire]>, options: CompileOptions, ) -> TensorProgram

Source

pub fn evaluate_tensor( &self, source: &[f32], program: &TensorProgram, ) -> Result<Vec<f32>, Error>

Every wire’s value through the compiled program, with 0 for removed wires.

Source

pub fn validate_tensor_evaluation( &self, source: &[f32], program: &TensorProgram, tensor_values: &[f32], ) -> Result<(), String>

Check that a compiled evaluation reproduces every live value of the reference evaluator, compared with == like handwired’s ===.

Trait Implementations§

Source§

impl Clone for Circuit

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Circuit

Source§

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

Formats the value using the given formatter. Read more

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<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<E> StoreElem<usize> for E

Source§

fn store(items: &mut [E], index: usize, value: E)

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, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.