



'''simple docstring'''





import unittest

import numpy as np

from transformers import BertConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
 from transformers.models.bert.modeling_flax_bert import (
     FlaxBertForMaskedLM,
     FlaxBertForMultipleChoice,
     FlaxBertForNextSentencePrediction,
     FlaxBertForPreTraining,
     FlaxBertForQuestionAnswering,
     FlaxBertForSequenceClassification,
     FlaxBertForTokenClassification,
     FlaxBertModel,
 )

class 					_A (			unittest.TestCase					):



       def __init__(	self							:  List[Any]  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]  ,	__SCREAMING_SNAKE_CASE							:  str=13  ,	__SCREAMING_SNAKE_CASE							:  Tuple=7  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=True  ,	__SCREAMING_SNAKE_CASE							:  Dict=True  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=True  ,	__SCREAMING_SNAKE_CASE							:  Tuple=True  ,	__SCREAMING_SNAKE_CASE							:  int=99  ,	__SCREAMING_SNAKE_CASE							:  Tuple=32  ,	__SCREAMING_SNAKE_CASE							:  int=5  ,	__SCREAMING_SNAKE_CASE							:  Dict=4  ,	__SCREAMING_SNAKE_CASE							:  Dict=37  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]="gelu"  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=0.1  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=0.1  ,	__SCREAMING_SNAKE_CASE							:  Dict=512  ,	__SCREAMING_SNAKE_CASE							:  Dict=16  ,	__SCREAMING_SNAKE_CASE							:  Optional[Any]=2  ,	__SCREAMING_SNAKE_CASE							:  Dict=0.02  ,	__SCREAMING_SNAKE_CASE							:  Union[str, Any]=4  ,	):


           '''simple docstring'''

           __a		 =       parent
           __a		 =       batch_size
           __a		 =       seq_length
           __a		 =       is_training
           __a		 =       use_attention_mask
           __a		 =       use_token_type_ids
           __a		 =       use_labels
           __a		 =       vocab_size
           __a		 =       hidden_size
           __a		 =       num_hidden_layers
           __a		 =       num_attention_heads
           __a		 =       intermediate_size
           __a		 =       hidden_act
           __a		 =       hidden_dropout_prob
           __a		 =       attention_probs_dropout_prob
           __a		 =       max_position_embeddings
           __a		 =       type_vocab_size
           __a		 =       type_sequence_label_size
           __a		 =       initializer_range
           __a		 =       num_choices



       def 	_lowerCamelCase    (	self							:  int):


           '''simple docstring'''

           __a		 =       ids_tensor([self.batch_size, self.seq_length]  ,	self.vocab_size)

           __a		 =       None
           if self.use_attention_mask:
               __a		 =       random_attention_mask([self.batch_size, self.seq_length])

           __a		 =       None
           if self.use_token_type_ids:
               __a		 =       ids_tensor([self.batch_size, self.seq_length]  ,	self.type_vocab_size)

           __a		 =       BertConfig(
               vocab_size=self.vocab_size  ,	hidden_size=self.hidden_size  ,	num_hidden_layers=self.num_hidden_layers  ,	num_attention_heads=self.num_attention_heads  ,	intermediate_size=self.intermediate_size  ,	hidden_act=self.hidden_act  ,	hidden_dropout_prob=self.hidden_dropout_prob  ,	attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,	max_position_embeddings=self.max_position_embeddings  ,	type_vocab_size=self.type_vocab_size  ,	is_decoder=__SCREAMING_SNAKE_CASE  ,	initializer_range=self.initializer_range  ,	)

           return config, input_ids, token_type_ids, attention_mask



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


           '''simple docstring'''

           __a		 =       self.prepare_config_and_inputs()
           __a ,     __a ,     __a ,     __a		 =       config_and_inputs
           __a		 =       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
           return config, inputs_dict





       def 	_lowerCamelCase    (	self							:  int):


           '''simple docstring'''

           __a		 =       self.prepare_config_and_inputs()
           __a ,     __a ,     __a ,     __a		 =       config_and_inputs

           __a		 =       True
           __a		 =       floats_tensor([self.batch_size, self.seq_length, self.hidden_size])
           __a		 =       ids_tensor([self.batch_size, self.seq_length]  ,	vocab_size=2)

           return (
               config,
               input_ids,
               attention_mask,
               encoder_hidden_states,
               encoder_attention_mask,
           )


@require_flax
class 					_A (			__UpperCAmelCase   ,unittest.TestCase					):
       UpperCamelCase__						:					str		      =		True

       UpperCamelCase__						:					Dict		      =		(
           (
               FlaxBertModel,
               FlaxBertForPreTraining,
               FlaxBertForMaskedLM,
               FlaxBertForMultipleChoice,
               FlaxBertForQuestionAnswering,
               FlaxBertForNextSentencePrediction,
               FlaxBertForSequenceClassification,
               FlaxBertForTokenClassification,
               FlaxBertForQuestionAnswering,
           )
           if is_flax_available()
           else ()
       )



       def 	_lowerCamelCase    (	self							:  int):


           '''simple docstring'''

           __a		 =       FlaxBertModelTester(self)





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


           '''simple docstring'''

           __a		 =       FlaxBertModel.from_pretrained('''bert-base-cased''')
           __a		 =       model(np.ones((1, 1)))
           self.assertIsNotNone(__SCREAMING_SNAKE_CASE)










import inspect
from typing import List, Optional, Tuple, Union

import numpy as np
import PIL
import torch
import torch.utils.checkpoint

