fix: rewrite layer_tb and neuron_parallel_tb for current INT8 architecture

Both testbenches instantiated their DUTs with a FRAC_BITS parameter and
Q8.8 fixed-point 16-bit values, which no longer exist in rtl/neuron_parallel.v
(now plain INT8, DATA_WIDTH=8, hardcoded +127 saturation, ReLU-only clamp).
This made both tests fail elaboration ("parameter FRAC_BITS not found").

Rewrote both benches with integer INT8 stimuli and expectations matching
the current core (no RTL changes): neuron_parallel_tb covers a mixed
vector, ReLU, positive saturation, and mixed values with a boundary
negative bias; layer_tb covers 8 neurons exercising scale, ReLU,
saturation, bias-only, and a sparse weight pattern across groups.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01WQV3vS9TXaGDJ5cRfnfidt
This commit is contained in:
2026-09-02 14:16:34 +02:00
co-authored by Claude Sonnet 5
parent 896f56c675
commit 4b5cd4e558
7 changed files with 17623 additions and 8338 deletions
+3360 -3212
View File
File diff suppressed because one or more lines are too long
+6285 -3456
View File
File diff suppressed because it is too large Load Diff
+55 -60
View File
@@ -1,11 +1,12 @@
`timescale 1ns/1ps
module tb;
parameter DATA_WIDTH = 16;
parameter FRAC_BITS = 8;
parameter N_INPUTS = 64;
parameter DATA_WIDTH = 8;
parameter N_INPUTS = 32;
parameter N_NEURONS = 8;
parameter PARALLEL = 8;
parameter ACC_WIDTH = 40;
parameter ACC_WIDTH = 32;
reg clk;
reg rst;
@@ -26,12 +27,10 @@ module tb;
wire done;
integer i;
integer n;
integer errors;
layer #(
.DATA_WIDTH(DATA_WIDTH),
.FRAC_BITS(FRAC_BITS),
.N_INPUTS(N_INPUTS),
.N_NEURONS(N_NEURONS),
.PARALLEL(PARALLEL),
@@ -53,13 +52,6 @@ module tb;
forever #5 clk = ~clk;
end
function signed [15:0] q8_8;
input real value;
begin
q8_8 = $rtoi(value * 256.0);
end
endfunction
task run_layer;
begin
@(posedge clk);
@@ -92,41 +84,41 @@ module tb;
/*
* Input vector:
* all inputs = 1.0
* all inputs = 1
*/
for (i = 0; i < N_INPUTS; i = i + 1)
x_bus[i*DATA_WIDTH +: DATA_WIDTH] = q8_8(1.0);
x_bus[i*DATA_WIDTH +: DATA_WIDTH] = 8'sd1;
/*
* Neuron 0:
* weights = 1.0
* weights = 1
* bias = 0
* result = 64
* result = 32
*/
for (i = 0; i < N_INPUTS; i = i + 1)
weights_bus[
0*N_INPUTS*DATA_WIDTH +
i*DATA_WIDTH +:
DATA_WIDTH
] = q8_8(1.0);
] = 8'sd1;
bias_bus[0*DATA_WIDTH +: DATA_WIDTH] = q8_8(0.0);
bias_bus[0*DATA_WIDTH +: DATA_WIDTH] = 8'sd0;
/*
* Neuron 1:
* weights = 0.5
* result = 32
* weights = 3
* result = 96
*/
for (i = 0; i < N_INPUTS; i = i + 1)
weights_bus[
1*N_INPUTS*DATA_WIDTH +
i*DATA_WIDTH +:
DATA_WIDTH
] = q8_8(0.5);
] = 8'sd3;
/*
* Neuron 2:
* weights = -0.5
* weights = -1
* result = -32 -> ReLU = 0
*/
for (i = 0; i < N_INPUTS; i = i + 1)
@@ -134,90 +126,93 @@ module tb;
2*N_INPUTS*DATA_WIDTH +
i*DATA_WIDTH +:
DATA_WIDTH
] = q8_8(-0.5);
] = -8'sd1;
/*
* Neuron 3:
* weights = 2.0
* result = 128 -> saturation = 127.996...
* weights = 8
* result = 256 -> INT8 saturation = 127
*/
for (i = 0; i < N_INPUTS; i = i + 1)
weights_bus[
3*N_INPUTS*DATA_WIDTH +
i*DATA_WIDTH +:
DATA_WIDTH
] = q8_8(2.0);
] = 8'sd8;
/*
* Neuron 4:
* weights = 0
* bias = +1
* result = 1
* bias = +5
* result = 5
*/
bias_bus[4*DATA_WIDTH +: DATA_WIDTH] = q8_8(1.0);
bias_bus[4*DATA_WIDTH +: DATA_WIDTH] = 8'sd5;
/*
* Neuron 5:
* weights = 0
* bias = -1
* bias = -5
* ReLU = 0
*/
bias_bus[5*DATA_WIDTH +: DATA_WIDTH] = q8_8(-1.0);
bias_bus[5*DATA_WIDTH +: DATA_WIDTH] = -8'sd5;
/*
* Neuron 6:
* weights = 1.0
* bias = -1.0
* result = 63
* weights = 1
* bias = -10
* result = 32 - 10 = 22
*/
for (i = 0; i < N_INPUTS; i = i + 1)
weights_bus[
6*N_INPUTS*DATA_WIDTH +
i*DATA_WIDTH +:
DATA_WIDTH
] = q8_8(1.0);
] = 8'sd1;
bias_bus[6*DATA_WIDTH +: DATA_WIDTH] = q8_8(-1.0);
bias_bus[6*DATA_WIDTH +: DATA_WIDTH] = -8'sd10;
/*
* Neuron 7:
* weights = 0.25
* bias = +1
* result = 16 + 1 = 17
* first 16 weights = 1, remaining 16 = 0
* bias = +5
* result = 16 + 5 = 21
*
* Exercises a sparse weight pattern across groups
* (PARALLEL = 8 -> GROUPS = 4).
*/
for (i = 0; i < N_INPUTS; i = i + 1)
weights_bus[
7*N_INPUTS*DATA_WIDTH +
i*DATA_WIDTH +:
DATA_WIDTH
] = q8_8(0.25);
] = (i < 16) ? 8'sd1 : 8'sd0;
bias_bus[7*DATA_WIDTH +: DATA_WIDTH] = q8_8(1.0);
bias_bus[7*DATA_WIDTH +: DATA_WIDTH] = 8'sd5;
$display("");
$display("==============================");
$display("LAYER TEST");
$display("LAYER TEST (INT8)");
$display("==============================");
run_layer;
$display("Neuron 0 = %5d expected = 16384", y_bus[0*16 +: 16]);
$display("Neuron 1 = %5d expected = 8192", y_bus[1*16 +: 16]);
$display("Neuron 2 = %5d expected = 0", y_bus[2*16 +: 16]);
$display("Neuron 3 = %5d expected = 32767", y_bus[3*16 +: 16]);
$display("Neuron 4 = %5d expected = 256", y_bus[4*16 +: 16]);
$display("Neuron 5 = %5d expected = 0", y_bus[5*16 +: 16]);
$display("Neuron 6 = %5d expected = 16128", y_bus[6*16 +: 16]);
$display("Neuron 7 = %5d expected = 4352", y_bus[7*16 +: 16]);
$display("Neuron 0 = %5d expected = 32", $signed(y_bus[0*DATA_WIDTH +: DATA_WIDTH]));
$display("Neuron 1 = %5d expected = 96", $signed(y_bus[1*DATA_WIDTH +: DATA_WIDTH]));
$display("Neuron 2 = %5d expected = 0", $signed(y_bus[2*DATA_WIDTH +: DATA_WIDTH]));
$display("Neuron 3 = %5d expected = 127", $signed(y_bus[3*DATA_WIDTH +: DATA_WIDTH]));
$display("Neuron 4 = %5d expected = 5", $signed(y_bus[4*DATA_WIDTH +: DATA_WIDTH]));
$display("Neuron 5 = %5d expected = 0", $signed(y_bus[5*DATA_WIDTH +: DATA_WIDTH]));
$display("Neuron 6 = %5d expected = 22", $signed(y_bus[6*DATA_WIDTH +: DATA_WIDTH]));
$display("Neuron 7 = %5d expected = 21", $signed(y_bus[7*DATA_WIDTH +: DATA_WIDTH]));
if (y_bus[0*16 +: 16] !== 16'sd16384) errors = errors + 1;
if (y_bus[1*16 +: 16] !== 16'sd8192) errors = errors + 1;
if (y_bus[2*16 +: 16] !== 16'sd0) errors = errors + 1;
if (y_bus[3*16 +: 16] !== 16'sd32767) errors = errors + 1;
if (y_bus[4*16 +: 16] !== 16'sd256) errors = errors + 1;
if (y_bus[5*16 +: 16] !== 16'sd0) errors = errors + 1;
if (y_bus[6*16 +: 16] !== 16'sd16128) errors = errors + 1;
if (y_bus[7*16 +: 16] !== 16'sd4352) errors = errors + 1;
if ($signed(y_bus[0*DATA_WIDTH +: DATA_WIDTH]) !== 8'sd32) errors = errors + 1;
if ($signed(y_bus[1*DATA_WIDTH +: DATA_WIDTH]) !== 8'sd96) errors = errors + 1;
if ($signed(y_bus[2*DATA_WIDTH +: DATA_WIDTH]) !== 8'sd0) errors = errors + 1;
if ($signed(y_bus[3*DATA_WIDTH +: DATA_WIDTH]) !== 8'sd127) errors = errors + 1;
if ($signed(y_bus[4*DATA_WIDTH +: DATA_WIDTH]) !== 8'sd5) errors = errors + 1;
if ($signed(y_bus[5*DATA_WIDTH +: DATA_WIDTH]) !== 8'sd0) errors = errors + 1;
if ($signed(y_bus[6*DATA_WIDTH +: DATA_WIDTH]) !== 8'sd22) errors = errors + 1;
if ($signed(y_bus[7*DATA_WIDTH +: DATA_WIDTH]) !== 8'sd21) errors = errors + 1;
$display("busy = %0d", busy);
$display("done = %0d", done);
@@ -237,4 +232,4 @@ module tb;
$finish;
end
endmodule
endmodule
+780 -927
View File
File diff suppressed because it is too large Load Diff
+38 -51
View File
@@ -2,11 +2,10 @@
module tb;
parameter DATA_WIDTH = 16;
parameter FRAC_BITS = 8;
parameter N_INPUTS = 64;
parameter DATA_WIDTH = 8;
parameter N_INPUTS = 32;
parameter PARALLEL = 8;
parameter ACC_WIDTH = 40;
parameter ACC_WIDTH = 32;
reg clk;
reg rst;
@@ -25,7 +24,6 @@ module tb;
neuron_parallel #(
.DATA_WIDTH(DATA_WIDTH),
.FRAC_BITS(FRAC_BITS),
.N_INPUTS(N_INPUTS),
.PARALLEL(PARALLEL),
.ACC_WIDTH(ACC_WIDTH)
@@ -47,16 +45,6 @@ module tb;
forever #5 clk = ~clk;
end
// ------------------------------------------------------------
// Helper: convert real value to Q8.8
// ------------------------------------------------------------
function signed [15:0] q8_8;
input real value;
begin
q8_8 = $rtoi(value * 256.0);
end
endfunction
// ------------------------------------------------------------
// Start neuron and wait for completion
// ------------------------------------------------------------
@@ -79,14 +67,13 @@ module tb;
//
// Diverse vector:
//
// x0 = 1.5 w0 = 2.0 -> 3.0
// x1 = 2.0 w1 = -1.0 -> -2.0
// x2 = 0.5 w2 = 0.25 -> 0.125
// x0 = 3 w0 = 2 -> 6
// x1 = 4 w1 = -1 -> -4
// x2 = 2 w2 = 1 -> 2
//
// bias = 0.609375
// bias = 1
//
// total = 1.734375
// Q8.8 = 444
// total = 6 - 4 + 2 + 1 = 5
// ------------------------------------------------------------
task test_1;
begin
@@ -95,23 +82,23 @@ module tb;
x_bus = 0;
w_bus = 0;
bias = q8_8(0.609375);
bias = 8'sd1;
x_bus[0*16 +: 16] = q8_8(1.5);
w_bus[0*16 +: 16] = q8_8(2.0);
x_bus[0*DATA_WIDTH +: DATA_WIDTH] = 8'sd3;
w_bus[0*DATA_WIDTH +: DATA_WIDTH] = 8'sd2;
x_bus[1*16 +: 16] = q8_8(2.0);
w_bus[1*16 +: 16] = q8_8(-1.0);
x_bus[1*DATA_WIDTH +: DATA_WIDTH] = 8'sd4;
w_bus[1*DATA_WIDTH +: DATA_WIDTH] = -8'sd1;
x_bus[2*16 +: 16] = q8_8(0.5);
w_bus[2*16 +: 16] = q8_8(0.25);
x_bus[2*DATA_WIDTH +: DATA_WIDTH] = 8'sd2;
w_bus[2*DATA_WIDTH +: DATA_WIDTH] = 8'sd1;
run_neuron;
$display("RTL = %0d", y);
$display("EXPECTED = 444");
$display("EXPECTED = 5");
if (y !== 16'sd444) begin
if (y !== 8'sd5) begin
$display("FAIL - TEST 1");
errors = errors + 1;
end
@@ -137,8 +124,8 @@ module tb;
bias = 0;
for (i = 0; i < N_INPUTS; i = i + 1) begin
x_bus[i*16 +: 16] = q8_8(1.0);
w_bus[i*16 +: 16] = q8_8(-1.0);
x_bus[i*DATA_WIDTH +: DATA_WIDTH] = 8'sd1;
w_bus[i*DATA_WIDTH +: DATA_WIDTH] = -8'sd1;
end
run_neuron;
@@ -146,7 +133,7 @@ module tb;
$display("RTL = %0d", y);
$display("EXPECTED = 0");
if (y !== 16'sd0) begin
if (y !== 8'sd0) begin
$display("FAIL - TEST 2");
errors = errors + 1;
end
@@ -160,7 +147,7 @@ module tb;
// TEST 3
//
// Large positive result.
// Must saturate to +32767.
// Must saturate to +127 (INT8).
// ------------------------------------------------------------
task test_3;
begin
@@ -172,16 +159,16 @@ module tb;
bias = 0;
for (i = 0; i < N_INPUTS; i = i + 1) begin
x_bus[i*16 +: 16] = q8_8(127.0);
w_bus[i*16 +: 16] = q8_8(2.0);
x_bus[i*DATA_WIDTH +: DATA_WIDTH] = 8'sd100;
w_bus[i*DATA_WIDTH +: DATA_WIDTH] = 8'sd2;
end
run_neuron;
$display("RTL = %0d", y);
$display("EXPECTED = 32767");
$display("EXPECTED = 127");
if (y !== 16'sd32767) begin
if (y !== 8'sd127) begin
$display("FAIL - TEST 3");
errors = errors + 1;
end
@@ -196,12 +183,12 @@ module tb;
//
// Mixed positive/negative products.
//
// 32 x (+1)
// 32 x (-1)
// sum = 0
// bias = -1
// 16 x (2 * 1) = 32
// 16 x (-1 * 1) = -16
// sum = 16
// bias = -16
//
// ReLU -> 0
// total = 0 -> ReLU boundary -> 0
// ------------------------------------------------------------
task test_4;
begin
@@ -210,16 +197,16 @@ module tb;
x_bus = 0;
w_bus = 0;
bias = q8_8(-1.0);
bias = -8'sd16;
for (i = 0; i < N_INPUTS; i = i + 1) begin
if ((i % 2) == 0) begin
x_bus[i*16 +: 16] = q8_8(2.0);
w_bus[i*16 +: 16] = q8_8(0.5);
x_bus[i*DATA_WIDTH +: DATA_WIDTH] = 8'sd2;
w_bus[i*DATA_WIDTH +: DATA_WIDTH] = 8'sd1;
end
else begin
x_bus[i*16 +: 16] = q8_8(-1.0);
w_bus[i*16 +: 16] = q8_8(1.0);
x_bus[i*DATA_WIDTH +: DATA_WIDTH] = -8'sd1;
w_bus[i*DATA_WIDTH +: DATA_WIDTH] = 8'sd1;
end
end
@@ -228,7 +215,7 @@ module tb;
$display("RTL = %0d", y);
$display("EXPECTED = 0");
if (y !== 16'sd0) begin
if (y !== 8'sd0) begin
$display("FAIL - TEST 4");
errors = errors + 1;
end
@@ -259,7 +246,7 @@ module tb;
$display("");
$display("==============================");
$display("NEURON_PARALLEL TESTBENCH");
$display("NEURON_PARALLEL TESTBENCH (INT8)");
$display("==============================");
test_1;
@@ -283,4 +270,4 @@ module tb;
$finish;
end
endmodule
endmodule
+7080 -632
View File
File diff suppressed because one or more lines are too long