MCPcopy Create free account
hub / github.com/OpenNMT/CTranslate2 / test_batch_translation

Function test_batch_translation

python/tests/test_translator.py:101–129  ·  view source on GitHub ↗
(max_batch_size)

Source from the content-addressed store, hash-verified

99
100@pytest.mark.parametrize("max_batch_size", [0, 1])
101def test_batch_translation(max_batch_size):
102 translator = _get_transliterator()
103 output = translator.translate_batch(
104 [["آ", "ت", "ز", "م", "و", "ن"], ["آ", "ت", "ش", "ي", "س", "و", "ن"]],
105 max_batch_size=max_batch_size,
106 return_scores=True,
107 )
108 assert len(output) == 2
109 assert output[0].hypotheses == [["a", "t", "z", "m", "o", "n"]]
110 assert output[1].hypotheses == [["a", "c", "h", "i", "s", "o", "n"]]
111 assert output[0].scores[0] < 0
112 assert not output[0].attention
113
114 expected_repr = (
115 "TranslationResult(hypotheses=%s, scores=%s, attention=[], logits=[])"
116 % (
117 output[0].hypotheses,
118 output[0].scores,
119 )
120 )
121 assert repr(output[0]) == expected_repr
122
123 # Check backward compatibility with previous result format.
124 assert len(output[0]) == 1 # One hypothesis.
125 assert len(output[1]) == 1
126 assert output[0][0]["tokens"] == ["a", "t", "z", "m", "o", "n"]
127 assert output[0][0]["score"] < 0
128 assert "attention" not in output[0][0]
129 assert output[1][0]["tokens"] == ["a", "c", "h", "i", "s", "o", "n"]
130
131
132def test_batch_translation_async():

Callers

nothing calls this directly

Calls 2

_get_transliteratorFunction · 0.85
translate_batchMethod · 0.45

Tested by

no test coverage detected