Skip to content

Commit f347fad

Browse files
jameswexLIT team
authored andcommitted
Add thread lock to glue model tokenizer call and update demos version.
Fixes race condition when using multiple interpreters in separate threads of LIT server with same model. PiperOrigin-RevId: 417452193
1 parent 833f11d commit f347fad

File tree

1 file changed

+5
-2
lines changed

1 file changed

+5
-2
lines changed

lit_nlp/examples/models/glue_models.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
import os
55
import re
6+
import threading
67
from typing import Optional, Dict, List, Iterable
78

89
import attr
@@ -58,6 +59,7 @@ def __init__(self,
5859
**config_kw):
5960
self.config = GlueModelConfig(**config_kw)
6061
self._load_model(model_name_or_path)
62+
self._lock = threading.Lock()
6163

6264
def _load_model(self, model_name_or_path):
6365
"""Load model. Can be overridden for testing."""
@@ -76,8 +78,9 @@ def _load_model(self, model_name_or_path):
7678
config=model_config)
7779

7880
def _get_tokens(self, ex: JsonDict, field_name: str) -> List[str]:
79-
return (ex.get("tokens_" + field_name) or
80-
self.tokenizer.tokenize(ex[field_name]))
81+
with self._lock:
82+
return (ex.get("tokens_" + field_name) or
83+
self.tokenizer.tokenize(ex[field_name]))
8184

8285
def _preprocess(self, inputs: Iterable[JsonDict]) -> Dict[str, tf.Tensor]:
8386
# Use pretokenized input if available.

0 commit comments

Comments
 (0)