Blame view

tests/test_translation_local_backends.py 15.8 KB
14e67b71   tangwang   分句后的 batching 现在是...
1
2
3
  import logging
  
  import pytest
0fd2f875   tangwang   translate
4
5
6
  import torch
  
  from translation.backends.local_seq2seq import MarianMTTranslationBackend, NLLBTranslationBackend
14e67b71   tangwang   分句后的 batching 现在是...
7
  from translation.backends.local_ctranslate2 import NLLBCTranslate2TranslationBackend
39306492   tangwang   fix(translation):...
8
  from translation.languages import build_nllb_language_catalog, resolve_nllb_language_code
0fd2f875   tangwang   translate
9
  from translation.service import TranslationService
294c3d0a   tangwang   实现第一版“按模型预算智能分句”的...
10
  from translation.text_splitter import compute_safe_input_token_limit, split_text_for_translation
0fd2f875   tangwang   translate
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
  
  
  class _FakeBatch(dict):
      def to(self, device):
          self["device"] = device
          return self
  
  
  class _FakeTokenizer:
      def __init__(self):
          self.src_lang = None
          self.pad_token = "</s>"
          self.eos_token = "</s>"
          self.lang_code_to_id = {"eng_Latn": 101, "zho_Hans": 202}
          self.last_call = None
  
      def __call__(self, texts, **kwargs):
          self.last_call = {"texts": list(texts), **kwargs}
          return _FakeBatch({"input_ids": torch.tensor([[1, 2, 3]])})
  
      def batch_decode(self, generated, skip_special_tokens=True):
          del generated, skip_special_tokens
          return ["translated" for _ in range(len(self.last_call["texts"]))]
  
      def convert_tokens_to_ids(self, token):
          return self.lang_code_to_id[token]
  
  
  class _FakeModel:
      def to(self, device):
          self.device = device
          return self
  
      def eval(self):
          return self
  
      def generate(self, **kwargs):
          self.last_generate_kwargs = kwargs
          return [[42]]
  
  
14e67b71   tangwang   分句后的 batching 现在是...
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
  class _FakeCT2Tokenizer:
      def __init__(self, src_lang=None):
          self.src_lang = src_lang
          self.pad_token = "</s>"
          self.eos_token = "</s>"
          self.last_call = None
  
      def __call__(self, texts, **kwargs):
          self.last_call = {"texts": list(texts), **kwargs}
          return {"input_ids": [[1, 2, 3] for _ in texts]}
  
      def convert_ids_to_tokens(self, ids):
          del ids
          return ["tok_a", "tok_b", "tok_c"]
  
      def convert_tokens_to_ids(self, tokens):
          if isinstance(tokens, list):
              return [1 for _ in tokens]
          return 1
  
      def decode(self, token_ids, skip_special_tokens=True):
          del token_ids, skip_special_tokens
          return "translated"
  
  
  class _FakeCT2Result:
      def __init__(self, tokens):
          self.hypotheses = [tokens]
  
  
  class _FakeCT2Translator:
      def __init__(self):
          self.last_translate_batch_kwargs = None
  
      def translate_batch(self, source_tokens, **kwargs):
          self.last_translate_batch_kwargs = {"source_tokens": source_tokens, **kwargs}
          target_prefix = kwargs.get("target_prefix") or []
          return [
              _FakeCT2Result((target_prefix[idx] or []) + ["translated_token"])
              for idx, _ in enumerate(source_tokens)
          ]
  
  
0fd2f875   tangwang   translate
95
96
97
98
99
  def _stub_load_model(self):
      self.tokenizer = _FakeTokenizer()
      self.seq2seq_model = _FakeModel()
  
  
14e67b71   tangwang   分句后的 batching 现在是...
100
101
102
103
104
  def _stub_load_ct2_runtime(self):
      self.tokenizer = _FakeCT2Tokenizer()
      self.translator = _FakeCT2Translator()
  
  
0fd2f875   tangwang   translate
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
  def test_marian_language_validation(monkeypatch):
      monkeypatch.setattr(MarianMTTranslationBackend, "_load_model", _stub_load_model)
      backend = MarianMTTranslationBackend(
          name="opus-mt-zh-en",
          model_id="Helsinki-NLP/opus-mt-zh-en",
          model_dir="./models/translation/Helsinki-NLP/opus-mt-zh-en",
          device="cpu",
          torch_dtype="float32",
          batch_size=1,
          max_input_length=16,
          max_new_tokens=16,
          num_beams=1,
          source_langs=["zh"],
          target_langs=["en"],
      )
  
      result = backend.translate("测试", source_lang="zh", target_lang="en")
      assert result == "translated"
  
14e67b71   tangwang   分句后的 batching 现在是...
124
      with pytest.raises(ValueError, match="source languages"):
