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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
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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
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
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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
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
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
481
482
483
484
485
486
487
import ctranslate2
import functools

try:
    from transformers import AutoTokenizer
    autotokenizer_ok = True
except ImportError:
    AutoTokenizer = object
    autotokenizer_ok = False

try:
    from typing import Literal
except ImportError:
    from typing_extensions import Literal

from typing import Any, Union, List
import os

from hf_hub_ctranslate2.util import utils as _utils


class CTranslate2ModelfromHuggingfaceHub:
    """CTranslate2 compatibility class for Translator and Generator"""

    def __init__(
        self,
        model_name_or_path: str,
        device: Literal["cpu", "cuda"] = "cuda",
        device_index=0,
        compute_type: Literal["int8_float16", "int8"] = "int8_float16",
        tokenizer: Union[AutoTokenizer, None] = None,
        hub_kwargs: dict = {},
        **kwargs: Any,
    ):
        # adaptions from https://github.com/guillaumekln/faster-whisper
        if os.path.isdir(model_name_or_path):
            model_path = model_name_or_path
        else:
            try:
                model_path = _utils._download_model(
                    model_name_or_path, hub_kwargs=hub_kwargs, local_files_only=True,
                )
            except Exception:
                hub_kwargs["local_files_only"] = True
                model_path = _utils._download_model(
                    model_name_or_path, hub_kwargs=hub_kwargs, local_files_only=True,
                )
        self.model = self.ctranslate_class(
            model_path,
            device=device,
            device_index=device_index,
            compute_type=compute_type,
            **kwargs,
        )

        if tokenizer is not None:
            self.tokenizer = tokenizer
        else:
            if "tokenizer.json" in os.listdir(model_path):
                if not autotokenizer_ok:
                    raise ValueError(
                        "`pip install transformers` missing to load AutoTokenizer."
                    )
                self.tokenizer = AutoTokenizer.from_pretrained(model_path, fast=True)
            else:
                raise ValueError(
                    "no suitable Tokenizer found. "
                    "Please set one via tokenizer=AutoTokenizer.from_pretrained(..) arg."
                )

    def _forward(self, *args: Any, **kwds: Any) -> Any:
        raise NotImplementedError

    def tokenize_encode(self, text, *args, **kwargs):
        return [
            self.tokenizer.convert_ids_to_tokens(self.tokenizer.encode(p)) for p in text
        ]

    def tokenize_decode(self, tokens_out, *args, **kwargs):
        raise NotImplementedError

    def generate(
        self,
        text: Union[str, List[str]],
        encode_kwargs={},
        decode_kwargs={},
        *forward_args,
        **forward_kwds: Any,
    ):
        orig_type = list
        if isinstance(text, str):
            orig_type = str
            text = [text]
        token_list = self.tokenize_encode(text, **encode_kwargs)
        tokens_out = self._forward(token_list, *forward_args, **forward_kwds)
        texts_out = self.tokenize_decode(tokens_out, **decode_kwargs)
        if orig_type == str:
            return texts_out[0]
        else:
            return texts_out


