The best way to train a neural network with int8 quantization awareness in one layer is to simulate the quantization during training. This is often called "quantization-aware training" (QAT). Here's how it applies to the Ethereal code and general best practices:
Ethereal's Approach (and its limitations):
Ethereal's current implementation doesn't use QAT. It trains with full precision (floating-point) and then quantizes the weights afterward. This post-training quantization can lead to a significant drop in accuracy, especially for layers with very low precision like int8. While Ethereal uses some mitigations like scaling and shifting, it doesn't address the core issue of the network not being trained to handle quantized weights.
Quantization-Aware Training (QAT): The Recommended Approach
QAT involves simulating the quantization process during the forward pass of training. The backward pass, however, continues to use full-precision gradients. This allows the network to learn weights that are robust to quantization.
Here’s a breakdown of how to implement QAT, generally and in the context of Ethereal:
Fake Quantization: Introduce a “fake quantization” operation within the network's forward pass during training. This operation takes floating-point weights and simulates their quantization to int8, but importantly, keeps the underlying representation in floating-point. This is crucial for backpropagation.
Straight-Through Estimator: During backpropagation, use the “straight-through estimator” (STE). The STE simply passes the gradient through the fake quantization operation as if it were the identity function. While the gradient is not technically correct, the STE has been empirically shown to work well for QAT.
Placement of Fake Quantization: In Ethereal’s case, you want one layer (L1) to be aware of its quantization. Therefore, you'd insert the fake quantization operation immediately after the weight multiplication in the forward pass for L1. The other layers would remain in full precision during training.
Code Example (Conceptual - Adapting Ethereal's halfkp_relu_quant_affine_relu):
c
// ... (Existing Code)
// Inside the loop for L1 calculations:
vepi16 quantized_weights = fake_quantize_to_int8(wgt); // Introduce fake quantization
vepi16 sum0 = vepi16_maubs(vepi16_relu_packu(inp[0], inp[1]), quantized_weights); // Use quantized weights
// ... (Rest of the existing L1 and subsequent layer calculations)
fake_quantize_to_int8 Function (Conceptual):
c
vepi16 fake_quantize_to_int8(vepi8 weights_float) {
// Implement the logic to quantize the floating-point weights
// to int8 range, but keep the result in a floating-point
// representation for backpropagation. This would typically
// involve scaling and clamping.
// ... (Quantization Logic) ...
return quantized_weights_float;
}
Key Considerations for Ethereal:
Data Type: Ethereal uses various vectorized data types. You'll need to adapt the fake quantization and STE to work correctly with these types (vepi8, vepi16, etc.).
Performance: QAT adds some overhead during training. However, the improved accuracy after quantization usually justifies the cost.
Training Framework: If you were to integrate a more standard deep learning training framework (like TensorFlow or PyTorch), implementing QAT would be significantly simpler as these frameworks often have built-in support for quantization-aware training.
By implementing QAT, Ethereal's L1 layer would be explicitly trained to handle the effects of int8 quantization, leading to a more accurate and robust evaluation function after quantization.