0fd2f875   tangwang   translate
125
          backend.translate("test", source_lang="en", target_lang="zh")
0fd2f875   tangwang   translate
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
  
  
  def test_nllb_uses_src_lang_and_forced_bos(monkeypatch):
      monkeypatch.setattr(NLLBTranslationBackend, "_load_model", _stub_load_model)
      backend = NLLBTranslationBackend(
          name="nllb-200-distilled-600m",
          model_id="facebook/nllb-200-distilled-600M",
          model_dir="./models/translation/facebook/nllb-200-distilled-600M",
          device="cpu",
          torch_dtype="float32",
          batch_size=1,
          max_input_length=16,
          max_new_tokens=16,
          num_beams=1,
      )
  
      result = backend.translate("test", source_lang="en", target_lang="zh")
  
      assert result == "translated"
      assert backend.tokenizer.src_lang == "eng_Latn"
      assert backend.seq2seq_model.last_generate_kwargs["forced_bos_token_id"] == 202
  
  
14e67b71   tangwang   分句后的 batching 现在是...
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
  def test_nllb_accepts_finnish_short_code(monkeypatch):
      monkeypatch.setattr(NLLBTranslationBackend, "_load_model", _stub_load_model)
      backend = NLLBTranslationBackend(
          name="nllb-200-distilled-600m",
          model_id="facebook/nllb-200-distilled-600M",
          model_dir="./models/translation/facebook/nllb-200-distilled-600M",
          device="cpu",
          torch_dtype="float32",
          batch_size=1,
          max_input_length=16,
          max_new_tokens=16,
          num_beams=1,
      )
  
      result = backend.translate("test", source_lang="fi", target_lang="zh")
  
      assert result == "translated"
      assert backend.tokenizer.src_lang == "fin_Latn"
      assert backend.seq2seq_model.last_generate_kwargs["forced_bos_token_id"] == 202
  
  
  def test_nllb_ctranslate2_accepts_finnish_short_code(monkeypatch):
      created_tokenizers = []
  
      def _fake_from_pretrained(source, src_lang=None, **kwargs):
          del source, kwargs
          tokenizer = _FakeCT2Tokenizer(src_lang=src_lang)
          created_tokenizers.append(tokenizer)
          return tokenizer
  
      monkeypatch.setattr(NLLBCTranslate2TranslationBackend, "_load_runtime", _stub_load_ct2_runtime)
      monkeypatch.setattr(
          "translation.backends.local_ctranslate2.AutoTokenizer.from_pretrained",
          _fake_from_pretrained,
      )
      backend = NLLBCTranslate2TranslationBackend(
          name="nllb-200-distilled-600m",
          model_id="facebook/nllb-200-distilled-600M",
          model_dir="./models/translation/facebook/nllb-200-distilled-600M",
          device="cpu",
          torch_dtype="float32",
          batch_size=1,
          max_input_length=16,
          max_new_tokens=16,
          num_beams=1,
      )
  
      result = backend.translate("test", source_lang="fi", target_lang="zh")
  
      assert result == "translated"
      assert len(created_tokenizers) == 1
      assert created_tokenizers[0].src_lang == "fin_Latn"
      assert backend.translator.last_translate_batch_kwargs["target_prefix"] == [["zho_Hans"]]
  
  
f07947a5   tangwang   Improve portabili...
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
  def test_nllb_ctranslate2_falls_back_to_model_id_when_local_dir_is_wrong_type(tmp_path, monkeypatch):
      wrong_dir = tmp_path / "wrong-nllb"
      wrong_dir.mkdir()
      (wrong_dir / "config.json").write_text('{"model_type":"led"}', encoding="utf-8")
  
      monkeypatch.setattr(NLLBCTranslate2TranslationBackend, "_load_runtime", _stub_load_ct2_runtime)
  
      backend = NLLBCTranslate2TranslationBackend(
          name="nllb-200-distilled-600m",
          model_id="facebook/nllb-200-distilled-600M",
          model_dir=str(wrong_dir),
          device="cpu",
          torch_dtype="float32",
          batch_size=1,
          max_input_length=16,
          max_new_tokens=16,
          num_beams=1,
      )
  
      assert backend._model_source() == "facebook/nllb-200-distilled-600M"
      assert backend._tokenizer_source() == "facebook/nllb-200-distilled-600M"
  
  
  def test_nllb_ctranslate2_falls_back_to_model_id_when_local_dir_is_incomplete(tmp_path, monkeypatch):
      incomplete_dir = tmp_path / "incomplete-nllb"
      incomplete_dir.mkdir()
      (incomplete_dir / "ctranslate2-float16").mkdir()
  
      monkeypatch.setattr(NLLBCTranslate2TranslationBackend, "_load_runtime", _stub_load_ct2_runtime)
  
      backend = NLLBCTranslate2TranslationBackend(
          name="nllb-200-distilled-600m",
          model_id="facebook/nllb-200-distilled-600M",
          model_dir=str(incomplete_dir),
          device="cpu",
          torch_dtype="float32",
          batch_size=1,
          max_input_length=16,
          max_new_tokens=16,
          num_beams=1,
      )
  
      assert backend._model_source() == "facebook/nllb-200-distilled-600M"
  
  
