# Conceptual Python code for linear symmetric quantization import numpy as np def quantize_tensor_symmetric(tensor_fp, num_bits): """ Applies symmetric quantization to a floating-point tensor. Assumes zero_point = 0 for simplicity. """ q_min = -(2**(num_bits - 1)) q_max = (2**(num_bits - 1)) - 1 # Determine scale factor abs_max_val = np.max(np.abs(tensor_fp)) scale_factor = abs_max_val / q_max if q_max != 0 else 1.0 # Quantize q_tensor = np.round(tensor_fp / scale_factor) # Clip to quantization range q_tensor = np.clip(q_tensor, q_min, q_max) return q_tensor.astype(np.int64), scale_factor, 0 # Returns quantized tensor, scale, zero_point def dequantize_tensor_symmetric(q_tensor, scale_factor, zero_point): """ Dequantizes a symmetric quantized tensor. """ return (q_tensor - zero_point) * scale_factor # Example usage fp_weights = np.random.randn(4, 4) * 10 print(f"Original FP weights:\n{fp_weights}\n") num_bits = 4 q_weights, scale, zero_point = quantize_tensor_symmetric(fp_weights, num_bits) print(f"{num_bits}-bit Quantized weights:\n{q_weights}\n") print(f"Scale factor: {scale}, Zero point: {zero_point}\n") dequantized_weights = dequantize_tensor_symmetric(q_weights, scale, zero_point) print(f"Dequantized FP weights:\n{dequantized_weights}\n") print(f"Error (RMSE): {np.sqrt(np.mean((fp_weights - dequantized_weights)**2))}\n")