




'''simple docstring'''






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    A						:




				'''simple docstring'''





				def __init__(self					,    _UpperCAmelCase					,    )      ->			int:
							__UpperCamelCase :     List[Any]										=  parent
							__UpperCamelCase :     int										=  1_3
							__UpperCamelCase :     Union[str, Any]										=  7
							__UpperCamelCase :     List[Any]										=  True
							__UpperCamelCase :     int										=  True
							__UpperCamelCase :     Any										=  True
							__UpperCamelCase :     Optional[Any]										=  True
							__UpperCamelCase :     Optional[Any]										=  True
							__UpperCamelCase :     Optional[Any]										=  9_9
							__UpperCamelCase :     Union[str, Any]										=  3_2
							__UpperCamelCase :     int										=  2
							__UpperCamelCase :     Optional[Any]										=  4
							__UpperCamelCase :     Dict										=  3_7
							__UpperCamelCase :     Dict										=  "gelu"
							__UpperCamelCase :     Any										=  0.1
							__UpperCamelCase :     Optional[int]										=  0.1
							__UpperCamelCase :     int										=  5_1_2
							__UpperCamelCase :     List[Any]										=  1_6
							__UpperCamelCase :     Optional[Any]										=  2
							__UpperCamelCase :     List[Any]										=  0.02
							__UpperCamelCase :     List[Any]										=  3
							__UpperCamelCase :     List[str]										=  4
							__UpperCamelCase :     Tuple										=  None
							__UpperCamelCase :     Union[str, Any]										=  self.vocab_size - 1
				def 			a_			(self     )      ->			List[Any]:
							__UpperCamelCase :     int										=  ids_tensor([self.batch_size, self.seq_length]					,    self.vocab_size     )

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

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

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

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

							__UpperCamelCase :     Union[str, Any]										=  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					,    )

							__UpperCamelCase :     List[Any]										=  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 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    *_UpperCAmelCase     )      ->			Any:
							__UpperCamelCase :     Tuple										=  TFOpenAIGPTModel(config=lowerCAmelCase__     )
							__UpperCamelCase :     Optional[Any]										=  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							__UpperCamelCase :     Optional[int]										=  model(lowerCAmelCase__     )

							__UpperCamelCase :     Optional[Any]										=  [input_ids, input_mask]
							__UpperCamelCase :     Union[str, Any]										=  model(lowerCAmelCase__     )

							__UpperCamelCase :     Optional[Any]										=  model(lowerCAmelCase__     )

							self.parent.assertEqual(result.last_hidden_state.shape					,    (self.batch_size, self.seq_length, self.hidden_size)     )
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    *_UpperCAmelCase     )      ->			Tuple:
							__UpperCamelCase :     Dict										=  TFOpenAIGPTLMHeadModel(config=lowerCAmelCase__     )
							__UpperCamelCase :     Optional[Any]										=  {"input_ids": input_ids, "attention_mask": input_mask, "token_type_ids": token_type_ids}
							__UpperCamelCase :     Any										=  model(lowerCAmelCase__     )
							self.parent.assertEqual(result.logits.shape					,    (self.batch_size, self.seq_length, self.vocab_size)     )
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    *_UpperCAmelCase     )      ->			Any:
							__UpperCamelCase :     str										=  TFOpenAIGPTDoubleHeadsModel(config=lowerCAmelCase__     )

							__UpperCamelCase :     int										=  tf.tile(tf.expand_dims(lowerCAmelCase__					,    1     )					,    (1, self.num_choices, 1)     )
							__UpperCamelCase :     Tuple										=  tf.tile(tf.expand_dims(lowerCAmelCase__					,    1     )					,    (1, self.num_choices, 1)     )
							__UpperCamelCase :     Union[str, Any]										=  tf.tile(tf.expand_dims(lowerCAmelCase__					,    1     )					,    (1, self.num_choices, 1)     )

							__UpperCamelCase :     List[Any]										=  {
							    "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,
							}
							__UpperCamelCase :     Tuple										=  model(lowerCAmelCase__     )
							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 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    *_UpperCAmelCase     )      ->			Optional[int]:
							__UpperCamelCase :     Union[str, Any]										=  self.num_labels
							__UpperCamelCase :     Tuple										=  ids_tensor([self.batch_size]					,    self.type_sequence_label_size     )
							__UpperCamelCase :     Optional[int]										=  {
							    "input_ids": input_ids,
							    "attention_mask": input_mask,
							    "token_type_ids": token_type_ids,
							    "labels": sequence_labels,
							}
							__UpperCamelCase :     Optional[int]										=  TFOpenAIGPTForSequenceClassification(lowerCAmelCase__     )
							__UpperCamelCase :     Optional[Any]										=  model(lowerCAmelCase__     )
							self.parent.assertEqual(result.logits.shape					,    (self.batch_size, self.num_labels)     )





				def 			a_			(self     )      ->			Optional[int]:
							__UpperCamelCase :     List[Any]										=  self.prepare_config_and_inputs()

							(
							    __UpperCamelCase	
							) :     Optional[int]										=  config_and_inputs

							__UpperCamelCase :     int										=  {"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						):




				'''simple docstring'''





				A       =							(
				    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
				    if is_tf_available()
				    else ()
				)
				A       =							(
				    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
				)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
				A       =							(
				    {
				        "feature-extraction": TFOpenAIGPTModel,
				        "text-classification": TFOpenAIGPTForSequenceClassification,
				        "text-generation": TFOpenAIGPTLMHeadModel,
				        "zero-shot": TFOpenAIGPTForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				A       =							False
				A       =							False
				def 			a_			(self					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase					,    _UpperCAmelCase     )      ->			Tuple:







							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 			a_			(self     )      ->			str:
							__UpperCamelCase :     Optional[int]										=  TFOpenAIGPTModelTester(self     )
							__UpperCamelCase :     Any										=  ConfigTester(self					,    config_class=lowerCAmelCase__					,    n_embd=3_7     )
				def 			a_			(self     )      ->			int:
							self.config_tester.run_common_tests()
				def 			a_			(self     )      ->			Tuple:
							__UpperCamelCase :     Dict										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__     )
				def 			a_			(self     )      ->			str:
							__UpperCamelCase :     Tuple										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_lm_head(*lowerCAmelCase__     )
				def 			a_			(self     )      ->			Union[str, Any]:
							__UpperCamelCase :     int										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_double_head(*lowerCAmelCase__     )
				def 			a_			(self     )      ->			str:
							__UpperCamelCase :     Optional[int]										=  self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__     )





				@slow
				def 			a_			(self     )      ->			int:







							for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										__UpperCamelCase :     str										=  TFOpenAIGPTModel.from_pretrained(lowerCAmelCase__     )
										self.assertIsNotNone(lowerCAmelCase__     )







@require_tf
class    A						(					unittest.TestCase						):




				'''simple docstring'''





				@slow
				def 			a_			(self     )      ->			Union[str, Any]:
							__UpperCamelCase :     Tuple										=  TFOpenAIGPTLMHeadModel.from_pretrained("openai-gpt"     )
							__UpperCamelCase :     Optional[int]										=  tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]]					,    dtype=tf.intaa     )  # the president is
							__UpperCamelCase :     Optional[Any]										=  [
							    4_8_1,
							    4_7_3_5,
							    5_4_4,
							    2_4_6,
							    9_6_3,
							    8_7_0,
							    7_6_2,
							    2_3_9,
							    2_4_4,
							    4_0_4_7_7,
							    2_4_4,
							    2_4_9,
							    7_1_9,
							    8_8_1,
							    4_8_7,
							    5_4_4,
							    2_4_0,
							    2_4_4,
							    6_0_3,
							    4_8_1,
							]  # the president is a very good man. " \n " i\'m sure he is, " said the

							__UpperCamelCase :     List[str]										=  model.generate(lowerCAmelCase__					,    do_sample=lowerCAmelCase__     )
							self.assertListEqual(output_ids[0].numpy().tolist()					,    lowerCAmelCase__     )









