t27.aiРусский

Same neuron, new weights

You will learn

How one ternary neuron computes a majority vote with one set of weights and a different function with another.

maj3 in bitnet_majority.t27 packs three trits with pack3, takes dot27 against w_all_p, an all-P weight chunk, and quantizes at 0: the result is the sign of a + b + c. weighted_vote is the same neuron with weights pack3(2, 2, 0) and gives the sign of a + b - c, so the weights pick the function. The browser skips all 12 tests because it does not know assert_eq yet; the native t27c runs all 12, all pass, none vacuous. The recording lowers w_all_p by 16, which turns the weight on lane 2 from P to Z, so maj3 no longer sees c. Exactly one test fails, maj_p_n_n: +1 - 1 - 1 is N, but +1 - 1 is Z. Note that wv_p_z_p passes 0, which is N, as its middle input. Every byte in the recording was printed by the command; only the typing is staged.

Try it

In the recording, find the expected and the actual value of maj_p_n_n; then in the spec frame find wv_p_z_p and work out what weighted_vote(2, 1, 2) would return.

Open the interactive lesson →

t27c on bitnet_majority.t27 -- One neuron, two functions, native
t27c on bitnet_majority.t27 -- One neuron, two functions, native ↗

t27c on the t27c lab (Railway), spec at t27 5ff0ec512: 12 tests pass natively; one maj3 weight moved from P to Z fails exactly one test, maj_p_n_n; git restores the spec.

specs/ternary/bitnet_majority.t27

module BitnetMajority;
// Sign-only ternary multiply of two packed trits {N=0b00, Z=0b01, P=0b10}.
fn tmul(ta: u8, tb: u8) -> i8 {
    if (ta == 1) { return 0; }
    if (tb == 1) { return 0; }
    if (ta == tb) { return 1; }
    return -1;
}
// 27-trit ternary dot product of two 54-bit packed vectors.
fn dot27(a: u64, b: u64) -> i16 {
    var acc : i16 = 0;
    var i : u32 = 0;
    while (i < 27) {
        var ta : u8 = ((a >> (i << 1)) & 3) as u8;
        var tb : u8 = ((b >> (i << 1)) & 3) as u8;
        acc = acc + tmul(ta, tb) as i16;
        i = i + 1;
    }
    return acc;
}
// Ternary sign/threshold quantizer: v > +t -> P, v < -t -> N, else Z.
fn quantize(v: i16, threshold: i16) -> u8 {
    if (v > threshold) { return 2; }
    if (v < -threshold) { return 0; }
    return 1;
}
// Pack 3 trits into the low 3 lanes of a chunk; the rest are Z.
fn pack3(t0: u8, t1: u8, t2: u8) -> u64 {
    var z : u64 = 6004799503160661;
    var cleared : u64 = z & 18446744073709551552;
    return cleared | (t0 as u64) | ((t1 as u64) << 2) | ((t2 as u64) << 4);
}
// Ternary majority of three trits = sign of (a + b + c), realized as a spec-
// first single ternary neuron: pack the inputs, dot with an all-+1 weight chunk
// (so the dot product is exactly a + b + c over the three active lanes), then
// quantize at threshold 0. A recognizable named function computed by the
// BitNet stack. Inputs/output are packed trits {N=0, Z=1, P=2}.
pub fn maj3(a: u8, b: u8, c: u8) -> u8 {
    var chunk : u64 = pack3(a, b, c);
    var w_all_p : u64 = 12009599006321322;
    return quantize(dot27(chunk, w_all_p), 0);
}
// The SAME single neuron computes a DIFFERENT named function when the weights
// change -- the essence of a trained model. With per-input weights [+1, +1, -1]
// it computes sign(a + b - c) (weight P contributes +input, weight N contributes
// -input). Demonstrates that weights define the function, not the topology.
pub fn weighted_vote(a: u8, b: u8, c: u8) -> u8 {
    var chunk : u64 = pack3(a, b, c);
    var weights : u64 = pack3(2, 2, 0);
    return quantize(dot27(chunk, weights), 0);
}
test maj_p_p_n { assert_eq(maj3(2, 2, 0), 2); }
test maj_p_n_z { assert_eq(maj3(2, 0, 1), 1); }
test maj_n_n_p { assert_eq(maj3(0, 0, 2), 0); }
test maj_p_p_p { assert_eq(maj3(2, 2, 2), 2); }
test maj_z_z_z { assert_eq(maj3(1, 1, 1), 1); }
test maj_n_n_n { assert_eq(maj3(0, 0, 0), 0); }
test maj_p_n_n { assert_eq(maj3(2, 0, 0), 0); }
// weighted_vote = sign(a + b - c): flipping the 3rd weight to -1 changes the function.
test wv_p_p_p { assert_eq(weighted_vote(2, 2, 2), 2); }
test wv_p_z_p { assert_eq(weighted_vote(2, 0, 2), 0); }
test wv_z_z_z { assert_eq(weighted_vote(1, 1, 1), 1); }
test wv_p_n_n { assert_eq(weighted_vote(2, 0, 0), 2); }
test wv_z_z_p { assert_eq(weighted_vote(1, 1, 2), 0); }
endmodule

Open the lesson's spec in the player ↗

All lessons