1- use std:: borrow :: Cow ;
1+ use std:: fmt ;
22
33use bytemuck:: { Pod , Zeroable } ;
44use rand:: Rng ;
@@ -21,11 +21,18 @@ pub enum BinaryQuantizedEuclidean {}
2121
2222/// The header of `BinaryQuantizedEuclidean` leaf nodes.
2323#[ repr( C ) ]
24- #[ derive( Pod , Zeroable , Debug , Clone , Copy ) ]
24+ #[ derive( Pod , Zeroable , Clone , Copy ) ]
2525pub struct NodeHeaderBinaryQuantizedEuclidean {
2626 /// An extra constant term to determine the offset of the plane
2727 bias : f32 ,
2828}
29+ impl fmt:: Debug for NodeHeaderBinaryQuantizedEuclidean {
30+ fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
31+ f. debug_struct ( "NodeHeaderBinaryQuantizedEuclidean" )
32+ . field ( "bias" , & format ! ( "{:.4}" , self . bias) )
33+ . finish ( )
34+ }
35+ }
2936
3037impl Distance for BinaryQuantizedEuclidean {
3138 const DEFAULT_OVERSAMPLING : usize = 3 ;
@@ -59,29 +66,30 @@ impl Distance for BinaryQuantizedEuclidean {
5966 fn create_split < ' a , R : Rng > (
6067 children : & ' a ImmutableSubsetLeafs < Self > ,
6168 rng : & mut R ,
62- ) -> heed:: Result < Cow < ' a , UnalignedVector < Self :: VectorCodec > > > {
69+ ) -> heed:: Result < Leaf < ' a , Self > > {
6370 let [ node_p, node_q] = two_means :: < Self , Euclidean , R > ( rng, children, false ) ?;
6471 let vector: Vec < f32 > =
6572 node_p. vector . iter ( ) . zip ( node_q. vector . iter ( ) ) . map ( |( p, q) | p - q) . collect ( ) ;
6673 let mut normal = Leaf {
6774 header : NodeHeaderBinaryQuantizedEuclidean { bias : 0.0 } ,
68- vector : UnalignedVector :: from_slice ( & vector) ,
75+ vector : UnalignedVector :: from_vec ( vector) ,
6976 } ;
7077 Self :: normalize ( & mut normal) ;
7178
72- Ok ( Cow :: Owned ( normal. vector . into_owned ( ) ) )
79+ normal. header . bias = normal
80+ . vector
81+ . iter ( )
82+ . zip ( UnalignedVector :: < BinaryQuantized > :: from_vec ( node_p. vector . to_vec ( ) ) . iter ( ) )
83+ . zip ( UnalignedVector :: < BinaryQuantized > :: from_vec ( node_q. vector . to_vec ( ) ) . iter ( ) )
84+ . map ( |( ( n, p) , q) | -n * ( p + q) / 2.0 )
85+ . sum ( ) ;
86+
87+ Ok ( normal)
7388 }
7489
7590 fn margin ( p : & Leaf < Self > , q : & Leaf < Self > ) -> f32 {
7691 p. header . bias + dot_product_binary_quantized ( & p. vector , & q. vector )
7792 }
78-
79- fn margin_no_header (
80- p : & UnalignedVector < Self :: VectorCodec > ,
81- q : & UnalignedVector < Self :: VectorCodec > ,
82- ) -> f32 {
83- dot_product_binary_quantized ( p, q)
84- }
8593}
8694
8795/// For the binary quantized squared euclidean distance:
0 commit comments