"""simple docstring"""
import gc
import random
import unittest

import numpy as np
import torch
from transformers import XLMRobertaTokenizerFast

from diffusers import DDIMScheduler, KandinskyPipeline, KandinskyPriorPipeline, UNetaDConditionModel, VQModel
from diffusers.pipelines.kandinsky.text_encoder import MCLIPConfig, MultilingualCLIP
from diffusers.utils import floats_tensor, load_numpy, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..test_pipelines_common import PipelineTesterMixin, assert_mean_pixel_difference


enable_full_determinism()







class  _SCREAMING_SNAKE_CASE       (     SCREAMING_SNAKE_CASE__			,				unittest.TestCase				):



 '''simple docstring'''





 __UpperCAmelCase		   =							KandinskyPipeline
 __UpperCAmelCase		   =							[
     """prompt""",
     """image_embeds""",
     """negative_image_embeds""",
 ]
 __UpperCAmelCase		   =							["""prompt""", """negative_prompt""", """image_embeds""", """negative_image_embeds"""]
 __UpperCAmelCase		   =							[
     """generator""",
     """height""",
     """width""",
     """latents""",
     """guidance_scale""",
     """negative_prompt""",
     """num_inference_steps""",
     """return_dict""",
     """guidance_scale""",
     """num_images_per_prompt""",
     """output_type""",
     """return_dict""",
 ]
 __UpperCAmelCase		   =							False






 @property
 def lowercase_      (self   ):

  '''simple docstring'''







  return 32






 @property
 def lowercase_      (self   ):

  '''simple docstring'''







  return 32






 @property
 def lowercase_      (self   ):

  '''simple docstring'''







  return self.time_input_dim






 @property
 def lowercase_      (self   ):

  '''simple docstring'''







  return self.time_input_dim * 4






 @property
 def lowercase_      (self   ):

  '''simple docstring'''







  return 1_00






 @property
 def lowercase_      (self   ):

  '''simple docstring'''







  _UpperCamelCase      :	Tuple					      =  XLMRobertaTokenizerFast.from_pretrained("YiYiXu/tiny-random-mclip-base"   )
  return tokenizer






 @property
 def lowercase_      (self   ):

  '''simple docstring'''







  torch.manual_seed(0   )
  _UpperCamelCase      :	List[str]					      =  MCLIPConfig(
      numDims=self.cross_attention_dim					,     transformerDimensions=self.text_embedder_hidden_size					,     hidden_size=self.text_embedder_hidden_size					,     intermediate_size=37					,     num_attention_heads=4					,     num_hidden_layers=5					,     vocab_size=10_05					,     )

  _UpperCamelCase      :	Optional[Any]					      =  MultilingualCLIP(lowerCAmelCase__   )
  _UpperCamelCase      :	List[str]					      =  text_encoder.eval()

  return text_encoder






 @property
 def lowercase_      (self   ):

  '''simple docstring'''







  torch.manual_seed(0   )

  _UpperCamelCase      :	Dict					      =  {
      "in_channels": 4,
      # Out channels is double in channels because predicts mean and variance
      "out_channels": 8,
      "addition_embed_type": "text_image",
      "down_block_types": ("ResnetDownsampleBlock2D", "SimpleCrossAttnDownBlock2D"),
      "up_block_types": ("SimpleCrossAttnUpBlock2D", "ResnetUpsampleBlock2D"),
      "mid_block_type": "UNetMidBlock2DSimpleCrossAttn",
      "block_out_channels": (self.block_out_channels_a, self.block_out_channels_a * 2),
      "layers_per_block": 1,
      "encoder_hid_dim": self.text_embedder_hidden_size,
      "encoder_hid_dim_type": "text_image_proj",
      "cross_attention_dim": self.cross_attention_dim,
      "attention_head_dim": 4,
      "resnet_time_scale_shift": "scale_shift",
      "class_embed_type": None,
  }

  _UpperCamelCase      :	int					      =  UNetaDConditionModel(**lowerCAmelCase__   )
  return model






 @property
 def lowercase_      (self   ):

  '''simple docstring'''







  return {
      "block_out_channels": [32, 64],
      "down_block_types": ["DownEncoderBlock2D", "AttnDownEncoderBlock2D"],
      "in_channels": 3,
      "latent_channels": 4,
      "layers_per_block": 1,
      "norm_num_groups": 8,
      "norm_type": "spatial",
      "num_vq_embeddings": 12,
      "out_channels": 3,
      "up_block_types": [
          "AttnUpDecoderBlock2D",
          "UpDecoderBlock2D",
      ],
      "vq_embed_dim": 4,
  }






 @property
 def lowercase_      (self   ):

  '''simple docstring'''







  torch.manual_seed(0   )
  _UpperCamelCase      :	Dict					      =  VQModel(**self.dummy_movq_kwargs   )
  return model






 def lowercase_      (self   ):

  '''simple docstring'''







  _UpperCamelCase      :	List[str]					      =  self.dummy_text_encoder
  _UpperCamelCase      :	str					      =  self.dummy_tokenizer
  _UpperCamelCase      :	List[str]					      =  self.dummy_unet
  _UpperCamelCase      :	Tuple					      =  self.dummy_movq

  _UpperCamelCase      :	Dict					      =  DDIMScheduler(
      num_train_timesteps=10_00					,     beta_schedule="linear"					,     beta_start=0.0_0085					,     beta_end=0.012					,     clip_sample=lowerCAmelCase__					,     set_alpha_to_one=lowerCAmelCase__					,     steps_offset=1					,     prediction_type="epsilon"					,     thresholding=lowerCAmelCase__					,     )

  _UpperCamelCase      :	Any					      =  {
      "text_encoder": text_encoder,
      "tokenizer": tokenizer,
      "unet": unet,
      "scheduler": scheduler,
      "movq": movq,
  }
  return components






 def lowercase_      (self					,     lowerCAmelCase__					,     lowerCAmelCase__=0   ):

  '''simple docstring'''







  _UpperCamelCase      :	Dict					      =  floats_tensor((1, self.cross_attention_dim)					,     rng=random.Random(lowerCAmelCase__   )   ).to(lowerCAmelCase__   )
  _UpperCamelCase      :	int					      =  floats_tensor((1, self.cross_attention_dim)					,     rng=random.Random(seed + 1   )   ).to(lowerCAmelCase__   )
  if str(lowerCAmelCase__   ).startswith("mps"   ):
   _UpperCamelCase      :	Optional[Any]					      =  torch.manual_seed(lowerCAmelCase__   )
  else:
   _UpperCamelCase      :	List[Any]					      =  torch.Generator(device=lowerCAmelCase__   ).manual_seed(lowerCAmelCase__   )
  _UpperCamelCase      :	Optional[Any]					      =  {
      "prompt": "horse",
      "image_embeds": image_embeds,
      "negative_image_embeds": negative_image_embeds,
      "generator": generator,
      "height": 64,
      "width": 64,
      "guidance_scale": 4.0,
      "num_inference_steps": 2,
      "output_type": "np",
  }
  return inputs






 def lowercase_      (self   ):

  '''simple docstring'''







  _UpperCamelCase      :	Optional[Any]					      =  "cpu"

  _UpperCamelCase      :	Any					      =  self.get_dummy_components()

  _UpperCamelCase      :	Tuple					      =  self.pipeline_class(**lowerCAmelCase__   )
  _UpperCamelCase      :	Optional[int]					      =  pipe.to(lowerCAmelCase__   )

  pipe.set_progress_bar_config(disable=lowerCAmelCase__   )

  _UpperCamelCase      :	Union[str, Any]					      =  pipe(**self.get_dummy_inputs(lowerCAmelCase__   )   )
  _UpperCamelCase      :	Union[str, Any]					      =  output.images

  _UpperCamelCase      :	str					      =  pipe(
      **self.get_dummy_inputs(lowerCAmelCase__   )					,     return_dict=lowerCAmelCase__					,     )[0]

  _UpperCamelCase      :	Tuple					      =  image[0, -3:, -3:, -1]
  _UpperCamelCase      :	Optional[int]					      =  image_from_tuple[0, -3:, -3:, -1]

  assert image.shape == (1, 64, 64, 3)

  _UpperCamelCase      :	Union[str, Any]					      =  np.array(
      [0.32_8663, 1.0, 0.2321_6873, 1.0, 0.9271_7564, 0.463_9046, 0.9689_4777, 0.3171_3378, 0.629_3953]   )

  assert (
      np.abs(image_slice.flatten() - expected_slice   ).max() < 1E-2
  ), F" expected_slice {expected_slice}, but got {image_slice.flatten()}"
  assert (
      np.abs(image_from_tuple_slice.flatten() - expected_slice   ).max() < 1E-2
  ), F" expected_slice {expected_slice}, but got {image_from_tuple_slice.flatten()}"







