




from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


lowerCamelCase__				:     Tuple			       =							logging.get_logger(__name__)

lowerCamelCase__				:     Any			       =							{
    'mnaylor/mega-base-wikitext': 'https://huggingface.co/mnaylor/mega-base-wikitext/resolve/main/config.json',
}





class  lowerCamelCase_							(   _SCREAMING_SNAKE_CASE      ):


 '''simple docstring'''







 lowercase_       					=					"mega"



 def __init__(							self						:	Any       ,       _lowerCAmelCase						:	Optional[Any]=30_522       ,       _lowerCAmelCase						:	Any=128       ,       _lowerCAmelCase						:	str=4       ,       _lowerCAmelCase						:	Any=256       ,       _lowerCAmelCase						:	int=16       ,       _lowerCAmelCase						:	Optional[Any]=True       ,       _lowerCAmelCase						:	str=64       ,       _lowerCAmelCase						:	Optional[int]=False       ,       _lowerCAmelCase						:	List[Any]=-1       ,       _lowerCAmelCase						:	Any=None       ,       _lowerCAmelCase						:	int=True       ,       _lowerCAmelCase						:	Optional[int]="scalenorm"       ,       _lowerCAmelCase						:	List[str]=True       ,       _lowerCAmelCase						:	Union[str, Any]="silu"       ,       _lowerCAmelCase						:	int="softmax"       ,       _lowerCAmelCase						:	Optional[Any]=0.1       ,       _lowerCAmelCase						:	List[str]=0.1       ,       _lowerCAmelCase						:	Tuple=0.1       ,       _lowerCAmelCase						:	Any=False       ,       _lowerCAmelCase						:	Optional[Any]=True       ,       _lowerCAmelCase						:	Optional[int]=256       ,       _lowerCAmelCase						:	List[Any]=True       ,       _lowerCAmelCase						:	Optional[int]=0.1       ,       _lowerCAmelCase						:	Any=2_048       ,       _lowerCAmelCase						:	Any=False       ,       _lowerCAmelCase						:	Optional[int]=2       ,       _lowerCAmelCase						:	Union[str, Any]=0.02       ,       _lowerCAmelCase						:	List[str]=0.2       ,       _lowerCAmelCase						:	str=0.02       ,       _lowerCAmelCase						:	str=1.0       ,       _lowerCAmelCase						:	str=1       ,       _lowerCAmelCase						:	str=0       ,       _lowerCAmelCase						:	Any=2       ,       _lowerCAmelCase						:	int="rotary"       ,       _lowerCAmelCase						:	int=None       ,       _lowerCAmelCase						:	str=True       ,       _lowerCAmelCase						:	Tuple=True       ,       **_lowerCAmelCase						:	Dict       ,       ):
       super().__init__(pad_token_id=_lowerCAmelCase       ,       bos_token_id=_lowerCAmelCase       ,       eos_token_id=_lowerCAmelCase       ,       **_lowerCAmelCase		)

       SCREAMING_SNAKE_CASE_					=    vocab_size
       SCREAMING_SNAKE_CASE_					=    hidden_size
       SCREAMING_SNAKE_CASE_					=    num_hidden_layers
       SCREAMING_SNAKE_CASE_					=    activation
       SCREAMING_SNAKE_CASE_					=    attention_activation
       SCREAMING_SNAKE_CASE_					=    intermediate_size
       SCREAMING_SNAKE_CASE_					=    ema_projection_size
       SCREAMING_SNAKE_CASE_					=    bidirectional
       SCREAMING_SNAKE_CASE_					=    shared_representation_size
       SCREAMING_SNAKE_CASE_					=    use_chunking
       SCREAMING_SNAKE_CASE_					=    chunk_size
       SCREAMING_SNAKE_CASE_					=    truncation
       SCREAMING_SNAKE_CASE_					=    normalize_before_mega
       SCREAMING_SNAKE_CASE_					=    normalization_type
       SCREAMING_SNAKE_CASE_					=    norm_affine
       SCREAMING_SNAKE_CASE_					=    dropout_prob
       SCREAMING_SNAKE_CASE_					=    hidden_dropout_prob
       SCREAMING_SNAKE_CASE_					=    attention_probs_dropout_prob
       SCREAMING_SNAKE_CASE_					=    use_feature_dropout
       SCREAMING_SNAKE_CASE_					=    use_normalized_ffn
       SCREAMING_SNAKE_CASE_					=    nffn_hidden_size
       SCREAMING_SNAKE_CASE_					=    normalize_before_ffn
       SCREAMING_SNAKE_CASE_					=    nffn_activation_dropout_prob
       SCREAMING_SNAKE_CASE_					=    max_positions
       SCREAMING_SNAKE_CASE_					=    add_token_type_embeddings
       SCREAMING_SNAKE_CASE_					=    type_vocab_size
       SCREAMING_SNAKE_CASE_					=    initializer_range
       SCREAMING_SNAKE_CASE_					=    ema_delta_alpha_range
       SCREAMING_SNAKE_CASE_					=    ema_beta_range
       SCREAMING_SNAKE_CASE_					=    ema_gamma_omega_range
       SCREAMING_SNAKE_CASE_					=    relative_positional_bias
       SCREAMING_SNAKE_CASE_					=    use_cache
       SCREAMING_SNAKE_CASE_					=    classifier_dropout
       SCREAMING_SNAKE_CASE_					=    add_lm_hidden_dense_layer
       SCREAMING_SNAKE_CASE_					=    1  # not used but required by Hugging Face





class  lowerCamelCase_							(   _SCREAMING_SNAKE_CASE      ):


 '''simple docstring'''







 @property
 def    lowerCAmelCase_						(							self						:	Dict		):
       if self.task == "multiple-choice":
             SCREAMING_SNAKE_CASE_					=    {0: 'batch', 1: 'choice', 2: 'sequence'}
       else:
             SCREAMING_SNAKE_CASE_					=    {0: 'batch', 1: 'sequence'}
       return OrderedDict(
           [
               ('input_ids', dynamic_axis),
               ('attention_mask', dynamic_axis),
           ]		)





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__				:     int			       =							False





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


 '''simple docstring'''







 def    lowerCAmelCase_						(							self						:	Optional[Any]		):
       # clean up the VRAM after each test
       super().tearDown()
       gc.collect()
       torch.cuda.empty_cache()



 def    lowerCAmelCase_						(							self						:	Optional[int]		):
       SCREAMING_SNAKE_CASE_					=    VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'		)
       # remove text_unet
       pipe.remove_unused_weights()
       pipe.to(_lowerCAmelCase		)
       pipe.set_progress_bar_config(disable=_lowerCAmelCase		)

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

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

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

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

       SCREAMING_SNAKE_CASE_					=    generator.manual_seed(0		)
       SCREAMING_SNAKE_CASE_					=    pipe(
           prompt='first prompt'       ,       image=_lowerCAmelCase       ,       text_to_image_strength=0.75       ,       generator=_lowerCAmelCase       ,       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						:	Any		):
       SCREAMING_SNAKE_CASE_					=    VersatileDiffusionDualGuidedPipeline.from_pretrained('shi-labs/versatile-diffusion'		)
       pipe.remove_unused_weights()
       pipe.to(_lowerCAmelCase		)
       pipe.set_progress_bar_config(disable=_lowerCAmelCase		)

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

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

       assert image.shape == (1, 512, 512, 3)
       SCREAMING_SNAKE_CASE_					=    np.array([0.0787, 0.0849, 0.0826, 0.0812, 0.0807, 0.0795, 0.0818, 0.0798, 0.0779]		)

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