forked from lance-format/lance
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_f16.py
More file actions
52 lines (40 loc) · 1.37 KB
/
test_f16.py
File metadata and controls
52 lines (40 loc) · 1.37 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors
from pathlib import Path
import lance
import numpy as np
import pyarrow as pa
import pytest
torch = pytest.importorskip("torch")
@pytest.mark.parametrize("accelerator", [None, "cuda"])
def test_f16_embeddings(tmp_path: Path, accelerator: str):
if not torch.cuda.is_available() and accelerator == "cuda":
pytest.skip("CUDA not available")
DIM = 16
TOTAL = 256
values = np.random.random(TOTAL * DIM).astype(np.float16)
fsl = pa.FixedSizeListArray.from_arrays(values, DIM)
data = pa.Table.from_arrays([fsl, np.arange(TOTAL)], names=["vec", "id"])
ds = lance.write_dataset(data, tmp_path)
assert ds.schema.field("vec").type.value_type == pa.float16()
ds = ds.create_index(
"vec",
"IVF_PQ",
replace=True,
num_partitions=2,
num_sub_vectors=2,
accelerator=accelerator,
)
# Can use float32 to search
query = np.random.random(DIM).astype(np.float32)
rst32 = ds.to_table(nearest={"column": "vec", "q": query})
# Can use float16 to search
rst16 = ds.to_table(nearest={"column": "vec", "q": query.astype(np.float16)})
rst_py = ds.to_table(
nearest={
"column": "vec",
"q": query.tolist(),
}
)
assert rst16 == rst32
assert rst16 == rst_py