From 9f5f953a95c8617cd2fdd24f7402af632c1b07c2 Mon Sep 17 00:00:00 2001 From: tslil Date: Sat, 19 Sep 2026 20:24:08 +0100 Subject: transposing the weights gives ~2.5x speedup --- include/nn.c | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) (limited to 'include/nn.c') diff --git a/include/nn.c b/include/nn.c index 7a42a02..9f15ec2 100644 --- a/include/nn.c +++ b/include/nn.c @@ -119,18 +119,19 @@ float eval_net(const float *board, const unsigned int n_inp, const float *w, const unsigned int n_dense, const float parity) { float dense1[FIVE_DENSE_NUM]; // FIVE_DENSE_NUM == SIX_DENSE_NUM - float z = logit_b; - - for (unsigned int d1 = 0; d1 < n_dense; d1++) { - float h = biases[d1]; - const float *wd = w + d1 * n_inp; - for (unsigned int fl = 0; fl < n_inp; fl++) - h += board[fl] * wd[fl]; - dense1[d1] = fast_silu(h); + for (unsigned int d1 = 0; d1 < n_dense; d1++) + dense1[d1] = biases[d1]; + for (unsigned int fl = 0; fl < n_inp; fl++) { + const float b = board[fl]; + const float *wrow = w + fl * n_dense; + for (unsigned int d1 = 0; d1 < n_dense; d1++) + dense1[d1] += b * wrow[d1]; } + for (unsigned int d1 = 0; d1 < n_dense; d1++) + dense1[d1] = fast_silu(dense1[d1]); + float z = logit_b; for (unsigned int d1 = 0; d1 < n_dense; d1++) z += dense1[d1] * logit_w[d1]; - return parity * fast_tanh(z); } -- cgit v1.3.1