"""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						,			__lowerCAmelCase						,			__lowerCAmelCase=13						,			__lowerCAmelCase=7						,			__lowerCAmelCase=True						,			__lowerCAmelCase=True						,			__lowerCAmelCase=True						,			__lowerCAmelCase=True						,			__lowerCAmelCase=99						,			__lowerCAmelCase=32						,			__lowerCAmelCase=2						,			__lowerCAmelCase=4						,			__lowerCAmelCase=37						,			__lowerCAmelCase="gelu"						,			__lowerCAmelCase=0.1						,			__lowerCAmelCase=0.1						,			__lowerCAmelCase=512						,			__lowerCAmelCase=16						,			__lowerCAmelCase=2						,			__lowerCAmelCase=0.0_2						,			__lowerCAmelCase=3						,			__lowerCAmelCase=4						,			__lowerCAmelCase=None						,			):







            """simple docstring"""



            lowercase								=      parent
            lowercase								=      13
            lowercase								=      7
            lowercase								=      True
            lowercase								=      True
            lowercase								=      True
            lowercase								=      True
            lowercase								=      99
            lowercase								=      384
            lowercase								=      2
            lowercase								=      4
            lowercase								=      37
            lowercase								=      """gelu"""
            lowercase								=      0.1
            lowercase								=      0.1
            lowercase								=      512
            lowercase								=      16
            lowercase								=      2
            lowercase								=      0.0_2
            lowercase								=      3
            lowercase								=      4
            lowercase								=      128
            lowercase								=      2
            lowercase								=      9
            lowercase								=      1
            lowercase								=      None

      def 							A__			(		self		):







            """simple docstring"""



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

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

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

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

            lowercase								=      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=__lowerCAmelCase						,			)

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

      def 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      TFConvBertModel(config=__lowerCAmelCase		)
            lowercase								=      {"""input_ids""": input_ids, """attention_mask""": input_mask, """token_type_ids""": token_type_ids}

            lowercase								=      [input_ids, input_mask]
            lowercase								=      model(__lowerCAmelCase		)

            lowercase								=      model(__lowerCAmelCase		)

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

      def 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase		):







            """simple docstring"""



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

      def 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      self.num_labels
            lowercase								=      TFConvBertForSequenceClassification(config=__lowerCAmelCase		)
            lowercase								=      {
                """input_ids""": input_ids,
                """attention_mask""": input_mask,
                """token_type_ids""": token_type_ids,
            }

            lowercase								=      model(__lowerCAmelCase		)
            self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.num_labels)		)

      def 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      self.num_choices
            lowercase								=      TFConvBertForMultipleChoice(config=__lowerCAmelCase		)
            lowercase								=      tf.tile(tf.expand_dims(__lowerCAmelCase						,			1		)						,			(1, self.num_choices, 1)		)
            lowercase								=      tf.tile(tf.expand_dims(__lowerCAmelCase						,			1		)						,			(1, self.num_choices, 1)		)
            lowercase								=      tf.tile(tf.expand_dims(__lowerCAmelCase						,			1		)						,			(1, self.num_choices, 1)		)
            lowercase								=      {
                """input_ids""": multiple_choice_inputs_ids,
                """attention_mask""": multiple_choice_input_mask,
                """token_type_ids""": multiple_choice_token_type_ids,
            }
            lowercase								=      model(__lowerCAmelCase		)
            self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.num_choices)		)

      def 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      self.num_labels
            lowercase								=      TFConvBertForTokenClassification(config=__lowerCAmelCase		)
            lowercase								=      {
                """input_ids""": input_ids,
                """attention_mask""": input_mask,
                """token_type_ids""": token_type_ids,
            }
            lowercase								=      model(__lowerCAmelCase		)
            self.parent.assertEqual(result.logits.shape						,			(self.batch_size, self.seq_length, self.num_labels)		)

      def 							A__			(		self						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase						,			__lowerCAmelCase		):







            """simple docstring"""



            lowercase								=      TFConvBertForQuestionAnswering(config=__lowerCAmelCase		)
            lowercase								=      {
                """input_ids""": input_ids,
                """attention_mask""": input_mask,
                """token_type_ids""": token_type_ids,
            }

            lowercase								=      model(__lowerCAmelCase		)
            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 							A__			(		self		):







            """simple docstring"""



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






@require_tf
class 					_A		(			lowerCAmelCase    ,       lowerCAmelCase    ,       unittest.TestCase      ):
      snake_case__     :  Union[str, Any]				    =      (
          (
              TFConvBertModel,
              TFConvBertForMaskedLM,
              TFConvBertForQuestionAnswering,
              TFConvBertForSequenceClassification,
              TFConvBertForTokenClassification,
              TFConvBertForMultipleChoice,
          )
          if is_tf_available()
          else ()
      )
      snake_case__     :  Dict				    =      (
          {
              'feature-extraction': TFConvBertModel,
              'fill-mask': TFConvBertForMaskedLM,
              'question-answering': TFConvBertForQuestionAnswering,
              'text-classification': TFConvBertForSequenceClassification,
              'token-classification': TFConvBertForTokenClassification,
              'zero-shot': TFConvBertForSequenceClassification,
          }
          if is_tf_available()
          else {}
      )
      snake_case__     :  str				    =      False
      snake_case__     :  Union[str, Any]				    =      False
      snake_case__     :  Optional[Any]				    =      False

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      TFConvBertModelTester(self		)
            lowercase								=      ConfigTester(self						,			config_class=__lowerCAmelCase						,			hidden_size=37		)

      def 							A__			(		self		):







            """simple docstring"""



            self.config_tester.run_common_tests()

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_model(*__lowerCAmelCase		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_masked_lm(*__lowerCAmelCase		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_multiple_choice(*__lowerCAmelCase		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_question_answering(*__lowerCAmelCase		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_sequence_classification(*__lowerCAmelCase		)

      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      self.model_tester.prepare_config_and_inputs()
            self.model_tester.create_and_check_for_token_classification(*__lowerCAmelCase		)

      @slow
      def 							A__			(		self		):







            """simple docstring"""



            lowercase				,				lowercase								=      self.model_tester.prepare_config_and_inputs_for_common()
            lowercase								=      True
            lowercase								=      True

            if hasattr(__lowerCAmelCase						,			"""use_cache"""		):
                  lowercase								=      True

            lowercase								=      getattr(self.model_tester						,			"""encoder_seq_length"""						,			self.model_tester.seq_length		)
            lowercase								=      getattr(self.model_tester						,			"""key_length"""						,			__lowerCAmelCase		)

            for model_class in self.all_model_classes:
                  lowercase								=      self._prepare_for_class(__lowerCAmelCase						,			__lowerCAmelCase		)
                  lowercase								=      model_class(__lowerCAmelCase		)
                  lowercase								=      len(model(__lowerCAmelCase		)		)

                  with tempfile.TemporaryDirectory() as tmpdirname:
                        model.save_pretrained(__lowerCAmelCase						,			saved_model=__lowerCAmelCase		)
                        lowercase								=      os.path.join(__lowerCAmelCase						,			"""saved_model"""						,			"""1"""		)
                        lowercase								=      tf.keras.models.load_model(__lowerCAmelCase		)
                        lowercase								=      model(__lowerCAmelCase		)

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

                        self.assertEqual(len(__lowerCAmelCase		)						,			__lowerCAmelCase		)

                        lowercase								=      getattr(
                            self.model_tester						,			"""expected_num_hidden_layers"""						,			self.model_tester.num_hidden_layers + 1		)

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

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







            """simple docstring"""



            lowercase								=      TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""		)
            self.assertIsNotNone(__lowerCAmelCase		)







      def 							A__			(		self		):







            """simple docstring"""



            lowercase				,				lowercase								=      self.model_tester.prepare_config_and_inputs_for_common()
            lowercase								=      True
            lowercase								=      getattr(self.model_tester						,			"""decoder_seq_length"""						,			self.model_tester.seq_length		)
            lowercase								=      getattr(self.model_tester						,			"""encoder_seq_length"""						,			self.model_tester.seq_length		)
            lowercase								=      getattr(self.model_tester						,			"""key_length"""						,			__lowerCAmelCase		)
            lowercase								=      getattr(self.model_tester						,			"""key_length"""						,			__lowerCAmelCase		)

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

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

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

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

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






@require_tf
class 					_A		(			unittest.TestCase      ):

      @slow
      def 							A__			(		self		):







            """simple docstring"""



            lowercase								=      TFConvBertModel.from_pretrained("""YituTech/conv-bert-base"""		)
            lowercase								=      tf.constant([[0, 1, 2, 3, 4, 5]]		)
            lowercase								=      model(__lowerCAmelCase		)[0]

            lowercase								=      [1, 6, 768]
            self.assertEqual(output.shape						,			__lowerCAmelCase		)

            lowercase								=      tf.constant(
                [
                    [
                        [-0.0_3_4_7_5_4_9_3, -0.4_6_8_6_0_3_4, -0.3_0_6_3_8_8_3_2],
                        [0.2_2_6_3_7_2_4_8, -0.2_6_9_8_8_6_4_6, -0.7_4_2_3_4_2_4],
                        [0.1_0_3_2_4_8_6_8, -0.4_5_0_1_3_5_0_8, -0.5_8_2_8_0_7_8_4],
                    ]
                ]		)
            tf.debugging.assert_near(output[:, :3, :3]						,			__lowerCAmelCase						,			atol=1E-4		)




"""simple docstring"""






import torch

from diffusers import StableDiffusionPipeline


__lowerCAmelCase       :			str   	="""path-to-your-trained-model"""
__lowerCAmelCase       :			int   	=StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.floataa).to("""cuda""")

__lowerCAmelCase       :			Optional[int]   	="""A photo of sks dog in a bucket"""
__lowerCAmelCase       :			int   	=pipe(prompt, num_inference_steps=5_0, guidance_scale=7.5).images[0]

image.save("""dog-bucket.png""")



