
import gc
import unittest

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

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    StableDiffusionSAGPipeline,
    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 				lowercase		(					__lowerCAmelCase       ,  __lowerCAmelCase       ,  unittest.TestCase		):
							lowercase_       :       Tuple       					=StableDiffusionSAGPipeline
							lowercase_       :       Dict       					=TEXT_TO_IMAGE_PARAMS
							lowercase_       :       Any       					=TEXT_TO_IMAGE_BATCH_PARAMS
							lowercase_       :       Dict       					=TEXT_TO_IMAGE_IMAGE_PARAMS
							lowercase_       :       Union[str, Any]       					=TEXT_TO_IMAGE_IMAGE_PARAMS
							lowercase_       :       int       					=False




							def  A__      (				self):
												torch.manual_seed(0)
												lowercase							=						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   ,)
												lowercase							=						DDIMScheduler(
												    beta_start=0.00085   ,beta_end=0.012   ,beta_schedule='''scaled_linear'''   ,clip_sample=lowerCAmelCase_   ,set_alpha_to_one=lowerCAmelCase_   ,)
												torch.manual_seed(0)
												lowercase							=						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)
												lowercase							=						CLIPTextConfig(
												    bos_token_id=0   ,eos_token_id=2   ,hidden_size=3_2   ,intermediate_size=3_7   ,layer_norm_eps=1E-05   ,num_attention_heads=4   ,num_hidden_layers=5   ,pad_token_id=1   ,vocab_size=1_0_0_0   ,)
												lowercase							=						CLIPTextModel(lowerCAmelCase_)
												lowercase							=						CLIPTokenizer.from_pretrained('''hf-internal-testing/tiny-random-clip''')

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




							def  A__      (				self   ,A__   ,A__=0):
												if str(lowerCAmelCase_).startswith('''mps'''):
																	lowercase							=						torch.manual_seed(lowerCAmelCase_)
												else:
																	lowercase							=						torch.Generator(device=lowerCAmelCase_).manual_seed(lowerCAmelCase_)
												lowercase							=						{
												    '''prompt''': '''.''',
												    '''generator''': generator,
												    '''num_inference_steps''': 2,
												    '''guidance_scale''': 1.0,
												    '''sag_scale''': 1.0,
												    '''output_type''': '''numpy''',
												}
												return inputs





							def  A__      (				self):
												super().test_inference_batch_single_identical(expected_max_diff=3E-3)







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




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




							def  A__      (				self):
												lowercase							=						StableDiffusionSAGPipeline.from_pretrained('''CompVis/stable-diffusion-v1-4''')
												lowercase							=						sag_pipe.to(lowerCAmelCase_)
												sag_pipe.set_progress_bar_config(disable=lowerCAmelCase_)

												lowercase							=						'''.'''
												lowercase							=						torch.manual_seed(0)
												lowercase							=						sag_pipe(
												    [prompt]   ,generator=lowerCAmelCase_   ,guidance_scale=7.5   ,sag_scale=1.0   ,num_inference_steps=2_0   ,output_type='''np''')

												lowercase							=						output.images

												lowercase							=						image[0, -3:, -3:, -1]

												assert image.shape == (1, 5_1_2, 5_1_2, 3)
												lowercase							=						np.array([0.1568, 0.1738, 0.1695, 0.1693, 0.1507, 0.1705, 0.1547, 0.1751, 0.1949])

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




							def  A__      (				self):
												lowercase							=						StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''')
												lowercase							=						sag_pipe.to(lowerCAmelCase_)
												sag_pipe.set_progress_bar_config(disable=lowerCAmelCase_)

												lowercase							=						'''.'''
												lowercase							=						torch.manual_seed(0)
												lowercase							=						sag_pipe(
												    [prompt]   ,generator=lowerCAmelCase_   ,guidance_scale=7.5   ,sag_scale=1.0   ,num_inference_steps=2_0   ,output_type='''np''')

												lowercase							=						output.images

												lowercase							=						image[0, -3:, -3:, -1]

												assert image.shape == (1, 5_1_2, 5_1_2, 3)
												lowercase							=						np.array([0.3459, 0.2876, 0.2537, 0.3002, 0.2671, 0.2160, 0.3026, 0.2262, 0.2371])

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





							def  A__      (				self):
												lowercase							=						StableDiffusionSAGPipeline.from_pretrained('''stabilityai/stable-diffusion-2-1-base''')
												lowercase							=						sag_pipe.to(lowerCAmelCase_)
												sag_pipe.set_progress_bar_config(disable=lowerCAmelCase_)

												lowercase							=						'''.'''
												lowercase							=						torch.manual_seed(0)
												lowercase							=						sag_pipe(
												    [prompt]   ,width=7_6_8   ,height=5_1_2   ,generator=lowerCAmelCase_   ,guidance_scale=7.5   ,sag_scale=1.0   ,num_inference_steps=2_0   ,output_type='''np'''   ,)

												lowercase							=						output.images

												assert image.shape == (1, 5_1_2, 7_6_8, 3)





import argparse

import torch

from ...utils import logging
from . import AlbertConfig, AlbertForPreTraining, load_tf_weights_in_albert


logging.set_verbosity_info()






def  snake_case  (       snake_case__			:int   ,	snake_case__			:List[str]   ,	snake_case__			:Union[str, Any])						->   str:
       # Initialise PyTorch model
       _A        =  AlbertConfig.from_json_file(snake_case__)
       print(F'''Building PyTorch model from configuration: {config}''')
       _A        =  AlbertForPreTraining(snake_case__)

       # Load weights from tf checkpoint
       load_tf_weights_in_albert(snake_case__   ,	snake_case__   ,	snake_case__)

       # Save pytorch-model
       print(F'''Save PyTorch model to {pytorch_dump_path}''')
       torch.save(model.state_dict()   ,	snake_case__)


if __name__ == "__main__":
    _SCREAMING_SNAKE_CASE										=						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(
        '--albert_config_file',
        default=None,
        type=str,
        required=True,
        help=(
            'The config json file corresponding to the pre-trained ALBERT 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.'
    )
    _SCREAMING_SNAKE_CASE										=						parser.parse_args()
    convert_tf_checkpoint_to_pytorch(args.tf_checkpoint_path, args.albert_config_file, args.pytorch_dump_path)




