





"""simple docstring"""




from __future__ import annotations

import unittest

from transformers import XGLMConfig, XGLMTokenizer, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
    import tensorflow as tf

    from transformers.models.xglm.modeling_tf_xglm import (
        TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFXGLMForCausalLM,
        TFXGLMModel,
    )

@require_tf
class 		SCREAMING_SNAKE_CASE__    :
      A_						:    Union[str, Any]							=   XGLMConfig
      A_						:    List[str]							=   {}
      A_						:    str							=   'gelu'


      def __init__(self	:				Optional[Any]    ,      a__	:				Optional[Any]    ,      a__	:				int=14    ,      a__	:				Dict=7    ,      a__	:				Optional[Any]=True    ,      a__	:				Optional[int]=True    ,      a__	:				List[Any]=True    ,      a__	:				Optional[int]=99    ,      a__	:				Union[str, Any]=32    ,      a__	:				Union[str, Any]=2    ,      a__	:				List[str]=4    ,      a__	:				Optional[int]=37    ,      a__	:				List[Any]="gelu"    ,      a__	:				Tuple=0.1    ,      a__	:				str=0.1    ,      a__	:				int=512    ,      a__	:				Tuple=0.0_2    ,      ):





         """simple docstring"""



         __snake_case								=       parent
         __snake_case								=       batch_size
         __snake_case								=       seq_length
         __snake_case								=       is_training
         __snake_case								=       use_input_mask
         __snake_case								=       use_labels
         __snake_case								=       vocab_size
         __snake_case								=       d_model
         __snake_case								=       num_hidden_layers
         __snake_case								=       num_attention_heads
         __snake_case								=       ffn_dim
         __snake_case								=       activation_function
         __snake_case								=       activation_dropout
         __snake_case								=       attention_dropout
         __snake_case								=       max_position_embeddings
         __snake_case								=       initializer_range
         __snake_case								=       None
         __snake_case								=       0
         __snake_case								=       2
         __snake_case								=       1


      def        a			(self	:				Tuple				):





         """simple docstring"""



         return XGLMConfig.from_pretrained('''facebook/xglm-564M'''				)


      def        a			(self	:				List[str]				):





         """simple docstring"""



         __snake_case								=       tf.clip_by_value(
             ids_tensor([self.batch_size, self.seq_length]    ,      self.vocab_size				)    ,      clip_value_min=0    ,      clip_value_max=3				)

         __snake_case								=       None
         if self.use_input_mask:
            __snake_case								=       random_attention_mask([self.batch_size, self.seq_length]				)

         __snake_case								=       self.get_config()

         __snake_case								=       floats_tensor([self.num_hidden_layers, self.num_attention_heads]    ,      2				)

         return (
             config,
             input_ids,
             input_mask,
             head_mask,
         )


      def        a			(self	:				Dict				):





         """simple docstring"""



         return XGLMConfig(
             vocab_size=self.vocab_size    ,      d_model=self.hidden_size    ,      num_layers=self.num_hidden_layers    ,      attention_heads=self.num_attention_heads    ,      ffn_dim=self.ffn_dim    ,      activation_function=self.activation_function    ,      activation_dropout=self.activation_dropout    ,      attention_dropout=self.attention_dropout    ,      max_position_embeddings=self.max_position_embeddings    ,      initializer_range=self.initializer_range    ,      use_cache=__a    ,      bos_token_id=self.bos_token_id    ,      eos_token_id=self.eos_token_id    ,      pad_token_id=self.pad_token_id    ,      return_dict=__a    ,      )


      def        a			(self	:				List[Any]				):





         """simple docstring"""



         __snake_case								=       self.prepare_config_and_inputs()

         (
             __snake_case      
         )								=       config_and_inputs

         __snake_case								=       {
             'input_ids': input_ids,
             'head_mask': head_mask,
         }

         return config, inputs_dict






