






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

import numpy as np
import torch
from PIL import Image
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    EulerAncestralDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionInstructPixaPixPipeline,
    UNetaDConditionModel,
)
from diffusers.image_processor import VaeImageProcessor
from diffusers.utils import floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    IMAGE_TO_IMAGE_IMAGE_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_VARIATION_PARAMS,
)
from ..test_pipelines_common import PipelineKarrasSchedulerTesterMixin, PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()



class        UpperCamelCase__ (				a_							,	a_							,	a_							,	unittest.TestCase):







    """simple docstring"""
    __UpperCAmelCase           = StableDiffusionInstructPixaPixPipeline
    __UpperCAmelCase           = TEXT_GUIDED_IMAGE_VARIATION_PARAMS - {"""height""", """width""", """cross_attention_kwargs"""}
    __UpperCAmelCase           = TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
    __UpperCAmelCase           = IMAGE_TO_IMAGE_IMAGE_PARAMS
    __UpperCAmelCase           = IMAGE_TO_IMAGE_IMAGE_PARAMS







    def 				a__			(		self :	Optional[int] ):





           '''simple docstring'''

           torch.manual_seed(0 )
           __magic_name__      				=    UNetaDConditionModel(
               block_out_channels=(3_2, 6_4)				,    layers_per_block=2				,    sample_size=3_2				,    in_channels=8				,    out_channels=4				,    down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')				,    up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')				,    cross_attention_dim=3_2				,    )
           __magic_name__      				=    PNDMScheduler(skip_prk_steps=UpperCamelCase_ )
           torch.manual_seed(0 )
           __magic_name__      				=    AutoencoderKL(
               block_out_channels=[3_2, 6_4]				,    in_channels=3				,    out_channels=3				,    down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D']				,    up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D']				,    latent_channels=4				,    )
           torch.manual_seed(0 )
           __magic_name__      				=    CLIPTextConfig(
               bos_token_id=0				,    eos_token_id=2				,    hidden_size=3_2				,    intermediate_size=3_7				,    layer_norm_eps=1e-0_5				,    num_attention_heads=4				,    num_hidden_layers=5				,    pad_token_id=1				,    vocab_size=1_0_0_0				,    )
           __magic_name__      				=    CLIPTextModel(UpperCamelCase_ )
           __magic_name__      				=    CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip' )

           __magic_name__      				=    {
               'unet': unet,
               'scheduler': scheduler,
               'vae': vae,
               'text_encoder': text_encoder,
               'tokenizer': tokenizer,
               'safety_checker': None,
               'feature_extractor': None,
           }
           return components







    def 				a__			(		self :	List[str]				,    UpperCamelCase_ :	Dict				,    UpperCamelCase_ :	str=0 ):





           '''simple docstring'''

           __magic_name__      				=    floats_tensor((1, 3, 3_2, 3_2)				,    rng=random.Random(UpperCamelCase_ ) ).to(UpperCamelCase_ )
           __magic_name__      				=    image.cpu().permute(0				,    2				,    3				,    1 )[0]
           __magic_name__      				=    Image.fromarray(np.uinta(UpperCamelCase_ ) ).convert('RGB' )
           if str(UpperCamelCase_ ).startswith('mps' ):
                  __magic_name__      				=    torch.manual_seed(UpperCamelCase_ )
           else:
                  __magic_name__      				=    torch.Generator(device=UpperCamelCase_ ).manual_seed(UpperCamelCase_ )
           __magic_name__      				=    {
               'prompt': 'A painting of a squirrel eating a burger',
               'image': image,
               'generator': generator,
               'num_inference_steps': 2,
               'guidance_scale': 6.0,
               'image_guidance_scale': 1,
               'output_type': 'numpy',
           }
           return inputs







    def 				a__			(		self :	Dict ):





           '''simple docstring'''

           __magic_name__      				=    'cpu'  # ensure determinism for the device-dependent torch.Generator
           __magic_name__      				=    self.get_dummy_components()
           __magic_name__      				=    StableDiffusionInstructPixaPixPipeline(**UpperCamelCase_ )
           __magic_name__      				=    sd_pipe.to(UpperCamelCase_ )
           sd_pipe.set_progress_bar_config(disable=UpperCamelCase_ )

           __magic_name__      				=    self.get_dummy_inputs(UpperCamelCase_ )
           __magic_name__      				=    sd_pipe(**UpperCamelCase_ ).images
           __magic_name__      				=    image[0, -3:, -3:, -1]
           assert image.shape == (1, 3_2, 3_2, 3)
           __magic_name__      				=    np.array([0.7_526, 0.3_750, 0.4_547, 0.6_117, 0.5_866, 0.5_016, 0.4_327, 0.5_642, 0.4_815] )

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







    def 				a__			(		self :	int ):





           '''simple docstring'''

           __magic_name__      				=    'cpu'  # ensure determinism for the device-dependent torch.Generator
           __magic_name__      				=    self.get_dummy_components()
           __magic_name__      				=    StableDiffusionInstructPixaPixPipeline(**UpperCamelCase_ )
           __magic_name__      				=    sd_pipe.to(UpperCamelCase_ )
           sd_pipe.set_progress_bar_config(disable=UpperCamelCase_ )

           __magic_name__      				=    self.get_dummy_inputs(UpperCamelCase_ )
           __magic_name__      				=    'french fries'
           __magic_name__      				=    sd_pipe(**UpperCamelCase_				,    negative_prompt=UpperCamelCase_ )
           __magic_name__      				=    output.images
           __magic_name__      				=    image[0, -3:, -3:, -1]

           assert image.shape == (1, 3_2, 3_2, 3)
           __magic_name__      				=    np.array([0.7_511, 0.3_642, 0.4_553, 0.6_236, 0.5_797, 0.5_013, 0.4_343, 0.5_611, 0.4_831] )

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







    def 				a__			(		self :	str ):





           '''simple docstring'''

           __magic_name__      				=    'cpu'  # ensure determinism for the device-dependent torch.Generator
           __magic_name__      				=    self.get_dummy_components()
           __magic_name__      				=    StableDiffusionInstructPixaPixPipeline(**UpperCamelCase_ )
           __magic_name__      				=    sd_pipe.to(UpperCamelCase_ )
           sd_pipe.set_progress_bar_config(disable=UpperCamelCase_ )

           __magic_name__      				=    self.get_dummy_inputs(UpperCamelCase_ )
           __magic_name__      				=    [inputs['prompt']] * 2

           __magic_name__      				=    np.array(inputs['image'] ).astype(np.floataa ) / 255.0
           __magic_name__      				=    torch.from_numpy(UpperCamelCase_ ).unsqueeze(0 ).to(UpperCamelCase_ )
           __magic_name__      				=    image / 2 + 0.5
           __magic_name__      				=    image.permute(0				,    3				,    1				,    2 )
           __magic_name__      				=    image.repeat(2				,    1				,    1				,    1 )

           __magic_name__      				=    sd_pipe(**UpperCamelCase_ ).images
           __magic_name__      				=    image[-1, -3:, -3:, -1]

           assert image.shape == (2, 3_2, 3_2, 3)
           __magic_name__      				=    np.array([0.5_812, 0.5_748, 0.5_222, 0.5_908, 0.5_695, 0.7_174, 0.6_804, 0.5_523, 0.5_579] )

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







    def 				a__			(		self :	str ):





           '''simple docstring'''

           __magic_name__      				=    'cpu'  # ensure determinism for the device-dependent torch.Generator
           __magic_name__      				=    self.get_dummy_components()
           __magic_name__      				=    EulerAncestralDiscreteScheduler(
               beta_start=0.00_085				,    beta_end=0.012				,    beta_schedule='scaled_linear' )
           __magic_name__      				=    StableDiffusionInstructPixaPixPipeline(**UpperCamelCase_ )
           __magic_name__      				=    sd_pipe.to(UpperCamelCase_ )
           sd_pipe.set_progress_bar_config(disable=UpperCamelCase_ )

           __magic_name__      				=    self.get_dummy_inputs(UpperCamelCase_ )
           __magic_name__      				=    sd_pipe(**UpperCamelCase_ ).images
           __magic_name__      				=    image[0, -3:, -3:, -1]

           __magic_name__      				=    [round(UpperCamelCase_				,    4 ) for x in image_slice.flatten().tolist()]
           print(','.join([str(UpperCamelCase_ ) for x in slice] ) )

           assert image.shape == (1, 3_2, 3_2, 3)
           __magic_name__      				=    np.array([0.7_417, 0.3_842, 0.4_732, 0.5_776, 0.5_891, 0.5_139, 0.4_052, 0.5_673, 0.4_986] )

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







    def 				a__			(		self :	Optional[Any] ):





           '''simple docstring'''

           super().test_inference_batch_single_identical(expected_max_diff=3e-3 )







    def 				a__			(		self :	List[str] ):





           '''simple docstring'''

           __magic_name__      				=    self.get_dummy_components()
           __magic_name__      				=    StableDiffusionInstructPixaPixPipeline(**UpperCamelCase_ )
           __magic_name__      				=    VaeImageProcessor(do_resize=UpperCamelCase_				,    do_normalize=UpperCamelCase_ )
           __magic_name__      				=    pipe.to(UpperCamelCase_ )
           pipe.set_progress_bar_config(disable=UpperCamelCase_ )

           __magic_name__      				=    pipe(**self.get_dummy_inputs_by_type(UpperCamelCase_				,    input_image_type='pt' ) )[0]

           __magic_name__      				=    components['vae']
           __magic_name__      				=    self.get_dummy_inputs_by_type(UpperCamelCase_				,    input_image_type='pt' )

           for image_param in self.image_latents_params:
                  if image_param in inputs.keys():
                         __magic_name__      				=    vae.encode(inputs[image_param] ).latent_dist.mode()

           __magic_name__      				=    pipe(**UpperCamelCase_ )[0]

           __magic_name__      				=    np.abs(out - out_latents_inputs ).max()
           self.assertLess(UpperCamelCase_				,    1e-4				,    'passing latents as image input generate different result from passing image' )



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







    """simple docstring"""







    def 				a__			(		self :	Union[str, Any] ):





           '''simple docstring'''

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







    def 				a__			(		self :	int				,    UpperCamelCase_ :	List[str]=0 ):





           '''simple docstring'''

           __magic_name__      				=    torch.manual_seed(UpperCamelCase_ )
           __magic_name__      				=    load_image(
               'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_pix2pix/example.jpg' )
           __magic_name__      				=    {
               'prompt': 'turn him into a cyborg',
               'image': image,
               'generator': generator,
               'num_inference_steps': 3,
               'guidance_scale': 7.5,
               'image_guidance_scale': 1.0,
               'output_type': 'numpy',
           }
           return inputs







    def 				a__			(		self :	Dict ):





           '''simple docstring'''

           __magic_name__      				=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               'timbrooks/instruct-pix2pix'				,    safety_checker=UpperCamelCase_ )
           pipe.to(UpperCamelCase_ )
           pipe.set_progress_bar_config(disable=UpperCamelCase_ )
           pipe.enable_attention_slicing()

           __magic_name__      				=    self.get_inputs()
           __magic_name__      				=    pipe(**UpperCamelCase_ ).images
           __magic_name__      				=    image[0, -3:, -3:, -1].flatten()

           assert image.shape == (1, 5_1_2, 5_1_2, 3)
           __magic_name__      				=    np.array([0.5_902, 0.6_015, 0.6_027, 0.5_983, 0.6_092, 0.6_061, 0.5_765, 0.5_785, 0.5_555] )

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







    def 				a__			(		self :	str ):





           '''simple docstring'''

           __magic_name__      				=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               'timbrooks/instruct-pix2pix'				,    safety_checker=UpperCamelCase_ )
           __magic_name__      				=    LMSDiscreteScheduler.from_config(pipe.scheduler.config )
           pipe.to(UpperCamelCase_ )
           pipe.set_progress_bar_config(disable=UpperCamelCase_ )
           pipe.enable_attention_slicing()

           __magic_name__      				=    self.get_inputs()
           __magic_name__      				=    pipe(**UpperCamelCase_ ).images
           __magic_name__      				=    image[0, -3:, -3:, -1].flatten()

           assert image.shape == (1, 5_1_2, 5_1_2, 3)
           __magic_name__      				=    np.array([0.6_578, 0.6_817, 0.6_972, 0.6_761, 0.6_856, 0.6_916, 0.6_428, 0.6_516, 0.6_301] )

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







    def 				a__			(		self :	Dict ):





           '''simple docstring'''

           __magic_name__      				=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               'timbrooks/instruct-pix2pix'				,    safety_checker=UpperCamelCase_ )
           __magic_name__      				=    DDIMScheduler.from_config(pipe.scheduler.config )
           pipe.to(UpperCamelCase_ )
           pipe.set_progress_bar_config(disable=UpperCamelCase_ )
           pipe.enable_attention_slicing()

           __magic_name__      				=    self.get_inputs()
           __magic_name__      				=    pipe(**UpperCamelCase_ ).images
           __magic_name__      				=    image[0, -3:, -3:, -1].flatten()

           assert image.shape == (1, 5_1_2, 5_1_2, 3)
           __magic_name__      				=    np.array([0.3_828, 0.3_834, 0.3_818, 0.3_792, 0.3_865, 0.3_752, 0.3_792, 0.3_847, 0.3_753] )

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







    def 				a__			(		self :	Tuple ):





           '''simple docstring'''

           __magic_name__      				=    0

           def callback_fn(UpperCamelCase_ :	int				,    UpperCamelCase_ :	int				,    UpperCamelCase_ :	torch.FloatTensor ) -> None:
                  __magic_name__      				=    True
                  nonlocal number_of_steps
                  number_of_steps += 1
                  if step == 1:
                         __magic_name__      				=    latents.detach().cpu().numpy()
                         assert latents.shape == (1, 4, 6_4, 6_4)
                         __magic_name__      				=    latents[0, -3:, -3:, -1]
                         __magic_name__      				=    np.array([-0.2_463, -0.4_644, -0.9_756, 1.5_176, 1.4_414, 0.7_866, 0.9_897, 0.8_521, 0.7_983] )

                         assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2
                  elif step == 2:
                         __magic_name__      				=    latents.detach().cpu().numpy()
                         assert latents.shape == (1, 4, 6_4, 6_4)
                         __magic_name__      				=    latents[0, -3:, -3:, -1]
                         __magic_name__      				=    np.array([-0.2_644, -0.4_626, -0.9_653, 1.5_176, 1.4_551, 0.7_686, 0.9_805, 0.8_452, 0.8_115] )

                         assert np.abs(latents_slice.flatten() - expected_slice ).max() < 5e-2

           __magic_name__      				=    False

           __magic_name__      				=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               'timbrooks/instruct-pix2pix'				,    safety_checker=UpperCamelCase_				,    torch_dtype=torch.floataa )
           __magic_name__      				=    pipe.to(UpperCamelCase_ )
           pipe.set_progress_bar_config(disable=UpperCamelCase_ )
           pipe.enable_attention_slicing()

           __magic_name__      				=    self.get_inputs()
           pipe(**UpperCamelCase_				,    callback=UpperCamelCase_				,    callback_steps=1 )
           assert callback_fn.has_been_called
           assert number_of_steps == 3







    def 				a__			(		self :	List[Any] ):





           '''simple docstring'''

           torch.cuda.empty_cache()
           torch.cuda.reset_max_memory_allocated()
           torch.cuda.reset_peak_memory_stats()

           __magic_name__      				=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               'timbrooks/instruct-pix2pix'				,    safety_checker=UpperCamelCase_				,    torch_dtype=torch.floataa )
           __magic_name__      				=    pipe.to(UpperCamelCase_ )
           pipe.set_progress_bar_config(disable=UpperCamelCase_ )
           pipe.enable_attention_slicing(1 )
           pipe.enable_sequential_cpu_offload()

           __magic_name__      				=    self.get_inputs()
           __magic_name__      				=    pipe(**UpperCamelCase_ )

           __magic_name__      				=    torch.cuda.max_memory_allocated()
           # make sure that less than 2.2 GB is allocated
           assert mem_bytes < 2.2 * 1_0**9







    def 				a__			(		self :	List[str] ):





           '''simple docstring'''

           __magic_name__      				=    self.get_inputs()
           # resize to resolution that is divisible by 8 but not 16 or 32
           __magic_name__      				=    inputs['image'].resize((5_0_4, 5_0_4) )

           __magic_name__      				=    'timbrooks/instruct-pix2pix'
           __magic_name__      				=    StableDiffusionInstructPixaPixPipeline.from_pretrained(
               UpperCamelCase_				,    safety_checker=UpperCamelCase_				,    )
           pipe.to(UpperCamelCase_ )
           pipe.set_progress_bar_config(disable=UpperCamelCase_ )
           pipe.enable_attention_slicing()

           __magic_name__      				=    pipe(**UpperCamelCase_ )
           __magic_name__      				=    output.images[0]

           __magic_name__      				=    image[2_5_5:2_5_8, 3_8_3:3_8_6, -1]

           assert image.shape == (5_0_4, 5_0_4, 3)
           __magic_name__      				=    np.array([0.2_726, 0.2_529, 0.2_664, 0.2_655, 0.2_641, 0.2_642, 0.2_591, 0.2_649, 0.2_590] )

           assert np.abs(image_slice.flatten() - expected_slice ).max() < 5e-3