@slow
@require_torch_gpu
class  _SCREAMING_SNAKE_CASE       (     unittest.TestCase				):



 '''simple docstring'''





 def lowercase_      (self   ):

  '''simple docstring'''







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






 def lowercase_      (self   ):

  '''simple docstring'''







  _UpperCamelCase      :	Tuple					      =  load_numpy(
      "https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main"
      "/kandinsky/kandinsky_text2img_cat_fp16.npy"   )

  _UpperCamelCase      :	Optional[int]					      =  KandinskyPriorPipeline.from_pretrained(
      "kandinsky-community/kandinsky-2-1-prior"					,     torch_dtype=torch.floataa   )
  pipe_prior.to(lowerCAmelCase__   )

  _UpperCamelCase      :	List[str]					      =  KandinskyPipeline.from_pretrained("kandinsky-community/kandinsky-2-1"					,     torch_dtype=torch.floataa   )
  _UpperCamelCase      :	List[Any]					      =  pipeline.to(lowerCAmelCase__   )
  pipeline.set_progress_bar_config(disable=lowerCAmelCase__   )

  _UpperCamelCase      :	Optional[Any]					      =  "red cat, 4k photo"

  _UpperCamelCase      :	str					      =  torch.Generator(device="cuda"   ).manual_seed(0   )
  _UpperCamelCase      ,	_UpperCamelCase      :	Optional[Any]					      =  pipe_prior(
      lowerCAmelCase__					,     generator=lowerCAmelCase__					,     num_inference_steps=5					,     negative_prompt=""					,     ).to_tuple()

  _UpperCamelCase      :	Optional[Any]					      =  torch.Generator(device="cuda"   ).manual_seed(0   )
  _UpperCamelCase      :	Any					      =  pipeline(
      lowerCAmelCase__					,     image_embeds=lowerCAmelCase__					,     negative_image_embeds=lowerCAmelCase__					,     generator=lowerCAmelCase__					,     num_inference_steps=1_00					,     output_type="np"					,     )

  _UpperCamelCase      :	Optional[Any]					      =  output.images[0]

  assert image.shape == (5_12, 5_12, 3)

  assert_mean_pixel_difference(lowerCAmelCase__					,     lowerCAmelCase__   )



