Skip to content

Commit 633b097

Browse files
author
nnethercott
committed
add debug impls for all node headers
different platforms show varying numbers of digits in f32 debug impl => failing tests on linux vs macos runners. to fix this we ensure only 4 digits are printed
1 parent 69b6185 commit 633b097

File tree

8 files changed

+67
-9
lines changed

8 files changed

+67
-9
lines changed

src/distance/binary_quantized_cosine.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use bytemuck::{Pod, Zeroable};
24
use rand::Rng;
35

@@ -18,10 +20,17 @@ pub enum BinaryQuantizedCosine {}
1820

1921
/// The header of `BinaryQuantizedCosine` leaf nodes.
2022
#[repr(C)]
21-
#[derive(Pod, Zeroable, Debug, Clone, Copy)]
23+
#[derive(Pod, Zeroable, Clone, Copy)]
2224
pub struct NodeHeaderBinaryQuantizedCosine {
2325
norm: f32,
2426
}
27+
impl fmt::Debug for NodeHeaderBinaryQuantizedCosine {
28+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29+
f.debug_struct("NodeHeaderBinaryQuantizedCosine")
30+
.field("norm", &format!("{:.4}", self.norm))
31+
.finish()
32+
}
33+
}
2534

2635
impl Distance for BinaryQuantizedCosine {
2736
const DEFAULT_OVERSAMPLING: usize = 3;

src/distance/binary_quantized_euclidean.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use bytemuck::{Pod, Zeroable};
24
use rand::Rng;
35

@@ -19,11 +21,18 @@ pub enum BinaryQuantizedEuclidean {}
1921

2022
/// The header of `BinaryQuantizedEuclidean` leaf nodes.
2123
#[repr(C)]
22-
#[derive(Pod, Zeroable, Debug, Clone, Copy)]
24+
#[derive(Pod, Zeroable, Clone, Copy)]
2325
pub struct NodeHeaderBinaryQuantizedEuclidean {
2426
/// An extra constant term to determine the offset of the plane
2527
bias: f32,
2628
}
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+
}
2736

2837
impl Distance for BinaryQuantizedEuclidean {
2938
const DEFAULT_OVERSAMPLING: usize = 3;

src/distance/binary_quantized_manhattan.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use bytemuck::{Pod, Zeroable};
24
use rand::Rng;
35

@@ -18,11 +20,18 @@ pub enum BinaryQuantizedManhattan {}
1820

1921
/// The header of BinaryQuantizedEuclidean leaf nodes.
2022
#[repr(C)]
21-
#[derive(Pod, Zeroable, Debug, Clone, Copy)]
23+
#[derive(Pod, Zeroable, Clone, Copy)]
2224
pub struct NodeHeaderBinaryQuantizedManhattan {
2325
/// An extra constant term to determine the offset of the plane
2426
bias: f32,
2527
}
28+
impl fmt::Debug for NodeHeaderBinaryQuantizedManhattan {
29+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
30+
f.debug_struct("NodeHeaderBinaryQuantizedManhattan")
31+
.field("bias", &format!("{:.4}", self.bias))
32+
.finish()
33+
}
34+
}
2635

2736
impl Distance for BinaryQuantizedManhattan {
2837
const DEFAULT_OVERSAMPLING: usize = 3;

src/distance/cosine.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use bytemuck::{Pod, Zeroable};
24
use rand::Rng;
35

@@ -16,10 +18,15 @@ pub enum Cosine {}
1618

1719
/// The header of Cosine leaf nodes.
1820
#[repr(C)]
19-
#[derive(Pod, Zeroable, Debug, Clone, Copy)]
21+
#[derive(Pod, Zeroable, Clone, Copy)]
2022
pub struct NodeHeaderCosine {
2123
norm: f32,
2224
}
25+
impl fmt::Debug for NodeHeaderCosine {
26+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27+
f.debug_struct("NodeHeaderCosine").field("norm", &format!("{:.4}", self.norm)).finish()
28+
}
29+
}
2330

