


'''simple docstring'''





import random
import unittest

import numpy as np
import torch

from diffusers import DDIMScheduler, LDMSuperResolutionPipeline, UNetaDModel, VQModel
from diffusers.utils import PIL_INTERPOLATION, floats_tensor, load_image, slow, torch_device
from diffusers.utils.testing_utils import enable_full_determinism, require_torch


enable_full_determinism()
class 	a__				(				unittest.TestCase      ):





			'''simple docstring'''

			@property
			def 				lowerCAmelCase    (     self       : Union[str, Any]  )			->		List[str]:
						__A=  1
						__A=  3
						__A=  (32, 32)

						__A=  floats_tensor((batch_size, num_channels) + sizes ,						rng=random.Random(0  )  ).to(lowerCAmelCase_  )
						return image
			@property
			def 				lowerCAmelCase    (     self       : Any  )			->		Dict:
						torch.manual_seed(0  )
						__A=  UNetaDModel(
						    block_out_channels=(32, 64) ,						layers_per_block=2 ,						sample_size=32 ,						in_channels=6 ,						out_channels=3 ,						down_block_types=('DownBlock2D', 'AttnDownBlock2D') ,						up_block_types=('AttnUpBlock2D', 'UpBlock2D') ,						)
						return model
			@property
			def 				lowerCAmelCase    (     self       : str  )			->		int:
						torch.manual_seed(0  )
						__A=  VQModel(
						    block_out_channels=[32, 64] ,						in_channels=3 ,						out_channels=3 ,						down_block_types=['DownEncoderBlock2D', 'DownEncoderBlock2D'] ,						up_block_types=['UpDecoderBlock2D', 'UpDecoderBlock2D'] ,						latent_channels=3 ,						)
						return model
			def 				lowerCAmelCase    (     self       : Tuple  )			->		Dict:
						__A=  'cpu'
						__A=  self.dummy_uncond_unet
						__A=  DDIMScheduler()
						__A=  self.dummy_vq_model

						__A=  LDMSuperResolutionPipeline(unet=lowerCAmelCase_ ,						vqvae=lowerCAmelCase_ ,						scheduler=lowerCAmelCase_  )
						ldm.to(lowerCAmelCase_  )
						ldm.set_progress_bar_config(disable=lowerCAmelCase_  )

						__A=  self.dummy_image.to(lowerCAmelCase_  )

						__A=  torch.Generator(device=lowerCAmelCase_  ).manual_seed(0  )
						__A=  ldm(image=lowerCAmelCase_ ,						generator=lowerCAmelCase_ ,						num_inference_steps=2 ,						output_type='numpy'  ).images

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

						assert image.shape == (1, 64, 64, 3)
						__A=  np.array([0.86_78, 0.82_45, 0.63_81, 0.68_30, 0.43_85, 0.55_99, 0.46_41, 0.62_01, 0.51_50]  )

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

			@unittest.skipIf(torch_device != 'cuda' ,						'This test requires a GPU'  )
			def 				lowerCAmelCase    (     self       : Tuple  )			->		Tuple:
						__A=  self.dummy_uncond_unet
						__A=  DDIMScheduler()
						__A=  self.dummy_vq_model

						# put models in fp16
						__A=  unet.half()
						__A=  vqvae.half()

						__A=  LDMSuperResolutionPipeline(unet=lowerCAmelCase_ ,						vqvae=lowerCAmelCase_ ,						scheduler=lowerCAmelCase_  )
						ldm.to(lowerCAmelCase_  )
						ldm.set_progress_bar_config(disable=lowerCAmelCase_  )

						__A=  self.dummy_image.to(lowerCAmelCase_  )

						__A=  ldm(lowerCAmelCase_ ,						num_inference_steps=2 ,						output_type='numpy'  ).images

						assert image.shape == (1, 64, 64, 3)