39306492   tangwang   fix(translation):...
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
  def test_nllb_resolves_flores_short_tags_and_iso_no():
      cat = build_nllb_language_catalog(None)
      assert resolve_nllb_language_code("ca", cat) == "cat_Latn"
      assert resolve_nllb_language_code("da", cat) == "dan_Latn"
      assert resolve_nllb_language_code("eu", cat) == "eus_Latn"
      assert resolve_nllb_language_code("gl", cat) == "glg_Latn"
      assert resolve_nllb_language_code("hu", cat) == "hun_Latn"
      assert resolve_nllb_language_code("id", cat) == "ind_Latn"
      assert resolve_nllb_language_code("nl", cat) == "nld_Latn"
      assert resolve_nllb_language_code("no", cat) == "nob_Latn"
      assert resolve_nllb_language_code("ro", cat) == "ron_Latn"
      assert resolve_nllb_language_code("SV", cat) == "swe_Latn"
      assert resolve_nllb_language_code("tr", cat) == "tur_Latn"
      assert resolve_nllb_language_code("deu_Latn", cat) == "deu_Latn"
  
  
cd4ce66d   tangwang   trans logs
265
  def test_translation_service_preloads_enabled_backends(monkeypatch):
0fd2f875   tangwang   translate
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
      created = []
  
      def _fake_create_backend(self, *, name, backend_type, cfg):
          del self, cfg
          created.append((name, backend_type))
  
          class _Backend:
              model = name
  
              @property
              def supports_batch(self):
                  return True
  
              def translate(self, text, target_lang, source_lang=None, scene=None):
                  del target_lang, source_lang, scene
                  return text
  
          return _Backend()
  
      monkeypatch.setattr(TranslationService, "_create_backend", _fake_create_backend)
      config = {
          "service_url": "http://127.0.0.1:6006",
          "timeout_sec": 10.0,
          "default_model": "opus-mt-en-zh",
          "default_scene": "general",
          "capabilities": {
              "opus-mt-en-zh": {
                  "enabled": True,
                  "backend": "local_marian",
cd4ce66d   tangwang   trans logs
295
                  "use_cache": True,
0fd2f875   tangwang   translate
296
297
298
299
300
301
302
303
304
305
306
307
                  "model_id": "dummy",
                  "model_dir": "dummy",
                  "device": "cpu",
                  "torch_dtype": "float32",
                  "batch_size": 1,
                  "max_input_length": 8,
                  "max_new_tokens": 8,
                  "num_beams": 1,
              },
              "nllb-200-distilled-600m": {
                  "enabled": True,
                  "backend": "local_nllb",
cd4ce66d   tangwang   trans logs
308
                  "use_cache": True,
0fd2f875   tangwang   translate
309
310
311
312
313
314
315
316
317
318
319
                  "model_id": "dummy",
                  "model_dir": "dummy",
                  "device": "cpu",
                  "torch_dtype": "float32",
                  "batch_size": 1,
                  "max_input_length": 8,
                  "max_new_tokens": 8,
                  "num_beams": 1,
              },
          },
          "cache": {
0fd2f875   tangwang   translate
320
321
              "ttl_seconds": 60,
              "sliding_expiration": True,
0fd2f875   tangwang   translate
322
323
324
325
326
327
          },
      }
  
      service = TranslationService(config)
  
      assert service.available_models == ["opus-mt-en-zh", "nllb-200-distilled-600m"]
cd4ce66d   tangwang   trans logs
328
329
330
331
332
      assert service.loaded_models == ["opus-mt-en-zh", "nllb-200-distilled-600m"]
      assert created == [
          ("opus-mt-en-zh", "local_marian"),
          ("nllb-200-distilled-600m", "local_nllb"),
      ]
0fd2f875   tangwang   translate
333
334
  
      backend = service.get_backend("opus-mt-en-zh")
0fd2f875   tangwang   translate
335
      assert backend.model == "opus-mt-en-zh"