2431
impl Distance for Cosine {
2532
type Header = NodeHeaderCosine;

src/distance/dot_product.rs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use bytemuck::{Pod, Zeroable};
24
use heed::{RwPrefix, RwTxn};
35
use rand::Rng;
@@ -19,12 +21,20 @@ pub enum DotProduct {}
1921

2022
/// The header of DotProduct leaf nodes.
2123
#[repr(C)]
22-
#[derive(Pod, Zeroable, Debug, Clone, Copy)]
24+
#[derive(Pod, Zeroable, Clone, Copy)]
2325
pub struct NodeHeaderDotProduct {
2426
extra_dim: f32,
2527
/// An extra constant term to determine the offset of the plane
2628
norm: f32,
2729
}
30+
impl fmt::Debug for NodeHeaderDotProduct {
31+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32+
f.debug_struct("NodeHeaderDotProduct")
33+
.field("extra_dim", &format!("{:.4}", self.extra_dim))
34+
.field("norm", &format!("{:.4}", self.norm))
35+
.finish()
36+
}
37+
}
2838

2939
impl Distance for DotProduct {
3040
type Header = NodeHeaderDotProduct;

src/distance/euclidean.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use bytemuck::{Pod, Zeroable};
24
use rand::Rng;
35

@@ -17,11 +19,16 @@ pub enum Euclidean {}
1719

1820
/// The header of Euclidean leaf nodes.
1921
#[repr(C)]
20-
#[derive(Pod, Zeroable, Debug, Clone, Copy)]
22+
#[derive(Pod, Zeroable, Clone, Copy)]
2123
pub struct NodeHeaderEuclidean {
2224
/// An extra constant term to determine the offset of the plane
2325
bias: f32,
2426
}
27+
impl fmt::Debug for NodeHeaderEuclidean {
28+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
29+
f.debug_struct("NodeHeaderEuclidean").field("bias", &format!("{:.4}", self.bias)).finish()
30+
}
31+
}
2532

2633
impl Distance for Euclidean {
2734
type Header = NodeHeaderEuclidean;

src/distance/manhattan.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
use std::fmt;
2+
13
use bytemuck::{Pod, Zeroable};
24
use rand::Rng;
35

@@ -16,11 +18,16 @@ pub enum Manhattan {}
1618

1719
/// The header of Manhattan leaf nodes.
1820
#[repr(C)]
19-
#[derive(Pod, Zeroable, Debug, Clone, Copy)]
21+
#[derive(Pod, Zeroable, Clone, Copy)]
2022
pub struct NodeHeaderManhattan {
2123
/// An extra constant term to determine the offset of the plane
2224
bias: f32,
2325
}
26+
impl fmt::Debug for NodeHeaderManhattan {
27+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28+
f.debug_struct("NodeHeaderManhattan").field("bias", &format!("{:.4}", self.bias)).finish()
29+
}
30+
}
2431

2532
impl Distance for Manhattan {
2633
type Header = NodeHeaderManhattan;

src/reader.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -347,7 +347,7 @@ impl<'t, D: Distance> Reader<'t, D> {
347347
let key = Key::new(self.index, item);
348348
match self.database_get(rtxn, &key)?.ok_or(Error::missing_key(key))? {
349349
GenericReadNode::Leaf(_) => {
350-
if opt.candidates.map_or(true, |c| c.contains(item.item)) {
350+
if opt.candidates.is_none_or(|c| c.contains(item.item)) {
351351
nns.push(item.unwrap_item());
352352
}
353353
}
@@ -364,7 +364,7 @@ impl<'t, D: Distance> Reader<'t, D> {
364364
right,
365365
}) => {
366366
let margin = match normal {
367-
Some(normal) => D::margin(&normal, &query_leaf),
367+
Some(normal) => D::margin(&normal, query_leaf),
368368
None => 0.0,
369369
};
370370
queue.push((OrderedFloat(D::pq_distance(dist, margin, Side::Left)), left));

0 commit comments

Comments
 (0)