'''simple docstring'''
# This model implementation is heavily based on:

import gc
import random
import tempfile
import unittest

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

from diffusers import (
    AutoencoderKL,
    ControlNetModel,
    DDIMScheduler,
    StableDiffusionControlNetInpaintPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion.pipeline_stable_diffusion_controlnet import MultiControlNetModel
from diffusers.utils import floats_tensor, load_image, load_numpy, randn_tensor, slow, torch_device
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import (
    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS,
    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS,
    TEXT_TO_IMAGE_IMAGE_PARAMS,
)
from ..test_pipelines_common import (
    PipelineKarrasSchedulerTesterMixin,
    PipelineLatentTesterMixin,
    PipelineTesterMixin,
)


enable_full_determinism()






class       __lowercase		(    lowercase_      ,				lowercase_      ,				lowercase_      ,				unittest.TestCase    ):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		 =    StableDiffusionControlNetInpaintPipeline
     SCREAMING_SNAKE_CASE__		 =    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
     SCREAMING_SNAKE_CASE__		 =    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
     SCREAMING_SNAKE_CASE__		 =    frozenset({'''control_image'''}    )  # skip `image` and `mask` for now, only test for control_image
     SCREAMING_SNAKE_CASE__		 =    TEXT_TO_IMAGE_IMAGE_PARAMS







     def lowerCAmelCase  (		self     ):
         torch.manual_seed(0     )
         __UpperCamelCase						:						Union[str, Any]		     =	UNetaDConditionModel(
             block_out_channels=(3_2, 6_4)  ,      layers_per_block=2  ,      sample_size=3_2  ,      in_channels=9  ,      out_channels=4  ,      down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')  ,      up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')  ,      cross_attention_dim=3_2  ,      )
         torch.manual_seed(0     )
         __UpperCamelCase						:						Dict		     =	ControlNetModel(
             block_out_channels=(3_2, 6_4)  ,      layers_per_block=2  ,      in_channels=4  ,      down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')  ,      cross_attention_dim=3_2  ,      conditioning_embedding_out_channels=(1_6, 3_2)  ,      )
         torch.manual_seed(0     )
         __UpperCamelCase						:						Tuple		     =	DDIMScheduler(
             beta_start=0.0_0_0_8_5  ,      beta_end=0.0_1_2  ,      beta_schedule='scaled_linear'  ,      clip_sample=a__  ,      set_alpha_to_one=a__  ,      )
         torch.manual_seed(0     )
         __UpperCamelCase						:						List[str]		     =	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     )
         __UpperCamelCase						:						List[str]		     =	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  ,      )
         __UpperCamelCase						:						Optional[int]		     =	CLIPTextModel(a__     )
         __UpperCamelCase						:						int		     =	CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'     )

         __UpperCamelCase						:						Optional[int]		     =	{
             'unet': unet,
             'controlnet': controlnet,
             'scheduler': scheduler,
             'vae': vae,
             'text_encoder': text_encoder,
             'tokenizer': tokenizer,
             'safety_checker': None,
             'feature_extractor': None,
         }
         return components







     def lowerCAmelCase  (		self  ,      _lowerCamelCase  ,      _lowerCamelCase=0     ):
         if str(a__     ).startswith('mps'     ):
             __UpperCamelCase						:						Tuple		     =	torch.manual_seed(a__     )
         else:
             __UpperCamelCase						:						Any		     =	torch.Generator(device=a__     ).manual_seed(a__     )

         __UpperCamelCase						:						Tuple		     =	2
         __UpperCamelCase						:						Optional[int]		     =	randn_tensor(
             (1, 3, 3_2 * controlnet_embedder_scale_factor, 3_2 * controlnet_embedder_scale_factor)  ,      generator=a__  ,      device=torch.device(a__     )  ,      )
         __UpperCamelCase						:						Union[str, Any]		     =	floats_tensor((1, 3, 3_2, 3_2)  ,      rng=random.Random(a__     )     ).to(a__     )
         __UpperCamelCase						:						str		     =	init_image.cpu().permute(0  ,      2  ,      3  ,      1     )[0]

         __UpperCamelCase						:						Union[str, Any]		     =	Image.fromarray(np.uinta(a__     )     ).convert('RGB'     ).resize((6_4, 6_4)     )
         __UpperCamelCase						:						Union[str, Any]		     =	Image.fromarray(np.uinta(init_image + 4     )     ).convert('RGB'     ).resize((6_4, 6_4)     )

         __UpperCamelCase						:						Optional[int]		     =	{
             'prompt': 'A painting of a squirrel eating a burger',
             'generator': generator,
             'num_inference_steps': 2,
             'guidance_scale': 6.0,
             'output_type': 'numpy',
             'image': image,
             'mask_image': mask_image,
             'control_image': control_image,
         }

         return inputs







     def lowerCAmelCase  (		self     ):
         return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3     )







     @unittest.skipIf(
         torch_device != 'cuda' or not is_xformers_available()  ,      reason='XFormers attention is only available with CUDA and `xformers` installed'  ,      )
     def lowerCAmelCase  (		self     ):
         self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3     )







     def lowerCAmelCase  (		self     ):
         self._test_inference_batch_single_identical(expected_max_diff=2E-3     )






