Skip to content

Commit cfa6f50

Browse files
committed
perf: reduce memory usage
1 parent 112827e commit cfa6f50

File tree

8 files changed

+17
-16
lines changed

8 files changed

+17
-16
lines changed

server/features/translator/nllb.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def translate_generator(self, text: str, source_language: Language, target_langu
139139
results = self.translator.generate_tokens(
140140
(source_language, *self.tokeniser.encode(text).tokens),
141141
target_prefix,
142-
max_decoding_length=4096,
142+
max_decoding_length=1024,
143143
sampling_temperature=0,
144144
no_repeat_ngram_size=3,
145145
suppress_sequences=(target_prefix,),
@@ -169,7 +169,7 @@ def translate(self, text: str, source_language: Language, target_language: Langu
169169
translated_text (str)
170170
the translated text
171171
"""
172-
return self.tokeniser.decode(list(self.translate_generator(text, source_language, target_language)))
172+
return self.tokeniser.decode(tuple(self.translate_generator(text, source_language, target_language)))
173173

174174
def translate_stream(self, text: str, source_language: Language, target_language: Language) -> Iterator[str]:
175175
"""

server/schemas/health.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from server.config import Config
44

55

6-
class Health(Struct, kw_only=True, gc=False):
6+
class Health(Struct, kw_only=True, frozen=True, gc=False):
77
"""
88
Summary
99
-------

server/schemas/v1/language.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from server.typedefs import Confidence, Language
66

77

8-
class LanguageResult(Struct, kw_only=True):
8+
class LanguageResult(Struct, kw_only=True, frozen=True, gc=False):
99
"""
1010
Summary
1111
-------

server/schemas/v1/tokens.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from msgspec import Meta, Struct
44

55

6-
class Tokens(Struct, kw_only=True):
6+
class Tokens(Struct, kw_only=True, frozen=True, gc=False):
77
"""
88
Summary
99
-------

server/schemas/v1/translated.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
from msgspec import Meta, Struct
44

55

6-
class Translated(Struct, kw_only=True):
6+
class Translated(Struct, kw_only=True, frozen=True, gc=False):
77
"""
88
Summary
99
-------

server/schemas/v1/translation.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
from server.typedefs import Language
66

77

8-
class Translation(Struct, kw_only=True):
8+
class Translation(Struct, kw_only=True, frozen=True, gc=False):
99
"""
1010
Summary
1111
-------

server/typings/ctranslate2/__init__.pyi

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,8 @@ class Translator:
6363
@overload
6464
def translate_batch(
6565
self,
66-
source: Iterable[list[str]],
67-
target_prefix: Iterable[list[str] | None] | None = None,
66+
source: Iterable[Sequence[str]],
67+
target_prefix: Iterable[Sequence[str] | None] | None = None,
6868
*,
6969
max_batch_size: int = 0,
7070
batch_type: BatchTypes = "examples",
@@ -77,7 +77,7 @@ class Translator:
7777
repetition_penalty: float = 1,
7878
no_repeat_ngram_size: int = 0,
7979
disable_unks: bool = False,
80-
supress_sequences: list[list[str]] | None = None,
80+
suppress_sequences: Sequence[Sequence[str]] | None = None,
8181
end_token: str | list[str] | list[int] | None = None,
8282
return_end_token: bool = False,
8383
prefix_bias_beta: float = 0,
@@ -98,8 +98,8 @@ class Translator:
9898
@overload
9999
def translate_batch(
100100
self,
101-
source: Iterable[list[str]],
102-
target_prefix: Iterable[list[str] | None] | None = None,
101+
source: Iterable[Sequence[str]],
102+
target_prefix: Iterable[Sequence[str] | None] | None = None,
103103
*,
104104
max_batch_size: int = 0,
105105
batch_type: BatchTypes = "examples",
@@ -112,7 +112,7 @@ class Translator:
112112
repetition_penalty: float = 1,
113113
no_repeat_ngram_size: int = 0,
114114
disable_unks: bool = False,
115-
supress_sequences: list[list[str]] | None = None,
115+
suppress_sequences: Sequence[Sequence[str]] | None = None,
116116
end_token: str | list[str] | list[int] | None = None,
117117
return_end_token: bool = False,
118118
prefix_bias_beta: float = 0,
@@ -132,8 +132,8 @@ class Translator:
132132
) -> list[AsyncTranslationResult]: ...
133133
def translate_iterable(
134134
self,
135-
source: Iterable[list[str]],
136-
target_prefix: Iterable[list[str]] | None = None,
135+
source: Iterable[Sequence[str]],
136+
target_prefix: Iterable[Sequence[str]] | None = None,
137137
max_batch_size: int = 32,
138138
batch_type: BatchTypes = "examples",
139139
*,
@@ -145,7 +145,7 @@ class Translator:
145145
repetition_penalty: float = 1,
146146
no_repeat_ngram_size: int = 0,
147147
disable_unks: bool = False,
148-
supress_sequences: list[list[str]] | None = None,
148+
suppress_sequences: list[list[str]] | None = None,
149149
end_token: str | list[str] | list[int] | None = None,
150150
return_end_token: bool = False,
151151
prefix_bias_beta: float = 0,

server/typings/tokenizers/__init__.pyi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ class Encoding(Sized):
77
class Tokenizer:
88
@classmethod
99
def from_file(cls, path: str) -> Self: ...
10+
def token_to_id(self, token: str) -> int: ...
1011
def decode(self, ids: Sequence[int], skip_special_tokens: bool = True) -> str: ...
1112
def decode_batch(self, ids: Sequence[Sequence[int]], skip_special_tokens: bool = True) -> list[str]: ...
1213
def encode(

0 commit comments

Comments
 (0)