






import collections
import types

import numpy as np

from ..utils import (
    add_end_docstrings,
    is_tensorflow_probability_available,
    is_tf_available,
    is_torch_available,
    requires_backends,
)
from .base import PIPELINE_INIT_ARGS, ArgumentHandler, Dataset, Pipeline, PipelineException


if is_torch_available():
					import torch

					from ..models.auto.modeling_auto import (
					    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
					    MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING,
					)

if is_tf_available() and is_tensorflow_probability_available():
					import tensorflow as tf
					import tensorflow_probability as tfp

					from ..models.auto.modeling_tf_auto import (
					    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
					    TF_MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING,
					)


class 			__SCREAMING_SNAKE_CASE    (      UpperCamelCase__):






						def __call__(		self   ,  _UpperCamelCase=None   ,  _UpperCamelCase=None   ,  **_UpperCamelCase      ):






												"""simple docstring"""
												requires_backends(self   ,  'pandas'      )
												import pandas as pd

												if table is None:
																		raise ValueError('Keyword argument `table` cannot be None.'      )
												elif query is None:
																		if isinstance(_UpperCamelCase   ,  _UpperCamelCase      ) and table.get('query'      ) is not None and table.get('table'      ) is not None:
																								lowerCAmelCase__			  =  [table]
																		elif isinstance(_UpperCamelCase   ,  _UpperCamelCase      ) and len(_UpperCamelCase      ) > 0:
																								if not all(isinstance(_UpperCamelCase   ,  _UpperCamelCase      ) for d in table      ):
																														raise ValueError(
																														    F"Keyword argument `table` should be a list of dict, but is {(type(_UpperCamelCase      ) for d in table)}"      )

																								if table[0].get('query'      ) is not None and table[0].get('table'      ) is not None:
																														lowerCAmelCase__			  =  table
																								else:
																														raise ValueError(
																														    'If keyword argument `table` is a list of dictionaries, each dictionary should have a `table`'
																														    F" and `query` key, but only dictionary has keys {table[0].keys()} `table` and `query` keys."      )
																		elif Dataset is not None and isinstance(_UpperCamelCase   ,  _UpperCamelCase      ) or isinstance(_UpperCamelCase   ,  types.GeneratorType      ):
																								return table
																		else:
																								raise ValueError(
																								    'Invalid input. Keyword argument `table` should be either of type `dict` or `list`, but '
																								    F"is {type(_UpperCamelCase      )})"      )
												else:
																		lowerCAmelCase__			  =  [{'table': table, 'query': query}]

												for tqa_pipeline_input in tqa_pipeline_inputs:
																		if not isinstance(tqa_pipeline_input['table']   ,  pd.DataFrame      ):
																								if tqa_pipeline_input["table"] is None:
																														raise ValueError('Table cannot be None.'      )

																								lowerCAmelCase__			  =  pd.DataFrame(tqa_pipeline_input['table']      )

												return tqa_pipeline_inputs