class       __lowercase		(    lowercase_    ):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		 =    StableDiffusionControlNetInpaintPipeline
     SCREAMING_SNAKE_CASE__		 =    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
     SCREAMING_SNAKE_CASE__		 =    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS
     SCREAMING_SNAKE_CASE__		 =    frozenset([]    )







     def lowerCAmelCase  (		self     ):
         torch.manual_seed(0     )
         __UpperCamelCase						:						str		     =	UNetaDConditionModel(
             block_out_channels=(3_2, 6_4)  ,      layers_per_block=2  ,      sample_size=3_2  ,      in_channels=4  ,      out_channels=4  ,      down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')  ,      up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')  ,      cross_attention_dim=3_2  ,      )
         torch.manual_seed(0     )
         __UpperCamelCase						:						str		     =	ControlNetModel(
             block_out_channels=(3_2, 6_4)  ,      layers_per_block=2  ,      in_channels=4  ,      down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')  ,      cross_attention_dim=3_2  ,      conditioning_embedding_out_channels=(1_6, 3_2)  ,      )
         torch.manual_seed(0     )
         __UpperCamelCase						:						List[Any]		     =	DDIMScheduler(
             beta_start=0.0_0_0_8_5  ,      beta_end=0.0_1_2  ,      beta_schedule='scaled_linear'  ,      clip_sample=a__  ,      set_alpha_to_one=a__  ,      )
         torch.manual_seed(0     )
         __UpperCamelCase						:						Optional[Any]		     =	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     )
         __UpperCamelCase						:						Tuple		     =	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  ,      )
         __UpperCamelCase						:						List[Any]		     =	CLIPTextModel(a__     )
         __UpperCamelCase						:						Union[str, Any]		     =	CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'     )

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






