


import gc
import unittest

import numpy as np
import torch
from transformers import CLIPTextConfig, CLIPTextModel, CLIPTextModelWithProjection, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DiffusionPipeline,
    EulerDiscreteScheduler,
    StableDiffusionXLPipeline,
    UNetaDConditionModel,
)
from diffusers.utils import slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch_gpu

from ..pipeline_params import TEXT_TO_IMAGE_BATCH_PARAMS, TEXT_TO_IMAGE_IMAGE_PARAMS, TEXT_TO_IMAGE_PARAMS
from ..test_pipelines_common import PipelineLatentTesterMixin, PipelineTesterMixin


enable_full_determinism()





class 			lowerCAmelCase__(     snake_case__     ,     snake_case__     ,     unittest.TestCase    ):






    '''simple docstring'''





    A_	:  str								=			StableDiffusionXLPipeline
    A_	:  Optional[int]								=			TEXT_TO_IMAGE_PARAMS
    A_	:  List[str]								=			TEXT_TO_IMAGE_BATCH_PARAMS
    A_	:  Optional[Any]								=			TEXT_TO_IMAGE_IMAGE_PARAMS
    A_	:  Union[str, Any]								=			TEXT_TO_IMAGE_IMAGE_PARAMS





    def        _lowerCamelCase			(						self					: Dict						):





       '''simple docstring'''
       torch.manual_seed(0						)
       UpperCAmelCase_				:   List[str]							= UNetaDConditionModel(
           block_out_channels=(32, 64)							, layers_per_block=2							, sample_size=32							, in_channels=4							, out_channels=4							, down_block_types=('''DownBlock2D''', '''CrossAttnDownBlock2D''')							, up_block_types=('''CrossAttnUpBlock2D''', '''UpBlock2D''')							, attention_head_dim=(2, 4)							, use_linear_projection=__snake_case							, addition_embed_type='''text_time'''							, addition_time_embed_dim=8							, transformer_layers_per_block=(1, 2)							, projection_class_embeddings_input_dim=80							, cross_attention_dim=64							, )
       UpperCAmelCase_				:   Tuple							= EulerDiscreteScheduler(
           beta_start=0.00_085							, beta_end=0.012							, steps_offset=1							, beta_schedule='''scaled_linear'''							, timestep_spacing='''leading'''							, )
       torch.manual_seed(0						)
       UpperCAmelCase_				:   List[str]							= AutoencoderKL(
           block_out_channels=[32, 64]							, in_channels=3							, out_channels=3							, down_block_types=['''DownEncoderBlock2D''', '''DownEncoderBlock2D''']							, up_block_types=['''UpDecoderBlock2D''', '''UpDecoderBlock2D''']							, latent_channels=4							, sample_size=128							, )
       torch.manual_seed(0						)
       UpperCAmelCase_				:   Optional[int]							= CLIPTextConfig(
           bos_token_id=0							, eos_token_id=2							, hidden_size=32							, intermediate_size=37							, layer_norm_eps=1E-05							, num_attention_heads=4							, num_hidden_layers=5							, pad_token_id=1							, vocab_size=1_000							, hidden_act='''gelu'''							, projection_dim=32							, )
       UpperCAmelCase_				:   str							= CLIPTextModel(__snake_case						)
       UpperCAmelCase_				:   List[str]							= CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''							, local_files_only=__snake_case						)

       UpperCAmelCase_				:   Tuple							= CLIPTextModelWithProjection(__snake_case						)
       UpperCAmelCase_				:   Optional[Any]							= CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip'''							, local_files_only=__snake_case						)

       UpperCAmelCase_				:   Optional[int]							= {
           '''unet''': unet,
           '''scheduler''': scheduler,
           '''vae''': vae,
           '''text_encoder''': text_encoder,
           '''tokenizer''': tokenizer,
           '''text_encoder_2''': text_encoder_a,
           '''tokenizer_2''': tokenizer_a,
           # "safety_checker": None,
           # "feature_extractor": None,
       }
       return components





    def        _lowerCamelCase			(						self					: str							, __snake_case					: Dict							, __snake_case					: List[Any]=0						):





       '''simple docstring'''
       if str(__snake_case						).startswith('''mps'''						):
          UpperCAmelCase_				:   Tuple							= torch.manual_seed(__snake_case						)
       else:
          UpperCAmelCase_				:   Optional[int]							= torch.Generator(device=__snake_case						).manual_seed(__snake_case						)
       UpperCAmelCase_				:   str							= {
           '''prompt''': '''A painting of a squirrel eating a burger''',
           '''generator''': generator,
           '''num_inference_steps''': 2,
           '''guidance_scale''': 5.0,
           '''output_type''': '''numpy''',
       }
       return inputs





    def        _lowerCamelCase			(						self					: Optional[Any]						):





       '''simple docstring'''
       UpperCAmelCase_				:   Optional[int]							= '''cpu'''  # ensure determinism for the device-dependent torch.Generator
       UpperCAmelCase_				:   List[str]							= self.get_dummy_components()
       UpperCAmelCase_				:   Dict							= StableDiffusionXLPipeline(**__snake_case						)
       UpperCAmelCase_				:   Union[str, Any]							= sd_pipe.to(__snake_case						)
       sd_pipe.set_progress_bar_config(disable=__snake_case						)

       UpperCAmelCase_				:   Any							= self.get_dummy_inputs(__snake_case						)
       UpperCAmelCase_				:   Dict							= sd_pipe(**__snake_case						).images
       UpperCAmelCase_				:   Optional[Any]							= image[0, -3:, -3:, -1]

       assert image.shape == (1, 64, 64, 3)
       UpperCAmelCase_				:   List[str]							= np.array([0.5_873, 0.6_128, 0.4_797, 0.5_122, 0.5_674, 0.4_639, 0.5_227, 0.5_149, 0.4_747]						)

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





    def        _lowerCamelCase			(						self					: str						):





       '''simple docstring'''
       UpperCAmelCase_				:   Tuple							= self.get_dummy_components()
       UpperCAmelCase_				:   Dict							= StableDiffusionXLPipeline(**__snake_case						)
       UpperCAmelCase_				:   Optional[int]							= sd_pipe.to(__snake_case						)
       UpperCAmelCase_				:   str							= sd_pipe.to(__snake_case						)
       sd_pipe.set_progress_bar_config(disable=__snake_case						)

       # forward without prompt embeds
       UpperCAmelCase_				:   List[Any]							= self.get_dummy_inputs(__snake_case						)
       UpperCAmelCase_				:   Union[str, Any]							= 3 * ['''this is a negative prompt''']
       UpperCAmelCase_				:   Optional[int]							= negative_prompt
       UpperCAmelCase_				:   int							= 3 * [inputs['''prompt''']]

       UpperCAmelCase_				:   Optional[Any]							= sd_pipe(**__snake_case						)
       UpperCAmelCase_				:   int							= output.images[0, -3:, -3:, -1]

       # forward with prompt embeds
       UpperCAmelCase_				:   Union[str, Any]							= self.get_dummy_inputs(__snake_case						)
       UpperCAmelCase_				:   Dict							= 3 * ['''this is a negative prompt''']
       UpperCAmelCase_				:   Union[str, Any]							= 3 * [inputs.pop('''prompt'''						)]

       (
           (
           UpperCAmelCase_
       )		,   (
           UpperCAmelCase_
       )		,   (
           UpperCAmelCase_
       )		,   (
           UpperCAmelCase_
       )		,   
       )				:   Tuple							= sd_pipe.encode_prompt(__snake_case							, negative_prompt=__snake_case						)

       UpperCAmelCase_				:   str							= sd_pipe(
           **__snake_case							, prompt_embeds=__snake_case							, negative_prompt_embeds=__snake_case							, pooled_prompt_embeds=__snake_case							, negative_pooled_prompt_embeds=__snake_case							, )
       UpperCAmelCase_				:   List[Any]							= output.images[0, -3:, -3:, -1]

       # make sure that it's equal
       assert np.abs(image_slice_a.flatten() - image_slice_a.flatten()						).max() < 1E-4





    def        _lowerCamelCase			(						self					: List[str]						):





       '''simple docstring'''
       super().test_attention_slicing_forward_pass(expected_max_diff=3E-3						)





    def        _lowerCamelCase			(						self					: str						):





       '''simple docstring'''
       super().test_inference_batch_single_identical(expected_max_diff=3E-3						)





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






    '''simple docstring'''





    def        _lowerCamelCase			(						self					: Optional[Any]						):





       '''simple docstring'''
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()





    def        _lowerCamelCase			(						self					: Tuple							, __snake_case					: Optional[Any]							, __snake_case					: Dict="cpu"							, __snake_case					: str=torch.floataa							, __snake_case					: Dict=0						):





       '''simple docstring'''
       UpperCAmelCase_				:   List[Any]							= torch.Generator(device=__snake_case						).manual_seed(__snake_case						)
       UpperCAmelCase_				:   int							= np.random.RandomState(__snake_case						).standard_normal((1, 4, 64, 64)						)
       UpperCAmelCase_				:   Dict							= torch.from_numpy(__snake_case						).to(device=__snake_case							, dtype=__snake_case						)
       UpperCAmelCase_				:   Dict							= {
           '''prompt''': '''a photograph of an astronaut riding a horse''',
           '''latents''': latents,
           '''generator''': generator,
           '''num_inference_steps''': 3,
           '''guidance_scale''': 7.5,
           '''output_type''': '''numpy''',
       }
       return inputs





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





       '''simple docstring'''
       UpperCAmelCase_				:   str							= DiffusionPipeline.from_pretrained('''stabilityai/stable-diffusion-2-base'''						)
       pipe.to(__snake_case						)
       pipe.set_progress_bar_config(disable=__snake_case						)

       UpperCAmelCase_				:   Union[str, Any]							= self.get_inputs(__snake_case						)
       UpperCAmelCase_				:   Union[str, Any]							= pipe(**__snake_case						).images
       UpperCAmelCase_				:   Union[str, Any]							= image[0, -3:, -3:, -1].flatten()

       assert image.shape == (1, 512, 512, 3)
       UpperCAmelCase_				:   Optional[int]							= np.array([0.49_493, 0.47_896, 0.40_798, 0.54_214, 0.53_212, 0.48_202, 0.47_656, 0.46_329, 0.48_506]						)
       assert np.abs(image_slice - expected_slice						).max() < 7E-3



import argparse

from transformers import CanineConfig, CanineModel, CanineTokenizer, load_tf_weights_in_canine
from transformers.utils import logging


logging.set_verbosity_info()
def   snake_case_    (			__lowercase      ,							__lowercase							):
   # Initialize PyTorch model
   UpperCAmelCase_				:   Optional[Any]							= CanineConfig()
   UpperCAmelCase_				:   Any							= CanineModel(__lowercase							)
   model.eval()

   print(F'''Building PyTorch model from configuration: {config}'''							)

   # Load weights from tf checkpoint
   load_tf_weights_in_canine(__lowercase      ,							__lowercase      ,							__lowercase							)

   # Save pytorch-model (weights and configuration)
   print(F'''Save PyTorch model to {pytorch_dump_path}'''							)
   model.save_pretrained(__lowercase							)

   # Save tokenizer files
   UpperCAmelCase_				:   List[Any]							= CanineTokenizer()
   print(F'''Save tokenizer files to {pytorch_dump_path}'''							)
   tokenizer.save_pretrained(__lowercase							)


if __name__ == "__main__":
       __UpperCamelCase					:	int          =				argparse.ArgumentParser()
       # Required parameters
       parser.add_argument(
           '--tf_checkpoint_path',
           default=None,
           type=str,
           required=True,
           help='Path to the TensorFlow checkpoint. Should end with model.ckpt',
       )
       parser.add_argument(
           '--pytorch_dump_path',
           default=None,
           type=str,
           required=True,
           help='Path to a folder where the PyTorch model will be placed.',
       )
       __UpperCamelCase					:	str          =				parser.parse_args()
       convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.pytorch_dump_path)