pub struct Circuit { /* private fields */ }Implementations§
Source§impl Circuit
impl Circuit
pub fn new(source_size: usize) -> Circuit
pub fn source_size(&self) -> usize
Sourcepub fn width(&self) -> usize
pub fn width(&self) -> usize
Source cells plus operations: the length of an evaluation’s values.
pub fn operations(&self) -> &[Operation]
pub fn operation(&self, wire: Wire) -> Option<&Operation>
pub fn range(&self, wire: Wire) -> Range
pub fn grid(&self, wire: Wire) -> Option<f64>
pub fn is_exact_bit(&self, wire: Wire) -> bool
Sourcepub fn constant_value(&self, wire: Wire) -> Option<f64>
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).
pub fn is_non_negative(&self, wire: Wire) -> bool
Sourcepub fn assume_range(&mut self, wire: Wire, min: f64, max: f64)
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.
Sourcepub fn assume_range_allowing_readers(&mut self, wire: Wire, min: f64, max: f64)
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.
Sourcepub fn assume_grid(&mut self, wire: Wire, step: f64)
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.
pub fn assume_grid_allowing_readers(&mut self, wire: Wire, step: f64)
Sourcepub fn assume_bit(&mut self, wire: Wire)
pub fn assume_bit(&mut self, wire: Wire)
Declare that the wire holds exactly 0 or 1. Narrows its range to 0..1.
Sourcepub fn assume_one_hot(&mut self, members: &[Wire], allow_all_zero: bool)
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.
Sourcepub fn assume_one_hot_allowing_readers(
&mut self,
members: &[Wire],
allow_all_zero: bool,
)
pub fn assume_one_hot_allowing_readers( &mut self, members: &[Wire], allow_all_zero: bool, )
assume_one_hot for members that operations already read.
pub fn one_hot_groups(&self) -> &[OneHotGroup]
Sourcepub fn validate_one_hot_groups(&self, values: &[f32]) -> Result<(), String>
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).
Sourcepub fn mark_rounding_point(&mut self, wire: Wire)
pub fn mark_rounding_point(&mut self, wire: Wire)
Record an operation as a rounding point, e.g. each step of a round chain.
Sourcepub fn rounding_points(&self) -> Vec<(Wire, RoundingPointKind)>
pub fn rounding_points(&self) -> Vec<(Wire, RoundingPointKind)>
Every rounding point in wire order.
pub fn source(&self, index: usize) -> Wire
Sourcepub fn constant(&mut self, value: f64) -> Wire
pub fn constant(&mut self, value: f64) -> Wire
A wire that always holds f(value). Ordinary affine readers fold it into their bias.
Sourcepub fn affine(&mut self, form: impl Into<Lin>) -> Wire
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.
Sourcepub fn round_affine(&mut self, form: impl Into<Lin>) -> Wire
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.
Sourcepub fn reglu(&mut self, left: impl Into<Lin>, gate: impl Into<Lin>) -> Wire
pub fn reglu(&mut self, left: impl Into<Lin>, gate: impl Into<Lin>) -> Wire
left × max(0, gate).
Sourcepub fn boolean(&mut self, score: Wire) -> Wire
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.
Sourcepub fn multiply(&mut self, left: Wire, right: Wire) -> Wire
pub fn multiply(&mut self, left: Wire, right: Wire) -> Wire
left × right, one ReGLU when either side is declared nonnegative, three otherwise.
Sourcepub fn select(&mut self, gate: Wire, off: Wire, on: Wire) -> Wire
pub fn select(&mut self, gate: Wire, off: Wire, on: Wire) -> Wire
gate × on + (1 - gate) × off. Both sides always compute.
Sourcepub fn index_read(
&mut self,
index: Wire,
candidates: &[Wire],
out_of_range: OutOfRange,
) -> Wire
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.
Sourcepub fn index_read_affine(
&mut self,
index: Lin,
candidates: &[Wire],
below: OutOfRange,
above: OutOfRange,
declared_index_range: Option<Range>,
) -> Wire
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.
Sourcepub fn validate_source_ranges(&self, source: &[f32]) -> Result<(), Error>
pub fn validate_source_ranges(&self, source: &[f32]) -> Result<(), Error>
Check that every source cell is within its declared range.
Sourcepub fn evaluate_reference(&self, source: &[f32]) -> Result<Vec<f32>, Error>
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.
Sourcepub fn backpropagate_reference(
&self,
values: &[f32],
output_gradients: &[f32],
options: BackpropOptions,
) -> Result<Vec<f32>, Error>
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
impl Circuit
Sourcepub fn inline_circuit(
&mut self,
source: &Circuit,
source_wires: &[Wire],
allow_existing_readers: bool,
) -> Vec<Wire>
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
impl Circuit
Sourcepub fn compile(&self, roots: Option<&[Wire]>) -> TensorProgram
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.
pub fn compile_with( &self, roots: Option<&[Wire]>, options: CompileOptions, ) -> TensorProgram
Sourcepub fn evaluate_tensor(
&self,
source: &[f32],
program: &TensorProgram,
) -> Result<Vec<f32>, Error>
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.
Sourcepub fn validate_tensor_evaluation(
&self,
source: &[f32],
program: &TensorProgram,
tensor_values: &[f32],
) -> Result<(), String>
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 ===.