
'''simple docstring'''

import tempfile
import unittest

import numpy as np

import transformers
from transformers import GPTaConfig, GPTaTokenizer, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
   import jax
   import jax.numpy as jnp

   from transformers.modeling_flax_pytorch_utils import (
       convert_pytorch_state_dict_to_flax,
       load_flax_weights_in_pytorch_model,
   )
   from transformers.models.gpta.modeling_flax_gpta import FlaxGPTaLMHeadModel, FlaxGPTaModel

if is_torch_available():
   import torch


class 		_a     :







   """simple docstring"""




   def __init__(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE=14    ,__SCREAMING_SNAKE_CASE=7    ,__SCREAMING_SNAKE_CASE=True    ,__SCREAMING_SNAKE_CASE=True    ,__SCREAMING_SNAKE_CASE=False    ,__SCREAMING_SNAKE_CASE=True    ,__SCREAMING_SNAKE_CASE=99    ,__SCREAMING_SNAKE_CASE=32    ,__SCREAMING_SNAKE_CASE=5    ,__SCREAMING_SNAKE_CASE=4    ,__SCREAMING_SNAKE_CASE=37    ,__SCREAMING_SNAKE_CASE="gelu"    ,__SCREAMING_SNAKE_CASE=0.1    ,__SCREAMING_SNAKE_CASE=0.1    ,__SCREAMING_SNAKE_CASE=512    ,__SCREAMING_SNAKE_CASE=0.02    ,):
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		parent
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		batch_size
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		seq_length
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		is_training
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		use_input_mask
    SCREAMING_SNAKE_CASE  :  int								=		use_token_type_ids
    SCREAMING_SNAKE_CASE  :  Tuple								=		use_labels
    SCREAMING_SNAKE_CASE  :  str								=		vocab_size
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		hidden_size
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		num_hidden_layers
    SCREAMING_SNAKE_CASE  :  List[Any]								=		num_attention_heads
    SCREAMING_SNAKE_CASE  :  Any								=		intermediate_size
    SCREAMING_SNAKE_CASE  :  List[Any]								=		hidden_act
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		hidden_dropout_prob
    SCREAMING_SNAKE_CASE  :  List[Any]								=		attention_probs_dropout_prob
    SCREAMING_SNAKE_CASE  :  str								=		max_position_embeddings
    SCREAMING_SNAKE_CASE  :  Tuple								=		initializer_range
    SCREAMING_SNAKE_CASE  :  List[Any]								=		None
    SCREAMING_SNAKE_CASE  :  int								=		vocab_size - 1
    SCREAMING_SNAKE_CASE  :  Dict								=		vocab_size - 1
    SCREAMING_SNAKE_CASE  :  List[Any]								=		vocab_size - 1






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  List[Any]								=		ids_tensor([self.batch_size, self.seq_length]    ,self.vocab_size      )

    SCREAMING_SNAKE_CASE  :  Optional[int]								=		None
    if self.use_input_mask:
     SCREAMING_SNAKE_CASE  :  Optional[int]								=		random_attention_mask([self.batch_size, self.seq_length]      )

    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		GPTaConfig(
        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    ,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 (config, input_ids, input_mask)






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  int								=		self.prepare_config_and_inputs()
    SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE  :  List[Any]								=		config_and_inputs
    SCREAMING_SNAKE_CASE  :  int								=		{'input_ids': input_ids, 'attention_mask': attention_mask}
    return config, inputs_dict






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE  :  List[str]								=		self.prepare_config_and_inputs()

    SCREAMING_SNAKE_CASE  :  Any								=		floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
    SCREAMING_SNAKE_CASE  :  Dict								=		ids_tensor([self.batch_size, self.seq_length]    ,vocab_size=2      )

    return (
        config,
        input_ids,
        attention_mask,
        encoder_hidden_states,
        encoder_attention_mask,
    )






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      ):
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		20
    SCREAMING_SNAKE_CASE  :  List[str]								=		model_class_name(__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		model.init_cache(input_ids.shape[0]    ,__SCREAMING_SNAKE_CASE      )
    SCREAMING_SNAKE_CASE  :  List[Any]								=		jnp.ones((input_ids.shape[0], max_decoder_length)    ,dtype='i4'      )

    SCREAMING_SNAKE_CASE  :  List[str]								=		jnp.broadcast_to(
        jnp.arange(input_ids.shape[-1] - 1      )[None, :]    ,(input_ids.shape[0], input_ids.shape[-1] - 1)      )
    SCREAMING_SNAKE_CASE  :  Any								=		model(
        input_ids[:, :-1]    ,attention_mask=__SCREAMING_SNAKE_CASE    ,past_key_values=__SCREAMING_SNAKE_CASE    ,position_ids=__SCREAMING_SNAKE_CASE    ,)

    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]    ,dtype='i4'      )
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		model(
        input_ids[:, -1:]    ,attention_mask=__SCREAMING_SNAKE_CASE    ,past_key_values=outputs_cache.past_key_values    ,position_ids=__SCREAMING_SNAKE_CASE    ,)

    SCREAMING_SNAKE_CASE  :  str								=		model(__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  List[str]								=		np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])      )      )
    self.parent.assertTrue(diff < 1e-3    ,msg=f"""Max diff is {diff}"""      )






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      ):
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		20
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		model_class_name(__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  int								=		jnp.concatenate(
        [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1])      )]    ,axis=-1    ,)

    SCREAMING_SNAKE_CASE  :  Any								=		model.init_cache(input_ids.shape[0]    ,__SCREAMING_SNAKE_CASE      )
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		jnp.broadcast_to(
        jnp.arange(input_ids.shape[-1] - 1      )[None, :]    ,(input_ids.shape[0], input_ids.shape[-1] - 1)      )

    SCREAMING_SNAKE_CASE  :  List[Any]								=		model(
        input_ids[:, :-1]    ,attention_mask=__SCREAMING_SNAKE_CASE    ,past_key_values=__SCREAMING_SNAKE_CASE    ,position_ids=__SCREAMING_SNAKE_CASE    ,)
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]    ,dtype='i4'      )
    SCREAMING_SNAKE_CASE  :  List[Any]								=		model(
        input_ids[:, -1:]    ,past_key_values=outputs_cache.past_key_values    ,attention_mask=__SCREAMING_SNAKE_CASE    ,position_ids=__SCREAMING_SNAKE_CASE    ,)

    SCREAMING_SNAKE_CASE  :  Tuple								=		model(__SCREAMING_SNAKE_CASE    ,attention_mask=__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])      )      )
    self.parent.assertTrue(diff < 1e-3    ,msg=f"""Max diff is {diff}"""      )



