






from __future__ import annotations

import unittest

from transformers import CTRLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
					import tensorflow as tf

					from transformers.models.ctrl.modeling_tf_ctrl import (
					    TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
					    TFCTRLForSequenceClassification,
					    TFCTRLLMHeadModel,
					    TFCTRLModel,
					)





class        a__      (	lowerCAmelCase__		):






		def __init__(      self							, lowercase__							, )				->				Dict:
				__A    =							parent
				__A    =							13
				__A    =							7
				__A    =							True
				__A    =							True
				__A    =							True
				__A    =							True
				__A    =							True
				__A    =							99
				__A    =							32
				__A    =							2
				__A    =							4
				__A    =							37
				__A    =							"gelu"
				__A    =							0.1
				__A    =							0.1
				__A    =							512
				__A    =							16
				__A    =							2
				__A    =							0.02
				__A    =							3
				__A    =							4
				__A    =							None
				__A    =							self.vocab_size - 1






		def 					_lowerCamelCase					(      self						)				->				Optional[Any]:
				__A    =							ids_tensor([self.batch_size, self.seq_length]							, self.vocab_size						)

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

				__A    =							None
				if self.use_token_type_ids:
						__A    =							ids_tensor([self.batch_size, self.seq_length]							, self.type_vocab_size						)

				__A    =							None
				if self.use_mc_token_ids:
						__A    =							ids_tensor([self.batch_size, self.num_choices]							, self.seq_length						)

				__A    =							None
				__A    =							None
				__A    =							None
				if self.use_labels:
						__A    =							ids_tensor([self.batch_size]							, self.type_sequence_label_size						)
						__A    =							ids_tensor([self.batch_size, self.seq_length]							, self.num_labels						)
						__A    =							ids_tensor([self.batch_size]							, self.num_choices						)

				__A    =							CTRLConfig(
				    vocab_size=self.vocab_size							, n_embd=self.hidden_size							, n_layer=self.num_hidden_layers							, n_head=self.num_attention_heads							, n_positions=self.max_position_embeddings							, pad_token_id=self.pad_token_id							, )

				__A    =							ids_tensor([self.num_hidden_layers, self.num_attention_heads]							, 2						)

				return (
				    config,
				    input_ids,
				    input_mask,
				    head_mask,
				    token_type_ids,
				    mc_token_ids,
				    sequence_labels,
				    token_labels,
				    choice_labels,
				)






		def 					_lowerCamelCase					(      self							, lowercase__							, lowercase__							, lowercase__							, lowercase__							, lowercase__							, *lowercase__						)				->				Optional[Any]:
				__A    =							TFCTRLModel(config=lowercase__						)
				__A    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
				__A    =							model(lowercase__						)

				__A    =							[input_ids, None, input_mask]  # None is the input for 'past'
				__A    =							model(lowercase__						)

				__A    =							model(lowercase__						)

				self.parent.assertEqual(result.last_hidden_state.shape							, (self.batch_size, self.seq_length, self.hidden_size)						)






		def 					_lowerCamelCase					(      self							, lowercase__							, lowercase__							, lowercase__							, lowercase__							, lowercase__							, *lowercase__						)				->				str:
				__A    =							TFCTRLLMHeadModel(config=lowercase__						)
				__A    =							{"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
				__A    =							model(lowercase__						)
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.seq_length, self.vocab_size)						)






		def 					_lowerCamelCase					(      self							, lowercase__							, lowercase__							, lowercase__							, lowercase__							, lowercase__							, *lowercase__						)				->				Optional[int]:
				__A    =							self.num_labels
				__A    =							ids_tensor([self.batch_size]							, self.type_sequence_label_size						)
				__A    =							{
				    "input_ids": input_ids,
				    "token_type_ids": token_type_ids,
				    "labels": sequence_labels,
				}
				__A    =							TFCTRLForSequenceClassification(lowercase__						)
				__A    =							model(lowercase__						)
				self.parent.assertEqual(result.logits.shape							, (self.batch_size, self.num_labels)						)






		def 					_lowerCamelCase					(      self						)				->				Any:
				__A    =							self.prepare_config_and_inputs()

				(
				    (
				    __A
				) ,   (
				    __A
				) ,   (
				    __A
				) ,   (
				    __A
				) ,   (
				    __A
				) ,   (
				    __A
				) ,   (
				    __A
				) ,   (
				    __A
				) ,   (
				    __A
				) ,   
				)    =							config_and_inputs

				__A    =							{"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
				return config, inputs_dict





@require_tf
class        a__      (	lowerCAmelCase__	,       lowerCAmelCase__	,       unittest.TestCase		):
		UpperCAmelCase_							:       str 					= (TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
		UpperCAmelCase_							:       int 					= (TFCTRLLMHeadModel,) if is_tf_available() else ()
		UpperCAmelCase_							:       str 					= (
		    {
		        'feature-extraction': TFCTRLModel,
		        'text-classification': TFCTRLForSequenceClassification,
		        'text-generation': TFCTRLLMHeadModel,
		        'zero-shot': TFCTRLForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		UpperCAmelCase_							:       str 					= False
		UpperCAmelCase_							:       Optional[int] 					= False






		def 					_lowerCamelCase					(      self							, lowercase__							, lowercase__							, lowercase__							, lowercase__							, lowercase__						)				->				Optional[Any]:
				if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
						# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
						# `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
						# config could not be created.
						return True

				return False






		def 					_lowerCamelCase					(      self						)				->				Optional[Any]:
				__A    =							TFCTRLModelTester(self						)
				__A    =							ConfigTester(self							, config_class=lowercase__							, n_embd=37						)






		def 					_lowerCamelCase					(      self						)				->				Optional[Any]:
				self.config_tester.run_common_tests()






		def 					_lowerCamelCase					(      self						)				->				Tuple:
				__A    =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_ctrl_model(*lowercase__						)






		def 					_lowerCamelCase					(      self						)				->				Tuple:
				__A    =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_ctrl_lm_head(*lowercase__						)






		def 					_lowerCamelCase					(      self						)				->				Optional[Any]:
				__A    =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_ctrl_for_sequence_classification(*lowercase__						)






		def 					_lowerCamelCase					(      self						)				->				Any:
				__A ,   __A    =							self.model_tester.prepare_config_and_inputs_for_common()
				__A    =							[TFCTRLLMHeadModel]
				__A    =							[TFCTRLForSequenceClassification]

				for model_class in self.all_model_classes:
						__A    =							model_class(lowercase__						)
						model.build()  # may be needed for the get_bias() call below
						assert isinstance(model.get_input_embeddings()							, tf.keras.layers.Layer						)

						if model_class in list_lm_models:
								__A    =							model.get_output_embeddings()
								assert isinstance(lowercase__							, tf.keras.layers.Layer						)
								__A    =							model.get_bias()
								assert isinstance(lowercase__							, lowercase__						)
								for k, v in name.items():
										assert isinstance(lowercase__							, tf.Variable						)
						elif model_class in list_other_models_with_output_ebd:
								__A    =							model.get_output_embeddings()
								assert isinstance(lowercase__							, tf.keras.layers.Layer						)
								__A    =							model.get_bias()
								assert name is None
						else:
								__A    =							model.get_output_embeddings()
								assert x is None
								__A    =							model.get_bias()
								assert name is None






		@slow
		def 					_lowerCamelCase					(      self						)				->				int:
				for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						__A    =							TFCTRLModel.from_pretrained(lowercase__						)
						self.assertIsNotNone(lowercase__						)





@require_tf
class        a__      (	unittest.TestCase		):






		@slow
		def 					_lowerCamelCase					(      self						)				->				Dict:
				__A    =							TFCTRLLMHeadModel.from_pretrained("ctrl"						)
				__A    =							tf.convert_to_tensor([[11859, 0, 1611, 8]]							, dtype=tf.intaa						)  # Legal the president is
				__A    =							[
				    11859,
				    0,
				    1611,
				    8,
				    5,
				    150,
				    26449,
				    2,
				    19,
				    348,
				    469,
				    3,
				    2595,
				    48,
				    20740,
				    246533,
				    246533,
				    19,
				    30,
				    5,
				]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

				__A    =							model.generate(lowercase__							, do_sample=lowercase__						)
				self.assertListEqual(output_ids[0].numpy().tolist()							, lowercase__						)


"""simple docstring"""


from doctest import testmod
from math import sqrt

def 						UpperCamelCase				(	_lowerCAmelCase   :							int						):
    __a			      =						[]
    if num < 1:
        return facs
    facs.append(1						)
    if num == 1:
        return facs
    facs.append(_lowerCAmelCase						)
    for i in range(2				, int(sqrt(_lowerCAmelCase						)						) + 1						):
        if num % i == 0:  # If i is a factor of num
            facs.append(_lowerCAmelCase						)
            __a			      =						num // i  # num//i is the other factor of num
            if d != i:  # If d and i are distinct
                facs.append(_lowerCAmelCase						)  # we have found another factor
    facs.sort()
    return facs


if __name__ == "__main__":
 testmod(name="""factors_of_a_number""", verbose=True)
