



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 	__snake_case   :
		def __init__(							self    ,a_    ,a_=13    ,a_=7    ,a_=True    ,a_=True    ,a_=True    ,a_=True    ,a_=99    ,a_=32    ,a_=2    ,a_=4    ,a_=37    ,a_="gelu"    ,a_=0.1    ,a_=0.1    ,a_=512    ,a_=16    ,a_=2    ,a_=0.02    ,a_=3    ,a_=4    ,a_=None    ,):







						"""simple docstring"""
						lowerCAmelCase__		  =  parent
						lowerCAmelCase__		  =  13
						lowerCAmelCase__		  =  7
						lowerCAmelCase__		  =  True
						lowerCAmelCase__		  =  True
						lowerCAmelCase__		  =  True
						lowerCAmelCase__		  =  True
						lowerCAmelCase__		  =  99
						lowerCAmelCase__		  =  384
						lowerCAmelCase__		  =  2
						lowerCAmelCase__		  =  4
						lowerCAmelCase__		  =  37
						lowerCAmelCase__		  =  """gelu"""
						lowerCAmelCase__		  =  0.1
						lowerCAmelCase__		  =  0.1
						lowerCAmelCase__		  =  512
						lowerCAmelCase__		  =  16
						lowerCAmelCase__		  =  2
						lowerCAmelCase__		  =  0.02
						lowerCAmelCase__		  =  3
						lowerCAmelCase__		  =  4
						lowerCAmelCase__		  =  128
						lowerCAmelCase__		  =  2
						lowerCAmelCase__		  =  9
						lowerCAmelCase__		  =  1
						lowerCAmelCase__		  =  None
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  ids_tensor([self.batch_size, self.seq_length]    ,self.vocab_size						)

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

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

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

						lowerCAmelCase__		  =  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=a_    ,)

						return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  TFConvBertModel(config=a_						)
						lowerCAmelCase__		  =  {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

						lowerCAmelCase__		  =  [input_ids, input_mask]
						lowerCAmelCase__		  =  model(a_						)

						lowerCAmelCase__		  =  model(a_						)

						self.parent.assertEqual(result.last_hidden_state.shape    ,(self.batch_size, self.seq_length, self.hidden_size)						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  TFConvBertForMaskedLM(config=a_						)
						lowerCAmelCase__		  =  {
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}
						lowerCAmelCase__		  =  model(a_						)
						self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.seq_length, self.vocab_size)						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.num_labels
						lowerCAmelCase__		  =  TFConvBertForSequenceClassification(config=a_						)
						lowerCAmelCase__		  =  {
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}

						lowerCAmelCase__		  =  model(a_						)
						self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.num_labels)						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.num_choices
						lowerCAmelCase__		  =  TFConvBertForMultipleChoice(config=a_						)
						lowerCAmelCase__		  =  tf.tile(tf.expand_dims(a_    ,1						)    ,(1, self.num_choices, 1)						)
						lowerCAmelCase__		  =  tf.tile(tf.expand_dims(a_    ,1						)    ,(1, self.num_choices, 1)						)
						lowerCAmelCase__		  =  tf.tile(tf.expand_dims(a_    ,1						)    ,(1, self.num_choices, 1)						)
						lowerCAmelCase__		  =  {
						    """input_ids""": multiple_choice_inputs_ids,
						    """attention_mask""": multiple_choice_input_mask,
						    """token_type_ids""": multiple_choice_token_type_ids,
						}
						lowerCAmelCase__		  =  model(a_						)
						self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.num_choices)						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.num_labels
						lowerCAmelCase__		  =  TFConvBertForTokenClassification(config=a_						)
						lowerCAmelCase__		  =  {
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}
						lowerCAmelCase__		  =  model(a_						)
						self.parent.assertEqual(result.logits.shape    ,(self.batch_size, self.seq_length, self.num_labels)						)
		def 						SCREAMING_SNAKE_CASE_					(							self    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_    ,a_						):







						"""simple docstring"""
						lowerCAmelCase__		  =  TFConvBertForQuestionAnswering(config=a_						)
						lowerCAmelCase__		  =  {
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}

						lowerCAmelCase__		  =  model(a_						)
						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 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.prepare_config_and_inputs()
						(
						    lowerCAmelCase__    
						)		  =  config_and_inputs
						lowerCAmelCase__		  =  {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """attention_mask""": input_mask}
						return config, inputs_dict



