Skip to content

Commit f807534

Browse files
committed
Switched to f16 for halfvec
1 parent 71506c3 commit f807534

5 files changed

Lines changed: 17 additions & 22 deletions

File tree

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ postgres-types = { version = "0.2", default-features = false, optional = true }
1818
diesel = { version = "2", default-features = false, features = ["postgres"], optional = true }
1919
sqlx = { version = ">= 0.8, < 0.10", default-features = false, features = ["postgres"], optional = true }
2020
serde = { version = "1", features = ["derive"], optional = true }
21-
half = { version = "2", default-features = false, optional = true }
2221

2322
[dev-dependencies]
2423
postgres = { version = "0.19", default-features = false }
@@ -30,7 +29,7 @@ serde_json = "1"
3029

3130
[features]
3231
postgres = ["dep:postgres-types", "dep:bytes"]
33-
halfvec = ["dep:half"]
32+
halfvec = []
3433

3534
[package.metadata.docs.rs]
3635
features = ["halfvec"]

src/halfvec.rs

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,3 @@
1-
use half::f16;
2-
31
#[cfg(feature = "diesel")]
42
use crate::diesel_ext::halfvec::HalfVectorType;
53

@@ -27,7 +25,7 @@ impl From<HalfVector> for Vec<f16> {
2725
impl HalfVector {
2826
/// Creates a half vector from a `f32` slice.
2927
pub fn from_f32_slice(slice: &[f32]) -> HalfVector {
30-
HalfVector(slice.iter().map(|v| f16::from_f32(*v)).collect())
28+
HalfVector(slice.iter().map(|v| *v as f16).collect())
3129
}
3230

3331
/// Returns a copy of the half vector as a `Vec<f16>`.
@@ -63,19 +61,18 @@ impl HalfVector {
6361
#[cfg(test)]
6462
mod tests {
6563
use crate::HalfVector;
66-
use half::f16;
6764

6865
#[test]
6966
fn test_into() {
7067
let vec = HalfVector::from(vec![
71-
f16::from_f32(1.0),
72-
f16::from_f32(2.0),
73-
f16::from_f32(3.0),
68+
1.0,
69+
2.0,
70+
3.0,
7471
]);
7572
let f16_vec: Vec<f16> = vec.into();
7673
assert_eq!(
7774
f16_vec,
78-
vec![f16::from_f32(1.0), f16::from_f32(2.0), f16::from_f32(3.0)]
75+
vec![1.0, 2.0, 3.0]
7976
);
8077
}
8178

@@ -84,7 +81,7 @@ mod tests {
8481
let vec = HalfVector::from_f32_slice(&[1.0, 2.0, 3.0]);
8582
assert_eq!(
8683
vec.to_vec(),
87-
vec![f16::from_f32(1.0), f16::from_f32(2.0), f16::from_f32(3.0)]
84+
vec![1.0, 2.0, 3.0]
8885
);
8986
}
9087

@@ -93,7 +90,7 @@ mod tests {
9390
let vec = HalfVector::from_f32_slice(&[1.0, 2.0, 3.0]);
9491
assert_eq!(
9592
vec.as_slice(),
96-
&[f16::from_f32(1.0), f16::from_f32(2.0), f16::from_f32(3.0)]
93+
&[1.0, 2.0, 3.0]
9794
);
9895
}
9996
}

src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#![doc = include_str!("../README.md")]
2+
#![cfg_attr(feature = "halfvec", feature(f16))]
23

34
mod bit;
45
mod sparsevec;

src/postgres_ext/halfvec.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,6 @@ impl ToSql for HalfVector {
3838
#[cfg(test)]
3939
mod tests {
4040
use crate::HalfVector;
41-
use half::f16;
4241
use postgres::binary_copy::BinaryCopyInWriter;
4342
use postgres::types::{Kind, Type};
4443
use postgres::{Client, NoTls};
@@ -74,7 +73,7 @@ mod tests {
7473
let res_vec: HalfVector = row.get(0);
7574
assert_eq!(vec, res_vec);
7675
assert_eq!(
77-
vec![f16::from_f32(1.0), f16::from_f32(2.0), f16::from_f32(3.0)],
76+
vec![1.0, 2.0, 3.0],
7877
res_vec.to_vec()
7978
);
8079

@@ -110,14 +109,14 @@ mod tests {
110109
.copy_in("COPY postgres_half_items (embedding) FROM STDIN WITH (FORMAT BINARY)")?;
111110
let mut writer = BinaryCopyInWriter::new(writer, &[halfvec_type]);
112111
writer.write(&[&HalfVector::from(vec![
113-
f16::from_f32(1.0),
114-
f16::from_f32(2.0),
115-
f16::from_f32(3.0),
112+
1.0,
113+
2.0,
114+
3.0,
116115
])])?;
117116
writer.write(&[&HalfVector::from(vec![
118-
f16::from_f32(4.0),
119-
f16::from_f32(5.0),
120-
f16::from_f32(6.0),
117+
4.0,
118+
5.0,
119+
6.0,
121120
])])?;
122121
writer.finish()?;
123122

src/sqlx_ext/halfvec.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,6 @@ impl PgHasArrayType for HalfVector {
4242
#[cfg(test)]
4343
mod tests {
4444
use crate::HalfVector;
45-
use half::f16;
4645
use sqlx::postgres::PgPoolOptions;
4746
use sqlx::Row;
4847

@@ -82,7 +81,7 @@ mod tests {
8281
let res_vec: HalfVector = row.try_get("embedding").unwrap();
8382
assert_eq!(vec, res_vec);
8483
assert_eq!(
85-
vec![f16::from_f32(1.0), f16::from_f32(2.0), f16::from_f32(3.0)],
84+
vec![1.0, 2.0, 3.0],
8685
res_vec.to_vec()
8786
);
8887

0 commit comments

Comments
 (0)