@require_flax
class 		_a     (							SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			,    unittest.TestCase  ):







   """simple docstring"""




   A		= (FlaxGPTaModel, FlaxGPTaLMHeadModel) if is_flax_available() else ()
   A		= (FlaxGPTaLMHeadModel,) if is_flax_available() else ()






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Any								=		FlaxGPTaModelTester(self      )






   def 					__a			(       self      ):
    for model_class_name in self.all_model_classes:
     SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE  :  str								=		self.model_tester.prepare_config_and_inputs()
     self.model_tester.check_use_cache_forward(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self      ):
    for model_class_name in self.all_model_classes:
     SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE  :  int								=		self.model_tester.prepare_config_and_inputs()
     self.model_tester.check_use_cache_forward_with_attn_mask(
         __SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )






   @slow
   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		GPTaTokenizer.from_pretrained('gpt2'    ,pad_token='</s>'    ,padding_side='left'      )
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		tokenizer(['Hello this is a long string', 'Hey']    ,return_tensors='np'    ,padding=__SCREAMING_SNAKE_CASE    ,truncation=__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  List[Any]								=		FlaxGPTaLMHeadModel.from_pretrained('gpt2'      )
    SCREAMING_SNAKE_CASE  :  Any								=		False
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		model.config.eos_token_id

    SCREAMING_SNAKE_CASE  :  int								=		jax.jit(model.generate      )

    SCREAMING_SNAKE_CASE  :  Any								=		jit_generate(inputs['input_ids']    ,attention_mask=inputs['attention_mask']      ).sequences

    SCREAMING_SNAKE_CASE  :  Dict								=		tokenizer.batch_decode(__SCREAMING_SNAKE_CASE    ,skip_special_tokens=__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  Optional[int]								=		[
        'Hello this is a long string of words. I\'m going to start with the first one.\n',
        'Hey, I\'m not sure if I\'m going to be able to do',
    ]

    self.assertListEqual(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )






   @is_pt_flax_cross_test
   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE  :  List[Any]								=		self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
     with self.subTest(model_class.__name__      ):
      # prepare inputs
      SCREAMING_SNAKE_CASE  :  List[Any]								=		self._prepare_for_class(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )
      SCREAMING_SNAKE_CASE  :  Any								=		{k: torch.tensor(v.tolist()      ) for k, v in prepared_inputs_dict.items()}

      # load corresponding PyTorch class
      SCREAMING_SNAKE_CASE  :  int								=		model_class.__name__[4:]  # Skip the "Flax" at the beginning
      SCREAMING_SNAKE_CASE  :  List[Any]								=		getattr(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )

      SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE  :  List[Any]								=		pt_inputs['input_ids'].shape
      SCREAMING_SNAKE_CASE  :  Any								=		np.random.randint(0    ,seq_length - 1    ,size=(batch_size,)      )
      for batch_idx, start_index in enumerate(__SCREAMING_SNAKE_CASE      ):
       SCREAMING_SNAKE_CASE  :  Dict								=		0
       SCREAMING_SNAKE_CASE  :  Any								=		1
       SCREAMING_SNAKE_CASE  :  Any								=		0
       SCREAMING_SNAKE_CASE  :  int								=		1
      SCREAMING_SNAKE_CASE  :  Optional[int]								=		pt_model_class(__SCREAMING_SNAKE_CASE      ).eval()
      SCREAMING_SNAKE_CASE  :  List[Any]								=		model_class(__SCREAMING_SNAKE_CASE    ,dtype=jnp.floataa      )

      SCREAMING_SNAKE_CASE  :  List[Any]								=		convert_pytorch_state_dict_to_flax(pt_model.state_dict()    ,__SCREAMING_SNAKE_CASE      )
      SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		fx_state

      with torch.no_grad():
       SCREAMING_SNAKE_CASE  :  int								=		pt_model(**__SCREAMING_SNAKE_CASE      ).to_tuple()

      SCREAMING_SNAKE_CASE  :  Dict								=		fx_model(**__SCREAMING_SNAKE_CASE      ).to_tuple()
      self.assertEqual(len(__SCREAMING_SNAKE_CASE      )    ,len(__SCREAMING_SNAKE_CASE      )    ,'Output lengths differ between Flax and PyTorch'      )
      for fx_output, pt_output in zip(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      ):
       self.assert_almost_equals(fx_output[:, -1]    ,pt_output[:, -1].numpy()    ,4e-2      )

      with tempfile.TemporaryDirectory() as tmpdirname:
       pt_model.save_pretrained(__SCREAMING_SNAKE_CASE      )
       SCREAMING_SNAKE_CASE  :  str								=		model_class.from_pretrained(__SCREAMING_SNAKE_CASE    ,from_pt=__SCREAMING_SNAKE_CASE      )

      SCREAMING_SNAKE_CASE  :  Optional[int]								=		fx_model_loaded(**__SCREAMING_SNAKE_CASE      ).to_tuple()
      self.assertEqual(
          len(__SCREAMING_SNAKE_CASE      )    ,len(__SCREAMING_SNAKE_CASE      )    ,'Output lengths differ between Flax and PyTorch'      )
      for fx_output_loaded, pt_output in zip(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      ):
       self.assert_almost_equals(fx_output_loaded[:, -1]    ,pt_output[:, -1].numpy()    ,4e-2      )






   @is_pt_flax_cross_test
   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE  :  Dict								=		self.model_tester.prepare_config_and_inputs_for_common()
    for model_class in self.all_model_classes:
     with self.subTest(model_class.__name__      ):
      # prepare inputs
      SCREAMING_SNAKE_CASE  :  Dict								=		self._prepare_for_class(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )
      SCREAMING_SNAKE_CASE  :  Optional[Any]								=		{k: torch.tensor(v.tolist()      ) for k, v in prepared_inputs_dict.items()}

      # load corresponding PyTorch class
      SCREAMING_SNAKE_CASE  :  List[Any]								=		model_class.__name__[4:]  # Skip the "Flax" at the beginning
      SCREAMING_SNAKE_CASE  :  List[Any]								=		getattr(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )

      SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		pt_model_class(__SCREAMING_SNAKE_CASE      ).eval()
      SCREAMING_SNAKE_CASE  :  str								=		model_class(__SCREAMING_SNAKE_CASE    ,dtype=jnp.floataa      )

      SCREAMING_SNAKE_CASE  :  Optional[int]								=		load_flax_weights_in_pytorch_model(__SCREAMING_SNAKE_CASE    ,fx_model.params      )
      SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE  :  Any								=		pt_inputs['input_ids'].shape
      SCREAMING_SNAKE_CASE  :  Optional[Any]								=		np.random.randint(0    ,seq_length - 1    ,size=(batch_size,)      )
      for batch_idx, start_index in enumerate(__SCREAMING_SNAKE_CASE      ):
       SCREAMING_SNAKE_CASE  :  Optional[Any]								=		0
       SCREAMING_SNAKE_CASE  :  Optional[Any]								=		1
       SCREAMING_SNAKE_CASE  :  Tuple								=		0
       SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		1

      # make sure weights are tied in PyTorch
      pt_model.tie_weights()

      with torch.no_grad():
       SCREAMING_SNAKE_CASE  :  int								=		pt_model(**__SCREAMING_SNAKE_CASE      ).to_tuple()

      SCREAMING_SNAKE_CASE  :  Tuple								=		fx_model(**__SCREAMING_SNAKE_CASE      ).to_tuple()
      self.assertEqual(len(__SCREAMING_SNAKE_CASE      )    ,len(__SCREAMING_SNAKE_CASE      )    ,'Output lengths differ between Flax and PyTorch'      )
      for fx_output, pt_output in zip(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      ):
       self.assert_almost_equals(fx_output[:, -1]    ,pt_output[:, -1].numpy()    ,4e-2      )

      with tempfile.TemporaryDirectory() as tmpdirname:
       fx_model.save_pretrained(__SCREAMING_SNAKE_CASE      )
       SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		pt_model_class.from_pretrained(__SCREAMING_SNAKE_CASE    ,from_flax=__SCREAMING_SNAKE_CASE      )

      with torch.no_grad():
       SCREAMING_SNAKE_CASE  :  Dict								=		pt_model_loaded(**__SCREAMING_SNAKE_CASE      ).to_tuple()

      self.assertEqual(
          len(__SCREAMING_SNAKE_CASE      )    ,len(__SCREAMING_SNAKE_CASE      )    ,'Output lengths differ between Flax and PyTorch'      )
      for fx_output, pt_output in zip(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      ):
       self.assert_almost_equals(fx_output[:, -1]    ,pt_output[:, -1].numpy()    ,4e-2      )






   @slow
   def 					__a			(       self      ):
    for model_class_name in self.all_model_classes:
     SCREAMING_SNAKE_CASE  :  List[str]								=		model_class_name.from_pretrained('gpt2'    ,from_pt=__SCREAMING_SNAKE_CASE      )
     SCREAMING_SNAKE_CASE  :  List[Any]								=		model(np.ones((1, 1)      )      )
     self.assertIsNotNone(__SCREAMING_SNAKE_CASE      )




'''simple docstring'''

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     (							SCREAMING_SNAKE_CASE  ):







   """simple docstring"""




   def __init__(       self    ,__SCREAMING_SNAKE_CASE    ,):
    SCREAMING_SNAKE_CASE  :  Dict								=		parent
    SCREAMING_SNAKE_CASE  :  int								=		13
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		7
    SCREAMING_SNAKE_CASE  :  List[Any]								=		True
    SCREAMING_SNAKE_CASE  :  Dict								=		True
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		True
    SCREAMING_SNAKE_CASE  :  int								=		True
    SCREAMING_SNAKE_CASE  :  str								=		True
    SCREAMING_SNAKE_CASE  :  int								=		99
    SCREAMING_SNAKE_CASE  :  List[str]								=		32
    SCREAMING_SNAKE_CASE  :  List[str]								=		2
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		4
    SCREAMING_SNAKE_CASE  :  Tuple								=		37
    SCREAMING_SNAKE_CASE  :  str								=		'gelu'
    SCREAMING_SNAKE_CASE  :  Tuple								=		0.1
    SCREAMING_SNAKE_CASE  :  Any								=		0.1
    SCREAMING_SNAKE_CASE  :  List[str]								=		512
    SCREAMING_SNAKE_CASE  :  List[Any]								=		16
    SCREAMING_SNAKE_CASE  :  List[Any]								=		2
    SCREAMING_SNAKE_CASE  :  Any								=		0.02
    SCREAMING_SNAKE_CASE  :  List[str]								=		3
    SCREAMING_SNAKE_CASE  :  Any								=		4
    SCREAMING_SNAKE_CASE  :  List[Any]								=		None
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		self.vocab_size - 1






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Any								=		ids_tensor([self.batch_size, self.seq_length]    ,self.vocab_size      )

    SCREAMING_SNAKE_CASE  :  Dict								=		None
    if self.use_input_mask:
     SCREAMING_SNAKE_CASE  :  List[str]								=		random_attention_mask([self.batch_size, self.seq_length]      )

    SCREAMING_SNAKE_CASE  :  List[Any]								=		None
    if self.use_token_type_ids:
     SCREAMING_SNAKE_CASE  :  str								=		ids_tensor([self.batch_size, self.seq_length]    ,self.type_vocab_size      )

    SCREAMING_SNAKE_CASE  :  int								=		None
    if self.use_mc_token_ids:
     SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		ids_tensor([self.batch_size, self.num_choices]    ,self.seq_length      )

    SCREAMING_SNAKE_CASE  :  Optional[int]								=		None
    SCREAMING_SNAKE_CASE  :  List[Any]								=		None
    SCREAMING_SNAKE_CASE  :  str								=		None
    if self.use_labels:
     SCREAMING_SNAKE_CASE  :  Any								=		ids_tensor([self.batch_size]    ,self.type_sequence_label_size      )
     SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		ids_tensor([self.batch_size, self.seq_length]    ,self.num_labels      )
     SCREAMING_SNAKE_CASE  :  str								=		ids_tensor([self.batch_size]    ,self.num_choices      )

    SCREAMING_SNAKE_CASE  :  Dict								=		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    ,)

    SCREAMING_SNAKE_CASE  :  Tuple								=		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 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,*__SCREAMING_SNAKE_CASE      ):
    SCREAMING_SNAKE_CASE  :  int								=		TFCTRLModel(config=__SCREAMING_SNAKE_CASE      )
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		model(__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		[input_ids, None, input_mask]  # None is the input for 'past'
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		model(__SCREAMING_SNAKE_CASE      )

    SCREAMING_SNAKE_CASE  :  Tuple								=		model(__SCREAMING_SNAKE_CASE      )

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






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,*__SCREAMING_SNAKE_CASE      ):
    SCREAMING_SNAKE_CASE  :  Optional[int]								=		TFCTRLLMHeadModel(config=__SCREAMING_SNAKE_CASE      )
    SCREAMING_SNAKE_CASE  :  Any								=		{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
    SCREAMING_SNAKE_CASE  :  Any								=		model(__SCREAMING_SNAKE_CASE      )
    self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.seq_length, self.vocab_size)      )






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,*__SCREAMING_SNAKE_CASE      ):
    SCREAMING_SNAKE_CASE  :  Tuple								=		self.num_labels
    SCREAMING_SNAKE_CASE  :  Any								=		ids_tensor([self.batch_size]    ,self.type_sequence_label_size      )
    SCREAMING_SNAKE_CASE  :  Dict								=		{
        'input_ids': input_ids,
        'token_type_ids': token_type_ids,
        'labels': sequence_labels,
    }
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		TFCTRLForSequenceClassification(__SCREAMING_SNAKE_CASE      )
    SCREAMING_SNAKE_CASE  :  str								=		model(__SCREAMING_SNAKE_CASE      )
    self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.num_labels)      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  str								=		self.prepare_config_and_inputs()

    (
        (
        SCREAMING_SNAKE_CASE
    )				, (
        SCREAMING_SNAKE_CASE
    )				, (
        SCREAMING_SNAKE_CASE
    )				, (
        SCREAMING_SNAKE_CASE
    )				, (
        SCREAMING_SNAKE_CASE
    )				, (
        SCREAMING_SNAKE_CASE
    )				, (
        SCREAMING_SNAKE_CASE
    )				, (
        SCREAMING_SNAKE_CASE
    )				, (
        SCREAMING_SNAKE_CASE
    )				, 
    )  :  List[Any]								=		config_and_inputs

    SCREAMING_SNAKE_CASE  :  int								=		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
    return config, inputs_dict