@require_tf
class 	__snake_case   ( A_							, A_							, unittest.TestCase ):
		SCREAMING_SNAKE_CASE__								=	(
		    (
		        TFConvBertModel,
		        TFConvBertForMaskedLM,
		        TFConvBertForQuestionAnswering,
		        TFConvBertForSequenceClassification,
		        TFConvBertForTokenClassification,
		        TFConvBertForMultipleChoice,
		    )
		    if is_tf_available()
		    else ()
		)
		SCREAMING_SNAKE_CASE__								=	(
		    {
		        'feature-extraction': TFConvBertModel,
		        'fill-mask': TFConvBertForMaskedLM,
		        'question-answering': TFConvBertForQuestionAnswering,
		        'text-classification': TFConvBertForSequenceClassification,
		        'token-classification': TFConvBertForTokenClassification,
		        'zero-shot': TFConvBertForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		SCREAMING_SNAKE_CASE__								=	False
		SCREAMING_SNAKE_CASE__								=	False
		SCREAMING_SNAKE_CASE__								=	False
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  TFConvBertModelTester(self						)
						lowerCAmelCase__		  =  ConfigTester(self    ,config_class=a_    ,hidden_size=37						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						self.config_tester.run_common_tests()
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(*a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_masked_lm(*a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_multiple_choice(*a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_question_answering(*a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_sequence_classification(*a_						)
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_token_classification(*a_						)
		@slow
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs_for_common()
						lowerCAmelCase__		  =  True
						lowerCAmelCase__		  =  True

						if hasattr(a_    ,'use_cache'						):
										lowerCAmelCase__		  =  True

						lowerCAmelCase__		  =  getattr(self.model_tester    ,'encoder_seq_length'    ,self.model_tester.seq_length						)
						lowerCAmelCase__		  =  getattr(self.model_tester    ,'key_length'    ,a_						)

						for model_class in self.all_model_classes:
										lowerCAmelCase__		  =  self._prepare_for_class(a_    ,a_						)
										lowerCAmelCase__		  =  model_class(a_						)
										lowerCAmelCase__		  =  len(model(a_						)						)

										with tempfile.TemporaryDirectory() as tmpdirname:
														model.save_pretrained(a_    ,saved_model=a_						)
														lowerCAmelCase__		  =  os.path.join(a_    ,'saved_model'    ,'1'						)
														lowerCAmelCase__		  =  tf.keras.models.load_model(a_						)
														lowerCAmelCase__		  =  model(a_						)

														if self.is_encoder_decoder:
																		lowerCAmelCase__		  =  outputs["""encoder_hidden_states"""]
																		lowerCAmelCase__		  =  outputs["""encoder_attentions"""]
														else:
																		lowerCAmelCase__		  =  outputs["""hidden_states"""]
																		lowerCAmelCase__		  =  outputs["""attentions"""]

														self.assertEqual(len(a_						)    ,a_						)

														lowerCAmelCase__		  =  getattr(
														    self.model_tester    ,'expected_num_hidden_layers'    ,self.model_tester.num_hidden_layers + 1						)

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

														self.assertEqual(len(a_						)    ,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 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  TFConvBertModel.from_pretrained('YituTech/conv-bert-base'						)
						self.assertIsNotNone(a_						)



		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  self.model_tester.prepare_config_and_inputs_for_common()
						lowerCAmelCase__		  =  True
						lowerCAmelCase__		  =  getattr(self.model_tester    ,'decoder_seq_length'    ,self.model_tester.seq_length						)
						lowerCAmelCase__		  =  getattr(self.model_tester    ,'encoder_seq_length'    ,self.model_tester.seq_length						)
						lowerCAmelCase__		  =  getattr(self.model_tester    ,'key_length'    ,a_						)
						lowerCAmelCase__		  =  getattr(self.model_tester    ,'key_length'    ,a_						)

						def check_decoder_attentions_output(a_						):
										lowerCAmelCase__		  =  len(a_						)
										self.assertEqual(out_len % 2    ,0						)
										lowerCAmelCase__		  =  outputs.decoder_attentions
										self.assertEqual(len(a_						)    ,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(a_						):
										lowerCAmelCase__		  =  [
										    t.numpy() for t in (outputs.encoder_attentions if config.is_encoder_decoder else outputs.attentions)
										]
										self.assertEqual(len(a_						)    ,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:
										lowerCAmelCase__		  =  True
										lowerCAmelCase__		  =  False
										lowerCAmelCase__		  =  model_class(a_						)
										lowerCAmelCase__		  =  model(self._prepare_for_class(a_    ,a_						)						)
										lowerCAmelCase__		  =  len(a_						)
										self.assertEqual(config.output_hidden_states    ,a_						)
										check_encoder_attentions_output(a_						)

										if self.is_encoder_decoder:
														lowerCAmelCase__		  =  model_class(a_						)
														lowerCAmelCase__		  =  model(self._prepare_for_class(a_    ,a_						)						)
														self.assertEqual(config.output_hidden_states    ,a_						)
														check_decoder_attentions_output(a_						)

										# Check that output attentions can also be changed via the config
										del inputs_dict["output_attentions"]
										lowerCAmelCase__		  =  True
										lowerCAmelCase__		  =  model_class(a_						)
										lowerCAmelCase__		  =  model(self._prepare_for_class(a_    ,a_						)						)
										self.assertEqual(config.output_hidden_states    ,a_						)
										check_encoder_attentions_output(a_						)

										# Check attention is always last and order is fine
										lowerCAmelCase__		  =  True
										lowerCAmelCase__		  =  True
										lowerCAmelCase__		  =  model_class(a_						)
										lowerCAmelCase__		  =  model(self._prepare_for_class(a_    ,a_						)						)

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



@require_tf
class 	__snake_case   ( unittest.TestCase ):
		@slow
		def 						SCREAMING_SNAKE_CASE_					(							self						):







						"""simple docstring"""
						lowerCAmelCase__		  =  TFConvBertModel.from_pretrained('YituTech/conv-bert-base'						)
						lowerCAmelCase__		  =  tf.constant([[0, 1, 2, 3, 4, 5]]						)
						lowerCAmelCase__		  =  model(a_						)[0]

						lowerCAmelCase__		  =  [1, 6, 768]
						self.assertEqual(output.shape    ,a_						)

						lowerCAmelCase__		  =  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]    ,a_    ,atol=1e-4						)




"""simple docstring"""
# limitations under the License.

# NOTE: This file is deprecated and will be removed in a future version.
# It only exists so that temporarely `from diffusers.pipelines import DiffusionPipeline` works

from .pipelines import DiffusionPipeline, ImagePipelineOutput  # noqa: F401
from .utils import deprecate


deprecate(
    """pipelines_utils""",
    """0.22.0""",
    """Importing `DiffusionPipeline` or `ImagePipelineOutput` from diffusers.pipeline_utils is deprecated. Please import from diffusers.pipelines.pipeline_utils instead.""",
    standard_warn=False,
    stacklevel=3,
)