class TranslatorCT2fromHfHub(CTranslate2ModelfromHuggingfaceHub):
    def __init__(
        self,
        model_name_or_path: str,
        device: Literal["cpu", "cuda"] = "cuda",
        device_index=0,
        compute_type: Literal["int8_float16", "int8"] = "int8_float16",
        tokenizer: Union[AutoTokenizer, None] = None,
        hub_kwargs={},
        **kwargs: Any,
    ):
        """for ctranslate2.Translator models, in particular m2m-100
        Args:
            model_name_or_path (str): _description_
            device (Literal[cpu, cuda], optional): _description_. Defaults to "cuda".
            device_index (int, optional): _description_. Defaults to 0.
            compute_type (Literal[int8_float16, int8], optional): _description_. Defaults to "int8_float16".
            tokenizer (Union[AutoTokenizer, None], optional): _description_. Defaults to None.
            hub_kwargs (dict, optional): _description_. Defaults to {}.
            **kwargs (Any, optional): Any additional arguments
        """
        self.ctranslate_class = ctranslate2.Translator
        super().__init__(
            model_name_or_path,
            device,
            device_index,
            compute_type,
            tokenizer,
            hub_kwargs,
            **kwargs,
        )

    def _forward(self, *args, **kwds):
        return self.model.translate_batch(*args, **kwds)

    def tokenize_decode(self, tokens_out, *args, **kwargs):
        return [
            self.tokenizer.decode(
                self.tokenizer.convert_tokens_to_ids(tokens_out[i].hypotheses[0]),
                *args,
                **kwargs,
            )
            for i in range(len(tokens_out))
        ]

    def generate(
        self,
        text: Union[str, List[str]],
        encode_tok_kwargs={},
        decode_tok_kwargs={},
        *forward_args,
        **forward_kwds: Any,
    ):
        """_summary_
        Args:
            text (Union[str, List[str]]): Input texts
            encode_tok_kwargs (dict, optional): additional kwargs for tokenizer
            decode_tok_kwargs (dict, optional): additional kwargs for tokenizer
            max_batch_size (int, optional): Batch size. Defaults to 0.
            batch_type (str, optional): _. Defaults to "examples".
            asynchronous (bool, optional): Only False supported. Defaults to False.
            beam_size (int, optional): _. Defaults to 2.
            patience (float, optional): _. Defaults to 1.
            num_hypotheses (int, optional): _. Defaults to 1.
            length_penalty (float, optional): _. Defaults to 1.
            coverage_penalty (float, optional): _. Defaults to 0.
            repetition_penalty (float, optional): _. Defaults to 1.
            no_repeat_ngram_size (int, optional): _. Defaults to 0.
            disable_unk (bool, optional): _. Defaults to False.
            suppress_sequences (Optional[List[List[str]]], optional): _.
               Defaults to None.
            end_token (Optional[Union[str, List[str], List[int]]], optional): _.
               Defaults to None.
            return_end_token (bool, optional): _. Defaults to False.
            prefix_bias_beta (float, optional): _. Defaults to 0.
            max_input_length (int, optional): _. Defaults to 1024.
            max_decoding_length (int, optional): _. Defaults to 256.
            min_decoding_length (int, optional): _. Defaults to 1.
            use_vmap (bool, optional): _. Defaults to False.
            return_scores (bool, optional): _. Defaults to False.
            return_attention (bool, optional): _. Defaults to False.
            return_alternatives (bool, optional): _. Defaults to False.
            min_alternative_expansion_prob (float, optional): _. Defaults to 0.
            sampling_topk (int, optional): _. Defaults to 1.
            sampling_temperature (float, optional): _. Defaults to 1.
            replace_unknowns (bool, optional): _. Defaults to False.
            callback (_type_, optional): _. Defaults to None.
        Returns:
            Union[str, List[str]]: text as output, if list, same len as input
        """
        return super().generate(
            text,
            encode_kwargs=encode_tok_kwargs,
            decode_kwargs=decode_tok_kwargs,
            *forward_args,
            **forward_kwds,
        )