"""simple docstring"""
from __future__ import annotations

import random
import unittest

from transformers import TransfoXLConfig, 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
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import (
     TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST,
     TFTransfoXLForSequenceClassification,
     TFTransfoXLLMHeadModel,
     TFTransfoXLModel,
 )



class        UpperCamelCase__ :







    """simple docstring"""







    def __init__(		self :	int				,    UpperCamelCase_ :	Union[str, Any]				,    ):





           '''simple docstring'''

           __magic_name__      				=    parent
           __magic_name__      				=    1_3
           __magic_name__      				=    7
           __magic_name__      				=    3_0
           __magic_name__      				=    self.seq_length + self.mem_len
           __magic_name__      				=    1_5
           __magic_name__      				=    True
           __magic_name__      				=    True
           __magic_name__      				=    9_9
           __magic_name__      				=    [1_0, 5_0, 8_0]
           __magic_name__      				=    3_2
           __magic_name__      				=    3_2
           __magic_name__      				=    4
           __magic_name__      				=    8
           __magic_name__      				=    1_2_8
           __magic_name__      				=    2
           __magic_name__      				=    2
           __magic_name__      				=    None
           __magic_name__      				=    1
           __magic_name__      				=    0
           __magic_name__      				=    3
           __magic_name__      				=    self.vocab_size - 1
           __magic_name__      				=    0.01







    def 				a__			(		self :	Dict ):





           '''simple docstring'''

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

           __magic_name__      				=    None
           if self.use_labels:
                  __magic_name__      				=    ids_tensor([self.batch_size, self.seq_length]				,    self.vocab_size )

           __magic_name__      				=    TransfoXLConfig(
               vocab_size=self.vocab_size				,    mem_len=self.mem_len				,    clamp_len=self.clamp_len				,    cutoffs=self.cutoffs				,    d_model=self.hidden_size				,    d_embed=self.d_embed				,    n_head=self.num_attention_heads				,    d_head=self.d_head				,    d_inner=self.d_inner				,    div_val=self.div_val				,    n_layer=self.num_hidden_layers				,    eos_token_id=self.eos_token_id				,    pad_token_id=self.vocab_size - 1				,    init_range=self.init_range				,    num_labels=self.num_labels				,    )

           return (config, input_ids_a, input_ids_a, lm_labels)







    def 				a__			(		self :	Optional[Any] ):





           '''simple docstring'''

           random.seed(self.seed )
           tf.random.set_seed(self.seed )







    def 				a__			(		self :	Dict				,    UpperCamelCase_ :	Optional[int]				,    UpperCamelCase_ :	Optional[Any]				,    UpperCamelCase_ :	int				,    UpperCamelCase_ :	List[Any] ):





           '''simple docstring'''

           __magic_name__      				=    TFTransfoXLModel(UpperCamelCase_ )

           __magic_name__				,  __magic_name__      				=    model(UpperCamelCase_ ).to_tuple()

           __magic_name__      				=    {'input_ids': input_ids_a, 'mems': mems_a}

           __magic_name__				,  __magic_name__      				=    model(UpperCamelCase_ ).to_tuple()

           self.parent.assertEqual(hidden_states_a.shape				,    (self.batch_size, self.seq_length, self.hidden_size) )
           self.parent.assertEqual(hidden_states_a.shape				,    (self.batch_size, self.seq_length, self.hidden_size) )
           self.parent.assertListEqual(
               [mem.shape for mem in mems_a]				,    [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers				,    )
           self.parent.assertListEqual(
               [mem.shape for mem in mems_a]				,    [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers				,    )







    def 				a__			(		self :	Optional[int]				,    UpperCamelCase_ :	Union[str, Any]				,    UpperCamelCase_ :	Tuple				,    UpperCamelCase_ :	Optional[Any]				,    UpperCamelCase_ :	Tuple ):





           '''simple docstring'''

           __magic_name__      				=    TFTransfoXLLMHeadModel(UpperCamelCase_ )

           __magic_name__				,  __magic_name__      				=    model(UpperCamelCase_ ).to_tuple()

           __magic_name__      				=    {'input_ids': input_ids_a, 'labels': lm_labels}
           __magic_name__				,  __magic_name__      				=    model(UpperCamelCase_ ).to_tuple()

           __magic_name__				,  __magic_name__      				=    model([input_ids_a, mems_a] ).to_tuple()

           __magic_name__      				=    {'input_ids': input_ids_a, 'mems': mems_a, 'labels': lm_labels}

           __magic_name__				,  __magic_name__      				=    model(UpperCamelCase_ ).to_tuple()

           self.parent.assertEqual(lm_logits_a.shape				,    (self.batch_size, self.seq_length, self.vocab_size) )
           self.parent.assertListEqual(
               [mem.shape for mem in mems_a]				,    [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers				,    )

           self.parent.assertEqual(lm_logits_a.shape				,    (self.batch_size, self.seq_length, self.vocab_size) )
           self.parent.assertListEqual(
               [mem.shape for mem in mems_a]				,    [(self.mem_len, self.batch_size, self.hidden_size)] * self.num_hidden_layers				,    )







    def 				a__			(		self :	Union[str, Any]				,    UpperCamelCase_ :	Optional[int]				,    UpperCamelCase_ :	Tuple				,    UpperCamelCase_ :	Tuple				,    UpperCamelCase_ :	Dict ):





           '''simple docstring'''

           __magic_name__      				=    TFTransfoXLForSequenceClassification(UpperCamelCase_ )
           __magic_name__      				=    model(UpperCamelCase_ )
           self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.num_labels) )







    def 				a__			(		self :	Dict ):





           '''simple docstring'''

           __magic_name__      				=    self.prepare_config_and_inputs()
           ((__magic_name__)				,  (__magic_name__)				,  (__magic_name__)				,  (__magic_name__))      				=    config_and_inputs
           __magic_name__      				=    {'input_ids': input_ids_a}
           return config, inputs_dict



@require_tf
class        UpperCamelCase__ (				a_							,	a_							,	unittest.TestCase):







    """simple docstring"""
    __UpperCAmelCase           = (
        (TFTransfoXLModel, TFTransfoXLLMHeadModel, TFTransfoXLForSequenceClassification) if is_tf_available() else ()
    )
    __UpperCAmelCase           = () if is_tf_available() else ()
    __UpperCAmelCase           = (
        {
            """feature-extraction""": TFTransfoXLModel,
            """text-classification""": TFTransfoXLForSequenceClassification,
            """text-generation""": TFTransfoXLLMHeadModel,
            """zero-shot""": TFTransfoXLForSequenceClassification,
        }
        if is_tf_available()
        else {}
    )
    # TODO: add this test when TFTransfoXLLMHead has a linear output layer implemented
    __UpperCAmelCase           = False
    __UpperCAmelCase           = False
    __UpperCAmelCase           = False
    __UpperCAmelCase           = False







    def 				a__			(		self :	int				,    UpperCamelCase_ :	Dict				,    UpperCamelCase_ :	List[Any]				,    UpperCamelCase_ :	Optional[int]				,    UpperCamelCase_ :	List[str]				,    UpperCamelCase_ :	Tuple ):





           '''simple docstring'''

           if pipeline_test_casse_name == "TextGenerationPipelineTests":
                  # Get `ValueError: AttributeError: 'NoneType' object has no attribute 'new_ones'` or `AssertionError`.
                  # `TransfoXLConfig` was never used in pipeline tests: cannot create a simple
                  # tokenizer.
                  return True

           return False







    def 				a__			(		self :	Union[str, Any] ):





           '''simple docstring'''

           __magic_name__      				=    TFTransfoXLModelTester(self )
           __magic_name__      				=    ConfigTester(self				,    config_class=UpperCamelCase_				,    d_embed=3_7 )







    def 				a__			(		self :	List[str] ):





           '''simple docstring'''

           self.config_tester.run_common_tests()







    def 				a__			(		self :	int ):





           '''simple docstring'''

           self.model_tester.set_seed()
           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_transfo_xl_model(*UpperCamelCase_ )







    def 				a__			(		self :	str ):





           '''simple docstring'''

           self.model_tester.set_seed()
           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_transfo_xl_lm_head(*UpperCamelCase_ )







    def 				a__			(		self :	Any ):





           '''simple docstring'''

           __magic_name__      				=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_transfo_xl_for_sequence_classification(*UpperCamelCase_ )







    def 				a__			(		self :	List[str] ):





           '''simple docstring'''

           __magic_name__				,  __magic_name__      				=    self.model_tester.prepare_config_and_inputs_for_common()
           __magic_name__      				=    [TFTransfoXLForSequenceClassification]

           for model_class in self.all_model_classes:
                  __magic_name__      				=    model_class(UpperCamelCase_ )
                  assert isinstance(model.get_input_embeddings()				,    tf.keras.layers.Layer )
                  if model_class in list_other_models_with_output_ebd:
                         __magic_name__      				=    model.get_output_embeddings()
                         assert isinstance(UpperCamelCase_				,    tf.keras.layers.Layer )
                         __magic_name__      				=    model.get_bias()
                         assert name is None
                  else:
                         __magic_name__      				=    model.get_output_embeddings()
                         assert x is None
                         __magic_name__      				=    model.get_bias()
                         assert name is None







    def 				a__			(		self :	int ):





           '''simple docstring'''

           pass







    @slow
    def 				a__			(		self :	str ):





           '''simple docstring'''

           for model_name in TF_TRANSFO_XL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                  __magic_name__      				=    TFTransfoXLModel.from_pretrained(UpperCamelCase_ )
                  self.assertIsNotNone(UpperCamelCase_ )







    @unittest.skip(reason='This model doesn\'t play well with fit() due to not returning a single loss.' )
    def 				a__			(		self :	List[str] ):





           '''simple docstring'''

           pass



@require_tf
class        UpperCamelCase__ (				unittest.TestCase):







    """simple docstring"""







    @unittest.skip('Skip test until #12651 is resolved.' )
    @slow
    def 				a__			(		self :	Optional[Any] ):





           '''simple docstring'''

           __magic_name__      				=    TFTransfoXLLMHeadModel.from_pretrained('transfo-xl-wt103' )
           # fmt: off
           __magic_name__      				=    tf.convert_to_tensor([[3_3,1_2_9_7,2,1,1_0_0_9,4,1_1_0_9,1_1_7_3_9,4_7_6_2,3_5_8,5,2_5,2_4_5,2_2,1_7_0_6,1_7,2_0_0_9_8,5,3_2_1_5,2_1,3_7,1_1_1_0,3,1_3,1_0_4_1,4,2_4,6_0_3,4_9_0,2,7_1_4_7_7,2_0_0_9_8,1_0_4_4_4_7,2,2_0_9_6_1,1,2_6_0_4,4,1,3_2_9,3,6_2_2_4,8_3_1,1_6_0_0_2,2,8,6_0_3,7_8_9_6_7,2_9_5_4_6,2_3,8_0_3,2_0,2_5,4_1_6,5,8,2_3_2,4,2_7_7,6,1_8_5_5,4_6_0_1,3,2_9_5_4_6,5_4,8,3_6_0_9,5,5_7_2_1_1,4_9,4,1,2_7_7,1_8,8,1_7_5_5,1_5_6_9_1,3,3_4_1,2_5,4_1_6,6_9_3,4_2_5_7_3,7_1,1_7,4_0_1,9_4,3_1,1_7_9_1_9,2,2_9_5_4_6,7_8_7_3,1_8,1,4_3_5,2_3,1_1_0_1_1,7_5_5,5,5_1_6_7,3,7_9_8_3,9_8,8_4,2,2_9_5_4_6,3_2_6_7,8,3_6_0_9,4,1,4_8_6_5,1_0_7_5,2,6_0_8_7,7_1,6,3_4_6,8,5_8_5_4,3,2_9_5_4_6,8_2_4,1_4_0_0,1_8_6_8,2,1_9,1_6_0,2,3_1_1,8,5_4_9_6,2,2_0_9_2_0,1_7,2_5,1_5_0_9_7,3,2_4,2_4,0]]				,    dtype=tf.intaa )  # noqa: E231
           # fmt: on
           #  In 1991 , the remains of Russian Tsar Nicholas II and his family
           #  ( except for Alexei and Maria ) are discovered .
           #  The voice of Nicholas's young son , Tsarevich Alexei Nikolaevich , narrates the
           #  remainder of the story . 1883 Western Siberia ,
           #  a young Grigori Rasputin is asked by his father and a group of men to perform magic .
           #  Rasputin has a vision and denounces one of the men as a horse thief . Although his
           #  father initially slaps him for making such an accusation , Rasputin watches as the
           #  man is chased outside and beaten . Twenty years later , Rasputin sees a vision of
           #  the Virgin Mary , prompting him to become a priest . Rasputin quickly becomes famous ,
           #  with people , even a bishop , begging for his blessing . <eod> </s> <eos>

           # fmt: off
           __magic_name__      				=    [3_3,1_2_9_7,2,1,1_0_0_9,4,1_1_0_9,1_1_7_3_9,4_7_6_2,3_5_8,5,2_5,2_4_5,2_2,1_7_0_6,1_7,2_0_0_9_8,5,3_2_1_5,2_1,3_7,1_1_1_0,3,1_3,1_0_4_1,4,2_4,6_0_3,4_9_0,2,7_1_4_7_7,2_0_0_9_8,1_0_4_4_4_7,2,2_0_9_6_1,1,2_6_0_4,4,1,3_2_9,3,6_2_2_4,8_3_1,1_6_0_0_2,2,8,6_0_3,7_8_9_6_7,2_9_5_4_6,2_3,8_0_3,2_0,2_5,4_1_6,5,8,2_3_2,4,2_7_7,6,1_8_5_5,4_6_0_1,3,2_9_5_4_6,5_4,8,3_6_0_9,5,5_7_2_1_1,4_9,4,1,2_7_7,1_8,8,1_7_5_5,1_5_6_9_1,3,3_4_1,2_5,4_1_6,6_9_3,4_2_5_7_3,7_1,1_7,4_0_1,9_4,3_1,1_7_9_1_9,2,2_9_5_4_6,7_8_7_3,1_8,1,4_3_5,2_3,1_1_0_1_1,7_5_5,5,5_1_6_7,3,7_9_8_3,9_8,8_4,2,2_9_5_4_6,3_2_6_7,8,3_6_0_9,4,1,4_8_6_5,1_0_7_5,2,6_0_8_7,7_1,6,3_4_6,8,5_8_5_4,3,2_9_5_4_6,8_2_4,1_4_0_0,1_8_6_8,2,1_9,1_6_0,2,3_1_1,8,5_4_9_6,2,2_0_9_2_0,1_7,2_5,1_5_0_9_7,3,2_4,2_4,0,3_3,1,1_8_5_7,2,1,1_0_0_9,4,1_1_0_9,1_1_7_3_9,4_7_6_2,3_5_8,5,2_5,2_4_5,2_8,1_1_1_0,3,1_3,1_0_4_1,4,2_4,6_0_3,4_9_0,2,7_1_4_7_7,2_0_0_9_8,1_0_4_4_4_7,2,2_0_9_6_1,1,2_6_0_4,4,1,3_2_9,3,0]  # noqa: E231
           # fmt: on
           #  In 1991, the remains of Russian Tsar Nicholas II and his family (
           #  except for Alexei and Maria ) are discovered. The voice of young son,
           #  Tsarevich Alexei Nikolaevich, narrates the remainder of the story.
           #  1883 Western Siberia, a young Grigori Rasputin is asked by his father
           #  and a group of men to perform magic. Rasputin has a vision and
           #  denounces one of the men as a horse thief. Although his father initially
           #  slaps him for making such an accusation, Rasputin watches as the man
           #  is chased outside and beaten. Twenty years later, Rasputin sees a vision
           #  of the Virgin Mary, prompting him to become a priest.
           #  Rasputin quickly becomes famous, with people, even a bishop, begging for
           #  his blessing. <unk> <unk> <eos> In the 1990s, the remains of Russian Tsar
           # Nicholas II and his family were discovered. The voice of <unk> young son,
           # Tsarevich Alexei Nikolaevich, narrates the remainder of the story.<eos>

           __magic_name__      				=    model.generate(UpperCamelCase_				,    max_length=2_0_0				,    do_sample=UpperCamelCase_ )
           self.assertListEqual(output_ids[0].numpy().tolist()				,    UpperCamelCase_ )