
import gc
import tempfile
import unittest

import numpy as np
import torch

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


lowerCamelCase_				:							Any       =    False







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

     def 						__lowerCamelCase				(  self    ):
            # clean up the VRAM after each test
            super().tearDown()
            gc.collect()
            torch.cuda.empty_cache()

     def 						__lowerCamelCase				(  self    ):
            __snake_case						       =   VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'    )
            # remove text_unet
            pipe.remove_unused_weights()
            pipe.to(SCREAMING_SNAKE_CASE_    )
            pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_    )

            __snake_case						       =   load_image(
                'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'    )

            __snake_case						       =   torch.manual_seed(0    )
            __snake_case						       =   pipe(
                prompt='first prompt'							,       image=SCREAMING_SNAKE_CASE_							,       text_to_image_strength=0.7_5							,       generator=SCREAMING_SNAKE_CASE_							,       guidance_scale=7.5							,       num_inference_steps=2							,       output_type='numpy'							,       ).images

            with tempfile.TemporaryDirectory() as tmpdirname:
                   pipe.save_pretrained(SCREAMING_SNAKE_CASE_    )
                   __snake_case						       =   VersatileDiffusionDualGuidedPipeline.from_pretrained(SCREAMING_SNAKE_CASE_    )

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

            __snake_case						       =   generator.manual_seed(0    )
            __snake_case						       =   pipe(
                prompt='first prompt'							,       image=SCREAMING_SNAKE_CASE_							,       text_to_image_strength=0.7_5							,       generator=SCREAMING_SNAKE_CASE_							,       guidance_scale=7.5							,       num_inference_steps=2							,       output_type='numpy'							,       ).images

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

     def 						__lowerCamelCase				(  self    ):
            __snake_case						       =   VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'    )
            pipe.remove_unused_weights()
            pipe.to(SCREAMING_SNAKE_CASE_    )
            pipe.set_progress_bar_config(disable=SCREAMING_SNAKE_CASE_    )

            __snake_case						       =   'cyberpunk 2077'
            __snake_case						       =   load_image(
                'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/versatile_diffusion/benz.jpg'    )
            __snake_case						       =   torch.manual_seed(0    )
            __snake_case						       =   pipe(
                prompt=SCREAMING_SNAKE_CASE_							,       image=SCREAMING_SNAKE_CASE_							,       text_to_image_strength=0.7_5							,       generator=SCREAMING_SNAKE_CASE_							,       guidance_scale=7.5							,       num_inference_steps=50							,       output_type='numpy'							,       ).images

            __snake_case						       =   image[0, 253:256, 253:256, -1]

            assert image.shape == (1, 512, 512, 3)
            __snake_case						       =   np.array([0.0_7_8_7, 0.0_8_4_9, 0.0_8_2_6, 0.0_8_1_2, 0.0_8_0_7, 0.0_7_9_5, 0.0_8_1_8, 0.0_7_9_8, 0.0_7_7_9]    )

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


import argparse

from transformers import ConvBertConfig, ConvBertModel, TFConvBertModel, load_tf_weights_in_convbert
from transformers.utils import logging


logging.set_verbosity_info()
def        __lowercase(							__snake_case				:		Dict    ,__snake_case				:		List[str]    ,__snake_case				:		List[Any]					)       ->   str:
       __snake_case						       =   ConvBertConfig.from_json_file(__snake_case					)
       __snake_case						       =   ConvBertModel(__snake_case					)

       __snake_case						       =   load_tf_weights_in_convbert(__snake_case    ,__snake_case    ,__snake_case					)
       model.save_pretrained(__snake_case					)

       __snake_case						       =   TFConvBertModel.from_pretrained(__snake_case    ,from_pt=__snake_case					)
       tf_model.save_pretrained(__snake_case					)


if __name__ == "__main__":
 lowerCamelCase_				:							List[Any]       =    argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     "--tf_checkpoint_path", default=None, type=str, required=True, help="Path to the TensorFlow checkpoint path."
 )
 parser.add_argument(
     "--convbert_config_file",
     default=None,
     type=str,
     required=True,
     help=(
         "The config json file corresponding to the pre-trained ConvBERT model. \n"
         "This specifies the model architecture."
     ),
 )
 parser.add_argument(
     "--pytorch_dump_path", default=None, type=str, required=True, help="Path to the output PyTorch model."
 )
 lowerCamelCase_				:							Union[str, Any]       =    parser.parse_args()
 convert_orig_tfa_checkpoint_to_pytorch(args.tf_checkpoint_path, args.convbert_config_file, args.pytorch_dump_path)