class MultiLingualTranslatorCT2fromHfHub(CTranslate2ModelfromHuggingfaceHub):
    def __init__(
        self,
        model_name_or_path: str,
        device: Literal["cpu", "cuda"] = "cuda",
        device_index=0,
        compute_type: Literal["int8_float16", "int8"] = "int8_float16",
        tokenizer: Union[AutoTokenizer, None] = None,
        hub_kwargs={},
        **kwargs: Any,
    ):
        """for ctranslate2.Translator models
        Args:
            model_name_or_path (str): _description_
            device (Literal[cpu, cuda], optional): _description_. Defaults to "cuda".
            device_index (int, optional): _description_. Defaults to 0.
            compute_type (Literal[int8_float16, int8], optional): _description_. Defaults to "int8_float16".
            tokenizer (Union[AutoTokenizer, None], optional): _description_. Defaults to None.
            hub_kwargs (dict, optional): _description_. Defaults to {}.
            **kwargs (Any, optional): Any additional arguments
        """
        self.ctranslate_class = ctranslate2.Translator
        super().__init__(
            model_name_or_path,
            device,
            device_index,
            compute_type,
            tokenizer,
            hub_kwargs,
            **kwargs,
        )

    def _forward(self, *args, **kwds):
        target_prefix = [
            [self.tokenizer.lang_code_to_token[lng]] for lng in kwds.pop("tgt_lang")
        ]
        # target_prefix=[['__de__'], ['__fr__']]
        return self.model.translate_batch(*args, **kwds, target_prefix=target_prefix)

    def tokenize_encode(self, text, *args, **kwargs):
        tokens = []
        src_lang = kwargs.pop("src_lang")
        for t, src_language in zip(text, src_lang):
            self.tokenizer.src_lang = src_language
            tokens.append(
                self.tokenizer.convert_ids_to_tokens(self.tokenizer.encode(t))
            )
        return tokens

    def tokenize_decode(self, tokens_out, *args, **kwargs):
        return [
            self.tokenizer.decode(
                self.tokenizer.convert_tokens_to_ids(tokens_out[i].hypotheses[0][1:]),
                *args,
                **kwargs,
            )
            for i in range(len(tokens_out))
        ]

    def generate(
        self,
        text: Union[str, List[str]],
        src_lang: Union[str, List[str]],
        tgt_lang: Union[str, List[str]],
        *forward_args,
        **forward_kwds: Any,
    ):
        """_summary_
        Args:
            text (Union[str, List[str]]): Input texts
            src_lang (Union[str, List[str]]): soruce language of the Input texts
            tgt_lang (Union[str, List[str]]): target language for outputs
            max_batch_size (int, optional): Batch size. Defaults to 0.
            batch_type (str, optional): _. Defaults to "examples".
            asynchronous (bool, optional): Only False supported. Defaults to False.
            beam_size (int, optional): _. Defaults to 2.
            patience (float, optional): _. Defaults to 1.
            num_hypotheses (int, optional): _. Defaults to 1.
            length_penalty (float, optional): _. Defaults to 1.
            coverage_penalty (float, optional): _. Defaults to 0.
            repetition_penalty (float, optional): _. Defaults to 1.
            no_repeat_ngram_size (int, optional): _. Defaults to 0.
            disable_unk (bool, optional): _. Defaults to False.
            suppress_sequences (Optional[List[List[str]]], optional): _.
               Defaults to None.
            end_token (Optional[Union[str, List[str], List[int]]], optional): _.
               Defaults to None.
            return_end_token (bool, optional): _. Defaults to False.
            prefix_bias_beta (float, optional): _. Defaults to 0.
            max_input_length (int, optional): _. Defaults to 1024.
            max_decoding_length (int, optional): _. Defaults to 256.
            min_decoding_length (int, optional): _. Defaults to 1.
            use_vmap (bool, optional): _. Defaults to False.
            return_scores (bool, optional): _. Defaults to False.
            return_attention (bool, optional): _. Defaults to False.
            return_alternatives (bool, optional): _. Defaults to False.
            min_alternative_expansion_prob (float, optional): _. Defaults to 0.
            sampling_topk (int, optional): _. Defaults to 1.
            sampling_temperature (float, optional): _. Defaults to 1.
            replace_unknowns (bool, optional): _. Defaults to False.
            callback (_type_, optional): _. Defaults to None.
        Returns:
            Union[str, List[str]]: text as output, if list, same len as input
        """
        if not len(text) == len(src_lang) == len(tgt_lang):
            raise ValueError(
                f"unequal len: text={len(text)} src_lang={len(src_lang)} tgt_lang={len(tgt_lang)}"
            )
        forward_kwds["tgt_lang"] = tgt_lang
        return super().generate(
            text, *forward_args, **forward_kwds, encode_kwargs={"src_lang": src_lang}
        )


class EncoderCT2fromHfHub(CTranslate2ModelfromHuggingfaceHub):
    def __init__(
        self,
        model_name_or_path: str,
        device: Literal["cpu", "cuda"] = "cuda",
        device_index=0,
        compute_type: Literal["int8_float16", "int8"] = "int8_float16",
        tokenizer: Union[AutoTokenizer, None] = None,
        hub_kwargs={},
        **kwargs: Any,
    ):
        """for ctranslate2.Translator models, in particular m2m-100
        Args:
            model_name_or_path (str): _description_
            device (Literal[cpu, cuda], optional): _description_. Defaults to "cuda".
            device_index (int, optional): _description_. Defaults to 0.
            compute_type (Literal[int8_float16, int8], optional): _description_. Defaults to "int8_float16".
            tokenizer (Union[AutoTokenizer, None], optional): _description_. Defaults to None.
            hub_kwargs (dict, optional): _description_. Defaults to {}.
            **kwargs (Any, optional): Any additional arguments
        """
        self.ctranslate_class = ctranslate2.Encoder
        super().__init__(
            model_name_or_path,
            device,
            device_index,
            compute_type,
            tokenizer,
            hub_kwargs,
            **kwargs,
        )
        self.device = device
        if device == "cuda":
            try:
                import torch
            except ImportError:
                raise ValueError(
                    "decoding storageview on CUDA of encoder requires torch"
                )
            self.tensor_decode_method = functools.partial(
                torch.as_tensor, device=device
            )
            self.input_dtype=torch.int32
        else:
            try:
                import numpy as np
            except ImportError:
                raise ValueError(
                    "decoding storageview on CPU of encoder requires numpy"
                )
            self.tensor_decode_method = np.asarray

    def _forward(self, features, *args, **kwds):
        input_ids = features["input_ids"]
        tokens_out = self.model.forward_batch(input_ids, *args,  **kwds)
        outputs = dict(
            pooler_output = self.tensor_decode_method(tokens_out.pooler_output),
            last_hidden_state = self.tensor_decode_method(tokens_out.last_hidden_state),
            attention_mask=features["attention_mask"]
        )
        return outputs

    def tokenize_encode(self, text, *args, **kwargs):
        return self.tokenizer(text)

    def tokenize_decode(self, tokens_out, *args, **kwargs):
        return tokens_out

    def generate(
        self,
        text: Union[str, List[str]],
        encode_tok_kwargs={},
        decode_tok_kwargs={},
        *forward_args,
        **forward_kwds: Any,
    ):
        return super().generate(
            text,
            encode_kwargs=encode_tok_kwargs,
            decode_kwargs=decode_tok_kwargs,
            *forward_args,
            **forward_kwds,
        )