from ...models import UNetaDModel, VQModel
from ...schedulers import (
    DDIMScheduler,
    DPMSolverMultistepScheduler,
    EulerAncestralDiscreteScheduler,
    EulerDiscreteScheduler,
    LMSDiscreteScheduler,
    PNDMScheduler,
)
from ...utils import PIL_INTERPOLATION, randn_tensor
from ..pipeline_utils import DiffusionPipeline, ImagePipelineOutput





def   __snake_case			(    _UpperCAmelCase       ):
    __a ,     __a		 =       image.size
    __a ,     __a		 =       (x - x % 32 for x in (w, h))  # resize to integer multiple of 32
    __a		 =       image.resize((w, h)     ,				resample=PIL_INTERPOLATION['''lanczos''']       )
    __a		 =       np.array(_UpperCAmelCase       ).astype(np.floataa       ) / 2_55.0
    __a		 =       image[None].transpose(0     ,				3     ,				1     ,				2       )
    __a		 =       torch.from_numpy(_UpperCAmelCase       )
    return 2.0 * image - 1.0

class 					_A (			__UpperCAmelCase					):



       def __init__(	self							:  Any  ,	__SCREAMING_SNAKE_CASE							:  VQModel  ,	__SCREAMING_SNAKE_CASE							:  UNetaDModel  ,	__SCREAMING_SNAKE_CASE							:  Union[
               DDIMScheduler,
               PNDMScheduler,
               LMSDiscreteScheduler,
               EulerDiscreteScheduler,
               EulerAncestralDiscreteScheduler,
               DPMSolverMultistepScheduler,
           ]  ,	):


           '''simple docstring'''

           super().__init__()
           self.register_modules(vqvae=__SCREAMING_SNAKE_CASE  ,	unet=__SCREAMING_SNAKE_CASE  ,	scheduler=__SCREAMING_SNAKE_CASE)





       @torch.no_grad()
       def __call__(	self							:  List[Any]  ,	__SCREAMING_SNAKE_CASE							:  Union[torch.Tensor, PIL.Image.Image] = None  ,	__SCREAMING_SNAKE_CASE							:  Optional[int] = 1  ,	__SCREAMING_SNAKE_CASE							:  Optional[int] = 100  ,	__SCREAMING_SNAKE_CASE							:  Optional[float] = 0.0  ,	__SCREAMING_SNAKE_CASE							:  Optional[Union[torch.Generator, List[torch.Generator]]] = None  ,	__SCREAMING_SNAKE_CASE							:  Optional[str] = "pil"  ,	__SCREAMING_SNAKE_CASE							:  bool = True  ,	):


           '''simple docstring'''

           if isinstance(__SCREAMING_SNAKE_CASE  ,	PIL.Image.Image):
               __a		 =       1
           elif isinstance(__SCREAMING_SNAKE_CASE  ,	torch.Tensor):
               __a		 =       image.shape[0]
           else:
               raise ValueError(F'`image` has to be of type `PIL.Image.Image` or `torch.Tensor` but is {type(__SCREAMING_SNAKE_CASE)}')

           if isinstance(__SCREAMING_SNAKE_CASE  ,	PIL.Image.Image):
               __a		 =       preprocess(__SCREAMING_SNAKE_CASE)

           __a ,     __a		 =       image.shape[-2:]

           # in_channels should be 6: 3 for latents, 3 for low resolution image
           __a		 =       (batch_size, self.unet.config.in_channels // 2, height, width)
           __a		 =       next(self.unet.parameters()).dtype

           __a		 =       randn_tensor(__SCREAMING_SNAKE_CASE  ,	generator=__SCREAMING_SNAKE_CASE  ,	device=self.device  ,	dtype=__SCREAMING_SNAKE_CASE)

           __a		 =       image.to(device=self.device  ,	dtype=__SCREAMING_SNAKE_CASE)

           # set timesteps and move to the correct device
           self.scheduler.set_timesteps(__SCREAMING_SNAKE_CASE  ,	device=self.device)
           __a		 =       self.scheduler.timesteps

           # scale the initial noise by the standard deviation required by the scheduler
           __a		 =       latents * self.scheduler.init_noise_sigma

           # prepare extra kwargs for the scheduler step, since not all schedulers have the same signature.
           # eta (η) is only used with the DDIMScheduler, it will be ignored for other schedulers.
           # eta corresponds to η in DDIM paper: https://arxiv.org/abs/2010.02502
           # and should be between [0, 1]
           __a		 =       '''eta''' in set(inspect.signature(self.scheduler.step).parameters.keys())
           __a		 =       {}
           if accepts_eta:
               __a		 =       eta

           for t in self.progress_bar(__SCREAMING_SNAKE_CASE):
               # concat latents and low resolution image in the channel dimension.
               __a		 =       torch.cat([latents, image]  ,	dim=1)
               __a		 =       self.scheduler.scale_model_input(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE)
               # predict the noise residual
               __a		 =       self.unet(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE).sample
               # compute the previous noisy sample x_t -> x_t-1
               __a		 =       self.scheduler.step(__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	__SCREAMING_SNAKE_CASE  ,	**__SCREAMING_SNAKE_CASE).prev_sample

           # decode the image latents with the VQVAE
           __a		 =       self.vqvae.decode(__SCREAMING_SNAKE_CASE).sample
           __a		 =       torch.clamp(__SCREAMING_SNAKE_CASE  ,	-1.0  ,	1.0)
           __a		 =       image / 2 + 0.5
           __a		 =       image.cpu().permute(0  ,	2  ,	3  ,	1).numpy()

           if output_type == "pil":
               __a		 =       self.numpy_to_pil(__SCREAMING_SNAKE_CASE)

           if not return_dict:
               return (image,)

           return ImagePipelineOutput(images=__SCREAMING_SNAKE_CASE)





