


from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, 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.openai.modeling_tf_openai import (
      TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
      TFOpenAIGPTDoubleHeadsModel,
      TFOpenAIGPTForSequenceClassification,
      TFOpenAIGPTLMHeadModel,
      TFOpenAIGPTModel,
  )

class   lowerCamelCase						:

       def __init__(      self						,    a__						,    ):






        '''simple docstring'''






        snake_case :			Optional[Any]							=   parent
        snake_case :			List[str]							=   13
        snake_case :			int							=   7
        snake_case :			Any							=   True
        snake_case :			Tuple							=   True
        snake_case :			Any							=   True
        snake_case :			Optional[int]							=   True
        snake_case :			Optional[Any]							=   True
        snake_case :			Tuple							=   99
        snake_case :			Union[str, Any]							=   32
        snake_case :			Optional[int]							=   2
        snake_case :			int							=   4
        snake_case :			Dict							=   37
        snake_case :			List[Any]							=   '''gelu'''
        snake_case :			Optional[int]							=   0.1
        snake_case :			Any							=   0.1
        snake_case :			Optional[int]							=   5_12
        snake_case :			Optional[Any]							=   16
        snake_case :			str							=   2
        snake_case :			Optional[Any]							=   0.02
        snake_case :			List[str]							=   3
        snake_case :			Optional[Any]							=   4
        snake_case :			str							=   None
        snake_case :			Dict							=   self.vocab_size - 1

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Dict							=   ids_tensor([self.batch_size, self.seq_length]						,    self.vocab_size       )

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

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

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

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

        snake_case :			Optional[int]							=   OpenAIGPTConfig(
            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						,    )

        snake_case :			int							=   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						,    a__						,    a__						,    a__						,    a__						,    a__						,    *a__       ):






        '''simple docstring'''






        snake_case :			Tuple							=   TFOpenAIGPTModel(config=a__       )
        snake_case :			Tuple							=   {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        snake_case :			Dict							=   model(a__       )

        snake_case :			Dict							=   [input_ids, input_mask]
        snake_case :			Tuple							=   model(a__       )

        snake_case :			str							=   model(a__       )

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

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






        '''simple docstring'''






        snake_case :			List[str]							=   TFOpenAIGPTLMHeadModel(config=a__       )
        snake_case :			str							=   {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
        snake_case :			Optional[int]							=   model(a__       )
        self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.seq_length, self.vocab_size)       )

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






        '''simple docstring'''






        snake_case :			Tuple							=   TFOpenAIGPTDoubleHeadsModel(config=a__       )

        snake_case :			Any							=   tf.tile(tf.expand_dims(a__						,    1       )						,    (1, self.num_choices, 1)       )
        snake_case :			str							=   tf.tile(tf.expand_dims(a__						,    1       )						,    (1, self.num_choices, 1)       )
        snake_case :			List[Any]							=   tf.tile(tf.expand_dims(a__						,    1       )						,    (1, self.num_choices, 1)       )

        snake_case :			Optional[int]							=   {
            '''input_ids''': multiple_choice_inputs_ids,
            '''mc_token_ids''': mc_token_ids,
            '''attention_mask''': multiple_choice_input_mask,
            '''token_type_ids''': multiple_choice_token_type_ids,
        }
        snake_case :			List[str]							=   model(a__       )
        self.parent.assertEqual(
            result.logits.shape						,    (self.batch_size, self.num_choices, self.seq_length, self.vocab_size)       )
        self.parent.assertEqual(result.mc_logits.shape						,    (self.batch_size, self.num_choices)       )

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






        '''simple docstring'''






        snake_case :			int							=   self.num_labels
        snake_case :			Optional[int]							=   ids_tensor([self.batch_size]						,    self.type_sequence_label_size       )
        snake_case :			List[str]							=   {
            '''input_ids''': input_ids,
            '''attention_mask''': input_mask,
            '''token_type_ids''': token_type_ids,
            '''labels''': sequence_labels,
        }
        snake_case :			Optional[Any]							=   TFOpenAIGPTForSequenceClassification(a__       )
        snake_case :			Dict							=   model(a__       )
        self.parent.assertEqual(result.logits.shape						,    (self.batch_size, self.num_labels)       )



       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			int							=   self.prepare_config_and_inputs()

        (
            (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       (
            snake_case
        )	,       
        ) :			Union[str, Any]							=   config_and_inputs

        snake_case :			Tuple							=   {'''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  ):
       _UpperCamelCase     		=   (
           (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
           if is_tf_available()
           else ()
       )
       _UpperCamelCase     		=   (
           (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
       )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
       _UpperCamelCase     		=   (
           {
               """feature-extraction""": TFOpenAIGPTModel,
               """text-classification""": TFOpenAIGPTForSequenceClassification,
               """text-generation""": TFOpenAIGPTLMHeadModel,
               """zero-shot""": TFOpenAIGPTForSequenceClassification,
           }
           if is_tf_available()
           else {}
       )
       _UpperCamelCase     		=   False
       _UpperCamelCase     		=   False

       def 					UpperCAmelCase					(      self						,    a__						,    a__						,    a__						,    a__						,    a__       ):






        '''simple docstring'''






        if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
         # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
         # `OpenAIGPTConfig` 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'''






        snake_case :			int							=   TFOpenAIGPTModelTester(self       )
        snake_case :			Tuple							=   ConfigTester(self						,    config_class=a__						,    n_embd=37       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        self.config_tester.run_common_tests()

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Any							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_model(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			List[str]							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_lm_head(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			str							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_double_head(*a__       )

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Any							=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*a__       )



       @slow
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case :			Optional[Any]							=   TFOpenAIGPTModel.from_pretrained(a__       )
         self.assertIsNotNone(a__       )







@require_tf
class   lowerCamelCase						(	unittest.TestCase  ):

       @slow
       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			int							=   TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''       )
        snake_case :			Union[str, Any]							=   tf.convert_to_tensor([[4_81, 47_35, 5_44]]						,    dtype=tf.intaa       )  # the president is
        snake_case :			List[Any]							=   [
            4_81,
            47_35,
            5_44,
            2_46,
            9_63,
            8_70,
            7_62,
            2_39,
            2_44,
            4_04_77,
            2_44,
            2_49,
            7_19,
            8_81,
            4_87,
            5_44,
            2_40,
            2_44,
            6_03,
            4_81,
        ]  # the president is a very good man. " \n " i\'m sure he is, " said the

        snake_case :			int							=   model.generate(a__						,    do_sample=a__       )
        self.assertListEqual(output_ids[0].numpy().tolist()						,    a__       )










import gc
import tempfile
import unittest

import numpy as np
import torch

from diffusers import VersatileDiffusionDualGuidedPipeline
from diffusers.utils.testing_utils import load_image, nightly, require_torch_gpu, torch_device


a					:				Optional[int]       =      False

@nightly
@require_torch_gpu
class   lowerCamelCase						(	unittest.TestCase  ):

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        super().tearDown()
        gc.collect()
        torch.cuda.empty_cache()

       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Optional[int]							=   VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion'''       )
        # remove text_unet
        pipe.remove_unused_weights()
        pipe.to(a__       )
        pipe.set_progress_bar_config(disable=a__       )

        snake_case :			Union[str, Any]							=   load_image(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''       )

        snake_case :			str							=   torch.manual_seed(0       )
        snake_case :			Tuple							=   pipe(
            prompt='''first prompt'''						,    image=a__						,    text_to_image_strength=0.75						,    generator=a__						,    guidance_scale=7.5						,    num_inference_steps=2						,    output_type='''numpy'''						,    ).images

        with tempfile.TemporaryDirectory() as tmpdirname:
         pipe.save_pretrained(a__       )
         snake_case :			str							=   VersatileDiffusionDualGuidedPipeline.from_pretrained(a__       )

        pipe.to(a__       )
        pipe.set_progress_bar_config(disable=a__       )

        snake_case :			Optional[Any]							=   generator.manual_seed(0       )
        snake_case :			List[str]							=   pipe(
            prompt='''first prompt'''						,    image=a__						,    text_to_image_strength=0.75						,    generator=a__						,    guidance_scale=7.5						,    num_inference_steps=2						,    output_type='''numpy'''						,    ).images

        assert np.abs(image - new_image       ).sum() < 1e-5, "Models don't have the same forward pass"



       def 					UpperCAmelCase					(      self       ):






        '''simple docstring'''






        snake_case :			Tuple							=   VersatileDiffusionDualGuidedPipeline.from_pretrained('''shi-labs/versatile-diffusion'''       )
        pipe.remove_unused_weights()
        pipe.to(a__       )
        pipe.set_progress_bar_config(disable=a__       )

        snake_case :			int							=   '''cyberpunk 2077'''
        snake_case :			Dict							=   load_image(
            '''https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'''       )
        snake_case :			Optional[int]							=   torch.manual_seed(0       )
        snake_case :			Union[str, Any]							=   pipe(
            prompt=a__						,    image=a__						,    text_to_image_strength=0.75						,    generator=a__						,    guidance_scale=7.5						,    num_inference_steps=50						,    output_type='''numpy'''						,    ).images

        snake_case :			str							=   image[0, 2_53:2_56, 2_53:2_56, -1]

        assert image.shape == (1, 5_12, 5_12, 3)
        snake_case :			int							=   np.array([0.0787, 0.0849, 0.0826, 0.0812, 0.0807, 0.0795, 0.0818, 0.0798, 0.0779]       )

        assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1e-2






