Skip to main content

TensorProgram

Struct TensorProgram 

Source
pub struct TensorProgram {
    pub layers: Vec<TensorLayer>,
    pub roots: Vec<Wire>,
    pub source_size: usize,
    pub value_count: usize,
    pub wire_order: Vec<Wire>,
    pub width: usize,
}

Fields§

§layers: Vec<TensorLayer>§roots: Vec<Wire>§source_size: usize§value_count: usize

Source cells plus live operations.

§wire_order: Vec<Wire>

wire_order[packed] is the wire held at that packed position.

§width: usize

The circuit’s width, for full-width results.

Implementations§

Source§

impl TensorProgram

Source

pub fn stored_weight_count(&self) -> usize

Examples found in repository?
examples/toggle.rs (line 216)
207fn main() {
208    let mut toggle = Toggle::new();
209    let program = toggle.program();
210    println!(
211        "toggle: {} source cells, {} built operations, {} live in {} layers, {} stored weights",
212        toggle.circuit().source_size(),
213        toggle.circuit().operations().len(),
214        program.live_operation_count(),
215        program.layers.len(),
216        program.stored_weight_count(),
217    );
218    let press = |kind, x, y| [PointerEvent { kind, x, y }];
219    let frames: [(&str, Vec<PointerEvent>); 4] = [
220        ("press on the track", press(PointerKind::Down, 640.0, 400.0).to_vec()),
221        ("release on the track", press(PointerKind::Up, 640.0, 400.0).to_vec()),
222        ("press 1/64 px left of the track", press(PointerKind::Down, 613.984_4, 400.0).to_vec()),
223        ("release on the track", press(PointerKind::Up, 640.0, 400.0).to_vec()),
224    ];
225    for (label, events) in frames {
226        let output = toggle.step(1280.0, 800.0, &events).unwrap();
227        println!("{label:32} -> on {} armed {}", output.on, output.armed);
228    }
229}
Source

pub fn live_operation_count(&self) -> usize

Examples found in repository?
examples/fizzbuzz.rs (line 25)
21fn main() {
22    let net = fizzbuzz_circuit();
23    println!(
24        "fizzbuzz as a ReLU network: {} operations in {} layers",
25        net.program.live_operation_count(),
26        net.program.layers.len()
27    );
28    let mut line = Vec::new();
29    for n in 1..=100u8 {
30        let answer = net.eval(&(n,)).unwrap();
31        assert_eq!(answer, fizzbuzz(n), "the network got {n} wrong");
32        line.push(match answer {
33            3 => "FizzBuzz".to_string(),
34            2 => "Buzz".to_string(),
35            1 => "Fizz".to_string(),
36            _ => n.to_string(),
37        });
38    }
39    println!("{}", line.join(" "));
40    println!("100 of 100 correct, with no training.");
41}
More examples
Hide additional examples
examples/toggle.rs (line 214)
207fn main() {
208    let mut toggle = Toggle::new();
209    let program = toggle.program();
210    println!(
211        "toggle: {} source cells, {} built operations, {} live in {} layers, {} stored weights",
212        toggle.circuit().source_size(),
213        toggle.circuit().operations().len(),
214        program.live_operation_count(),
215        program.layers.len(),
216        program.stored_weight_count(),
217    );
218    let press = |kind, x, y| [PointerEvent { kind, x, y }];
219    let frames: [(&str, Vec<PointerEvent>); 4] = [
220        ("press on the track", press(PointerKind::Down, 640.0, 400.0).to_vec()),
221        ("release on the track", press(PointerKind::Up, 640.0, 400.0).to_vec()),
222        ("press 1/64 px left of the track", press(PointerKind::Down, 613.984_4, 400.0).to_vec()),
223        ("release on the track", press(PointerKind::Up, 640.0, 400.0).to_vec()),
224    ];
225    for (label, events) in frames {
226        let output = toggle.step(1280.0, 800.0, &events).unwrap();
227        println!("{label:32} -> on {} armed {}", output.on, output.armed);
228    }
229}
examples/ui_panel.rs (line 137)
132fn main() {
133    let compiled = ui_frame_circuit();
134    let report = compiled.verify(20_000).unwrap();
135    eprintln!(
136        "ui_frame: {} live operations in {} layers; verified on {} sampled inputs",
137        compiled.program.live_operation_count(),
138        compiled.program.layers.len(),
139        report.checked
140    );
141    let _ = (TOGGLE, SLIDER_LEFT);
142    let mut executor = TensorExecutor::new(&compiled.program);
143    let mut source = Vec::new();
144    let mut state = (false, false, false, 0u8, 0.0f32);
145    println!("frame,x,y,held,pressed,released,on,armed,dragging,level,thumb");
146    for (frame, (x, y, pressed, released, held)) in script().into_iter().enumerate() {
147        let args = (state.0, state.1, state.2, state.3, state.4, x, y, pressed, released);
148        source.clear();
149        args.encode(&mut source);
150        let outputs = executor.run(&source);
151        let next = (outputs[0] == 1.0, outputs[1] == 1.0, outputs[2] == 1.0, outputs[3] as u8, outputs[4]);
152        let expected = ui_frame(state.0, state.1, state.2, state.3, state.4, x, y, pressed, released);
153        assert_eq!(next, expected, "frame {frame}: the circuit disagrees with ui_frame()");
154        state = next;
155        println!(
156            "{frame},{x},{y},{},{},{},{},{},{},{},{}",
157            held as u8, pressed as u8, released as u8, state.0 as u8, state.1 as u8, state.2 as u8, state.3, state.4
158        );
159    }
160}
examples/life.rs (line 39)
34fn main() {
35    let compiled = life_step_circuit();
36    let report = compiled.verify(200).unwrap();
37    eprintln!(
38        "life_step: {} live operations in {} layers; verified on {} random grids",
39        compiled.program.live_operation_count(),
40        compiled.program.layers.len(),
41        report.checked
42    );
43    // Two gliders, a lightweight spaceship and an R-pentomino.
44    let mut grid = [[false; N]; N];
45    for (row, column) in [(1, 2), (2, 3), (3, 1), (3, 2), (3, 3)] {
46        grid[row][column] = true;
47    }
48    for (row, column) in [(14, 4), (15, 5), (16, 3), (16, 4), (16, 5)] {
49        grid[row][column] = true;
50    }
51    for (row, column) in [(4, 14), (4, 17), (5, 18), (6, 14), (6, 18), (7, 15), (7, 16), (7, 17), (7, 18)] {
52        grid[row][column] = true;
53    }
54    for (row, column) in [(16, 16), (16, 17), (17, 15), (17, 16), (18, 16)] {
55        grid[row][column] = true;
56    }
57    let mut executor = TensorExecutor::new(&compiled.program);
58    let mut source = Vec::new();
59    for generation in 0..120 {
60        println!(
61            "{}",
62            grid.iter().flat_map(|row| row.iter().map(|&alive| if alive { '#' } else { '.' })).collect::<String>()
63        );
64        source.clear();
65        (grid,).encode(&mut source);
66        let outputs = executor.run(&source);
67        let next: [[bool; N]; N] =
68            std::array::from_fn(|row| std::array::from_fn(|column| outputs[row * N + column] == 1.0));
69        assert_eq!(next, life_step(grid), "generation {generation}: the circuit disagrees with life_step()");
70        grid = next;
71    }
72}

Trait Implementations§

Source§

impl Clone for TensorProgram

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 TensorProgram

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.