@slow
@require_torch
class 	a__				(				unittest.TestCase      ):





			'''simple docstring'''

			def 				lowerCAmelCase    (     self       : Optional[int]  )			->		Any:
						__A=  load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main'
						    '/vq_diffusion/teddy_bear_pool.png'  )
						__A=  init_image.resize((64, 64) ,						resample=PIL_INTERPOLATION['lanczos']  )

						__A=  LDMSuperResolutionPipeline.from_pretrained('duongna/ldm-super-resolution' ,						device_map='auto'  )
						ldm.set_progress_bar_config(disable=lowerCAmelCase_  )

						__A=  torch.manual_seed(0  )
						__A=  ldm(image=lowerCAmelCase_ ,						generator=lowerCAmelCase_ ,						num_inference_steps=20 ,						output_type='numpy'  ).images

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

						assert image.shape == (1, 256, 256, 3)
						__A=  np.array([0.76_44, 0.76_79, 0.76_42, 0.76_33, 0.76_66, 0.75_60, 0.74_25, 0.72_57, 0.69_07]  )

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







'''simple docstring'''





from collections import OrderedDict
from typing import Mapping

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


UpperCAmelCase__      =							logging.get_logger(__name__)

UpperCAmelCase__      =							{
    '''google/mobilebert-uncased''': '''https://huggingface.co/google/mobilebert-uncased/resolve/main/config.json'''
}
class 	a__				(				a_      ):





			'''simple docstring'''

			A	:							int							= MOBILEBERT_PRETRAINED_CONFIG_ARCHIVE_MAP
			A	:							Optional[int]							= '''mobilebert'''
			def __init__(     self       : Optional[int] ,						lowerCAmelCase_       : str=30_522 ,						lowerCAmelCase_       : List[Any]=512 ,						lowerCAmelCase_       : Optional[int]=24 ,						lowerCAmelCase_       : str=4 ,						lowerCAmelCase_       : List[str]=512 ,						lowerCAmelCase_       : List[str]="relu" ,						lowerCAmelCase_       : Dict=0.0 ,						lowerCAmelCase_       : List[Any]=0.1 ,						lowerCAmelCase_       : Optional[Any]=512 ,						lowerCAmelCase_       : str=2 ,						lowerCAmelCase_       : Dict=0.02 ,						lowerCAmelCase_       : Any=1E-12 ,						lowerCAmelCase_       : List[Any]=0 ,						lowerCAmelCase_       : Union[str, Any]=128 ,						lowerCAmelCase_       : Any=True ,						lowerCAmelCase_       : Optional[Any]=True ,						lowerCAmelCase_       : List[Any]=128 ,						lowerCAmelCase_       : Dict=False ,						lowerCAmelCase_       : Tuple=True ,						lowerCAmelCase_       : Any=4 ,						lowerCAmelCase_       : Optional[Any]="no_norm" ,						lowerCAmelCase_       : Optional[Any]=True ,						lowerCAmelCase_       : Union[str, Any]=None ,						**lowerCAmelCase_       : Union[str, Any] ,						)			->		List[Any]:
						super().__init__(pad_token_id=lowerCAmelCase_ ,						**lowerCAmelCase_  )

						__A=  vocab_size
						__A=  hidden_size
						__A=  num_hidden_layers
						__A=  num_attention_heads
						__A=  hidden_act
						__A=  intermediate_size
						__A=  hidden_dropout_prob
						__A=  attention_probs_dropout_prob
						__A=  max_position_embeddings
						__A=  type_vocab_size
						__A=  initializer_range
						__A=  layer_norm_eps
						__A=  embedding_size
						__A=  trigram_input
						__A=  use_bottleneck
						__A=  intra_bottleneck_size
						__A=  use_bottleneck_attention
						__A=  key_query_shared_bottleneck
						__A=  num_feedforward_networks
						__A=  normalization_type
						__A=  classifier_activation

						if self.use_bottleneck:
									__A=  intra_bottleneck_size
						else:
									__A=  hidden_size

						__A=  classifier_dropout


class 	a__				(				a_      ):





			'''simple docstring'''

			@property
			def 				lowerCAmelCase    (     self       : List[str]  )			->		Mapping[str, Mapping[int, str]]:
						if self.task == "multiple-choice":
									__A=  {0: 'batch', 1: 'choice', 2: 'sequence'}
						else:
									__A=  {0: 'batch', 1: 'sequence'}
						return OrderedDict(
						    [
						        ('input_ids', dynamic_axis),
						        ('attention_mask', dynamic_axis),
						        ('token_type_ids', dynamic_axis),
						    ]  )



