Skip to content

Commit b525e27

Browse files
authored
fix(pii): cap each batched NER pass at one window of characters (#10099)
A batch of many texts each under NER_WINDOW_CHARS still went through one nlp.pipe pass, and that pass costs memory for every character in it, like one long text. Pack the short texts into batches of at most NER_WINDOW_CHARS in total and take the NER slot per batch.
1 parent 6bfe67f commit b525e27

1 file changed

Lines changed: 29 additions & 17 deletions

File tree

‎apps/pii/server.py‎

Lines changed: 29 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -364,9 +364,10 @@ def _analyze_many(
364364
score_threshold: float | None,
365365
ad_hoc_recognizers: list[PatternRecognizer] | None = None,
366366
):
367-
"""Analyze many texts, skipping the spaCy pass for regex-only requests and
368-
analyzing any text longer than NER_WINDOW_CHARS on its own (see
369-
`_analyze_long`)."""
367+
"""Analyze many texts, skipping the spaCy pass for regex-only requests. A text
368+
longer than NER_WINDOW_CHARS is analyzed on its own (see `_analyze_long`); the
369+
rest share NER passes of at most NER_WINDOW_CHARS in total, since a batched pass
370+
costs memory for every character in it, like one long text."""
370371
if _regex_only(entities, score_threshold):
371372
blank = _BLANK_ARTIFACTS.get(language)
372373
return [
@@ -380,21 +381,32 @@ def _analyze_many(
380381
)
381382
for text in texts
382383
]
383-
long_indices = {i for i, text in enumerate(texts) if len(text) > NER_WINDOW_CHARS}
384-
short_indices = [i for i in range(len(texts)) if i not in long_indices]
384+
long_indices: list[int] = []
385+
batches: list[list[int]] = []
386+
batch_chars = 0
387+
for i, text in enumerate(texts):
388+
if len(text) > NER_WINDOW_CHARS:
389+
long_indices.append(i)
390+
continue
391+
if not batches or batch_chars + len(text) > NER_WINDOW_CHARS:
392+
batches.append([])
393+
batch_chars = 0
394+
batches[-1].append(i)
395+
batch_chars += len(text)
385396
results: list[list[RecognizerResult]] = [[] for _ in texts]
386-
with _NER_SLOT:
387-
short_results = batch_analyzer.analyze_iterator(
388-
texts=[texts[i] for i in short_indices],
389-
language=language,
390-
batch_size=NLP_BATCH_SIZE,
391-
entities=entities or None,
392-
score_threshold=score_threshold,
393-
ad_hoc_recognizers=ad_hoc_recognizers or None,
394-
)
395-
for i, per_text in zip(short_indices, short_results):
396-
results[i] = list(per_text)
397-
for i in sorted(long_indices):
397+
for batch in batches:
398+
with _NER_SLOT:
399+
batch_results = batch_analyzer.analyze_iterator(
400+
texts=[texts[i] for i in batch],
401+
language=language,
402+
batch_size=NLP_BATCH_SIZE,
403+
entities=entities or None,
404+
score_threshold=score_threshold,
405+
ad_hoc_recognizers=ad_hoc_recognizers or None,
406+
)
407+
for i, per_text in zip(batch, batch_results):
408+
results[i] = list(per_text)
409+
for i in long_indices:
398410
results[i] = _analyze_long(
399411
texts[i], language, entities, score_threshold, ad_hoc_recognizers=ad_hoc_recognizers
400412
)

0 commit comments

Comments
 (0)