class GeneratorCT2fromHfHub(CTranslate2ModelfromHuggingfaceHub):
    def __init__(
        self,
        model_name_or_path: str,
        device: Literal["cpu", "cuda"] = "cuda",
        device_index=0,
        compute_type: Literal["int8_float16", "int8"] = "int8_float16",
        tokenizer: Union[AutoTokenizer, None] = None,
        hub_kwargs={},
        **kwargs: Any,
    ):
        """for ctranslate2.Generator models
        Args:
            model_name_or_path (str): _description_
            device (Literal[cpu, cuda], optional): _description_. Defaults to "cuda".
            device_index (int, optional): _description_. Defaults to 0.
            compute_type (Literal[int8_float16, int8], optional): _description_. Defaults to "int8_float16".
            tokenizer (Union[AutoTokenizer, None], optional): _description_. Defaults to None.
            hub_kwargs (dict, optional): _description_. Defaults to {}.
            **kwargs (Any, optional): Any additional arguments
        """
        self.ctranslate_class = ctranslate2.Generator
        super().__init__(
            model_name_or_path,
            device,
            device_index,
            compute_type,
            tokenizer,
            hub_kwargs,
            **kwargs,
        )

    def _forward(self, *args, **kwds):
        return self.model.generate_batch(*args, **kwds)

    def tokenize_decode(self, tokens_out, *args, **kwargs):
        return [
            self.tokenizer.decode(tokens_out[i].sequences_ids[0], *args, **kwargs)
            for i in range(len(tokens_out))
        ]

    def generate(
        self,
        text: Union[str, List[str]],
        encode_tok_kwargs={},
        decode_tok_kwargs={},
        *forward_args,
        **forward_kwds: Any,
    ):
        """_summary_
        Args:
            text (str | List[str]): Input texts
            encode_tok_kwargs (dict, optional): additional kwargs for tokenizer
            decode_tok_kwargs (dict, optional): additional kwargs for tokenizer
            max_batch_size (int, optional): _. Defaults to 0.
            batch_type (str, optional): _. Defaults to 'examples'.
            asynchronous (bool, optional): _. Defaults to False.
            beam_size (int, optional): _. Defaults to 1.
            patience (float, optional): _. Defaults to 1.
            num_hypotheses (int, optional): _. Defaults to 1.
            length_penalty (float, optional): _. Defaults to 1.
            repetition_penalty (float, optional): _. Defaults to 1.
            no_repeat_ngram_size (int, optional): _. Defaults to 0.
            disable_unk (bool, optional): _. Defaults to False.
            suppress_sequences (Optional[List[List[str]]], optional): _.
                Defaults to None.
            end_token (Optional[Union[str, List[str], List[int]]], optional): _.
                Defaults to None.
            return_end_token (bool, optional): _. Defaults to False.
            max_length (int, optional): _. Defaults to 512.
            min_length (int, optional): _. Defaults to 0.
            include_prompt_in_result (bool, optional): _. Defaults to True.
            return_scores (bool, optional): _. Defaults to False.
            return_alternatives (bool, optional): _. Defaults to False.
            min_alternative_expansion_prob (float, optional): _. Defaults to 0.
            sampling_topk (int, optional): _. Defaults to 1.
            sampling_temperature (float, optional): _. Defaults to 1.
        Returns:
            str | List[str]: text as output, if list, same len as input
        """
        return super().generate(
            text,
            encode_kwargs=encode_tok_kwargs,
            decode_kwargs=decode_tok_kwargs,
            *forward_args,
            **forward_kwds,
        )