codekingpro/portable-devtools
114k
1"""
2These functions match what the spec of hnswlib is.
3"""
4from typing import Union, cast
5import numpy as np
6from numpy.typing import NDArray
7
8Vector = NDArray[Union[np.int32, np.float32, np.int16, np.float16]]
9
10
11def l2(x: Vector, y: Vector) -> float:
12 return (np.linalg.norm(x - y) ** 2).item()
13
14
15def cosine(x: Vector, y: Vector) -> float:
16 # This epsilon is used to prevent division by zero, and the value is the same
17 # https://github.com/nmslib/hnswlib/blob/359b2ba87358224963986f709e593d799064ace6/python_bindings/bindings.cpp#L238
18
19 # We need to adapt the epsilon to the precision of the input
20 NORM_EPS = 1e-30
21 if x.dtype == np.float16 or y.dtype == np.float16:
22 NORM_EPS = 1e-7
23 return cast(
24 float,
25 (
26 1.0 - np.dot(x, y) / ((np.linalg.norm(x) * np.linalg.norm(y)) + NORM_EPS)
27 ).item(),
28 )
29
30
31def ip(x: Vector, y: Vector) -> float:
32 return cast(float, (1.0 - np.dot(x, y)).item())
33 