t27.aiEnglish

Детектор полосы

Вы узнаете

Почему веса нейрона работают как шаблон.

Дайте нейрону веса вертикальной полосы, и его сумма будет считать совпадения минус расхождения с ними. Вертикальная полоса даёт сумму 9 и ответ +1, перевёрнутая полоса — сумму -9 и ответ -1, горизонтальная полоса и диагональ дают 1, а шум — 0. Спека урока — тот же нейрон в железе: комбинационный нейрон BitNet на одном куске из 27 тритов.

Попробовать

Меняйте вход, пока сумма не станет ровно равна порогу 5; затем найдите самое маленькое изменение вертикальной полосы, после которого нейрон перестаёт срабатывать.

Открыть интерактивный урок →

A bar detector: one neuron, four patterns
A bar detector: one neuron, four patterns ↗

The weights are a vertical bar. Try every input: the bar fires +1, the inverted bar -1, other shapes and noise stay at 0, with the sums listed.

specs/ternary/comb_bitnet_neuron.t27

module CombBitnetNeuron;
// #1764: a full COMBINATIONAL BitNet neuron in one module = MAC + activation.
//
// `on_comb(a, b)` takes a packed 27-trit weight vector `a` and activation vector
// `b` on input data ports, computes their bit-exact ternary dot product, and
// re-ternarizes the sum to a trit output `result = quantize(dot27(a, b))`. That
// is exactly a BitNet neuron over one 27-trit chunk: weighted sum -> sign.
//
// It is a single self-contained hardware module: (a, b) input ports -> a LUT
// adder-tree (dot27) -> sign comparators (quantize) -> `result` output port,
// generated from spec and synthesizing to Artix-7 LUTs with NO flip-flops
// (purely combinational). Composed with the streaming accumulator
// (stream_ternary_mac.t27) this scales to a multi-chunk neuron; here the whole
// neuron is combinational, so a single (a, b) pair yields the activation in one
// LUT delay.
//
// dot27 / tmul / tp are the bit-exact primitives (#1743); quantize is the
// ternary activation (sign with a zero dead-band).

fn tmul(ta: u8, tb: u8) -> i8 {
    if (ta == 1) { return 0; }
    if (tb == 1) { return 0; }
    if (ta == tb) { return 1; }
    return -1;
}
fn tp(a: u64, b: u64, i: u32) -> i8 {
    return tmul(((a >> (i << 1)) & 3) as u8, ((b >> (i << 1)) & 3) as u8);
}
fn dot27(a: u64, b: u64) -> i8 {
    return tp(a,b,0) + tp(a,b,1) + tp(a,b,2) + tp(a,b,3) + tp(a,b,4)
         + tp(a,b,5) + tp(a,b,6) + tp(a,b,7) + tp(a,b,8) + tp(a,b,9)
         + tp(a,b,10) + tp(a,b,11) + tp(a,b,12) + tp(a,b,13) + tp(a,b,14)
         + tp(a,b,15) + tp(a,b,16) + tp(a,b,17) + tp(a,b,18) + tp(a,b,19)
         + tp(a,b,20) + tp(a,b,21) + tp(a,b,22) + tp(a,b,23) + tp(a,b,24)
         + tp(a,b,25) + tp(a,b,26);
}
// Ternary activation: sign of the weighted sum into a trit {N=0, Z=1, P=2}.
fn quantize(v: i8) -> u8 {
    if (v > 0) { return 2; }
    if (v < 0) { return 0; }
    return 1;
}

// The neuron: weighted sum then sign, exposed as `result` output port.
fn on_comb(a: u64, b: u64) -> u8 {
    return quantize(dot27(a, b));
}

test neuron_pp { assert_eq(on_comb(12009599006321322, 12009599006321322), 2); }
test neuron_np { assert_eq(on_comb(0, 12009599006321322), 0); }
test neuron_zz { assert_eq(on_comb(6004799503160661, 6004799503160661), 1); }
test neuron_nn { assert_eq(on_comb(0, 0), 2); }
endmodule

Открыть спеку урока в плеере ↗

Все уроки