Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: option to use faiss-gpu for dknn #92

Merged
merged 1 commit into from
Apr 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 20 additions & 7 deletions oodeel/methods/dknn.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,21 +38,28 @@ class DKNN(OODBaseDetector):
Args:
nearest: number of nearest neighbors to consider.
Defaults to 1.
use_gpu (bool): Flag to enable GPU acceleration for FAISS. Defaults to False.
"""

def __init__(
self,
nearest: int = 50,
):
def __init__(self, nearest: int = 50, use_gpu: bool = False):
super().__init__()

self.index = None
self.nearest = nearest
self.use_gpu = use_gpu

if self.use_gpu:
try:
self.res = faiss.StandardGpuResources()
except AttributeError as e:
raise ImportError(
"faiss-gpu is not installed, but use_gpu was set to True."
+ "Please install faiss-gpu or set use_gpu to False."
) from e
paulnovello marked this conversation as resolved.
Show resolved Hide resolved

def _fit_to_dataset(self, fit_dataset: Union[TensorType, DatasetType]) -> None:
"""
Constructs the index from ID data "fit_dataset", which will be used for
nearest neighbor search.
nearest neighbor search. Can operate on CPU or GPU based on the `use_gpu` flag.

Args:
fit_dataset: input dataset (ID) to construct the index with.
Expand All @@ -61,7 +68,13 @@ def _fit_to_dataset(self, fit_dataset: Union[TensorType, DatasetType]) -> None:
fit_projected = self.op.convert_to_numpy(fit_projected[0])
fit_projected = fit_projected.reshape(fit_projected.shape[0], -1)
norm_fit_projected = self._l2_normalization(fit_projected)
self.index = faiss.IndexFlatL2(norm_fit_projected.shape[1])

if self.use_gpu:
cpu_index = faiss.IndexFlatL2(norm_fit_projected.shape[1])
self.index = faiss.index_cpu_to_gpu(self.res, 0, cpu_index)
else:
self.index = faiss.IndexFlatL2(norm_fit_projected.shape[1])

self.index.add(norm_fit_projected)

def _score_tensor(self, inputs: TensorType) -> Tuple[np.ndarray]:
Expand Down
Loading