Skip to content

Commit e3763ca

Browse files
committed
feat: replace ProcessPoolExecutor with gevent threadpool for token counting
Signed-off-by: -LAN- <[email protected]>
1 parent 744c96d commit e3763ca

File tree

1 file changed

+6
-6
lines changed

1 file changed

+6
-6
lines changed

api/core/model_runtime/model_providers/__base/tokenizers/gpt2_tokenzier.py

+6-6
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
1-
from concurrent.futures import ProcessPoolExecutor
21
from os.path import abspath, dirname, join
32
from threading import Lock
4-
from typing import Any
3+
from typing import Any, cast
54

5+
import gevent.threadpool # type: ignore
66
from transformers import GPT2Tokenizer as TransformerGPT2Tokenizer # type: ignore
77

8-
_executor = ProcessPoolExecutor(max_workers=1)
9-
108
_tokenizer: Any = None
119
_lock = Lock()
10+
_pool = gevent.threadpool.ThreadPool(1)
1211

1312

1413
class GPT2Tokenizer:
@@ -23,8 +22,9 @@ def _get_num_tokens_by_gpt2(text: str) -> int:
2322

2423
@staticmethod
2524
def get_num_tokens(text: str) -> int:
26-
future = _executor.submit(GPT2Tokenizer._get_num_tokens_by_gpt2, text)
27-
return future.result()
25+
future = _pool.spawn(GPT2Tokenizer._get_num_tokens_by_gpt2, text)
26+
result = future.get(block=True)
27+
return cast(int, result)
2828

2929
@staticmethod
3030
def get_encoder() -> Any:

0 commit comments

Comments
 (0)