
"""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 __lowerCAmelCase						( SCREAMING_SNAKE_CASE__     ):





     def __init__(      self      ,	__UpperCAmelCase      ,	):

      '''simple docstring'''




      __UpperCamelCase							    =			parent
      __UpperCamelCase							    =			13
      __UpperCamelCase							    =			7
      __UpperCamelCase							    =			True
      __UpperCamelCase							    =			True
      __UpperCamelCase							    =			True
      __UpperCamelCase							    =			True
      __UpperCamelCase							    =			True
      __UpperCamelCase							    =			99
      __UpperCamelCase							    =			32
      __UpperCamelCase							    =			2
      __UpperCamelCase							    =			4
      __UpperCamelCase							    =			37
      __UpperCamelCase							    =			'gelu'
      __UpperCamelCase							    =			0.1
      __UpperCamelCase							    =			0.1
      __UpperCamelCase							    =			512
      __UpperCamelCase							    =			16
      __UpperCamelCase							    =			2
      __UpperCamelCase							    =			0.0_2
      __UpperCamelCase							    =			3
      __UpperCamelCase							    =			4
      __UpperCamelCase							    =			None
      __UpperCamelCase							    =			self.vocab_size - 1





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			ids_tensor([self.batch_size, self.seq_length]      ,	self.vocab_size )

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

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

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

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

      __UpperCamelCase							    =			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      ,	)

      __UpperCamelCase							    =			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 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	*__UpperCAmelCase ):

      '''simple docstring'''




      __UpperCamelCase							    =			TFCTRLModel(config=A__ )
      __UpperCamelCase							    =			{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
      __UpperCamelCase							    =			model(A__ )

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

      __UpperCamelCase							    =			model(A__ )

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





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	*__UpperCAmelCase ):

      '''simple docstring'''




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





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	*__UpperCAmelCase ):

      '''simple docstring'''




      __UpperCamelCase							    =			self.num_labels
      __UpperCamelCase							    =			ids_tensor([self.batch_size]      ,	self.type_sequence_label_size )
      __UpperCamelCase							    =			{
          'input_ids': input_ids,
          'token_type_ids': token_type_ids,
          'labels': sequence_labels,
      }
      __UpperCamelCase							    =			TFCTRLForSequenceClassification(A__ )
      __UpperCamelCase							    =			model(A__ )
      self.parent.assertEqual(result.logits.shape      ,	(self.batch_size, self.num_labels) )







     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			self.prepare_config_and_inputs()

      (
          (
          __UpperCamelCase		
      )    ,      (
          __UpperCamelCase		
      )    ,      (
          __UpperCamelCase		
      )    ,      (
          __UpperCamelCase		
      )    ,      (
          __UpperCamelCase		
      )    ,      (
          __UpperCamelCase		
      )    ,      (
          __UpperCamelCase		
      )    ,      (
          __UpperCamelCase		
      )    ,      (
          __UpperCamelCase		
      )    ,      		
      )							    =			config_and_inputs

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


@require_tf
class __lowerCAmelCase						( SCREAMING_SNAKE_CASE__     ,    SCREAMING_SNAKE_CASE__     ,    unittest.TestCase     ):
     lowercase		  =		(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
     lowercase		  =		(TFCTRLLMHeadModel,) if is_tf_available() else ()
     lowercase		  =		(
         {
             '''feature-extraction''': TFCTRLModel,
             '''text-classification''': TFCTRLForSequenceClassification,
             '''text-generation''': TFCTRLLMHeadModel,
             '''zero-shot''': TFCTRLForSequenceClassification,
         }
         if is_tf_available()
         else {}
     )
     lowercase		  =		False
     lowercase		  =		False





     def 		UpperCAmelCase   (      self      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase      ,	__UpperCAmelCase ):

      '''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 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			TFCTRLModelTester(self )
      __UpperCamelCase							    =			ConfigTester(self      ,	config_class=A__      ,	n_embd=37 )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      self.config_tester.run_common_tests()





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_ctrl_model(*A__ )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_ctrl_lm_head(*A__ )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_ctrl_for_sequence_classification(*A__ )





     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase    ,      __UpperCamelCase							    =			self.model_tester.prepare_config_and_inputs_for_common()
      __UpperCamelCase							    =			[TFCTRLLMHeadModel]
      __UpperCamelCase							    =			[TFCTRLForSequenceClassification]

      for model_class in self.all_model_classes:
       __UpperCamelCase							    =			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:
        __UpperCamelCase							    =			model.get_output_embeddings()
        assert isinstance(A__      ,	tf.keras.layers.Layer )
        __UpperCamelCase							    =			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:
        __UpperCamelCase							    =			model.get_output_embeddings()
        assert isinstance(A__      ,	tf.keras.layers.Layer )
        __UpperCamelCase							    =			model.get_bias()
        assert name is None
       else:
        __UpperCamelCase							    =			model.get_output_embeddings()
        assert x is None
        __UpperCamelCase							    =			model.get_bias()
        assert name is None







     @slow
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




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


@require_tf
class __lowerCAmelCase						( unittest.TestCase     ):





     @slow
     def 		UpperCAmelCase   (      self ):

      '''simple docstring'''




      __UpperCamelCase							    =			TFCTRLLMHeadModel.from_pretrained('ctrl' )
      __UpperCamelCase							    =			tf.convert_to_tensor([[1_1859, 0, 1611, 8]]      ,	dtype=tf.intaa )  # Legal the president is
      __UpperCamelCase							    =			[
          1_1859,
          0,
          1611,
          8,
          5,
          150,
          2_6449,
          2,
          19,
          348,
          469,
          3,
          2595,
          48,
          2_0740,
          24_6533,
          24_6533,
          19,
          30,
          5,
      ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

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





# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
import os
from typing import List



def 			UpperCamelCase       (				lowerCAmelCase__					,     lowerCAmelCase__						):
					'''simple docstring'''
					if name != "training_function" and name != "main":
										raise ValueError(f'Incorrect function name passed: {name}, choose either \'main\' or \'training_function\''						)
					lowercase      ,		lowercase		      =		[], False
					for line in lines:
										if not found_start and f'def {name}' in line:
															lowercase		      =		True
															good_lines.append(lowerCAmelCase__						)
															continue
										if found_start:
															if name == "training_function" and "def main" in line:
																				return good_lines
															if name == "main" and "if __name__" in line:
																				return good_lines
															good_lines.append(lowerCAmelCase__						)



def 			UpperCamelCase       (				lowerCAmelCase__						):
					'''simple docstring'''
					return [line for line in lines if not line.lstrip().startswith('''#'''						) and line != "\n"]



def 			UpperCamelCase       (				lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__					,     lowerCAmelCase__ = None						):
					'''simple docstring'''
					with open(lowerCAmelCase__					,     '''r'''						) as f:
										lowercase		      =		f.readlines()
					with open(os.path.abspath(os.path.join('''examples'''					,     '''nlp_example.py'''						)						)					,     '''r'''						) as f:
										lowercase		      =		f.readlines()
					with open(lowerCAmelCase__					,     '''r'''						) as f:
										lowercase		      =		f.readlines()
					if secondary_filename is not None:
										with open(lowerCAmelCase__					,     '''r'''						) as f:
															lowercase		      =		f.readlines()

    # This is our base, we remove all the code from here in our `full_filename` and `feature_filename` to find the new content
					if parser_only:
										lowercase		      =		clean_lines(get_function_contents_by_name(lowerCAmelCase__					,     '''main'''						)						)
										lowercase		      =		clean_lines(get_function_contents_by_name(lowerCAmelCase__					,     '''main'''						)						)
										lowercase		      =		clean_lines(get_function_contents_by_name(lowerCAmelCase__					,     '''main'''						)						)
										if secondary_filename is not None:
															lowercase		      =		clean_lines(get_function_contents_by_name(lowerCAmelCase__					,     '''main'''						)						)
					else:
										lowercase		      =		clean_lines(get_function_contents_by_name(lowerCAmelCase__					,     '''training_function'''						)						)
										lowercase		      =		clean_lines(get_function_contents_by_name(lowerCAmelCase__					,     '''training_function'''						)						)
										lowercase		      =		clean_lines(get_function_contents_by_name(lowerCAmelCase__					,     '''training_function'''						)						)
										if secondary_filename is not None:
															lowercase		      =		clean_lines(
															    get_function_contents_by_name(lowerCAmelCase__					,     '''training_function'''						)						)

					lowercase		      =		'''train_dataloader, eval_dataloader = get_dataloaders(accelerator, batch_size)\n'''

					# Specific code in our script that differs from the full version, aka what is new
					lowercase		      =		[]
					lowercase		      =		[]  # We keep track of the idxs just in case it's a repeated statement
					lowercase		      =		iter(lowerCAmelCase__						)
					for i in range(len(lowerCAmelCase__						) - 1						):
										if i not in passed_idxs:
															lowercase		      =		next(lowerCAmelCase__						)
															if (line not in full_file_func) and (line.lstrip() != _dl_line):
																				if "TESTING_MOCKED_DATALOADERS" not in line:
																									new_feature_code.append(lowerCAmelCase__						)
																									passed_idxs.append(lowerCAmelCase__						)
																				else:
																									# Skip over the `config['num_epochs'] = 2` statement
																									lowercase		      =		next(lowerCAmelCase__						)

    # Extract out just the new parts from the full_file_training_func
					lowercase		      =		[]
					lowercase		      =		[]  # We keep track of the idxs just in case it's a repeated statement
					for i, line in enumerate(lowerCAmelCase__						):
										if i not in passed_idxs:
															if (line not in full_file_func) and (line.lstrip() != _dl_line):
																				if "TESTING_MOCKED_DATALOADERS" not in line:
																									new_full_example_parts.append(lowerCAmelCase__						)
																									passed_idxs.append(lowerCAmelCase__						)

    # Finally, get the overall diff
					lowercase		      =		[line for line in new_feature_code if line not in new_full_example_parts]
					if secondary_filename is not None:
										lowercase		      =		[line for line in full_file_contents if line not in secondary_file_func]
										lowercase		      =		[line for line in diff_from_example if line not in diff_from_two]

					return diff_from_example