294c3d0a   tangwang   实现第一版“按模型预算智能分句”的...
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
  
  
  def test_compute_safe_input_token_limit_uses_decode_constraints():
      nllb_limit = compute_safe_input_token_limit(
          max_input_length=256,
          max_new_tokens=64,
          decoding_length_mode="source",
          decoding_length_extra=8,
      )
      opus_limit = compute_safe_input_token_limit(
          max_input_length=256,
          max_new_tokens=256,
      )
  
      assert nllb_limit == 56
      assert opus_limit == 248
  
  
  def test_split_text_for_translation_prefers_sentence_boundaries():
      text = (
          "这是一条很长的中文商品描述,包含材质、尺码和适用场景。"
          "适合春夏通勤,也适合日常出街穿搭;"
          "如果长度超了,应该优先按完整语义分句,而不是切成很碎的小片段。"
      )
  
      segments = split_text_for_translation(
          text,
          max_tokens=36,
          token_length_fn=len,
      )
  
      assert len(segments) >= 2
      assert "".join(segments) == text
      assert all(len(segment) <= 36 for segment in segments)
      assert segments[0].endswith(("。", ";"))
  
  
  class _SegmentingMarianBackend(MarianMTTranslationBackend):
      def _load_model(self):
          self.translated_batches = []
  
      def _token_count(self, text, target_lang, source_lang=None):
          del target_lang, source_lang
          return len(text)
  
      def _translate_batch(self, texts, target_lang, source_lang=None):
          del source_lang
          self.translated_batches.append(list(texts))
          if target_lang == "zh":
              return [f"<{text.strip()}>" for text in texts]
          return [f"[{text.strip()}]" for text in texts]
  
  
  def test_local_backend_splits_oversized_text_before_translation():
      backend = _SegmentingMarianBackend(
          name="opus-mt-en-zh",
          model_id="Helsinki-NLP/opus-mt-en-zh",
          model_dir="./models/translation/Helsinki-NLP/opus-mt-en-zh",
          device="cpu",
          torch_dtype="float32",
          batch_size=8,
          max_input_length=24,
          max_new_tokens=24,
          num_beams=1,
          source_langs=["en"],
          target_langs=["zh"],
      )
  
      text = (
          "This soft cotton dress is breathable and lightweight, "
          "works well for spring travel and everyday wear, "
          "and should be split on natural clause boundaries when it gets too long."
      )
  
      result = backend.translate(text, source_lang="en", target_lang="zh")
  
      assert result is not None
14e67b71   tangwang   分句后的 batching 现在是...
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
      all_segments = [piece for batch in backend.translated_batches for piece in batch]
      assert len(all_segments) >= 2
      assert all(len(batch) <= backend.batch_size for batch in backend.translated_batches)
      assert all(len(piece) <= 16 for piece in all_segments)
      assert result == "".join(f"<{piece.strip()}>" for piece in all_segments)
  
  
  def test_local_backend_batches_after_segmentation():
      backend = _SegmentingMarianBackend(
          name="opus-mt-en-zh",
          model_id="Helsinki-NLP/opus-mt-en-zh",
          model_dir="./models/translation/Helsinki-NLP/opus-mt-en-zh",
          device="cpu",
          torch_dtype="float32",
          batch_size=4,
          max_input_length=24,
          max_new_tokens=24,
          num_beams=1,
          source_langs=["en"],
          target_langs=["zh"],
      )
  
      texts = [
          "alpha beta gamma delta, epsilon zeta eta theta, iota kappa lambda mu.",
          "nu xi omicron pi, rho sigma tau upsilon, phi chi psi omega.",
          "dress shirt coat pants, socks shoes belt scarf, hat gloves bag watch.",
      ]
  
      result = backend.translate(texts, source_lang="en", target_lang="zh")
  
      assert isinstance(result, list)
      assert len(result) == 3
      assert len(backend.translated_batches) >= 2
      assert all(len(batch) <= backend.batch_size for batch in backend.translated_batches)
      assert sum(len(batch) for batch in backend.translated_batches) > backend.batch_size
      assert all(item is not None for item in result)
  
  
  def test_local_backend_logs_segmentation_and_inference_batches(caplog):
      backend = _SegmentingMarianBackend(
          name="opus-mt-en-zh",
          model_id="Helsinki-NLP/opus-mt-en-zh",
          model_dir="./models/translation/Helsinki-NLP/opus-mt-en-zh",
          device="cpu",
          torch_dtype="float32",
          batch_size=2,
          max_input_length=24,
          max_new_tokens=24,
          num_beams=1,
          source_langs=["en"],
          target_langs=["zh"],
      )
  
      texts = [
          "one two three four, five six seven eight, nine ten eleven twelve.",
          "thirteen fourteen fifteen sixteen, seventeen eighteen nineteen twenty.",
      ]
  
      with caplog.at_level(logging.INFO):
          backend.translate(texts, source_lang="en", target_lang="zh")
  
      messages = [record.getMessage() for record in caplog.records]
  
      assert any(message.startswith("Translation segmentation summary |") for message in messages)
      inference_logs = [
          message for message in messages if message.startswith("Translation inference batch |")
      ]
      assert len(inference_logs) >= 2