Skip to content

Commit

Permalink
feat(grouping): add clear cache logic on OOM (#956)
Browse files Browse the repository at this point in the history
  • Loading branch information
trillville authored Jul 25, 2024
1 parent c60b4de commit a934d98
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/seer/grouping/grouping.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import difflib
import gc
import logging
from functools import wraps
from typing import List, Optional

import numpy as np
Expand All @@ -9,6 +11,7 @@
from pydantic import BaseModel, ValidationInfo, field_validator
from sentence_transformers import SentenceTransformer
from sqlalchemy.dialects.postgresql import insert
from torch.cuda import OutOfMemoryError

from seer.db import DbGroupingRecord, Session
from seer.stubs import DummySentenceTransformer, can_use_model_stubs
Expand Down Expand Up @@ -112,6 +115,20 @@ def _load_model(model_path: str) -> SentenceTransformer:
)


def handle_out_of_memory(func):
@wraps(func)
def wrapper(*args, **kwargs):
try:
return func(*args, **kwargs)
except OutOfMemoryError:
logger.warning("Ran out of memory, clearing cache and retrying once")
gc.collect()
torch.cuda.empty_cache()
return func(*args, **kwargs)

return wrapper


class GroupingLookup:
model: SentenceTransformer

Expand All @@ -129,6 +146,7 @@ def __init__(self, model_path: str, data_path: str):
self.encode_text("IndexError: list index out of range") # Ensure warm start

@sentry_sdk.tracing.trace
@handle_out_of_memory
def encode_text(self, stacktrace: str) -> np.ndarray:
"""
Encodes the stacktrace using the sentence transformer model.
Expand All @@ -139,6 +157,7 @@ def encode_text(self, stacktrace: str) -> np.ndarray:
return self.model.encode(stacktrace)

@sentry_sdk.tracing.trace
@handle_out_of_memory
def encode_multiple_texts(self, stacktraces: List[str], batch_size: int = 1) -> np.ndarray:
"""
Encodes multiple stacktraces in batches using the sentence transformer model.
Expand Down

0 comments on commit a934d98

Please sign in to comment.