@add_end_docstrings(UpperCamelCase__)
class 			__SCREAMING_SNAKE_CASE    (      UpperCamelCase__):
						_SCREAMING_SNAKE_CASE		:				Union[str, Any]            =    """table,query"""






						def __init__(		self   ,  _UpperCamelCase=TableQuestionAnsweringArgumentHandler()   ,  *_UpperCamelCase   ,  **_UpperCamelCase      ):






												"""simple docstring"""
												super().__init__(*_UpperCamelCase   ,  **_UpperCamelCase      )
												lowerCAmelCase__			  =  args_parser

												self.check_model_type(
												    dict(
												        TF_MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.items()
												        + TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.items()      )
												    if self.framework == 'tf'
												    else dict(
												        MODEL_FOR_TABLE_QUESTION_ANSWERING_MAPPING.items() + MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING.items()      )      )

												lowerCAmelCase__			  =  bool(getattr(self.model.config   ,  'aggregation_labels'   ,  _UpperCamelCase      )      ) and bool(
												    getattr(self.model.config   ,  'num_aggregation_labels'   ,  _UpperCamelCase      )      )
												lowerCAmelCase__			  =  'tapas' if hasattr(self.model.config   ,  'aggregation_labels'      ) else None






						def 					UpperCamelCase__					(		self   ,  **_UpperCamelCase      ):






												"""simple docstring"""
												return self.model(**_UpperCamelCase      )






						def 					UpperCamelCase__					(		self   ,  **_UpperCamelCase      ):






												"""simple docstring"""



												if self.framework == "pt":
																		lowerCAmelCase__			  =  []
																		lowerCAmelCase__			  =  []
																		lowerCAmelCase__			  =  None
																		lowerCAmelCase__			  =  inputs['input_ids'].shape[0]

																		lowerCAmelCase__			  =  inputs['input_ids'].to(self.device      )
																		lowerCAmelCase__			  =  inputs['attention_mask'].to(self.device      )
																		lowerCAmelCase__			  =  inputs['token_type_ids'].to(self.device      )
																		lowerCAmelCase__			  =  None

																		for index in range(_UpperCamelCase      ):
																								# If sequences have already been processed, the token type IDs will be created according to the previous
																								# answer.
																								if prev_answers is not None:
																														lowerCAmelCase__			  =  token_type_ids_example[:, 3]  # shape (seq_len,)
																														lowerCAmelCase__			  =  np.zeros_like(prev_labels_example.cpu().numpy()      )  # shape (seq_len,)

																														lowerCAmelCase__			  =  token_type_ids[index]  # shape (seq_len, 7)
																														for i in range(model_labels.shape[0]      ):
																																				lowerCAmelCase__			  =  token_type_ids_example[:, 0].tolist()[i]
																																				lowerCAmelCase__			  =  token_type_ids_example[:, 1].tolist()[i] - 1
																																				lowerCAmelCase__			  =  token_type_ids_example[:, 2].tolist()[i] - 1

																																				if row_id >= 0 and col_id >= 0 and segment_id == 1:
																																										lowerCAmelCase__			  =  int(prev_answers[(col_id, row_id)]      )

																														lowerCAmelCase__			  =  torch.from_numpy(_UpperCamelCase      ).type(torch.long      ).to(self.device      )

																								lowerCAmelCase__			  =  input_ids[index]
																								lowerCAmelCase__			  =  attention_mask[index]  # shape (seq_len,)
																								lowerCAmelCase__			  =  token_type_ids[index]  # shape (seq_len, 7)
																								lowerCAmelCase__			  =  self.model(
																								    input_ids=input_ids_example.unsqueeze(0      )   ,  attention_mask=attention_mask_example.unsqueeze(0      )   ,  token_type_ids=token_type_ids_example.unsqueeze(0      )   ,  )
																								lowerCAmelCase__			  =  outputs.logits

																								if self.aggregate:
																														all_aggregations.append(outputs.logits_aggregation      )

																								all_logits.append(_UpperCamelCase      )

																								lowerCAmelCase__			  =  torch.distributions.Bernoulli(logits=_UpperCamelCase      )
																								lowerCAmelCase__			  =  dist_per_token.probs * attention_mask_example.type(torch.floataa      ).to(
																								    dist_per_token.probs.device      )

																								lowerCAmelCase__			  =  collections.defaultdict(_UpperCamelCase      )
																								for i, p in enumerate(probabilities.squeeze().tolist()      ):
																														lowerCAmelCase__			  =  token_type_ids_example[:, 0].tolist()[i]
																														lowerCAmelCase__			  =  token_type_ids_example[:, 1].tolist()[i] - 1
																														lowerCAmelCase__			  =  token_type_ids_example[:, 2].tolist()[i] - 1
																														if col >= 0 and row >= 0 and segment_id == 1:
																																				coords_to_probs[(col, row)].append(_UpperCamelCase      )

																								lowerCAmelCase__			  =  {key: np.array(coords_to_probs[key]      ).mean() > 0.5 for key in coords_to_probs}

																		lowerCAmelCase__			  =  torch.cat(tuple(_UpperCamelCase      )   ,  0      )

																		return (logits_batch,) if not self.aggregate else (logits_batch, torch.cat(tuple(_UpperCamelCase      )   ,  0      ))
												else:
																		lowerCAmelCase__			  =  []
																		lowerCAmelCase__			  =  []
																		lowerCAmelCase__			  =  None
																		lowerCAmelCase__			  =  inputs['input_ids'].shape[0]

																		lowerCAmelCase__			  =  inputs['input_ids']
																		lowerCAmelCase__			  =  inputs['attention_mask']
																		lowerCAmelCase__			  =  inputs['token_type_ids'].numpy()
																		lowerCAmelCase__			  =  None

																		for index in range(_UpperCamelCase      ):
																								# If sequences have already been processed, the token type IDs will be created according to the previous
																								# answer.
																								if prev_answers is not None:
																														lowerCAmelCase__			  =  token_type_ids_example[:, 3]  # shape (seq_len,)
																														lowerCAmelCase__			  =  np.zeros_like(_UpperCamelCase   ,  dtype=np.intaa      )  # shape (seq_len,)

																														lowerCAmelCase__			  =  token_type_ids[index]  # shape (seq_len, 7)
																														for i in range(model_labels.shape[0]      ):
																																				lowerCAmelCase__			  =  token_type_ids_example[:, 0].tolist()[i]
																																				lowerCAmelCase__			  =  token_type_ids_example[:, 1].tolist()[i] - 1
																																				lowerCAmelCase__			  =  token_type_ids_example[:, 2].tolist()[i] - 1

																																				if row_id >= 0 and col_id >= 0 and segment_id == 1:
																																										lowerCAmelCase__			  =  int(prev_answers[(col_id, row_id)]      )

																														lowerCAmelCase__			  =  model_labels

																								lowerCAmelCase__			  =  input_ids[index]
																								lowerCAmelCase__			  =  attention_mask[index]  # shape (seq_len,)
																								lowerCAmelCase__			  =  token_type_ids[index]  # shape (seq_len, 7)
																								lowerCAmelCase__			  =  self.model(
																								    input_ids=np.expand_dims(_UpperCamelCase   ,  axis=0      )   ,  attention_mask=np.expand_dims(_UpperCamelCase   ,  axis=0      )   ,  token_type_ids=np.expand_dims(_UpperCamelCase   ,  axis=0      )   ,  )
																								lowerCAmelCase__			  =  outputs.logits

																								if self.aggregate:
																														all_aggregations.append(outputs.logits_aggregation      )

																								all_logits.append(_UpperCamelCase      )

																								lowerCAmelCase__			  =  tfp.distributions.Bernoulli(logits=_UpperCamelCase      )
																								lowerCAmelCase__			  =  dist_per_token.probs_parameter() * tf.cast(_UpperCamelCase   ,  tf.floataa      )

																								lowerCAmelCase__			  =  collections.defaultdict(_UpperCamelCase      )
																								lowerCAmelCase__			  =  token_type_ids_example
																								for i, p in enumerate(tf.squeeze(_UpperCamelCase      ).numpy().tolist()      ):
																														lowerCAmelCase__			  =  token_type_ids_example[:, 0].tolist()[i]
																														lowerCAmelCase__			  =  token_type_ids_example[:, 1].tolist()[i] - 1
																														lowerCAmelCase__			  =  token_type_ids_example[:, 2].tolist()[i] - 1
																														if col >= 0 and row >= 0 and segment_id == 1:
																																				coords_to_probs[(col, row)].append(_UpperCamelCase      )

																								lowerCAmelCase__			  =  {key: np.array(coords_to_probs[key]      ).mean() > 0.5 for key in coords_to_probs}

																		lowerCAmelCase__			  =  tf.concat(tuple(_UpperCamelCase      )   ,  0      )

																		return (logits_batch,) if not self.aggregate else (logits_batch, tf.concat(tuple(_UpperCamelCase      )   ,  0      ))






						def __call__(		self   ,  *_UpperCamelCase   ,  **_UpperCamelCase      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  self._args_parser(*_UpperCamelCase   ,  **_UpperCamelCase      )

												lowerCAmelCase__			  =  super().__call__(_UpperCamelCase   ,  **_UpperCamelCase      )
												if len(_UpperCamelCase      ) == 1:
																		return results[0]
												return results






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase=None   ,  _UpperCamelCase=None   ,  _UpperCamelCase=None   ,  **_UpperCamelCase      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  {}
												if padding is not None:
																		lowerCAmelCase__			  =  padding
												if truncation is not None:
																		lowerCAmelCase__			  =  truncation

												lowerCAmelCase__			  =  {}
												if sequential is not None:
																		lowerCAmelCase__			  =  sequential
												return preprocess_params, forward_params, {}






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase=None   ,  _UpperCamelCase=True   ,  _UpperCamelCase=None      ):






												"""simple docstring"""



												if truncation is None:
																		if self.type == "tapas":
																								lowerCAmelCase__			  =  'drop_rows_to_fit'
																		else:
																								lowerCAmelCase__			  =  'do_not_truncate'

												lowerCAmelCase__  ,				lowerCAmelCase__			  =  pipeline_input['table'], pipeline_input['query']
												if table.empty:
																		raise ValueError('table is empty'      )
												if query is None or query == "":
																		raise ValueError('query is empty'      )
												lowerCAmelCase__			  =  self.tokenizer(_UpperCamelCase   ,  _UpperCamelCase   ,  return_tensors=self.framework   ,  truncation=_UpperCamelCase   ,  padding=_UpperCamelCase      )
												lowerCAmelCase__			  =  table
												return inputs






						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase   ,  _UpperCamelCase=False      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  model_inputs.pop('table'      )

												if self.type == "tapas":
																		if sequential:
																								lowerCAmelCase__			  =  self.sequential_inference(**_UpperCamelCase      )
																		else:
																								lowerCAmelCase__			  =  self.batch_inference(**_UpperCamelCase      )
												else:
																		lowerCAmelCase__			  =  self.model.generate(**_UpperCamelCase      )
												lowerCAmelCase__			  =  {'model_inputs': model_inputs, 'table': table, 'outputs': outputs}
												return model_outputs







						def 					UpperCamelCase__					(		self   ,  _UpperCamelCase      ):






												"""simple docstring"""
												lowerCAmelCase__			  =  model_outputs['model_inputs']
												lowerCAmelCase__			  =  model_outputs['table']
												lowerCAmelCase__			  =  model_outputs['outputs']
												if self.type == "tapas":
																		if self.aggregate:
																								lowerCAmelCase__  ,				lowerCAmelCase__			  =  outputs[:2]
																								lowerCAmelCase__			  =  self.tokenizer.convert_logits_to_predictions(_UpperCamelCase   ,  _UpperCamelCase   ,  _UpperCamelCase      )
																								lowerCAmelCase__  ,				lowerCAmelCase__			  =  predictions
																								lowerCAmelCase__			  =  {i: self.model.config.aggregation_labels[pred] for i, pred in enumerate(_UpperCamelCase      )}

																								lowerCAmelCase__			  =  self.model.config.no_aggregation_label_index
																								lowerCAmelCase__			  =  {
																								    i: aggregators[i] + ' > ' for i, pred in enumerate(_UpperCamelCase      ) if pred != no_agg_label_index
																								}
																		else:
																								lowerCAmelCase__			  =  outputs[0]
																								lowerCAmelCase__			  =  self.tokenizer.convert_logits_to_predictions(_UpperCamelCase   ,  _UpperCamelCase      )
																								lowerCAmelCase__			  =  predictions[0]
																								lowerCAmelCase__			  =  {}
																								lowerCAmelCase__			  =  {}
																		lowerCAmelCase__			  =  []
																		for index, coordinates in enumerate(_UpperCamelCase      ):
																								lowerCAmelCase__			  =  [table.iat[coordinate] for coordinate in coordinates]
																								lowerCAmelCase__			  =  aggregators.get(_UpperCamelCase   ,  ''      )
																								lowerCAmelCase__			  =  aggregators_prefix.get(_UpperCamelCase   ,  ''      )
																								lowerCAmelCase__			  =  {
																								    'answer': aggregator_prefix + ', '.join(_UpperCamelCase      ),
																								    'coordinates': coordinates,
																								    'cells': [table.iat[coordinate] for coordinate in coordinates],
																								}
																								if aggregator:
																														lowerCAmelCase__			  =  aggregator

																								answers.append(_UpperCamelCase      )
																		if len(_UpperCamelCase      ) == 0:
																								raise PipelineException('Empty answer'      )
												else:
																		lowerCAmelCase__			  =  [{'answer': answer} for answer in self.tokenizer.batch_decode(_UpperCamelCase   ,  skip_special_tokens=_UpperCamelCase      )]

												return answers if len(_UpperCamelCase      ) > 1 else answers[0]











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__       (					UpperCamelCase__      ):







     def __init__(	self  ,		A  ,		)  ->    List[Any]:



          '''simple docstring'''



          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.0_2
          a      =      3
          a      =      4
          a      =      None
          a      =      self.vocab_size - 1







     def 			lowerCAmelCase_				(	self )  ->    Union[str, Any]:



          '''simple docstring'''



          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  ,		A  ,		A  ,		A  ,		A  ,		A  ,		*A )  ->    Optional[int]:



          '''simple docstring'''



          a      =      TFCTRLModel(config=A )
          a      =      {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
          a      =      model(A )

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

          a      =      model(A )

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







     def 			lowerCAmelCase_				(	self  ,		A  ,		A  ,		A  ,		A  ,		A  ,		*A )  ->    Tuple:



          '''simple docstring'''



          a      =      TFCTRLLMHeadModel(config=A )
          a      =      {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
          a      =      model(A )
          self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.seq_length, self.vocab_size) )







     def 			lowerCAmelCase_				(	self  ,		A  ,		A  ,		A  ,		A  ,		A  ,		*A )  ->    List[Any]:



          '''simple docstring'''



          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(A )
          a      =      model(A )
          self.parent.assertEqual(result.logits.shape  ,		(self.batch_size, self.num_labels) )







     def 			lowerCAmelCase_				(	self )  ->    Union[str, Any]:



          '''simple docstring'''



          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__       (					UpperCamelCase__   ,					UpperCamelCase__   ,					unittest.TestCase      ):
     a		:			Any							=	(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
     a		:			Optional[Any]							=	(TFCTRLLMHeadModel,) if is_tf_available() else ()
     a		:			List[Any]							=	(
         {
             """feature-extraction""": TFCTRLModel,
             """text-classification""": TFCTRLForSequenceClassification,
             """text-generation""": TFCTRLLMHeadModel,
             """zero-shot""": TFCTRLForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     a		:			Union[str, Any]							=	False
     a		:			str							=	False







     def 			lowerCAmelCase_				(	self  ,		A  ,		A  ,		A  ,		A  ,		A )  ->    List[str]:



          '''simple docstring'''



          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[int]:



          '''simple docstring'''



          a      =      TFCTRLModelTester(self )
          a      =      ConfigTester(self  ,		config_class=A  ,		n_embd=37 )







     def 			lowerCAmelCase_				(	self )  ->    Tuple:



          '''simple docstring'''



          self.config_tester.run_common_tests()







     def 			lowerCAmelCase_				(	self )  ->    Optional[Any]:



          '''simple docstring'''



          a      =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_ctrl_model(*A )







     def 			lowerCAmelCase_				(	self )  ->    List[str]:



          '''simple docstring'''



          a      =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_ctrl_lm_head(*A )







     def 			lowerCAmelCase_				(	self )  ->    Optional[Any]:



          '''simple docstring'''



          a      =      self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_ctrl_for_sequence_classification(*A )







     def 			lowerCAmelCase_				(	self )  ->    Tuple:



          '''simple docstring'''



          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(A )
               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(A  ,		tf.keras.layers.Layer )
                    a      =      model.get_bias()
                    assert isinstance(A  ,		A )
                    for k, v in name.items():
                         assert isinstance(A  ,		tf.Variable )
               elif model_class in list_other_models_with_output_ebd:
                    a      =      model.get_output_embeddings()
                    assert isinstance(A  ,		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 )  ->    Tuple:



          '''simple docstring'''



          for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               a      =      TFCTRLModel.from_pretrained(A )
               self.assertIsNotNone(A )







@require_tf
class 			a__       (					unittest.TestCase      ):







     @slow
     def 			lowerCAmelCase_				(	self )  ->    Any:



          '''simple docstring'''



          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(A  ,		do_sample=A )
          self.assertListEqual(output_ids[0].numpy().tolist()  ,		A )