class       __lowercase		(    lowercase_      ,				lowercase_      ,				unittest.TestCase    ):







     """simple docstring"""






     SCREAMING_SNAKE_CASE__		 =    StableDiffusionControlNetInpaintPipeline
     SCREAMING_SNAKE_CASE__		 =    TEXT_GUIDED_IMAGE_INPAINTING_PARAMS
     SCREAMING_SNAKE_CASE__		 =    TEXT_GUIDED_IMAGE_INPAINTING_BATCH_PARAMS







     def lowerCAmelCase  (		self     ):
         torch.manual_seed(0     )
         __UpperCamelCase						:						List[str]		     =	UNetaDConditionModel(
             block_out_channels=(3_2, 6_4)  ,      layers_per_block=2  ,      sample_size=3_2  ,      in_channels=9  ,      out_channels=4  ,      down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')  ,      up_block_types=('CrossAttnUpBlock2D', 'UpBlock2D')  ,      cross_attention_dim=3_2  ,      )
         torch.manual_seed(0     )

         def init_weights(_lowerCamelCase     ):
             if isinstance(a__  ,      torch.nn.Convad     ):
                 torch.nn.init.normal(m.weight     )
                 m.bias.data.fill_(1.0     )

         __UpperCamelCase						:						Union[str, Any]		     =	ControlNetModel(
             block_out_channels=(3_2, 6_4)  ,      layers_per_block=2  ,      in_channels=4  ,      down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')  ,      cross_attention_dim=3_2  ,      conditioning_embedding_out_channels=(1_6, 3_2)  ,      )
         controlneta.controlnet_down_blocks.apply(a__     )

         torch.manual_seed(0     )
         __UpperCamelCase						:						Union[str, Any]		     =	ControlNetModel(
             block_out_channels=(3_2, 6_4)  ,      layers_per_block=2  ,      in_channels=4  ,      down_block_types=('DownBlock2D', 'CrossAttnDownBlock2D')  ,      cross_attention_dim=3_2  ,      conditioning_embedding_out_channels=(1_6, 3_2)  ,      )
         controlneta.controlnet_down_blocks.apply(a__     )

         torch.manual_seed(0     )
         __UpperCamelCase						:						List[Any]		     =	DDIMScheduler(
             beta_start=0.0_0_0_8_5  ,      beta_end=0.0_1_2  ,      beta_schedule='scaled_linear'  ,      clip_sample=a__  ,      set_alpha_to_one=a__  ,      )
         torch.manual_seed(0     )
         __UpperCamelCase						:						Optional[int]		     =	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     )
         __UpperCamelCase						:						Tuple		     =	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  ,      )
         __UpperCamelCase						:						Tuple		     =	CLIPTextModel(a__     )
         __UpperCamelCase						:						List[Any]		     =	CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip'     )

         __UpperCamelCase						:						Union[str, Any]		     =	MultiControlNetModel([controlneta, controlneta]     )

         __UpperCamelCase						:						Union[str, Any]		     =	{
             'unet': unet,
             'controlnet': controlnet,
             'scheduler': scheduler,
             'vae': vae,
             'text_encoder': text_encoder,
             'tokenizer': tokenizer,
             'safety_checker': None,
             'feature_extractor': None,
         }
         return components







     def lowerCAmelCase  (		self  ,      _lowerCamelCase  ,      _lowerCamelCase=0     ):
         if str(a__     ).startswith('mps'     ):
             __UpperCamelCase						:						str		     =	torch.manual_seed(a__     )
         else:
             __UpperCamelCase						:						str		     =	torch.Generator(device=a__     ).manual_seed(a__     )

         __UpperCamelCase						:						List[str]		     =	2

         __UpperCamelCase						:						Optional[int]		     =	[
             randn_tensor(
                 (1, 3, 3_2 * controlnet_embedder_scale_factor, 3_2 * controlnet_embedder_scale_factor)  ,      generator=a__  ,      device=torch.device(a__     )  ,      ),
             randn_tensor(
                 (1, 3, 3_2 * controlnet_embedder_scale_factor, 3_2 * controlnet_embedder_scale_factor)  ,      generator=a__  ,      device=torch.device(a__     )  ,      ),
         ]
         __UpperCamelCase						:						Tuple		     =	floats_tensor((1, 3, 3_2, 3_2)  ,      rng=random.Random(a__     )     ).to(a__     )
         __UpperCamelCase						:						List[str]		     =	init_image.cpu().permute(0  ,      2  ,      3  ,      1     )[0]

         __UpperCamelCase						:						int		     =	Image.fromarray(np.uinta(a__     )     ).convert('RGB'     ).resize((6_4, 6_4)     )
         __UpperCamelCase						:						Tuple		     =	Image.fromarray(np.uinta(init_image + 4     )     ).convert('RGB'     ).resize((6_4, 6_4)     )

         __UpperCamelCase						:						Union[str, Any]		     =	{
             'prompt': 'A painting of a squirrel eating a burger',
             'generator': generator,
             'num_inference_steps': 2,
             'guidance_scale': 6.0,
             'output_type': 'numpy',
             'image': image,
             'mask_image': mask_image,
             'control_image': control_image,
         }

         return inputs







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						int		     =	self.get_dummy_components()
         __UpperCamelCase						:						List[Any]		     =	self.pipeline_class(**a__     )
         pipe.to(a__     )

         __UpperCamelCase						:						Optional[Any]		     =	1_0.0
         __UpperCamelCase						:						Any		     =	4

         __UpperCamelCase						:						Tuple		     =	self.get_dummy_inputs(a__     )
         __UpperCamelCase						:						int		     =	steps
         __UpperCamelCase						:						Dict		     =	scale
         __UpperCamelCase						:						List[str]		     =	pipe(**a__     )[0]

         __UpperCamelCase						:						Any		     =	self.get_dummy_inputs(a__     )
         __UpperCamelCase						:						Union[str, Any]		     =	steps
         __UpperCamelCase						:						List[str]		     =	scale
         __UpperCamelCase						:						Tuple		     =	pipe(**a__  ,      control_guidance_start=0.1  ,      control_guidance_end=0.2     )[0]

         __UpperCamelCase						:						List[Any]		     =	self.get_dummy_inputs(a__     )
         __UpperCamelCase						:						Dict		     =	steps
         __UpperCamelCase						:						int		     =	scale
         __UpperCamelCase						:						List[Any]		     =	pipe(**a__  ,      control_guidance_start=[0.1, 0.3]  ,      control_guidance_end=[0.2, 0.7]     )[0]

         __UpperCamelCase						:						List[Any]		     =	self.get_dummy_inputs(a__     )
         __UpperCamelCase						:						Any		     =	steps
         __UpperCamelCase						:						str		     =	scale
         __UpperCamelCase						:						Any		     =	pipe(**a__  ,      control_guidance_start=0.4  ,      control_guidance_end=[0.5, 0.8]     )[0]

         # make sure that all outputs are different
         assert np.sum(np.abs(output_a - output_a     )     ) > 1E-3
         assert np.sum(np.abs(output_a - output_a     )     ) > 1E-3
         assert np.sum(np.abs(output_a - output_a     )     ) > 1E-3







     def lowerCAmelCase  (		self     ):
         return self._test_attention_slicing_forward_pass(expected_max_diff=2E-3     )







     @unittest.skipIf(
         torch_device != 'cuda' or not is_xformers_available()  ,      reason='XFormers attention is only available with CUDA and `xformers` installed'  ,      )
     def lowerCAmelCase  (		self     ):
         self._test_xformers_attention_forwardGenerator_pass(expected_max_diff=2E-3     )







     def lowerCAmelCase  (		self     ):
         self._test_inference_batch_single_identical(expected_max_diff=2E-3     )







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Tuple		     =	self.get_dummy_components()
         __UpperCamelCase						:						List[Any]		     =	self.pipeline_class(**a__     )
         pipe.to(a__     )
         pipe.set_progress_bar_config(disable=a__     )
         with tempfile.TemporaryDirectory() as tmpdir:
             try:
                 # save_pretrained is not implemented for Multi-ControlNet
                 pipe.save_pretrained(a__     )
             except NotImplementedError:
                 pass






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







     """simple docstring"""






     def lowerCAmelCase  (		self     ):
         super().tearDown()
         gc.collect()
         torch.cuda.empty_cache()







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						Union[str, Any]		     =	ControlNetModel.from_pretrained('lllyasviel/sd-controlnet-canny'     )

         __UpperCamelCase						:						Dict		     =	StableDiffusionControlNetInpaintPipeline.from_pretrained(
             'runwayml/stable-diffusion-inpainting'  ,      safety_checker=a__  ,      controlnet=a__     )
         pipe.enable_model_cpu_offload()
         pipe.set_progress_bar_config(disable=a__     )

         __UpperCamelCase						:						List[Any]		     =	torch.Generator(device='cpu'     ).manual_seed(0     )
         __UpperCamelCase						:						Dict		     =	load_image(
             'https://huggingface.co/lllyasviel/sd-controlnet-canny/resolve/main/images/bird.png'     ).resize((5_1_2, 5_1_2)     )

         __UpperCamelCase						:						int		     =	load_image(
             'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main'
             '/stable_diffusion_inpaint/input_bench_mask.png'     ).resize((5_1_2, 5_1_2)     )

         __UpperCamelCase						:						str		     =	'pitch black hole'

         __UpperCamelCase						:						Tuple		     =	load_image(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/bird_canny.png'     ).resize((5_1_2, 5_1_2)     )

         __UpperCamelCase						:						List[Any]		     =	pipe(
             a__  ,      image=a__  ,      mask_image=a__  ,      control_image=a__  ,      generator=a__  ,      output_type='np'  ,      num_inference_steps=3  ,      )

         __UpperCamelCase						:						List[Any]		     =	output.images[0]

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

         __UpperCamelCase						:						Optional[Any]		     =	load_numpy(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/inpaint.npy'     )

         assert np.abs(expected_image - image     ).max() < 9E-2







     def lowerCAmelCase  (		self     ):
         __UpperCamelCase						:						int		     =	ControlNetModel.from_pretrained('lllyasviel/control_v11p_sd15_inpaint'     )

         __UpperCamelCase						:						Any		     =	StableDiffusionControlNetInpaintPipeline.from_pretrained(
             'runwayml/stable-diffusion-v1-5'  ,      safety_checker=a__  ,      controlnet=a__     )
         __UpperCamelCase						:						Dict		     =	DDIMScheduler.from_config(pipe.scheduler.config     )
         pipe.enable_model_cpu_offload()
         pipe.set_progress_bar_config(disable=a__     )

         __UpperCamelCase						:						Optional[int]		     =	torch.Generator(device='cpu'     ).manual_seed(3_3     )

         __UpperCamelCase						:						Any		     =	load_image(
             'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy.png'     )
         __UpperCamelCase						:						Tuple		     =	init_image.resize((5_1_2, 5_1_2)     )

         __UpperCamelCase						:						Optional[int]		     =	load_image(
             'https://huggingface.co/datasets/diffusers/test-arrays/resolve/main/stable_diffusion_inpaint/boy_mask.png'     )
         __UpperCamelCase						:						Union[str, Any]		     =	mask_image.resize((5_1_2, 5_1_2)     )

         __UpperCamelCase						:						Dict		     =	'a handsome man with ray-ban sunglasses'

         def make_inpaint_condition(_lowerCamelCase  ,      _lowerCamelCase     ):
             __UpperCamelCase						:						Dict		     =	np.array(image.convert('RGB'     )     ).astype(np.floataa     ) / 2_5_5.0
             __UpperCamelCase						:						List[str]		     =	np.array(image_mask.convert('L'     )     ).astype(np.floataa     ) / 2_5_5.0

             assert image.shape[0:1] == image_mask.shape[0:1], "image and image_mask must have the same image size"
             __UpperCamelCase						:						Dict		     =	-1.0  # set as masked pixel
             __UpperCamelCase						:						Union[str, Any]		     =	np.expand_dims(a__  ,      0     ).transpose(0  ,      3  ,      1  ,      2     )
             __UpperCamelCase						:						str		     =	torch.from_numpy(a__     )
             return image

         __UpperCamelCase						:						str		     =	make_inpaint_condition(a__  ,      a__     )

         __UpperCamelCase						:						int		     =	pipe(
             a__  ,      image=a__  ,      mask_image=a__  ,      control_image=a__  ,      guidance_scale=9.0  ,      eta=1.0  ,      generator=a__  ,      num_inference_steps=2_0  ,      output_type='np'  ,      )
         __UpperCamelCase						:						str		     =	output.images[0]

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

         __UpperCamelCase						:						Union[str, Any]		     =	load_numpy(
             'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/sd_controlnet/boy_ray_ban.npy'     )

         assert np.abs(expected_image - image     ).max() < 9E-2





from collections import OrderedDict
from typing import Any, List, Mapping, Optional

from ... import PreTrainedTokenizer, TensorType, is_torch_available
from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import logging


_UpperCAmelCase    			=					logging.get_logger(__name__)

_UpperCAmelCase    			=					{
    "gpt2": "https://huggingface.co/gpt2/resolve/main/config.json",
    "gpt2-medium": "https://huggingface.co/gpt2-medium/resolve/main/config.json",
    "gpt2-large": "https://huggingface.co/gpt2-large/resolve/main/config.json",
    "gpt2-xl": "https://huggingface.co/gpt2-xl/resolve/main/config.json",
    "distilgpt2": "https://huggingface.co/distilgpt2/resolve/main/config.json",
}

class 		__magic_name__				( lowercase_					):


  """simple docstring"""





  _UpperCamelCase						 = "gpt2"
  _UpperCamelCase						 = ["past_key_values"]
  _UpperCamelCase						 = {
      "hidden_size": "n_embd",
      "max_position_embeddings": "n_positions",
      "num_attention_heads": "n_head",
      "num_hidden_layers": "n_layer",
  }


  def __init__(					self	,		a__=5_02_57	,		a__=10_24	,		a__=7_68	,		a__=12	,		a__=12	,		a__=None	,		a__="gelu_new"	,		a__=0.1	,		a__=0.1	,		a__=0.1	,		a__=1E-5	,		a__=0.02	,		a__="cls_index"	,		a__=True	,		a__=None	,		a__=True	,		a__=0.1	,		a__=True	,		a__=True	,		a__=5_02_56	,		a__=5_02_56	,		a__=False	,		a__=False	,		**a__	,		):
    _lowerCamelCase      =						vocab_size
    _lowerCamelCase      =						n_positions
    _lowerCamelCase      =						n_embd
    _lowerCamelCase      =						n_layer
    _lowerCamelCase      =						n_head
    _lowerCamelCase      =						n_inner
    _lowerCamelCase      =						activation_function
    _lowerCamelCase      =						resid_pdrop
    _lowerCamelCase      =						embd_pdrop
    _lowerCamelCase      =						attn_pdrop
    _lowerCamelCase      =						layer_norm_epsilon
    _lowerCamelCase      =						initializer_range
    _lowerCamelCase      =						summary_type
    _lowerCamelCase      =						summary_use_proj
    _lowerCamelCase      =						summary_activation
    _lowerCamelCase      =						summary_first_dropout
    _lowerCamelCase      =						summary_proj_to_labels
    _lowerCamelCase      =						scale_attn_weights
    _lowerCamelCase      =						use_cache
    _lowerCamelCase      =						scale_attn_by_inverse_layer_idx
    _lowerCamelCase      =						reorder_and_upcast_attn

    _lowerCamelCase      =						bos_token_id
    _lowerCamelCase      =						eos_token_id

    super().__init__(bos_token_id=a__	,		eos_token_id=a__	,		**a__		)







class 		__magic_name__				( lowercase_					):


  """simple docstring"""





  def __init__(					self	,		a__	,		a__ = "default"	,		a__ = None	,		a__ = False	,		):
    super().__init__(a__	,		task=a__	,		patching_specs=a__	,		use_past=a__		)
    if not getattr(self._config	,		'''pad_token_id'''	,		a__		):
      # TODO: how to do that better?
      _lowerCamelCase      =						0


  @property
  def     _UpperCAmelCase  (					self		):
    _lowerCamelCase      =						OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}		)
    if self.use_past:
      self.fill_with_past_key_values_(a__	,		direction='''inputs'''		)
      _lowerCamelCase      =						{0: '''batch''', 1: '''past_sequence + sequence'''}
    else:
      _lowerCamelCase      =						{0: '''batch''', 1: '''sequence'''}

    return common_inputs


  @property
  def     _UpperCAmelCase  (					self		):
    return self._config.n_layer


  @property
  def     _UpperCAmelCase  (					self		):
    return self._config.n_head


  def     _UpperCAmelCase  (					self	,		a__	,		a__ = -1	,		a__ = -1	,		a__ = False	,		a__ = None	,		):
    _lowerCamelCase      =						super(a__	,		self		).generate_dummy_inputs(
        a__	,		batch_size=a__	,		seq_length=a__	,		is_pair=a__	,		framework=a__		)

    # We need to order the input in the way they appears in the forward()
    _lowerCamelCase      =						OrderedDict({'''input_ids''': common_inputs['''input_ids''']}		)

    # Need to add the past_keys
    if self.use_past:
      if not is_torch_available():
        raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''		)
      else:
        import torch

        _lowerCamelCase    ,    _lowerCamelCase      =						common_inputs['''input_ids'''].shape
        # Not using the same length for past_key_values
        _lowerCamelCase      =						seqlen + 2
        _lowerCamelCase      =						(
            batch,
            self.num_attention_heads,
            past_key_values_length,
            self._config.hidden_size // self.num_attention_heads,
        )
        _lowerCamelCase      =						[
            (torch.zeros(a__		), torch.zeros(a__		)) for _ in range(self.num_layers		)
        ]

    _lowerCamelCase      =						common_inputs['''attention_mask''']
    if self.use_past:
      _lowerCamelCase      =						ordered_inputs['''attention_mask'''].dtype
      _lowerCamelCase      =						torch.cat(
          [ordered_inputs['''attention_mask'''], torch.ones(a__	,		a__	,		dtype=a__		)]	,		dim=1		)

    return ordered_inputs


  @property
  def     _UpperCAmelCase  (					self		):
    return 13




