Why don't chess engine NNUEs include a step of subtracting the embedding for each size and concat to inputs (so it would directly spot interactions at a high dimension before the middle layer)?
Assistant
User
It's 50% more compute, which can be offset by narrowing the first layer (workable because interations have already occurred in the pre layer)
Assistant
While reducing the first layer's width after pre-calculating interactions might offset some of the increased computational cost, it doesn't address all the concerns:
User
Is there a fast operation to flip the signs of int8s in avx2
Assistant
Yes, there's a fast way to flip the signs (negate) of int8_t values using AVX2:
This code leverages the _mm256_sub_epi8 intrinsic, which subtracts packed 8-bit integers. By subtracting the original values from a vector of zeros (_mm256_setzero_si256()), you effectively negate each element. This is a single, efficient AVX2 instruction.
User
How about a different flip for each int8 position
Assistant
Flipping the sign of each int8_t based on a per-element mask requires a slightly different approach. You can achieve this using the _mm256_sign_epi8 intrinsic:
values: This is the __m256i vector containing the int8_t values you want to potentially negate.
User
Is this faster than a full int8 matmul ie unsigned signed mult of int8 int8 (with accumulate is fine)
Assistant
No, _mm256_sign_epi8 (or even _mm256_sub_epi8 for full negation) is not faster than a full int8 matrix multiplication, especially when considering the accumulation step that's usually part of a matrix multiplication. Here's why:
Purpose:_mm256_sign_epi8 and _mm256_sub_epi8 are element-wise operations. They operate on individual bytes within a vector. Matrix multiplication, on the other hand, involves a series of multiply-accumulate operations across rows and columns.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The query asks why chess engine NNUEs don't subtract piece embeddings before concatenating them as input features, to potentially capture piece interactions earlier in the network. While theoretically plausible, there are several reasons why this isn't typically done:
Complexity and Interpretability: The current NNUE architecture is designed for incremental updates. Subtracting embeddings would complicate this process significantly. Imagine a piece moving: you'd need to subtract the old location embedding and add the new one and also recalculate and propagate the subtraction results for all other pieces. This added complexity makes the update process much harder to optimize and debug. The current design allows for a simpler, linear update process based on individual piece additions and removals.
Performance: NNUEs are highly optimized for speed. The incremental updates and the relatively simple feed-forward process are crucial for evaluating millions of positions per second. Introducing subtraction operations for every piece pair would introduce a significant performance overhead, especially considering the large input dimension.
Implicit Interaction Capture: While the NNUE architecture doesn't explicitly subtract embeddings, the hidden layers do implicitly capture interactions between pieces. The weights of the first hidden layer (l1_weights in the code) learn to combine the input features in a way that reflects piece interactions. This happens through the training process, where the network learns to approximate the evaluation function based on a massive dataset of chess positions. Subtracting embeddings explicitly might be redundant or even counterproductive, as it could constrain the network's ability to learn complex interactions.
Training Complexity: Adding subtraction would significantly increase the number of input features, potentially making the network harder to train. It could require more data and more careful tuning of hyperparameters to avoid overfitting.
Lack of Clear Benefit: There's no clear theoretical guarantee that subtracting embeddings would significantly improve the evaluation accuracy. It's possible that the added complexity and computational cost would outweigh any potential benefits. Empirical evidence from existing NNUE implementations suggests that the current architecture is already very effective at capturing piece interactions.
In summary, while subtracting embeddings might seem like a promising idea, the added complexity, performance overhead, and lack of guaranteed benefit likely explain why it's not a common practice in NNUE architectures. The current design prioritizes incremental updates, speed, and simplicity, while still effectively capturing piece interactions through the learned weights of the hidden layers.
Incremental Updates: The core challenge remains the complexity of incremental updates. Even with a narrower first layer, updating the pre-calculated interaction terms for every move would be significantly more complex than the current system of adding/subtracting individual piece embeddings. This complexity hinders efficient implementation and optimization, especially for highly optimized engines like Ethereal that rely on SIMD instructions.
Non-Obvious Optimal Width Reduction: While you suggest narrowing the first layer, it's not clear how much narrower it could become without losing important information. The current width is likely chosen based on extensive experimentation and might be close to optimal for the existing architecture. Reducing it arbitrarily could degrade performance.
Interaction Representation: Pre-calculating interactions as differences might not be the most efficient representation. The network's hidden layers are capable of learning complex non-linear relationships, which might be more expressive than simple differences. A wider first layer with individual piece embeddings allows the network more flexibility to learn these relationships.
Training Complexity: Even with a reduced width, the increased number of initial features (due to the pairwise subtractions) could still make training more complex. It's not just about the compute cost during evaluation; training also requires backpropagation and gradient updates, which become more complex with a larger input layer.
Alternative Optimization Strategies: Instead of pre-calculating interactions, other optimization strategies might be more effective. For example, reducing the size of the subsequent layers (L2, L3) or using more efficient activation functions could achieve similar performance gains without sacrificing the simplicity and update efficiency of the current design.
In short, while the idea of pre-calculating interactions and reducing the first layer width has some merit, it introduces significant complexity in incremental updates and doesn't guarantee a net benefit after accounting for training complexity and potential information loss. The current NNUE architecture's focus on efficient incremental updates and simpler training remains a strong argument in its favor.
mask: This is the __m256i mask. Each byte of the mask controls the corresponding byte in values. If a mask byte is negative (most significant bit set), the corresponding value byte will be negated. If the mask byte is positive or zero, the corresponding value byte will remain unchanged.
The result of _mm256_sign_epi8(values, mask) would be:
result: [-1, 2, -3, -4, -5, -6, -7, 8, ...]
As you can see, the values at indices 0, 2, 4, 6,... are negated because the corresponding mask bytes are negative.
Creating the Mask:
You'll need to create the mask vector based on your specific needs. You can use various intrinsics like _mm256_set_epi8, _mm256_setr_epi8, or load it from memory if you have a pre-computed mask. For example, to create the alternating mask shown above:
Or, a more general approach to set specific bytes:
__m256i create_mask(int indices_to_negate[], int num_indices) {
char mask_array[32] = { [Redacted InternationalBankingAccountNumber], 1, 1, 1, 1 };
for (int i = 0; i < num_indices; ++i) {
mask_array[indices_to_negate[i]] = -1;
}
return _mm256_loadu_si256(reinterpret_cast<__m256i*>(mask_array));
}
This approach provides a very efficient way to selectively negate int8_t values within an AVX2 register. Remember to include <immintrin.h> for AVX2 intrinsics.
Specialized Instructions: Modern processors (especially those supporting AVX2 and later) have dedicated instructions for integer matrix multiplication, like _mm256_maddubs_epi16 (multiply and add packed unsigned and signed bytes) and its variants. These instructions are highly optimized for the specific task of matrix multiplication and are significantly faster than performing individual multiplications and additions.
Throughput: Matrix multiplication instructions are designed for high throughput. They can perform many multiply-accumulate operations in parallel, exploiting the processor's SIMD capabilities to the fullest. _mm256_sign_epi8 and _mm256_sub_epi8, while fast for their respective operations, don't achieve the same level of throughput as dedicated matrix multiplication instructions.
Fused Multiply-Add: Instructions like _mm256_maddubs_epi16 perform a fused multiply-add operation. This means the multiplication and addition are performed in a single step, further enhancing performance compared to separate multiplication and addition operations.
In summary, while element-wise negation or sign manipulation using AVX2 intrinsics are efficient operations, they are not faster than dedicated int8 matrix multiplication instructions when considering the accumulation step that's integral to matrix multiplication. Matrix multiplication benefits from specialized hardware and higher throughput, making it significantly faster for its intended purpose.