Summarize and analyze the likely sources of ELO gains, especially from version 12.0 to current. Split into categories: NNUE, and search. Quote small blocks of code to explain each. Aim for a complete and thorough answer.
Anyone who has a previous version of Ethereal 13.00 (NNUE) or later, should have received an email from me by now containing a link to download the updated binaries and networks, as well as a comment about updates going forward. If you've not yet gotten yours from [Redacted Email], you should check your spam and then reach out to me.
Change Log:
14.00: Speedup Accumulator using Tiling (Thanks to [Redacted Person]'s author [Redacted Person])
13.99: Once again enable Smart Syzygy filtering of root moves
13.98: Use one-fourth of set threads to clear the Transposition Table
13.97: Improve Syzygy by reducing look ahead reads on Linux (Thanks Aloril + SF)
13.96: Fix Syzygy issue for wide DTZ tables
13.95: Simplify LMR logic in Ethereal to match that of other engines
13.94: Fix Syzygy issues when probing very long endgames (DTZ > 1000)
13.93: Fix a long standing, non-functional bug when storing null values into the TT
13.92: New Fischer Network: Repeat process using 13.85 Network for Data Generation
13.91: Simplify the History component of LMR during Tactical moves
13.90: Improve speed to report TB Win/Loss significantly
13.89: Default to using Clang and PGO builds
13.88: Increase ProbCut Margin from 80cp to 100cp
13.87: Perform 4x maddubs operations per madd(ones, acc). Big speed up.
13.86: New Standard Network: Repeat process using 13.84 Network for Data Generation
13.85: New Fischer Network: Same data as 13.80 final, but 2x512 L1
13.84: New Standard Network: Same process, but with a 2x512 L1 instead of 2x384
13.83: New Standard Network: Same process, but with a 2x384 L1 instead of 2x256
13.82: New Standard Network: 100m games of nodes=40k, 40x LR drop instead of 10x LR drop
13.81: Accept lower-depth tt-entries if we expect a research may follow (big brain idea)
13.80: New Fishcher Network: 100m games of nodes=20k, first network in a while +85 elo
13.79: New Standard Network: 100m games of nodes=20k, 40x LR drop instead of 10x LR drop
13.78: New Standard Network: 100m games of nodes=5k, 40x LR drop instead of 10x LR drop
13.77: Update some of the internal NNUE data processing tools internally
13.76: Simplify singularity() to simply wrap search() and reuse the MovePicker
Regression tests vs Ethereal 13.75:
Standard Chess using a very drawish book:
ELO | 45.54 +- 1.58 (95%)
CONF | 60.0+0.60s Threads=1 Hash=64MB
GAMES | N: 40000 W: 7091 L: 1878 D: 31031
Assistant
User
Spot all possibilities for the "tiling"; what might that mean here?
Assistant
User
Explain why the 4:1 ratio or the tiling of NUM_REGS might speed things up?
Assistant
User
what ensures they are occurring in parallel, that the branching is obvious?
Assistant
To ensure that the 4 elements are processed in parallel and that the branching is obvious, the code is likely using a combination of techniques such as:
SIMD instructions: The code might be using SIMD (Single Instruction, Multiple Data) instructions, which allow a single instruction to operate on multiple data elements simultaneously. This can be achieved using instruction sets such as SSE, AVX, or AVX-512.
Fischer Random Chess using a very shallow book:
ELO | 101.30 +- 2.08 (95%)
CONF | 60.0+0.60s Threads=1 Hash=64MB
GAMES | N: 40000 W: 13562 L: 2219 D: 24219
http://chess.grantnet.us/test/30797/
Ethereal 13 has been released, with both a free (Standard) version and a commercial NNUE (Efficiently-Updated Neural Network) version. The free version is available on Github, and offers minor elo gains over the previous version, with increased functionality for analysis. Ethereal continues its legacy of stability and reliability for analysis and engine play.
Ethereal 13 (NNUE) includes two NNUE Networks for evaluation. These Networks are the second of their kind, boasting themselves as the only other high level NNUEs not derived from, trained on, nor duplicated from the works of the Stockfish team. With it, Ethereal adds new ideas and techniques to extend existing machine learning algorithms, and adds original practices into the implementation. These new approaches in software design allow for older processors to outperform NNUEs found in other engines, while still harnessing the full power of the AVX/AVX2 instruction sets for CPUs with support.
Additionally, Ethereal is the first engine to offer a secondary Network trained exclusively for Fischer Random Chess (FRC). This Network outperforms the standard Network by significant margins in FRC play, showcasing a creative style by Ethereal, not reliant on Opening Theory. These Neural Networks offer up to +120 elo over Ethereal 12.75 in self-play for Standard and Fischer Random Chess, the culmination of over 238,000,000 games played between various versions of Ethereal.
The Standard version will run on any 64-bit machine, with source provided for Linux users, and binaries provided for Windows users. The NNUE version requires a CPU with the SSSE3 instruction set (which almost all CPUs have), and will not run on systems lacking these instructions. AVX/AVX2 have become ubiquitous in modern processor design, and are necessary to capitalize on the full strength of Neural Network technology. If you are unsure if your system is supported, please reach out to me via email at [Redacted Email] for assistance.
Stripe will verify your payment of $40 USD. Afterwards, you will receive an email from [Redacted Email] or [Redacted Email] within 24 hours containing a download link for the Standard/Fischer NNUE Networks, as well as the binary or executable files for Windows and Linux. You should select the binary matching your Operating System. Users with Intel CPUs containing BMI2 support (Also known as PEXT), should use the pext binary for optimal performance. AMD CPU users should use the popcnt binary, unless they have a 5XXX series Ryzen. AVX2 binaries will be the fastest, followed by AVX, and finally SSSE3. You should use the fastest your CPU allows.
If Stripe is not possible for you, PayPal is also an option. Using PayPal as a merchant account proved to be unworkable. However, you can send a payment of $40 USD to [Redacted Email] via the "Send & Request" button. This process is not yet automated, so I will have to build and send binaries by hand. If you are able to use Stripe, that is my preference. PayPal charges greater fees for foreign currency conversions, and I do not pass any fees on to the buyer.
The attached binaries below are the Standard version, and do not contain support for NNUE. They will be significantly weaker than the NNUE version described above.
Assets 8
7 people reacted
Ethereal 12.75
09 Oct 06:46
@[Redacted Person] [Redacted Person]
v12.75
[Redacted Person]
Ethereal 12.75
This is the final planned release of Ethereal.
Assets 9
Ethereal 12.50
08 Sep 09:56
@[Redacted Person] [Redacted Person]
V12.50
d5b229e
Ethereal 12.50
12.26: Tune all of the search margins using the skopt tuner
12.27: Penalize Queen's that may be prone to discovered attacks
12.28: Have QueenRelativePin only evaluate truely discovered attacks
12.29: Use knowledge of fail-highs/fail-lows to tweak LMR
12.30: Revert all of the changes from V12.29
12.31: Apply a slight non-functional speedup to Probcut
12.32: Fix a hanging pthread which used up resources over time
12.33: Add UCI option "AnalysisMode" to toggle TB behaviour
12.34: Replace Fathom with Pyrrhic, adding 7-man support
12.35: Kick up the version number due to Pyrrhic
12.36: Update to latest Pyrrhic and fix TB_LARGEST bugs
12.37: Tune all evaluation terms using a new dataset methodology
12.38: Tune and tweak King Safety interactions using a Standard book
12.39: Delete the old tuning code and upload the new AdaGrad tuner
12.40: Add an option to the tuner to print verbatim source code
12.41: Tune every term in the evaluation using a Standard book
12.42: Tune all of the "normal" terms using a larger Standard book
12.43: Increase max hash usage to 128GB
12.44: Redefine how we delta prune in the qsearch
12.45: Tune all of the "complexity" terms using a Standard book
12.46: Tune all of the "normal" terms using a FRC book
12.47: Reduce many of the pruning margins in the search
12.48: Add a 512KB Evaluation Cache to each Thread
12.49: Make the PSQTs Asymmetrical ([64], not [32])
12.50: Tune the PSQT using a mix of FRC + Standard games
12.01 Only partial evaluate stacked Passed Pawns
12.02 Always try to prove singularity if we have a candidate move
12.03 Increase LMR by 1 when extending to negate the impact
12.04 Save TB hits into the TT with the actual search depth
12.05 Only apply History Extensions on the PV to fix depth issues
12.06 Restore defaults for ContemptDrawPenalty and ContemptComplexity to 0
12.07 + Fix storing TB wins/losses into the TT ( Terje )
12.08 Fix an issue causing singular moves to be mistaken as MultiCut moves
12.09 Update the Tuning mechanism to have an output closer to what we want
12.10 Add ARMv7 and ARMv8 build targets to the makefile
12.11 Improve mate detection around aspiration windows and correct a ucicurrmove issue
12.12 Update the Killer Moves when we take a MultiCut
12.13 Tune the entire evaluation using a modified book and method
12.14 Add a command line utility to evaluate a file of positions to a given depth
12.15 Speedup move generation by ~6.5% in PERFT suite testing
12.16 Fix making use of the command line utility for tuning
12.17 Remove depth skipping schemes during SMP. Return to true LazySMP
12.18 Once again add a preliminary search before trying ProbCut
12.19 Use depth' = depth - LMR(depth, played) for continuation pruning
12.20 Implement support for "go searchmoves ..." per requests
12.21 Don't apply continuation pruning to our Killer Moves
12.22 * Disable history updates at depth 1 if the first quiet caused a cutoff ( Alayan )
12.23 * Disable history updates at depth 2 if the first quiet caused a cutoff ( [Redacted Person] )
12.24 * Disable history updates at depth 3 if the first quiet caused a cutoff ( Alayan )
12.25 Bug fix MultiPV, and in TB positions search as normal with a refined movelist
11.76 Score Knights/Rooks based on how open the position
is. Openness is a function of the number of open
files as well as the number of rammed pawns.
11.77 Moved Closedness to its own function for a speedup.
11.78 Penalize hanging pieces threatened by a King.
& 11.79 Implement Multi-Cut Pruning based on attempts to prove
a move to be Singular.
& 11.80 Remove the redundant attackersToSquare() function.
11.81 Simplify away the Razoring in the main search routine.
11.82 Add rough elo estimates for various search techniques.
11.83 Apply an extra penalty for Passers that are stacked.
11.84 Fix a small white/black mirroring issue in Passed Stack
Pawn evaluation introduced with patch 11.83.
11.85 Penalize mutually attacked squares that are poorly defended.
11.86 Tune the evaluation terms added in patch 11.85
11.87 Bonus for Bishops on an open, long, diagonal controlling
some of the central squares, a la Stockfish.
11.88 Penalize knights that are far from both of the Kings.
11.89 Increase the Static Exchange Evaluation Pruning depth to 9
11.90 Evaluate Backwards pawns by their ranks. Adjust the Pawn
piece-square table to account for this change.
11.91 Apply an early game bonus having sole control over central
squares. Taper off this bonus as the position simplifies.
11.92 Prefetch Transposition Table entries as early as possible.
11.93 Introduce Draw Contempt and Complexity Contempt.
11.94 Scale down the eval for a lone queen versus multiple other
minors and or majors, when the queen is given the advantage.
11.95 Scale up lone minor/rook vs lone minor/rook when there is
a difference of three or more pawns between the players.
& 11.96 Resolve fail-highs with progressively shallower searches.
11.97 When evaluating King Safety, exclude attacks to squares that
are twice protected by our pawns. Idea from Vizvezdenec.
11.98 Move 'space' evaluation terms to their own function.
Add [Redacted Person] to the authors output. Fix tuner and UCI options.
11.99 Advise the Kernal to use Transparent Huge Pages.
12.00 Add a small variation to drawn evaluations to avoid potential
blindness to 3-fold repetitions.
Patches denoted with an "*" are by [Redacted Person], those with "&" are by [Redacted Person]
author [Redacted Person], and those with "+" are from Github User [Redacted Person].
Assets 7
Ethereal 11.75
11 Nov 07:59
@[Redacted Person] [Redacted Person]
V11.75
32607c2
Ethereal 11.75
11.51: Speedup moveIsSingular() with our knowledge about the TT move's legality
11.52: Encode Castle moves as KxR. Report UCI currmove and UCI currmovenumber
11.53: Skip some bad captures after skipping quiets during moveIsSingular()
11.54: Do not let a Queen attack through our Rooks/Bishops for Mobility scoring
11.55: Speedup and simplify the logic behind storeTTEntry()
11.56: Full engine Tuning session using an average of a static and dynamic eval
11.57: Do not consider a pawn to be Isolated if it may capture another pawn
11.58: Syzygy probing speedup and simplification
11.59: Evaluate Complexity to adjust eval based on conversion chances
11.60: Add Complexity terms to the Tuner and run a Tuning session
11.61: Allow Tuning terms to be considered Midgame or Endgame only
11.62: Evaluate Kings based on nearest file-wise Pawn
11.63: Run a Tuning session on the King Pawn File Proximity term
11.64: Remove the Passed Pawn contribution to the Complexity calculation
11.65: Evaluate Stacked/Doubled Pawns differently based on ability to unstack
11.66: Add a few options to the Tuner in relation to static vs dynamic eval
11.67: Fix a bug in the logic behind patch 11.65
11.68: Fix a newline printing issue in the PERFT suite between Python 2/3
11.69: Do not ever consider an Isolated Pawn to be Backwards
11.70: Halve the eval bonus (but not malus) for Outposts on the A/H files
11.71: Never consider a King and lone Knight/Bishop to be winning
11.72: Do not perform history updates on illegal or skipped quiet moves
11.73: Apply Futility Move Pruning to moves with good history scores in some cases
11.74: Add support for MultiPV analysis
11.75: Add MultiPV information to the README
/******************************************************************************/
/* */
/* Ethereal is a UCI chess playing engine authored by [Redacted Person]. */
/* <https://github.com/AndyGrant/Ethereal> <[Redacted Email]> */
/* */
/* Ethereal is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* Ethereal is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/> */
/* */
/******************************************************************************/
#include <immintrin.h>
#include <stdio.h>
#include <string.h>
#include "accumulator.h"
#include "nnue.h"
#include "types.h"
#include "../bitboards.h"
#include "../thread.h"
#include "../types.h"
extern ALIGN64 int16_t in_weights[INSIZE * KPSIZE];
extern ALIGN64 int16_t in_biases[KPSIZE];
static int sq64_to_sq32(int sq) {
static const int Mirror[] = { 3, 2, 1, 0, 0, 1, 2, 3 };
return ((sq >> 1) & ~0x3) + Mirror[sq & 0x7];
}
static int nnue_index_delta(int piece, int relksq, int colour, int sq) {
const int ptype = pieceType(piece);
const int pcolour = pieceColour(piece);
const int relpsq = relativeSquare(colour, sq);
const int mksq = testBit(LEFT_FLANK, relksq) ? (relksq ^ 0x7) : relksq;
const int mpsq = testBit(LEFT_FLANK, relksq) ? (relpsq ^ 0x7) : relpsq;
return 640 * sq64_to_sq32(mksq) + (64 * (5 * (colour == pcolour) + ptype)) + mpsq;
}
static int nnue_index(Board *board, int relksq, int colour, int sq) {
return nnue_index_delta(board->squares[sq], relksq, colour, sq);
}
int nnue_can_update(NNUEAccumulator *accum, Board *board) {
NNUEAccumulator *start = board->thread->nnueStack;
if (accum == start) return 0;
if ((accum-1)->accurate) return 1;
while (accum != start) {
if (accum->accurate)
return 1;
if (accum->changes && pieceType(accum->deltas[0].piece) == KING)
return 0;
accum = accum - 1;
}
return 0;
}
void nnue_refresh_accumulators(NNUEAccumulator *accum, Board *board, int wrelksq, int brelksq) {
nnue_refresh_accumulator(accum, board, WHITE, wrelksq);
nnue_refresh_accumulator(accum, board, BLACK, brelksq);
accum->accurate = 1;
}
void nnue_refresh_accumulator(NNUEAccumulator *accum, Board *board, int colour, int relsq) {
const uint64_t white = board->colours[WHITE];
const uint64_t black = board->colours[BLACK];
const uint64_t kings = board->pieces[KING];
int indices[32], count = 0;
uint64_t pieces = (white | black) & ~kings;
vepi16 *biases, *outputs, *weights, registers[NUM_REGS];
// Compute the list of indices just once, to then be used multiple
// times while updating the accumulator using a tiling method
while (pieces) {
const int sq = poplsb(&pieces);
indices[count++] = nnue_index(board, relsq, colour, sq);
}
// Refresh completely, using all pieces as inputs except the Kings
// We do this by tiling over the accumulator, to get the compiler to
// produce more optimal code that does not emit extra move instructions
for (int offset = 0; offset < KPSIZE; offset += NUM_REGS * vepi16_cnt) {
biases = (vepi16*) &in_biases[offset];
outputs = (vepi16*) &accum->values[colour][offset];
for (int i = 0; i < NUM_REGS; i++)
registers[i] = biases[i];
for (int i = 0; i < count; i++) {
weights = (vepi16*) &in_weights[indices[i] * KPSIZE + offset];
for (int j = 0; j < NUM_REGS; j++)
registers[j] = vepi16_add(registers[j], weights[j]);
}
for (int i = 0; i < NUM_REGS; i++)
outputs[i] = registers[i];
}
}
void nnue_update_accumulator(NNUEAccumulator *accum, Board *board, int wrelksq, int brelksq) {
int add_list[2][3], remove_list[2][3];
int add = 0, remove = 0, refreshed[2] = { 0, 0 };
vepi16 *inputs, *outputs, *weights, registers[NUM_REGS];
// Recurse and update all out of date children
if (!(accum-1)->accurate)
nnue_update_accumulator((accum-1), board, wrelksq, brelksq);
// The last move was a NULL move so we can cheat and copy
if (!accum->changes) {
memcpy(accum->values, (accum-1)->values, sizeof(int16_t) * L1SIZE);
accum->accurate = 1;
return;
}
for (int i = 0; i < accum->changes; i++) {
const NNUEDelta *delta = &accum->deltas[i];
// Fully recompute a colour if its King has moved
if (pieceType(delta->piece) == KING) {
int colour = pieceColour(delta->piece);
int relksq = colour == WHITE ? wrelksq : brelksq;
nnue_refresh_accumulator(accum, board, colour, relksq);
refreshed[colour] = 1;
continue;
}
// Moving or placing a Piece to a Square
if (delta->to != SQUARE_NB) {
add_list[WHITE][add ] = nnue_index_delta(delta->piece, wrelksq, WHITE, delta->to);
add_list[BLACK][add++] = nnue_index_delta(delta->piece, brelksq, BLACK, delta->to);
}
// Moving or deleting a Piece from a Square
if (delta->from != SQUARE_NB) {
remove_list[WHITE][remove ] = nnue_index_delta(delta->piece, wrelksq, WHITE, delta->from);
remove_list[BLACK][remove++] = nnue_index_delta(delta->piece, brelksq, BLACK, delta->from);
}
}
// ------------------------------------------------------------------------------------------
if (!add && !remove) {
int old = refreshed[WHITE] ? BLACK : WHITE;
memcpy(accum->values[old], (accum-1)->values[old], sizeof(int16_t) * KPSIZE);
accum->accurate = 1;
return;
}
for (int colour = WHITE; colour <= BLACK; colour++) {
if (refreshed[colour])
continue;
for (int offset = 0; offset < KPSIZE; offset += NUM_REGS * vepi16_cnt) {
outputs = (vepi16*) &(accum-0)->values[colour][offset];
inputs = (vepi16*) &(accum-1)->values[colour][offset];
for (int i = 0; i < NUM_REGS; i++)
registers[i] = inputs[i];
for (int i = 0; i < add; i++) {
weights = (vepi16*) &in_weights[add_list[colour][i] * KPSIZE + offset];
for (int j = 0; j < NUM_REGS; j++)
registers[j] = vepi16_add(registers[j], weights[j]);
}
for (int i = 0; i < remove; i++) {
weights = (vepi16*) &in_weights[remove_list[colour][i] * KPSIZE + offset];
for (int j = 0; j < NUM_REGS; j++)
registers[j] = vepi16_sub(registers[j], weights[j]);
}
for (int i = 0; i < NUM_REGS; i++)
outputs[i] = registers[i];
}
}
accum->accurate = 1;
return;
}
code
/******************************************************************************/
/* */
/* Ethereal is a UCI chess playing engine authored by [Redacted Person]. */
/* <https://github.com/AndyGrant/Ethereal> <[Redacted Email]> */
/* */
/* Ethereal is free software: you can redistribute it and/or modify */
/* it under the terms of the GNU General Public License as published by */
/* the Free Software Foundation, either version 3 of the License, or */
/* (at your option) any later version. */
/* */
/* Ethereal is distributed in the hope that it will be useful, */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the */
/* GNU General Public License for more details. */
/* */
/* You should have received a copy of the GNU General Public License */
/* along with this program. If not, see <http://www.gnu.org/licenses/> */
/* */
/******************************************************************************/
#include <immintrin.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdalign.h>
#include "accumulator.h"
#include "nnue.h"
#include "types.h"
#include "utils.h"
#include "../bitboards.h"
#include "../board.h"
#include "../evaluate.h"
#include "../thread.h"
#include "../incbin/incbin.h"
#define SHIFT_L0 6
#define SHIFT_L1 5
#ifdef EVALFILE
const char *NNUEDefault = EVALFILE;
INCBIN(IncWeights, EVALFILE);
#endif
ALIGN64 int16_t in_weights[INSIZE * KPSIZE ];
ALIGN64 int8_t l1_weights[L1SIZE * L2SIZE ];
ALIGN64 float l2_weights[L2SIZE * L3SIZE ];
ALIGN64 float l3_weights[L3SIZE * OUTSIZE];
ALIGN64 int16_t in_biases[KPSIZE ];
ALIGN64 int32_t l1_biases[L2SIZE ];
ALIGN64 float l2_biases[L3SIZE ];
ALIGN64 float l3_biases[OUTSIZE];
static int NNUE_LOADED = 0;
static void scale_weights() {
// Delayed dequantization of the results of L1 forces an upshift in
// biases of L2 and L3 to compensate. This saves SRAI calls, as well as
// increases the precision of each layer, with no clear downsides.
for (int i = 0; i < L3SIZE; i++)
l2_biases[i] *= (1 << SHIFT_L1);
for (int i = 0; i < OUTSIZE; i++)
l3_biases[i] *= (1 << SHIFT_L1);
}
static void quant_transpose(int8_t *matrix, int rows, int cols) {
// Typical Matrix Transposition using int8_t. Ethereal's trainer
// stores weights in a way to allow faster updates, not computes
int8_t *cpy = malloc(sizeof(int8_t) * rows * cols);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
cpy[j * rows + i] = matrix[i * cols + j];
memcpy(matrix, cpy, sizeof(int8_t) * rows * cols);
free(cpy);
}
static void float_transpose(float *matrix, int rows, int cols) {
// Typical Matrix Transposition using floats. Ethereal's trainer
// stores weights in a way to allow faster updates, not computes
float *cpy = malloc(sizeof(float) * rows * cols);
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
cpy[j * rows + i] = matrix[i * cols + j];
memcpy(matrix, cpy, sizeof(float) * rows * cols);
free(cpy);
}
static void shuffle_input_layer() {
#if defined(USE_AVX2)
__m256i *wgt = (__m256i *) in_weights;
__m256i *bia = (__m256i *) in_biases;
// Interleave adjacent 256-bit chunks of 2-byte values. During
// halfkp_relu() adjacent chunks are split, with the A-half of
// chunk 1 swapping with A-half of chunk 2. This is done to both
// the weights and the biases, to avoid unshuffling them later.
for (int i = 0; i < KPSIZE / vepi16_cnt; i += 2) {
__m128i half1 = _mm256_extracti128_si256(bia[i+0], 1);
__m128i half2 = _mm256_extracti128_si256(bia[i+1], 0);
bia[i+0] = _mm256_inserti128_si256(bia[i+0], half2, 1);
bia[i+1] = _mm256_inserti128_si256(bia[i+1], half1, 0);
}
for (int i = 0; i < INSIZE * KPSIZE / vepi16_cnt; i += 2) {
__m128i half1 = _mm256_extracti128_si256(wgt[i+0], 1);
__m128i half2 = _mm256_extracti128_si256(wgt[i+1], 0);
wgt[i+0] = _mm256_inserti128_si256(wgt[i+0], half2, 1);
wgt[i+1] = _mm256_inserti128_si256(wgt[i+1], half1, 0);
}
#endif
}
static void abort_nnue(const char *reason) {
printf("info string %s\n", reason);
fflush(stdout); exit(EXIT_FAILURE);
}
INLINE void maddubs_x4(vepi32 *acc, const vepi8 *inp, const vepi8 *wgt, int i, int j, int k) {
static const int InChunks = L1SIZE / vepi8_cnt;
vepi16 sum0 = vepi16_maubs(inp[j+0], wgt[InChunks * (i * 8 + k) + j + 0]);
vepi16 sum1 = vepi16_maubs(inp[j+1], wgt[InChunks * (i * 8 + k) + j + 1]);
vepi16 sum2 = vepi16_maubs(inp[j+2], wgt[InChunks * (i * 8 + k) + j + 2]);
vepi16 sum3 = vepi16_maubs(inp[j+3], wgt[InChunks * (i * 8 + k) + j + 3]);
vepi16 sumX = vepi16_add(sum0, vepi16_add(sum1, vepi16_add(sum2, sum3)));
*acc = vepi32_add(*acc, vepi16_madd(vepi16_one, sumX));
}
INLINE void halfkp_relu(NNUEAccumulator *accum, uint8_t *outputs, int turn) {
// The accumulation of king-piece values has already been computed.
// Perform the ReLU operation on each accumuatlor, and place them
// such that the side-to-move is first, then the non-side-to-move
assert(KPSIZE % 64 == 0);
vepi16 *in_white = (vepi16 *) &accum->values[WHITE];
vepi16 *in_black = (vepi16 *) &accum->values[BLACK];
vepi8 *out_white = (vepi8 *) (turn == WHITE ? outputs : &outputs[KPSIZE]);
vepi8 *out_black = (vepi8 *) (turn == BLACK ? outputs : &outputs[KPSIZE]);
for (int i = 0; i < KPSIZE / vepi8_cnt; i += 2) {
vepi16 shift0A = vepi16_srai(in_white[(i + 0) * 2 + 0], SHIFT_L0);
vepi16 shift0B = vepi16_srai(in_white[(i + 0) * 2 + 1], SHIFT_L0);
vepi16 shift1A = vepi16_srai(in_white[(i + 1) * 2 + 0], SHIFT_L0);
vepi16 shift1B = vepi16_srai(in_white[(i + 1) * 2 + 1], SHIFT_L0);
out_white[i+0] = vepi16_packu(shift0A, shift0B);
out_white[i+1] = vepi16_packu(shift1A, shift1B);
}
for (int i = 0; i < KPSIZE / vepi8_cnt; i += 2) {
vepi16 shift0A = vepi16_srai(in_black[(i + 0) * 2 + 0], SHIFT_L0);
vepi16 shift0B = vepi16_srai(in_black[(i + 0) * 2 + 1], SHIFT_L0);
vepi16 shift1A = vepi16_srai(in_black[(i + 1) * 2 + 0], SHIFT_L0);
vepi16 shift1B = vepi16_srai(in_black[(i + 1) * 2 + 1], SHIFT_L0);
out_black[i+0] = vepi16_packu(shift0A, shift0B);
out_black[i+1] = vepi16_packu(shift1A, shift1B);
}
}
INLINE void quant_affine_relu(int8_t *weights, int32_t *biases, uint8_t *inputs, float *outputs) {
assert(L1SIZE % 64 == 0 && L2SIZE % 8 == 0);
const int InChunks = L1SIZE / vepi8_cnt;
const int OutChunks = L2SIZE / 8;
#if defined(USE_AVX2) || defined(USE_AVX)
const vepi32 zero = vepi32_zero();
#elif defined(USE_SSSE3)
const vps32 zero = vps32_zero();
#endif
const vepi8 *inp = (vepi8 *) inputs;
const vepi8 *wgt = (vepi8 *) weights;
const vepi32 *bia = (vepi32 *) biases;
vps32 *const out = (vps32 *) outputs;
for (int i = 0; i < OutChunks; i++) {
vepi32 acc0 = vepi32_zero();
vepi32 acc1 = vepi32_zero();
vepi32 acc2 = vepi32_zero();
vepi32 acc3 = vepi32_zero();
vepi32 acc4 = vepi32_zero();
vepi32 acc5 = vepi32_zero();
vepi32 acc6 = vepi32_zero();
vepi32 acc7 = vepi32_zero();
for (int j = 0; j < InChunks; j += 4) {
maddubs_x4(&acc0, inp, wgt, i, j, 0);
[Redacted Person]_x4(&acc1, inp, wgt, i, j, 1);
[Redacted Person]_[Redacted Person](&acc2, inp, wgt, i, j, 2);
[Redacted Person]_x4(&acc3, inp, wgt, i, j, 3);
[Redacted Person]_[Redacted Person](&acc4, inp, wgt, i, j, 4);
[Redacted Person]_x4(&acc5, inp, wgt, i, j, 5);
[Redacted Person]_x4(&acc6, inp, wgt, i, j, 6);
[Redacted Person]x4(&acc7, inp, wgt, i, j, 7);
}
acc0 = vepi32_hadd(acc0, acc1);
acc2 = vepi32_hadd(acc2, acc3);
acc0 = vepi32_hadd(acc0, acc2);
acc4 = vepi32_hadd(acc4, acc5);
acc6 = vepi32_hadd(acc6, acc7);
acc4 = vepi32_hadd(acc4, acc6);
#if defined(USE_AVX2)
__m128i sumabcd1 = _mm256_extracti128_si256(acc0, 0);
__m128i sumabcd2 = _mm256_extracti128_si256(acc0, 1);
__m128i sumefgh1 = _mm256_extracti128_si256(acc4, 0);
__m128i sumefgh2 = _mm256_extracti128_si256(acc4, 1);
sumabcd1 = _mm_add_epi32(sumabcd1, sumabcd2);
[Redacted SWIFTCode] = _mm_add_epi32(sumefgh1, sumefgh2);
acc0 = _mm256_inserti128_si256(_mm256_castsi128_si256(sumabcd1), sumefgh1, 1);
acc0 = _mm256_add_epi32(acc0, bia[i]);
acc0 = _mm256_max_epi32(acc0, zero);
out[i] = _mm256_cvtepi32_ps(acc0);
#elif defined (USE_AVX)
__m128 ps0 = _mm_cvtepi32_ps(vepi32_max(zero, vepi32_add(bia[i * 2 + 0], acc0)));
__m128 ps1 = _mm_cvtepi32_ps(vepi32_max(zero, vepi32_add(bia[i * 2 + 1], acc4)));
out[i] = _mm256_insertf128_ps(out[i], ps0, 0);
out[i] = _mm256_insertf128_ps(out[i], ps1, 1);
#elif defined (USE_SSSE3)
out[i * 2 + 0] = vps32_max(zero, _mm_cvtepi32_ps(vepi32_add(bia[i * 2 + 0], acc0)));
out[i * 2 + 1] = vps32_max(zero, _mm_cvtepi32_ps(vepi32_add(bia[i * 2 + 1], acc4)));
#endif
}
}
INLINE void float_affine_relu(float *weights, float *biases, float *inputs, float *outputs) {
assert(L2SIZE % 8 == 0 && L3SIZE % 8 == 0);
const int InChunks = L2SIZE / vps32_cnt;
const int OutChunks = L3SIZE / 8;
const vps32 zero = vps32_zero();
const vps32 *inp = (vps32 *) inputs;
const vps32 *bia = (vps32 *) biases;
const vps32 *wgt = (vps32 *) weights;
vps32 *const out = (vps32 *) outputs;
for (int i = 0; i < OutChunks; i++) {
vps32 acc0 = vps32_mul(wgt[InChunks * (i * 8 + 0) + 0], inp[0]);
vps32 acc1 = vps32_mul(wgt[InChunks * (i * 8 + 1) + 0], inp[0]);
vps32 acc2 = vps32_mul(wgt[InChunks * (i * 8 + 2) + 0], inp[0]);
vps32 acc3 = vps32_mul(wgt[InChunks * (i * 8 + 3) + 0], inp[0]);
vps32 acc4 = vps32_mul(wgt[InChunks * (i * 8 + 4) + 0], inp[0]);
vps32 acc5 = vps32_mul(wgt[InChunks * (i * 8 + 5) + 0], inp[0]);
vps32 acc6 = vps32_mul(wgt[InChunks * (i * 8 + 6) + 0], inp[0]);
vps32 acc7 = vps32_mul(wgt[InChunks * (i * 8 + 7) + 0], inp[0]);
for (int j = 1; j < InChunks; j++) {
acc0 = vps32_fma(wgt[InChunks * (i * 8 + 0) + j], inp[j], acc0);
acc1 = vps32_fma(wgt[InChunks * (i * 8 + 1) + j], inp[j], acc1);
acc2 = vps32_fma(wgt[InChunks * (i * 8 + 2) + j], inp[j], acc2);
acc3 = vps32_fma(wgt[InChunks * (i * 8 + 3) + j], inp[j], acc3);
acc4 = vps32_fma(wgt[InChunks * (i * 8 + 4) + j], inp[j], acc4);
acc5 = vps32_fma(wgt[InChunks * (i * 8 + 5) + j], inp[j], acc5);
acc6 = vps32_fma(wgt[InChunks * (i * 8 + 6) + j], inp[j], acc6);
acc7 = vps32_fma(wgt[InChunks * (i * 8 + 7) + j], inp[j], acc7);
}
acc0 = vps32_hadd(acc0, acc1);
acc2 = vps32_hadd(acc2, acc3);
acc4 = vps32_hadd(acc4, acc5);
acc6 = vps32_hadd(acc6, acc7);
acc0 = vps32_hadd(acc0, acc2);
acc4 = vps32_hadd(acc4, acc6);
#if defined(USE_AVX2) || defined(USE_AVX)
__m128 sumabcd1 = _mm256_extractf128_ps(acc0, 0);
__m128 sumabcd2 = _mm256_extractf128_ps(acc0, 1);
__m128 sumefgh1 = _mm256_extractf128_ps(acc4, 0);
__m128 sumefgh2 = _mm256_extractf128_ps(acc4, 1);
sumabcd1 = _mm_add_ps(sumabcd1, sumabcd2);
[Redacted SWIFTCode] = _mm_add_ps([Redacted InternationalBankingAccountNumber], sumefgh2);
acc0 = _mm256_insertf128_ps(_mm256_castps128_ps256(sumabcd1), sumefgh1, 1);
out[i] = _mm256_max_ps(zero, _mm256_add_ps(bia[i], acc0));
#elif defined(USE_SSSE3)
out[i * 2 + 0] = vps32_max(zero, vps32_add(bia[i * 2 + 0], acc0));
out[i * 2 + 1] = vps32_max(zero, vps32_add(bia[i * 2 + 1], acc4));
#endif
}
}
INLINE void output_transform(float *weights, float *biases, float *inputs, float *outputs) {
assert(L3SIZE % 8 == 0);
const int InChunks = L3SIZE / vps32_cnt;
const vps32 *inp = (vps32 *) inputs;
const vps32 *wgt = (vps32 *) weights;
vps32 acc = vps32_mul(wgt[0], inp[0]);
for (int i = 1; i < InChunks; i++)
acc = vps32_fma(wgt[i], inp[i], acc);
#if defined(USE_AVX) || defined(USE_AVX2)
const __m128 hiQuad = _mm256_extractf128_ps(acc, 1);
const __m128 loQuad = _mm256_castps256_ps128(acc);
const __m128 sumQuad = _mm_add_ps(loQuad, hiQuad);
#elif defined(USE_SSSE3)
const __m128 sumQuad = acc;
#endif
const __m128 hiDual = _mm_movehl_ps(sumQuad, sumQuad);
const __m128 sumDual = _mm_add_ps(sumQuad, hiDual);
const __m128 hi = _mm_shuffle_ps(sumDual, sumDual, 0x1);
const __m128 sum = _mm_add_ss(sumDual, hi);
*outputs = (_mm_cvtss_f32(sum) + *biases);
}
void nnue_init(const char* fname) {
// Reads an NNUE file specificed by a User. If the datasize does not match
// the compiled NNUE configuration, abort. Afterwords, scale some weights
// for speed optimizations, and transpose the weights in L1 and L2
FILE *fin = fopen(fname, "rb");
if ( fread(in_biases, sizeof(int16_t), KPSIZE, fin) != (size_t) KPSIZE
|| fread(in_weights, sizeof(int16_t), INSIZE * KPSIZE, fin) != (size_t) INSIZE * KPSIZE)
abort_nnue("Unable to read NNUE File");
if ( fread(l1_biases, sizeof(int32_t), L2SIZE, fin) != (size_t) L2SIZE
|| fread(l1_weights, sizeof(int8_t), L1SIZE * L2SIZE, fin) != (size_t) L1SIZE * L2SIZE)
abort_nnue("Unable to read NNUE File");
if ( fread(l2_biases, sizeof(float), L3SIZE, fin) != (size_t) L3SIZE
|| fread(l2_weights, sizeof(float), L2SIZE * L3SIZE, fin) != (size_t) L2SIZE * L3SIZE)
abort_nnue("Unable to read NNUE File");
if ( fread(l3_biases, sizeof(float), OUTSIZE, fin) != (size_t) OUTSIZE
|| fread(l3_weights, sizeof(float), L3SIZE * OUTSIZE, fin) != (size_t) L3SIZE * OUTSIZE)
abort_nnue("Unable to read NNUE File");
scale_weights();
shuffle_input_layer();
quant_transpose(l1_weights, L1SIZE, L2SIZE);
float_transpose(l2_weights, L2SIZE, L3SIZE);
fclose(fin);
NNUE_LOADED = 1;
}
void nnue_incbin_init() {
// Inits from an NNUE file compiled into the binary. Assume the compiled
// data is correct for the given NNUE config. Afterwords, scale some
// weights for speed optimizations, and transpose the weights in L1 and L2
#ifdef EVALFILE
int8_t *data8; int16_t *data16; int32_t *data32; float *dataf;
// Input layer uses 16-bit Biases and Weights
data16 = (int16_t*) gIncWeightsData;
for (int i = 0; i < KPSIZE; i++)
in_biases[i] = *(data16++);
for (int i = 0; i < INSIZE * KPSIZE; i++)
in_weights[i] = *(data16++);
// Layer one uses 32-bit Biases and 8-bit Weights
data32 = (int32_t*) data16;
for (int i = 0; i < L2SIZE; i++)
l1_biases[i] = *(data32++);
data8 = (int8_t*) data32;
for (int i = 0; i < L1SIZE * L2SIZE; i++)
l1_weights[i] = *(data8++);
// Layer two and uses Floating Point Biases and Weights
dataf = (float*) data8;
for (int i = 0; i < L3SIZE; i++)
l2_biases[i] = *(dataf++);
for (int i = 0; i < L2SIZE * L3SIZE; i++)
l2_weights[i] = *(dataf++);
// Layer three and uses Floating Point Biases and Weights
for (int i = 0; i < OUTSIZE; i++)
l3_biases[i] = *(dataf++);
for (int i = 0; i < L3SIZE * OUTSIZE; i++)
l3_weights[i] = *(dataf++);
scale_weights();
shuffle_input_layer();
quant_transpose(l1_weights, L1SIZE, L2SIZE);
float_transpose(l2_weights, L2SIZE, L3SIZE);
NNUE_LOADED = 1;
#endif
}
int nnue_evaluate(Thread *thread, Board *board) {
int mg_eval, eg_eval;
const uint64_t white = board->colours[WHITE];
const uint64_t black = board->colours[BLACK];
const uint64_t kings = board->pieces[KING];
if (!NNUE_LOADED)
abort_nnue("NNUE File was not provided");
// For optimizations, auto-flag KvK as drawn
if (kings == (white | black)) return 0;
// Optimized computation of various input indices
int wrelksq = relativeSquare(WHITE, getlsb(white & kings));
int brelksq = relativeSquare(BLACK, getlsb(black & kings));
// Large enough to handle layer computations
ALIGN64 uint8_t out8[L1SIZE];
ALIGN64 float outN1[L1SIZE];
ALIGN64 float outN2[L1SIZE];
NNUEAccumulator *accum = &thread->nnueStack[thread->height];
if (!accum->accurate) {
// Possible to recurse and incrementally update each
if (nnue_can_update(accum, board))
nnue_update_accumulator(accum, board, wrelksq, brelksq);
// History is missing, we must refresh completely
else
nnue_refresh_accumulators(accum, board, wrelksq, brelksq);
}
// Feed-forward the entire evaluation function
halfkp_relu(accum, out8, board->turn);
quant_affine_relu(l1_weights, l1_biases, out8, outN1);
float_affine_relu(l2_weights, l2_biases, outN1, outN2);
output_transform(l3_weights, l3_biases, outN2, outN1);
// Perform the dequantization step and upscale the Midgame
mg_eval = 140 * ((int)(outN1[0]) >> SHIFT_L1) / 100;
eg_eval = 100 * ((int)(outN1[0]) >> SHIFT_L1) / 100;
// Cap the NNUE evaluation within [-2000, 2000]
mg_eval = MAX(-2000, MIN(2000, mg_eval));
eg_eval = MAX(-2000, MIN(2000, eg_eval));
return MakeScore(mg_eval, eg_eval);
}
ELO gains in Ethereal from version 12.0 to the current version can be attributed to improvements in both the search algorithm and the NNUE (Neural Network-based evaluation function). Here's a breakdown of the likely sources of ELO gains, categorized into NNUE and search improvements:
NNUE Improvements:
New networks: The introduction of new networks, such as the Fischer Network and the Standard Network, which were trained using different methods and data, contributed to ELO gains. These networks are more accurate and robust, leading to better evaluation and decision-making.
c
// New Fischer Network: Repeat process using 13.85 Network for Data Generation
// New Standard Network: Repeat process using 13.84 Network for Data Generation
code
2. **Tiling**: The implementation of tiling in the accumulator, which allows for more efficient computation of the NNUE evaluation, resulted in a significant speedup.
```c
// Speedup Accumulator using Tiling (Thanks to [Redacted Person]'s author [Redacted Person])
Simplified LMR logic: The simplification of the LMR (Late Move Reduction) logic in Ethereal to match that of other engines made the search more efficient.
c
// Simplify LMR logic in Ethereal to match that of other engines
code
Search Improvements:
1. **ProbCut Margin increase**: The increase in the ProbCut Margin from 80cp to 100cp improved the search by allowing for more aggressive pruning.
```c
// Increase ProbCut Margin from 80cp to 100cp
Simplified History component: The simplification of the History component of LMR during Tactical moves improved the search by reducing the complexity of the algorithm.
c
// Simplify the History component of LMR during Tactical moves
code
3. **Improved Syzygy probing**: The improvement in Syzygy probing, including the reduction of look-ahead reads on Linux and the fixing of issues with wide DTZ tables, contributed to ELO gains.
```c
// Improve Syzygy by reducing look ahead reads on Linux (Thanks Aloril + SF)
// Fix Syzygy issue for wide DTZ tables
MultiPV analysis: The addition of MultiPV analysis allowed for more thorough exploration of different lines, leading to ELO gains.
c
// Add MultiPV information to the README
code
5. **Miscellaneous improvements**: Other miscellaneous improvements, such as the use of Transparent Huge Pages, the resolution of fail-highs with progressively shallower searches, and the tuning of evaluation terms, also contributed to ELO gains.
```c
// Advise the Kernal to use Transparent Huge Pages
// Resolve fail-highs with progressively shallower searches
// Tune all of the "normal" terms using a Standard book
These improvements collectively contributed to the significant ELO gains observed in Ethereal from version 12.0 to the current version.
In the context of the NNUE (Neural Network-based evaluation function) accumulator, "tiling" likely refers to a technique used to improve the efficiency of computations. Here are some possible meanings of "tiling" in this context:
Memory tiling: This involves dividing the input data into smaller, contiguous blocks (tiles) that fit within the cache memory. By processing the data in tiles, the algorithm can reduce memory access latency and improve cache locality, leading to faster computation.
In the context of the NNUE accumulator, memory tiling might involve dividing the input weights or activations into smaller blocks that can be processed independently, reducing memory access overhead.
Computation tiling: This involves dividing the computation into smaller, independent tasks (tiles) that can be executed in parallel or in a more efficient order. By breaking down the computation into smaller tiles, the algorithm can reduce dependencies between tasks and improve overall efficiency.
In the context of the NNUE accumulator, computation tiling might involve dividing the matrix multiplications or activation functions into smaller tasks that can be executed independently, reducing the computational overhead.
Loop tiling: This involves dividing the loops in the algorithm into smaller, nested loops (tiles) that can be executed more efficiently. By reducing the loop overhead and improving cache locality, loop tiling can lead to significant performance improvements.
In the context of the NNUE accumulator, loop tiling might involve dividing the loops that iterate over the input data or weights into smaller, nested loops that can be executed more efficiently.
Data tiling: This involves dividing the input data into smaller, overlapping blocks (tiles) that can be processed independently. By processing the data in overlapping tiles, the algorithm can reduce the overhead of boundary conditions and improve overall efficiency.
In the context of the NNUE accumulator, data tiling might involve dividing the input data into smaller, overlapping blocks that can be processed independently, reducing the overhead of boundary conditions.
To identify the specific type of tiling used in the NNUE accumulator, we would need to examine the code changes introduced by the "Speedup Accumulator using Tiling" commit. The code snippet you provided does not contain enough information to determine the exact type of tiling used.
However, based on the context and the mention of "Speedup Accumulator using Tiling", it is likely that the tiling technique used is related to memory tiling or computation tiling, as these techniques are commonly used to improve the efficiency of neural network computations.
The 4:1 ratio and the tiling of NUM_REGS are likely used to optimize the NNUE accumulator's performance by improving cache locality and reducing memory access overhead. Here's a possible explanation:
4:1 ratio:
The 4:1 ratio might refer to the ratio of the number of elements processed in a single iteration of the accumulator's loop to the number of elements stored in a cache line.
Modern CPUs typically have a cache hierarchy with different levels of cache memory, each with its own size and access latency. The L1 cache, for example, is the smallest and fastest cache level, with a typical size of 32KB to 64KB.
Assuming a 64-byte cache line size (which is a common size for many modern CPUs), a 4:1 ratio would mean that the accumulator processes 4 elements (e.g., 4 16-bit integers) in a single iteration, which fits within a single 64-byte cache line.
By processing 4 elements at a time, the accumulator can reduce the number of memory accesses required to fetch the input data. This can lead to a significant speedup, especially when the input data is large and doesn't fit entirely within the L1 cache.
Tiling of NUM_REGS:
The tiling of NUM_REGS likely refers to the division of the accumulator's registers into smaller, contiguous blocks (tiles) that can be processed independently.
In the context of the NNUE accumulator, NUM_REGS might represent the number of registers used to store the intermediate results of the accumulation. By dividing these registers into smaller tiles, the accumulator can reduce the number of memory accesses required to store and load the intermediate results.
For example, if NUM_REGS is 16, the accumulator might divide these registers into 4 tiles of 4 registers each. This would allow the accumulator to process each tile independently, reducing the number of memory accesses required to store and load the intermediate results.
By reducing the number of memory accesses and improving cache locality, the tiling of NUM_REGS can lead to a significant speedup in the NNUE accumulator's performance.
Combination of 4:1 ratio and tiling:
The combination of the 4:1 ratio and the tiling of NUM_REGS might allow the NNUE accumulator to achieve even better performance.
By processing 4 elements at a time (4:1 ratio) and dividing the registers into smaller tiles (tiling), the accumulator can reduce the number of memory accesses required to fetch the input data and store the intermediate results. This can lead to a significant reduction in memory access overhead and a corresponding increase in performance.
Overall, the 4:1 ratio and the tiling of NUM_REGS are likely used to optimize the NNUE accumulator's performance by improving cache locality and reducing memory access overhead. By processing multiple elements at a time and dividing the registers into smaller tiles, the accumulator can achieve better performance and efficiency.
For example, the code might use the _mm256_add_epi16 instruction to add 16 16-bit integers in parallel.
c
__m256i a = _mm256_loadu_si256((__m256i*) &in_white[i]);
__m256i b = _mm256_loadu_si256((__m256i*) &weights[i]);
__m256i result = _mm256_add_epi16(a, b);
code
2. **Loop unrolling**: The code might be using loop unrolling to process multiple elements in a single iteration. This can help to reduce the overhead of loop control statements and improve parallelism.
For example, the code might use a loop that processes 4 elements at a time, like this:
```c
for (int i = 0; i < KPSIZE / 4; i += 4) {
// Process 4 elements in parallel
vepi16 sum0 = vepi16_maubs(inp[j+0], wgt[InChunks * (i * 8 + k) + j + 0]);
vepi16 sum1 = vepi16_maubs(inp[j+1], wgt[InChunks * (i * 8 + k) + j + 1]);
vepi16 sum2 = vepi16_maubs(inp[j+2], wgt[InChunks * (i * 8 + k) + j + 2]);
vepi16 sum3 = vepi16_maubs(inp[j+3], wgt[InChunks * (i * 8 + k) + j + 3]);
// ...
}
Compiler optimizations: The compiler might be optimizing the code to use parallel processing and SIMD instructions automatically. This can be achieved using compiler flags such as -O3 or -mavx2.
Intrinsic functions: The code might be using intrinsic functions, which are functions that are built into the compiler and provide direct access to hardware instructions. Intrinsic functions can be used to write parallel code that is optimized for specific hardware architectures.
For example, the code might use the _mm256_hadd_epi16 intrinsic function to perform a horizontal add operation on 16 16-bit integers in parallel.
c
__m256i a = _mm256_loadu_si256((__m256i*) &in_white[i]);
__m256i b = _mm256_loadu_si256((__m256i*) &weights[i]);
__m256i result = _mm256_hadd_epi16(a, b);
code
By using these techniques, the code can ensure that the 4 elements are processed in parallel and that the branching is obvious, resulting in improved performance and efficiency.