

'''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 	lowerCAmelCase  :
       lowerCAmelCase_												=			XGLMConfig
       lowerCAmelCase_												=			{}
       lowerCAmelCase_												=			"gelu"







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







            """simple docstring"""







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







       def       snake_case (				self      :			str   ):







            """simple docstring"""







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







       def       snake_case (				self      :			Tuple   ):







            """simple docstring"""







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

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

            __lowercase      		=self.get_config()

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

            return (
                config,
                input_ids,
                input_mask,
                head_mask,
            )







       def       snake_case (				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=SCREAMING_SNAKE_CASE__    ,   bos_token_id=self.bos_token_id    ,   eos_token_id=self.eos_token_id    ,   pad_token_id=self.pad_token_id    ,   return_dict=SCREAMING_SNAKE_CASE__    ,   )







       def       snake_case (				self      :			List[str]   ):







            """simple docstring"""







            __lowercase      		=self.prepare_config_and_inputs()

            (
                (
                __lowercase							
            )		,  (
                __lowercase							
            )		,  (
                __lowercase							
            )		,  (
                __lowercase							
            )		,  							
            )      		=config_and_inputs

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

            return config, inputs_dict






@require_tf
class 	lowerCAmelCase  (   _lowerCAmelCase ,      _lowerCAmelCase ,      unittest.TestCase ):
       lowerCAmelCase_												=			(TFXGLMModel, TFXGLMForCausalLM) if is_tf_available() else ()
       lowerCAmelCase_												=			(TFXGLMForCausalLM,) if is_tf_available() else ()
       lowerCAmelCase_												=			(
           {"feature-extraction": TFXGLMModel, "text-generation": TFXGLMForCausalLM} if is_tf_available() else {}
       )
       lowerCAmelCase_												=			False
       lowerCAmelCase_												=			False
       lowerCAmelCase_												=			False







       def       snake_case (				self      :			str   ):







            """simple docstring"""







            __lowercase      		=TFXGLMModelTester(self   )
            __lowercase      		=ConfigTester(self    ,   config_class=SCREAMING_SNAKE_CASE__    ,   n_embd=37   )







       def       snake_case (				self      :			Any   ):







            """simple docstring"""







            self.config_tester.run_common_tests()







       @slow
       def       snake_case (				self      :			Optional[int]   ):







            """simple docstring"""







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







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







            """simple docstring"""







            super().test_resize_token_embeddings()






@require_tf
class 	lowerCAmelCase  (   unittest.TestCase ):







       @slow
       def       snake_case (				self      :			str    ,   __lowercase      :			List[Any]=True   ):







            """simple docstring"""







            __lowercase      		=TFXGLMForCausalLM.from_pretrained('facebook/xglm-564M'   )
            __lowercase      		=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
            __lowercase      		=[2, 268, 9865, 67, 11, 1988, 57252, 9865, 5, 984, 67, 1988, 213838, 1658, 53, 70446, 33, 6657, 278, 1581]
            # fmt: on
            __lowercase      		=model.generate(SCREAMING_SNAKE_CASE__    ,   do_sample=SCREAMING_SNAKE_CASE__    ,   num_beams=1   )
            if verify_outputs:
                 self.assertListEqual(output_ids[0].numpy().tolist()    ,   SCREAMING_SNAKE_CASE__   )







       @slow
       def       snake_case (				self      :			Optional[Any]   ):







            """simple docstring"""







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

            tf.random.set_seed(0   )
            __lowercase      		=tokenizer('Today is a nice day and'    ,   return_tensors='tf'   )
            __lowercase      		=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'   ):
                 __lowercase      		=model.generate(SCREAMING_SNAKE_CASE__    ,   do_sample=SCREAMING_SNAKE_CASE__    ,   seed=[7, 0]   )
            __lowercase      		=tokenizer.decode(output_ids[0]    ,   skip_special_tokens=SCREAMING_SNAKE_CASE__   )

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







       @slow
       def       snake_case (				self      :			Dict   ):







            """simple docstring"""







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

            __lowercase      		='left'

            # use different length sentences to test batching
            __lowercase      		=[
                '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',
            ]

            __lowercase      		=tokenizer(SCREAMING_SNAKE_CASE__    ,   return_tensors='tf'    ,   padding=SCREAMING_SNAKE_CASE__   )
            __lowercase      		=inputs['input_ids']

            __lowercase      		=model.generate(input_ids=SCREAMING_SNAKE_CASE__    ,   attention_mask=inputs['attention_mask']    ,   max_new_tokens=12   )

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

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

            __lowercase      		=tokenizer.batch_decode(SCREAMING_SNAKE_CASE__    ,   skip_special_tokens=SCREAMING_SNAKE_CASE__   )
            __lowercase      		=tokenizer.decode(output_non_padded[0]    ,   skip_special_tokens=SCREAMING_SNAKE_CASE__   )
            __lowercase      		=tokenizer.decode(output_padded[0]    ,   skip_special_tokens=SCREAMING_SNAKE_CASE__   )

            __lowercase      		=[
                '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(SCREAMING_SNAKE_CASE__    ,   SCREAMING_SNAKE_CASE__   )
            self.assertListEqual(SCREAMING_SNAKE_CASE__    ,   [non_padded_sentence, padded_sentence]   )







"""simple docstring"""







def       _lowerCamelCase				(							UpperCAmelCase_ :       int     )	-> int:






	"""simple docstring"""





	if not isinstance(UpperCAmelCase_,	UpperCAmelCase_     ):
		raise ValueError("Input must be an integer"     )
	if input_num <= 0:
		raise ValueError("Input must be positive"     )
	return sum(
	    divisor for divisor in range(1,	input_num // 2 + 1     ) if input_num % divisor == 0     )


if __name__ == "__main__":
			import doctest

			doctest.testmod()




