naveentatikonda commented on issue #13350:
URL: https://github.com/apache/lucene/issues/13350#issuecomment-2118316039

   @benwtrent I tried to setup luceneutil but I was running into a ton of 
compilation errors when building it against latest lucene src code. I have a 
doubt in the existing quantization process of 
[quantizing](https://github.com/apache/lucene/blob/main/lucene/core/src/java/org/apache/lucene/util/quantization/ScalarQuantizer.java#L126-L148)
 a float value in a vector.  
   
   ```
   // For 8 bits, this logic quantizes float value into uint8 range in the form 
of floats [0.0, 255.0]
   
   float dx = v - minQuantile;
   float dxc = Math.max(minQuantile, Math.min(maxQuantile, v)) - minQuantile;
   float dxs = scale * dxc;
   ```
   
   Here to bring it into signed int8 range of [-128, 127] we are casting it 
into byte. But, this leads to change of sign and magnitude for values > 127 and 
< 255 like 128 will be casted as -128 and 129 as -127 and so on...Is this a 
right way of quantizing because for spacetypes like inner product the sign 
matters when we are computing the distance and score ?  
   ```
   dest[destIndex] = (byte) Math.round(dxs);
   ```
   
   I have added a [simple unit 
test](https://github.com/naveentatikonda/lucene/commit/101400d5ae21bfa4009e8ee24692f410101255ea)
 to show that because of this type casting, even if we dequantize the quantized 
vector we don't get the original vector back.
   
   Also, the corrective offset calculation correlates with the mathematical 
derivation in 
[documentation](https://lucene.apache.org/core/9_9_1/core/org/apache/lucene/util/ScalarQuantizer.html).
 But, for max inner product I have a simple example which is reranking the 
results because of this corrective offset.
   ```
   minQuantile * (v - minQuantile / 2.0F) + (dx - dxq) * dxq
   ```
   
   ```
   Let, 
   V1 -> [-100.0, 20.0]
   V2 -> [100.0, 20.0]
   Query Vec -> [100.0, 20.0]
   
   When we perform search against float vectors, the ranking of results are  
V2, V1 with score calculation as
   Q.V1 -> 1/(1+10400) = 0.00009614
   Q.V2 -> 10401
   
   When these vectors are quantized with minQuantile as -100.0 and maxQuantile 
as 100.0, 
   the order of the results changes to V1, V2
   
   V1 -> [-100.0, 20.0] after quantization becomes [0, -103] with corrective 
offset as -2000
   V2 -> [100.0, 20.0] after quantization becomes [-1, 102] with corrective 
offset as -18000
   QVec same as V2 becomes [-1, 102] with query offset as -18000
   
   Q.V1 ->  q * v1 * alpha^2 + corrective offset + query offset 
             = -10506 * 0.61514807 -2000 -18000 = -26462.746 = 1/(1+26462.746) 
= 3.7787544E-5
   
   Q.V2 -> 10406 * 0.61514807 -18000 - 18000 = -29599.385 = 1/(1+29599.385) = 
3.3783344E-5
   ```
    


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@lucene.apache.org
For additional commands, e-mail: issues-h...@lucene.apache.org

Reply via email to