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
NNUEs (Neural Network-based Universal Evaluators) in chess engines are designed to efficiently evaluate positions by leveraging neural networks. The architecture of these networks typically involves several layers, including input, hidden, and output layers.
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
With a 50% increase in compute, offset by narrowing the first layer, the idea of subtracting the embedding for each side and concatenating it to the inputs becomes more feasible. By allowing interactions to occur in the pre-layer, you're effectively creating a more informative input representation, which can be beneficial for the network's performance.
Narrowing the first layer can help to reduce the dimensionality of the input space, making it more manageable for the network to process. This, combined with the additional information provided by the concatenated embeddings, might lead to improved performance.
In this context, the approach you're suggesting can be seen as a form of feature engineering, where you're explicitly designing the input representation to capture relevant information about the chess position. By doing so, you're providing the network with a more robust foundation for learning and making predictions.
It's also worth noting that the additional compute required for this approach might be justified if it leads to improved performance and a stronger chess engine. The trade-off between compute resources and playing strength is a common consideration in chess engine development.
Have you experimented with this approach, or is this a theoretical idea you're considering implementing? I'm curious to know more about your thoughts on this and any potential results you've observed.
User
Is there a fast operation to flip the signs of int8s in avx2
Assistant
Yes, you can use the _mm256_sign_epi8 intrinsic in AVX2 to flip the signs of int8 values.
The _mm256_sign_epi8 intrinsic takes two arguments: the input vector and a vector of signs. By passing a vector of all -1 values as the second argument, you effectively flip the signs of all the int8 values in the input vector.
Alternatively, you can use the _mm256_xor_si256 intrinsic to flip the signs by XORing the input vector with a vector of all -1 values:
Both of these methods should be very fast and efficient.
Note that if you're using a compiler that supports GCC-style vector extensions (e.g., GCC, Clang), you can also use the __builtin_ia32_psignb256 built-in function to flip the signs:
However, this is not as portable as using the intrinsic.
User
How about a different flip for each int8 position
Assistant
If you want to flip the signs of each int8 value based on a separate mask (i.e., a different flip for each position), you can use the _mm256_sign_epi8 intrinsic with a vector of signs that corresponds to the desired flips.
Here's an example:
__m256i input = _mm256_loadu_si256((const __m256i*)data); // load 32 int8 values
__m256i signs = _mm256_loadu_si256((const __m256i*)sign_mask); // load 32 sign values (1 for no flip, -1 for flip)
__m256i flipped = _mm256_sign_epi8(input, signs); // flip signs based on sign mask
In this example, the signs vector contains 32 int8 values, where each value is either 1 (no flip) or -1 (flip). The _mm256_sign_epi8 intrinsic will then flip the signs of the corresponding int8 values in the input vector based on the values in the signs vector.
User
Is this faster than a full int8 matmul ie unsigned signed mult of int8 int8 (with accumulate is fine)
Assistant
The performance comparison between the sign-flipping approaches I mentioned earlier and a full int8 matrix multiplication (matmul) depends on the specific hardware and implementation details.
However, in general, a full int8 matmul with accumulation is likely to be faster than the sign-flipping approaches for several reasons:
Native support: Modern CPU architectures, such as Intel's Ice Lake and later, have native support for matrix multiplication instructions, like and . These instructions are optimized for performance and can take advantage of the CPU's execution units.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
The idea of subtracting the embedding for each side and concatenating it to the inputs is an interesting one. However, there are several reasons why this approach might not be commonly used in NNUEs:
Dimensionality: NNUEs already operate on a relatively high-dimensional space, especially when considering the number of possible chess positions. Increasing the dimensionality further by concatenating additional features might make the network more prone to overfitting, particularly if the additional features are not highly informative.
Feature correlation: The embeddings for each side are likely to be highly correlated, as they represent similar information (e.g., piece placements, attacks, etc.). Concatenating these features might introduce redundancy and make it more challenging for the network to learn meaningful patterns.
Interactions: While directly spotting interactions at a high dimension before the middle layer might seem beneficial, NNUEs are designed to learn complex interactions between pieces and squares through the multiple layers. The network can learn to represent these interactions in a more abstract and efficient manner, rather than relying on explicit feature engineering.
Efficiency: NNUEs are typically optimized for performance and efficiency, as they need to evaluate a large number of positions during the search process. Adding additional computations, such as subtracting and concatenating embeddings, might increase the evaluation time and negatively impact the engine's overall performance.
Training data: The quality and quantity of the training data play a crucial role in the development of NNUEs. If the training data does not contain sufficient examples of positions where the proposed feature would be beneficial, the network might not learn to utilize it effectively.
In summary, while the idea of subtracting the embedding for each side and concatenating it to the inputs is an interesting one, it might not be a common approach in NNUEs due to concerns about dimensionality, feature correlation, interactions, efficiency, and training data. However, it's always worth exploring new ideas and experimenting with different architectures to improve the performance of NNUEs.
_mm256_sign_epi8
Alternatively, you can use the _mm256_xor_si256 intrinsic with a vector of mask values, where each mask value is either 0 (no flip) or -1 (flip):
__m256i mask = _mm256_loadu_si256((const __m256i*)flip_mask); // load 32 mask values (0 for no flip, -1 for flip)
__m256i flipped = _mm256_xor_si256(input, mask); // flip signs based on mask
In this case, the mask vector contains 32 int8 values, where each value is either 0 (no flip) or -1 (flip). The _mm256_xor_si256 intrinsic will then flip the signs of the corresponding int8 values in the input vector based on the values in the mask vector.
Both of these methods allow you to flip the signs of each int8 value based on a separate mask, giving you more control over the flipping process.
int8
VPMADDUBSW
VPMADDWD
SIMD parallelism: Matrix multiplication can be easily parallelized using SIMD (Single Instruction, Multiple Data) instructions, which can process multiple elements simultaneously. This can lead to significant performance improvements compared to the sign-flipping approaches, which might require more sequential operations.
Pipelining: Modern CPUs have deep pipelines, which allow them to process multiple instructions in parallel. Matrix multiplication can be pipelined more efficiently than the sign-flipping approaches, reducing the overall execution time.
That being said, the performance difference between the two approaches will depend on the specific use case and implementation details. If you're working with small matrices, the sign-flipping approaches might be faster due to the overhead of setting up the matmul operation. However, for larger matrices, the [Redacted Person] approach is likely to be faster.
To give you a rough estimate, here are some numbers based on Intel's Ice Lake architecture:
VPMADDUBSW (matmul) instruction: 1-2 cycles per element (depending on the matrix size and layout)
_mm256_sign_epi8 (sign-flipping) instruction: 2-4 cycles per element (depending on the implementation and CPU model)
Keep in mind that these numbers are approximate and can vary depending on the specific use case and hardware.
In summary, while the sign-flipping approaches can be useful in certain situations, a full int8 matmul with accumulation is likely to be faster for larger matrices due to native hardware support, SIMD parallelism, and pipelining.