@require_tf
class 		_a     (							SCREAMING_SNAKE_CASE			,    SCREAMING_SNAKE_CASE			,    unittest.TestCase  ):







   """simple docstring"""




   A		= (TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
   A		= (TFCTRLLMHeadModel,) if is_tf_available() else ()
   A		= (
       {
           'feature-extraction': TFCTRLModel,
           'text-classification': TFCTRLForSequenceClassification,
           'text-generation': TFCTRLLMHeadModel,
           'zero-shot': TFCTRLForSequenceClassification,
       }
       if is_tf_available()
       else {}
   )
   A		= False
   A		= False






   def 					__a			(       self    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      ):
    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 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  List[Any]								=		TFCTRLModelTester(self      )
    SCREAMING_SNAKE_CASE  :  Dict								=		ConfigTester(self    ,config_class=__SCREAMING_SNAKE_CASE    ,n_embd=37      )






   def 					__a			(       self      ):
    self.config_tester.run_common_tests()






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Tuple								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_model(*__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  str								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_lm_head(*__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_for_sequence_classification(*__SCREAMING_SNAKE_CASE      )






   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE				, SCREAMING_SNAKE_CASE  :  Tuple								=		self.model_tester.prepare_config_and_inputs_for_common()
    SCREAMING_SNAKE_CASE  :  Any								=		[TFCTRLLMHeadModel]
    SCREAMING_SNAKE_CASE  :  str								=		[TFCTRLForSequenceClassification]

    for model_class in self.all_model_classes:
     SCREAMING_SNAKE_CASE  :  List[str]								=		model_class(__SCREAMING_SNAKE_CASE      )
     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:
      SCREAMING_SNAKE_CASE  :  Dict								=		model.get_output_embeddings()
      assert isinstance(__SCREAMING_SNAKE_CASE    ,tf.keras.layers.Layer      )
      SCREAMING_SNAKE_CASE  :  Any								=		model.get_bias()
      assert isinstance(__SCREAMING_SNAKE_CASE    ,__SCREAMING_SNAKE_CASE      )
      for k, v in name.items():
       assert isinstance(__SCREAMING_SNAKE_CASE    ,tf.Variable      )
     elif model_class in list_other_models_with_output_ebd:
      SCREAMING_SNAKE_CASE  :  Any								=		model.get_output_embeddings()
      assert isinstance(__SCREAMING_SNAKE_CASE    ,tf.keras.layers.Layer      )
      SCREAMING_SNAKE_CASE  :  str								=		model.get_bias()
      assert name is None
     else:
      SCREAMING_SNAKE_CASE  :  List[Any]								=		model.get_output_embeddings()
      assert x is None
      SCREAMING_SNAKE_CASE  :  int								=		model.get_bias()
      assert name is None






   @slow
   def 					__a			(       self      ):
    for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     SCREAMING_SNAKE_CASE  :  List[str]								=		TFCTRLModel.from_pretrained(__SCREAMING_SNAKE_CASE      )
     self.assertIsNotNone(__SCREAMING_SNAKE_CASE      )



@require_tf
class 		_a     (							unittest.TestCase  ):







   """simple docstring"""




   @slow
   def 					__a			(       self      ):
    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		TFCTRLLMHeadModel.from_pretrained('ctrl'      )
    SCREAMING_SNAKE_CASE  :  Any								=		tf.convert_to_tensor([[11859, 0, 1611, 8]]    ,dtype=tf.intaa      )  # Legal the president is
    SCREAMING_SNAKE_CASE  :  Optional[Any]								=		[
        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

    SCREAMING_SNAKE_CASE  :  Union[str, Any]								=		model.generate(__SCREAMING_SNAKE_CASE    ,do_sample=__SCREAMING_SNAKE_CASE      )
    self.assertListEqual(output_ids[0].numpy().tolist()    ,__SCREAMING_SNAKE_CASE      )


