



"""simple docstring"""







from __future__ import annotations

import os
import tempfile
import unittest

from transformers import ConvBertConfig, 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 import (
			    TFConvBertForMaskedLM,
			    TFConvBertForMultipleChoice,
			    TFConvBertForQuestionAnswering,
			    TFConvBertForSequenceClassification,
			    TFConvBertForTokenClassification,
			    TFConvBertModel,
			)

class       _a  :



			def __init__(			self							:				Union[str, Any]   ,	_lowercase							:				int   ,	_lowercase							:				Optional[Any]=13   ,	_lowercase							:				List[Any]=7   ,	_lowercase							:				str=True   ,	_lowercase							:				Tuple=True   ,	_lowercase							:				List[str]=True   ,	_lowercase							:				Optional[Any]=True   ,	_lowercase							:				List[Any]=99   ,	_lowercase							:				Optional[int]=32   ,	_lowercase							:				Dict=2   ,	_lowercase							:				str=4   ,	_lowercase							:				Union[str, Any]=37   ,	_lowercase							:				int="gelu"   ,	_lowercase							:				Tuple=0.1   ,	_lowercase							:				int=0.1   ,	_lowercase							:				Tuple=512   ,	_lowercase							:				str=16   ,	_lowercase							:				int=2   ,	_lowercase							:				Tuple=0.02   ,	_lowercase							:				List[Any]=3   ,	_lowercase							:				Tuple=4   ,	_lowercase							:				Tuple=None   ,	)							->					Tuple:
									snake_case   :			Dict						    = parent
									snake_case   :			List[Any]						    = 13
									snake_case   :			Tuple						    = 7
									snake_case   :			Optional[Any]						    = True
									snake_case   :			Optional[int]						    = True
									snake_case   :			Tuple						    = True
									snake_case   :			int						    = True
									snake_case   :			List[Any]						    = 99
									snake_case   :			Tuple						    = 384
									snake_case   :			Union[str, Any]						    = 2
									snake_case   :			Dict						    = 4
									snake_case   :			Optional[int]						    = 37
									snake_case   :			List[Any]						    = "gelu"
									snake_case   :			Union[str, Any]						    = 0.1
									snake_case   :			Optional[Any]						    = 0.1
									snake_case   :			int						    = 512
									snake_case   :			List[Any]						    = 16
									snake_case   :			int						    = 2
									snake_case   :			Any						    = 0.02
									snake_case   :			List[Any]						    = 3
									snake_case   :			Dict						    = 4
									snake_case   :			Any						    = 128
									snake_case   :			str						    = 2
									snake_case   :			Optional[Any]						    = 9
									snake_case   :			List[Any]						    = 1
									snake_case   :			List[Any]						    = None



			def 				__lowercase					(			self							:				List[Any]		)							->					Optional[int]:
									snake_case   :			Optional[Any]						    = ids_tensor([self.batch_size, self.seq_length]   ,	self.vocab_size		)

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

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

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

									snake_case   :			Union[str, Any]						    = ConvBertConfig(
									    vocab_size=self.vocab_size   ,	hidden_size=self.hidden_size   ,	num_hidden_layers=self.num_hidden_layers   ,	num_attention_heads=self.num_attention_heads   ,	intermediate_size=self.intermediate_size   ,	hidden_act=self.hidden_act   ,	hidden_dropout_prob=self.hidden_dropout_prob   ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,	max_position_embeddings=self.max_position_embeddings   ,	type_vocab_size=self.type_vocab_size   ,	initializer_range=self.initializer_range   ,	return_dict=_lowercase   ,	)

									return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



			def 				__lowercase					(			self							:				int   ,	_lowercase							:				Any   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				Optional[int]   ,	_lowercase							:				int   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				int   ,	_lowercase							:				Optional[int]		)							->					List[Any]:
									snake_case   :			Optional[Any]						    = TFConvBertModel(config=_lowercase		)
									snake_case   :			Any						    = {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}

									snake_case   :			List[str]						    = [input_ids, input_mask]
									snake_case   :			List[str]						    = model(_lowercase		)

									snake_case   :			Optional[Any]						    = model(_lowercase		)

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



			def 				__lowercase					(			self							:				str   ,	_lowercase							:				List[Any]   ,	_lowercase							:				Tuple   ,	_lowercase							:				int   ,	_lowercase							:				int   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				Optional[int]   ,	_lowercase							:				Optional[int]		)							->					str:
									snake_case   :			Tuple						    = TFConvBertForMaskedLM(config=_lowercase		)
									snake_case   :			List[Any]						    = {
									    "input_ids": input_ids,
									    "attention_mask": input_mask,
									    "token_type_ids": token_type_ids,
									}
									snake_case   :			Optional[int]						    = model(_lowercase		)
									self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.seq_length, self.vocab_size)		)



			def 				__lowercase					(			self							:				Optional[Any]   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				Dict   ,	_lowercase							:				List[str]   ,	_lowercase							:				Dict   ,	_lowercase							:				Dict   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				Union[str, Any]		)							->					Dict:
									snake_case   :			str						    = self.num_labels
									snake_case   :			Tuple						    = TFConvBertForSequenceClassification(config=_lowercase		)
									snake_case   :			Union[str, Any]						    = {
									    "input_ids": input_ids,
									    "attention_mask": input_mask,
									    "token_type_ids": token_type_ids,
									}

									snake_case   :			Union[str, Any]						    = model(_lowercase		)
									self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.num_labels)		)



			def 				__lowercase					(			self							:				List[Any]   ,	_lowercase							:				Optional[int]   ,	_lowercase							:				List[Any]   ,	_lowercase							:				Any   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				Any   ,	_lowercase							:				Optional[int]   ,	_lowercase							:				Optional[Any]		)							->					Optional[int]:
									snake_case   :			Any						    = self.num_choices
									snake_case   :			str						    = TFConvBertForMultipleChoice(config=_lowercase		)
									snake_case   :			List[str]						    = tf.tile(tf.expand_dims(_lowercase   ,	1		)   ,	(1, self.num_choices, 1)		)
									snake_case   :			List[Any]						    = tf.tile(tf.expand_dims(_lowercase   ,	1		)   ,	(1, self.num_choices, 1)		)
									snake_case   :			str						    = tf.tile(tf.expand_dims(_lowercase   ,	1		)   ,	(1, self.num_choices, 1)		)
									snake_case   :			Optional[int]						    = {
									    "input_ids": multiple_choice_inputs_ids,
									    "attention_mask": multiple_choice_input_mask,
									    "token_type_ids": multiple_choice_token_type_ids,
									}
									snake_case   :			str						    = model(_lowercase		)
									self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.num_choices)		)



			def 				__lowercase					(			self							:				Tuple   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				List[Any]   ,	_lowercase							:				Union[str, Any]   ,	_lowercase							:				Tuple   ,	_lowercase							:				Tuple		)							->					List[Any]:
									snake_case   :			Tuple						    = self.num_labels
									snake_case   :			Optional[int]						    = TFConvBertForTokenClassification(config=_lowercase		)
									snake_case   :			Any						    = {
									    "input_ids": input_ids,
									    "attention_mask": input_mask,
									    "token_type_ids": token_type_ids,
									}
									snake_case   :			Tuple						    = model(_lowercase		)
									self.parent.assertEqual(result.logits.shape   ,	(self.batch_size, self.seq_length, self.num_labels)		)



			def 				__lowercase					(			self							:				List[str]   ,	_lowercase							:				Tuple   ,	_lowercase							:				List[Any]   ,	_lowercase							:				Any   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				str   ,	_lowercase							:				Optional[Any]   ,	_lowercase							:				Optional[Any]		)							->					Any:
									snake_case   :			Tuple						    = TFConvBertForQuestionAnswering(config=_lowercase		)
									snake_case   :			Tuple						    = {
									    "input_ids": input_ids,
									    "attention_mask": input_mask,
									    "token_type_ids": token_type_ids,
									}

									snake_case   :			Tuple						    = model(_lowercase		)
									self.parent.assertEqual(result.start_logits.shape   ,	(self.batch_size, self.seq_length)		)
									self.parent.assertEqual(result.end_logits.shape   ,	(self.batch_size, self.seq_length)		)



			def 				__lowercase					(			self							:				Tuple		)							->					Optional[int]:
									snake_case   :			Tuple						    = self.prepare_config_and_inputs()
									(
									    (
									    snake_case
									)    ,			(
									    snake_case
									)    ,			(
									    snake_case
									)    ,			(
									    snake_case
									)    ,			(
									    snake_case
									)    ,			(
									    snake_case
									)    ,			(
									    snake_case
									)    ,			
									)   :			List[str]						    = config_and_inputs
									snake_case   :			str						    = {"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):
			__magic_name__     					=     (
			    (
			        TFConvBertModel,
			        TFConvBertForMaskedLM,
			        TFConvBertForQuestionAnswering,
			        TFConvBertForSequenceClassification,
			        TFConvBertForTokenClassification,
			        TFConvBertForMultipleChoice,
			    )
			    if is_tf_available()
			    else ()
			)
			__magic_name__     					=     (
			    {
			        """feature-extraction""": TFConvBertModel,
			        """fill-mask""": TFConvBertForMaskedLM,
			        """question-answering""": TFConvBertForQuestionAnswering,
			        """text-classification""": TFConvBertForSequenceClassification,
			        """token-classification""": TFConvBertForTokenClassification,
			        """zero-shot""": TFConvBertForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			__magic_name__     					=     False
			__magic_name__     					=     False
			__magic_name__     					=     False



			def 				__lowercase					(			self							:				Tuple		)							->					Dict:
									snake_case   :			Optional[int]						    = TFConvBertModelTester(self		)
									snake_case   :			int						    = ConfigTester(self   ,	config_class=_lowercase   ,	hidden_size=37		)



			def 				__lowercase					(			self							:				Any		)							->					List[str]:
									self.config_tester.run_common_tests()



			def 				__lowercase					(			self							:				Dict		)							->					Any:
									snake_case   :			int						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*_lowercase		)



			def 				__lowercase					(			self							:				Any		)							->					int:
									snake_case   :			Optional[int]						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_masked_lm(*_lowercase		)



			def 				__lowercase					(			self							:				int		)							->					Optional[int]:
									snake_case   :			Tuple						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_multiple_choice(*_lowercase		)



			def 				__lowercase					(			self							:				Union[str, Any]		)							->					Optional[Any]:
									snake_case   :			Dict						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_question_answering(*_lowercase		)



			def 				__lowercase					(			self							:				int		)							->					Dict:
									snake_case   :			List[str]						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_sequence_classification(*_lowercase		)



			def 				__lowercase					(			self							:				Any		)							->					Tuple:
									snake_case   :			Union[str, Any]						    = self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_token_classification(*_lowercase		)



			@slow
			def 				__lowercase					(			self							:				List[str]		)							->					int:
									snake_case    ,			snake_case   :			Optional[int]						    = self.model_tester.prepare_config_and_inputs_for_common()
									snake_case   :			str						    = True
									snake_case   :			str						    = True

									if hasattr(_lowercase   ,	"use_cache"		):
															snake_case   :			Dict						    = True

									snake_case   :			Dict						    = getattr(self.model_tester   ,	"encoder_seq_length"   ,	self.model_tester.seq_length		)
									snake_case   :			Optional[Any]						    = getattr(self.model_tester   ,	"key_length"   ,	_lowercase		)

									for model_class in self.all_model_classes:
															snake_case   :			Dict						    = self._prepare_for_class(_lowercase   ,	_lowercase		)
															snake_case   :			Any						    = model_class(_lowercase		)
															snake_case   :			str						    = len(model(_lowercase		)		)

															with tempfile.TemporaryDirectory() as tmpdirname:
																					model.save_pretrained(_lowercase   ,	saved_model=_lowercase		)
																					snake_case   :			List[Any]						    = os.path.join(_lowercase   ,	"saved_model"   ,	"1"		)
																					snake_case   :			str						    = tf.keras.models.load_model(_lowercase		)
																					snake_case   :			Optional[Any]						    = model(_lowercase		)

																					if self.is_encoder_decoder:
																											snake_case   :			Optional[Any]						    = outputs["encoder_hidden_states"]
																											snake_case   :			Optional[Any]						    = outputs["encoder_attentions"]
																					else:
																											snake_case   :			List[str]						    = outputs["hidden_states"]
																											snake_case   :			List[str]						    = outputs["attentions"]

																					self.assertEqual(len(_lowercase		)   ,	_lowercase		)

																					snake_case   :			Optional[Any]						    = getattr(
																					    self.model_tester   ,	"expected_num_hidden_layers"   ,	self.model_tester.num_hidden_layers + 1		)

																					self.assertEqual(len(_lowercase		)   ,	_lowercase		)
																					self.assertListEqual(
																					    list(output_hidden_states[0].shape[-2:]		)   ,	[self.model_tester.seq_length, self.model_tester.hidden_size]   ,	)

																					self.assertEqual(len(_lowercase		)   ,	self.model_tester.num_hidden_layers		)
																					self.assertListEqual(
																					    list(output_attentions[0].shape[-3:]		)   ,	[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]   ,	)



			@slow
			def 				__lowercase					(			self							:				Optional[int]		)							->					Any:
									snake_case   :			Optional[Any]						    = TFConvBertModel.from_pretrained("YituTech/conv-bert-base"		)
									self.assertIsNotNone(_lowercase		)



			def 				__lowercase					(			self							:				Optional[int]		)							->					Optional[int]:
									snake_case    ,			snake_case   :			List[str]						    = self.model_tester.prepare_config_and_inputs_for_common()
									snake_case   :			Optional[Any]						    = True
									snake_case   :			str						    = getattr(self.model_tester   ,	"decoder_seq_length"   ,	self.model_tester.seq_length		)
									snake_case   :			int						    = getattr(self.model_tester   ,	"encoder_seq_length"   ,	self.model_tester.seq_length		)
									snake_case   :			Tuple						    = getattr(self.model_tester   ,	"key_length"   ,	_lowercase		)
									snake_case   :			Optional[int]						    = getattr(self.model_tester   ,	"key_length"   ,	_lowercase		)

									def check_decoder_attentions_output(_lowercase							:				Optional[int]		):
															snake_case   :			Dict						    = len(_lowercase		)
															self.assertEqual(out_len % 2   ,	0		)
															snake_case   :			Dict						    = outputs.decoder_attentions
															self.assertEqual(len(_lowercase		)   ,	self.model_tester.num_hidden_layers		)
															self.assertListEqual(
															    list(decoder_attentions[0].shape[-3:]		)   ,	[self.model_tester.num_attention_heads / 2, decoder_seq_length, decoder_key_length]   ,	)

									def check_encoder_attentions_output(_lowercase							:				int		):
															snake_case   :			List[Any]						    = [
															    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
															]
															self.assertEqual(len(_lowercase		)   ,	self.model_tester.num_hidden_layers		)
															self.assertListEqual(
															    list(attentions[0].shape[-3:]		)   ,	[self.model_tester.num_attention_heads / 2, encoder_seq_length, encoder_key_length]   ,	)

									for model_class in self.all_model_classes:
															snake_case   :			Dict						    = True
															snake_case   :			Union[str, Any]						    = False
															snake_case   :			str						    = model_class(_lowercase		)
															snake_case   :			Optional[Any]						    = model(self._prepare_for_class(_lowercase   ,	_lowercase		)		)
															snake_case   :			int						    = len(_lowercase		)
															self.assertEqual(config.output_hidden_states   ,	_lowercase		)
															check_encoder_attentions_output(_lowercase		)

															if self.is_encoder_decoder:
																					snake_case   :			List[Any]						    = model_class(_lowercase		)
																					snake_case   :			Tuple						    = model(self._prepare_for_class(_lowercase   ,	_lowercase		)		)
																					self.assertEqual(config.output_hidden_states   ,	_lowercase		)
																					check_decoder_attentions_output(_lowercase		)

															# Check that output attentions can also be changed via the config
															del inputs_dict["output_attentions"]
															snake_case   :			Dict						    = True
															snake_case   :			Optional[Any]						    = model_class(_lowercase		)
															snake_case   :			Tuple						    = model(self._prepare_for_class(_lowercase   ,	_lowercase		)		)
															self.assertEqual(config.output_hidden_states   ,	_lowercase		)
															check_encoder_attentions_output(_lowercase		)

															# Check attention is always last and order is fine
															snake_case   :			Tuple						    = True
															snake_case   :			Tuple						    = True
															snake_case   :			Optional[int]						    = model_class(_lowercase		)
															snake_case   :			Union[str, Any]						    = model(self._prepare_for_class(_lowercase   ,	_lowercase		)		)

															self.assertEqual(out_len + (2 if self.is_encoder_decoder else 1)   ,	len(_lowercase		)		)
															self.assertEqual(model.config.output_hidden_states   ,	_lowercase		)
															check_encoder_attentions_output(_lowercase		)



@require_tf
class       _a  ( unittest.TestCase):



			@slow
			def 				__lowercase					(			self							:				int		)							->					List[Any]:
									snake_case   :			Optional[Any]						    = TFConvBertModel.from_pretrained("YituTech/conv-bert-base"		)
									snake_case   :			int						    = tf.constant([[0, 1, 2, 3, 4, 5]]		)
									snake_case   :			Dict						    = model(_lowercase		)[0]

									snake_case   :			Tuple						    = [1, 6, 768]
									self.assertEqual(output.shape   ,	_lowercase		)

									snake_case   :			str						    = tf.constant(
									    [
									        [
									            [-0.03475493, -0.4686034, -0.30638832],
									            [0.22637248, -0.26988646, -0.7423424],
									            [0.10324868, -0.45013508, -0.58280784],
									        ]
									    ]		)
									tf.debugging.assert_near(output[:, :3, :3]   ,	_lowercase   ,	atol=1E-4		)








"""simple docstring"""







# Copyright 2023 The HuggingFace Inc. 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 re

from ..utils import cached_file


# docstyle-ignore
A		=						'\nHuman: <<task>>\n\nAssistant: '


A		=						'huggingface-tools/default-prompts'
A		=						{'chat': 'chat_prompt_template.txt', 'run': 'run_prompt_template.txt'}

def  __SCREAMING_SNAKE_CASE		(			lowerCamelCase_:							Optional[int]     ,		lowerCamelCase_:							str     ,		lowerCamelCase_:							Tuple="run" ):
						"""simple docstring"""






						if prompt_or_repo_id is None:
												snake_case   :			Any						    = DEFAULT_PROMPTS_REPO

						# prompt is considered a repo ID when it does not contain any kind of space
						if re.search("\\s"     ,		lowerCamelCase_ ) is not None:
												return prompt_or_repo_id

						snake_case   :			Optional[int]						    = cached_file(
						    lowerCamelCase_     ,		PROMPT_FILES[mode]     ,		repo_type="dataset"     ,		user_agent={"agent": agent_name} )
						with open(lowerCamelCase_     ,		"r"     ,		encoding="utf-8" ) as f:
												return f.read()