@require_tf
class 		SCREAMING_SNAKE_CASE__    (	SCREAMING_SNAKE_CASE_						,				SCREAMING_SNAKE_CASE_						,				unittest.TestCase  ):
      A_						:    Dict							=   (TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
      A_						:    Any							=   (TFXGLMForCausalLM,) if is_tf_available() else ()
      A_						:    Any							=   (
          {'feature-extraction': TFXGLMModel, 'text-generation': TFXGLMForCausalLM} if is_tf_available() else {}
      )
      A_						:    List[Any]							=   False
      A_						:    Dict							=   False
      A_						:    List[Any]							=   False


      def        a			(self	:				Any				):





         """simple docstring"""



         __snake_case								=       TFXGLMModelTester(self				)
         __snake_case								=       ConfigTester(self    ,      config_class=__a    ,      n_embd=37				)


      def        a			(self	:				Optional[Any]				):





         """simple docstring"""



         self.config_tester.run_common_tests()


      @slow
      def        a			(self	:				Dict				):





         """simple docstring"""



         for model_name in TF_XGLM_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            __snake_case								=       TFXGLMModel.from_pretrained(__a				)
            self.assertIsNotNone(__a				)


      @unittest.skip(reason='''Currently, model embeddings are going to undergo a major refactor.'''				)
      def        a			(self	:				Any				):





         """simple docstring"""



         super().test_resize_token_embeddings()






@require_tf
class 		SCREAMING_SNAKE_CASE__    (	unittest.TestCase  ):


      @slow
      def        a			(self	:				Optional[Any]    ,      a__	:				Optional[Any]=True				):





         """simple docstring"""



         __snake_case								=       TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''				)
         __snake_case								=       tf.convert_to_tensor([[2, 268, 9865]]    ,      dtype=tf.intaa				)  # The dog
         # </s> The dog is a very friendly dog. He is very affectionate and loves to play with other
         # fmt: off
         __snake_case								=       [2, 268, 9865, 67, 11, 1988, 5_7252, 9865, 5, 984, 67, 1988, 21_3838, 1658, 53, 7_0446, 33, 6657, 278, 1581]
         # fmt: on
         __snake_case								=       model.generate(__a    ,      do_sample=__a    ,      num_beams=1				)
         if verify_outputs:
            self.assertListEqual(output_ids[0].numpy().tolist()    ,      __a				)


      @slow
      def        a			(self	:				Any				):





         """simple docstring"""



         __snake_case								=       XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''				)
         __snake_case								=       TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''				)

         tf.random.set_seed(0				)
         __snake_case								=       tokenizer('''Today is a nice day and'''    ,      return_tensors='''tf'''				)
         __snake_case								=       tokenized.input_ids
         # forces the generation to happen on CPU, to avoid GPU-related quirks (and assure same output regardless of the available devices)
         with tf.device(''':/CPU:0'''				):
            __snake_case								=       model.generate(__a    ,      do_sample=__a    ,      seed=[7, 0]				)
         __snake_case								=       tokenizer.decode(output_ids[0]    ,      skip_special_tokens=__a				)

         __snake_case								=       (
             'Today is a nice day and warm evening here over Southern Alberta!! Today when they closed schools due'
         )
         self.assertEqual(__a    ,      __a				)


      @slow
      def        a			(self	:				Tuple				):





         """simple docstring"""



         __snake_case								=       TFXGLMForCausalLM.from_pretrained('''facebook/xglm-564M'''				)
         __snake_case								=       XGLMTokenizer.from_pretrained('''facebook/xglm-564M'''				)

         __snake_case								=       'left'

         # use different length sentences to test batching
         __snake_case								=       [
             'This is an extremelly long sentence that only exists to test the ability of the model to cope with '
             'left-padding, such as in batched generation. The output for the sequence below should be the same '
             'regardless of whether left padding is applied or not. When',
             'Hello, my dog is a little',
         ]

         __snake_case								=       tokenizer(__a    ,      return_tensors='''tf'''    ,      padding=__a				)
         __snake_case								=       inputs['input_ids']

         __snake_case								=       model.generate(input_ids=__a    ,      attention_mask=inputs['''attention_mask''']    ,      max_new_tokens=12				)

         __snake_case								=       tokenizer(sentences[0]    ,      return_tensors='''tf'''				).input_ids
         __snake_case								=       model.generate(input_ids=__a    ,      max_new_tokens=12				)

         __snake_case								=       tokenizer(sentences[1]    ,      return_tensors='''tf'''				).input_ids
         __snake_case								=       model.generate(input_ids=__a    ,      max_new_tokens=12				)

         __snake_case								=       tokenizer.batch_decode(__a    ,      skip_special_tokens=__a				)
         __snake_case								=       tokenizer.decode(output_non_padded[0]    ,      skip_special_tokens=__a				)
         __snake_case								=       tokenizer.decode(output_padded[0]    ,      skip_special_tokens=__a				)

         __snake_case								=       [
             'This is an extremelly long sentence that only exists to test the ability of the model to cope with '
             'left-padding, such as in batched generation. The output for the sequence below should be the same '
             'regardless of whether left padding is applied or not. When left padding is applied, the sequence will be '
             'a single',
             'Hello, my dog is a little bit of a shy one, but he is very friendly',
         ]
         self.assertListEqual(__a    ,      __a				)
         self.assertListEqual(__a    ,      [non_padded_sentence, padded_sentence]				)








from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tokenizers_available,
    is_torch_available,
)


snake_case_           =      {}

try:
    if not is_sentencepiece_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    snake_case_           =      ['NllbTokenizer']

try:
    if not is_tokenizers_available():
        raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
    pass
else:
    snake_case_           =      ['NllbTokenizerFast']


if TYPE_CHECKING:
    try:
        if not is_sentencepiece_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_nllb import NllbTokenizer

    try:
        if not is_tokenizers_available():
            raise OptionalDependencyNotAvailable()
    except OptionalDependencyNotAvailable:
        pass
    else:
        from .tokenization_nllb_fast import NllbTokenizerFast

else:
    import sys

    snake_case_           =      _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)

