


from dataclasses import dataclass
from typing import Dict, Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .attention_processor import AttentionProcessor, AttnProcessor
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, DiagonalGaussianDistribution, Encoder







@dataclass
class 						_snake_case    ( _snake_case				):
 SCREAMING_SNAKE_CASE__ =			42







class 						_snake_case    ( _snake_case		,       _snake_case				):
 SCREAMING_SNAKE_CASE__ =			True




 @register_to_config
 def __init__(						self   ,    _lowerCamelCase = 3   ,    _lowerCamelCase = 3   ,    _lowerCamelCase = ("DownEncoderBlock2D",)   ,    _lowerCamelCase = ("UpDecoderBlock2D",)   ,    _lowerCamelCase = (64,)   ,    _lowerCamelCase = 1   ,    _lowerCamelCase = "silu"   ,    _lowerCamelCase = 4   ,    _lowerCamelCase = 32   ,    _lowerCamelCase = 32   ,    _lowerCamelCase = 0.1_8215   ,    ):
       super().__init__()

       # pass init params to Encoder
       a		:Tuple									=			Encoder(
           in_channels=_lowerCamelCase   ,    out_channels=_lowerCamelCase   ,    down_block_types=_lowerCamelCase   ,    block_out_channels=_lowerCamelCase   ,    layers_per_block=_lowerCamelCase   ,    act_fn=_lowerCamelCase   ,    norm_num_groups=_lowerCamelCase   ,    double_z=_lowerCamelCase   ,    )

       # pass init params to Decoder
       a		:int									=			Decoder(
           in_channels=_lowerCamelCase   ,    out_channels=_lowerCamelCase   ,    up_block_types=_lowerCamelCase   ,    block_out_channels=_lowerCamelCase   ,    layers_per_block=_lowerCamelCase   ,    norm_num_groups=_lowerCamelCase   ,    act_fn=_lowerCamelCase   ,    )

       a		:Any									=			nn.Convad(2 * latent_channels   ,    2 * latent_channels   ,    1       )
       a		:Tuple									=			nn.Convad(_lowerCamelCase   ,    _lowerCamelCase   ,    1       )

       a		:Dict									=			False
       a		:Optional[Any]									=			False

       # only relevant if vae tiling is enabled
       a		:Optional[Any]									=			self.config.sample_size
       a		:Any									=			(
           self.config.sample_size[0]
           if isinstance(self.config.sample_size   ,    (list, tuple)       )
           else self.config.sample_size
       )
       a		:Tuple									=			int(sample_size / (2 ** (len(self.config.block_out_channels       ) - 1))       )
       a		:Tuple									=			0.25




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase=False       ):
       if isinstance(_lowerCamelCase   ,    (Encoder, Decoder)       ):
             a		:Optional[Any]									=			value




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase = True       ):
       a		:Union[str, Any]									=			use_tiling




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       self.enable_tiling(_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[str]									=			True




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[str]									=			False




 @property
 # Copied from diffusers.models.unet_2d_condition.UNet2DConditionModel.attn_processors
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[Any]									=			{}

       def fn_recursive_add_processors(_lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
             if hasattr(_lowerCamelCase   ,    '''set_processor'''       ):
                   a		:Union[str, Any]									=			module.processor

             for sub_name, child in module.named_children():
                   fn_recursive_add_processors(F'''{name}.{sub_name}'''   ,    _lowerCamelCase   ,    _lowerCamelCase       )

             return processors

       for name, module in self.named_children():
             fn_recursive_add_processors(_lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       )

       return processors




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase       ):
       a		:Optional[Any]									=			len(self.attn_processors.keys()       )

       if isinstance(_lowerCamelCase   ,    _lowerCamelCase       ) and len(_lowerCamelCase       ) != count:
             raise ValueError(
                 F'''A dict of processors was passed, but the number of processors {len(_lowerCamelCase       )} does not match the'''
                 F''' number of attention layers: {count}. Please make sure to pass {count} processor classes.'''       )

       def fn_recursive_attn_processor(_lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
             if hasattr(_lowerCamelCase   ,    '''set_processor'''       ):
                   if not isinstance(_lowerCamelCase   ,    _lowerCamelCase       ):
                         module.set_processor(_lowerCamelCase       )
                   else:
                         module.set_processor(processor.pop(F'''{name}.processor'''       )       )

             for sub_name, child in module.named_children():
                   fn_recursive_attn_processor(F'''{name}.{sub_name}'''   ,    _lowerCamelCase   ,    _lowerCamelCase       )

       for name, module in self.named_children():
             fn_recursive_attn_processor(_lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       self.set_attn_processor(AttnProcessor()       )




 @apply_forward_hook
 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase = True       ):
       if self.use_tiling and (x.shape[-1] > self.tile_sample_min_size or x.shape[-2] > self.tile_sample_min_size):
             return self.tiled_encode(_lowerCamelCase   ,    return_dict=_lowerCamelCase       )

       if self.use_slicing and x.shape[0] > 1:
             a		:Any									=			[self.encoder(_lowerCamelCase       ) for x_slice in x.split(1       )]
             a		:Tuple									=			torch.cat(_lowerCamelCase       )
       else:
             a		:Dict									=			self.encoder(_lowerCamelCase       )

       a		:Union[str, Any]									=			self.quant_conv(_lowerCamelCase       )
       a		:Any									=			DiagonalGaussianDistribution(_lowerCamelCase       )

       if not return_dict:
             return (posterior,)

       return AutoencoderKLOutput(latent_dist=_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase = True       ):
       if self.use_tiling and (z.shape[-1] > self.tile_latent_min_size or z.shape[-2] > self.tile_latent_min_size):
             return self.tiled_decode(_lowerCamelCase   ,    return_dict=_lowerCamelCase       )

       a		:Union[str, Any]									=			self.post_quant_conv(_lowerCamelCase       )
       a		:Any									=			self.decoder(_lowerCamelCase       )

       if not return_dict:
             return (dec,)

       return DecoderOutput(sample=_lowerCamelCase       )




 @apply_forward_hook
 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase = True       ):
       if self.use_slicing and z.shape[0] > 1:
             a		:List[Any]									=			[self._decode(_lowerCamelCase       ).sample for z_slice in z.split(1       )]
             a		:int									=			torch.cat(_lowerCamelCase       )
       else:
             a		:Tuple									=			self._decode(_lowerCamelCase       ).sample

       if not return_dict:
             return (decoded,)

       return DecoderOutput(sample=_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       a		:Union[str, Any]									=			min(a.shape[2]   ,    b.shape[2]   ,    _lowerCamelCase       )
       for y in range(_lowerCamelCase       ):
             a		:Tuple									=			a[:, :, -blend_extent + y, :] * (1 - y / blend_extent) + b[:, :, y, :] * (y / blend_extent)
       return b




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase   ,    _lowerCamelCase       ):
       a		:Optional[Any]									=			min(a.shape[3]   ,    b.shape[3]   ,    _lowerCamelCase       )
       for x in range(_lowerCamelCase       ):
             a		:Optional[Any]									=			a[:, :, :, -blend_extent + x] * (1 - x / blend_extent) + b[:, :, :, x] * (x / blend_extent)
       return b




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase = True       ):
       a		:int									=			int(self.tile_sample_min_size * (1 - self.tile_overlap_factor)       )
       a		:Optional[int]									=			int(self.tile_latent_min_size * self.tile_overlap_factor       )
       a		:Dict									=			self.tile_latent_min_size - blend_extent

       # Split the image into 512x512 tiles and encode them separately.
       a		:Dict									=			[]
       for i in range(0   ,    x.shape[2]   ,    _lowerCamelCase       ):
             a		:int									=			[]
             for j in range(0   ,    x.shape[3]   ,    _lowerCamelCase       ):
                   a		:str									=			x[:, :, i : i + self.tile_sample_min_size, j : j + self.tile_sample_min_size]
                   a		:Dict									=			self.encoder(_lowerCamelCase       )
                   a		:Any									=			self.quant_conv(_lowerCamelCase       )
                   row.append(_lowerCamelCase       )
             rows.append(_lowerCamelCase       )
       a		:Union[str, Any]									=			[]
       for i, row in enumerate(_lowerCamelCase       ):
             a		:List[Any]									=			[]
             for j, tile in enumerate(_lowerCamelCase       ):
                   # blend the above tile and the left tile
                   # to the current tile and add the current tile to the result row
                   if i > 0:
                         a		:int									=			self.blend_v(rows[i - 1][j]   ,    _lowerCamelCase   ,    _lowerCamelCase       )
                   if j > 0:
                         a		:List[Any]									=			self.blend_h(row[j - 1]   ,    _lowerCamelCase   ,    _lowerCamelCase       )
                   result_row.append(tile[:, :, :row_limit, :row_limit]       )
             result_rows.append(torch.cat(_lowerCamelCase   ,    dim=3       )       )

       a		:Any									=			torch.cat(_lowerCamelCase   ,    dim=2       )
       a		:List[Any]									=			DiagonalGaussianDistribution(_lowerCamelCase       )

       if not return_dict:
             return (posterior,)

       return AutoencoderKLOutput(latent_dist=_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase = True       ):
       a		:Tuple									=			int(self.tile_latent_min_size * (1 - self.tile_overlap_factor)       )
       a		:Optional[int]									=			int(self.tile_sample_min_size * self.tile_overlap_factor       )
       a		:Optional[int]									=			self.tile_sample_min_size - blend_extent

       # Split z into overlapping 64x64 tiles and decode them separately.
       # The tiles have an overlap to avoid seams between tiles.
       a		:List[str]									=			[]
       for i in range(0   ,    z.shape[2]   ,    _lowerCamelCase       ):
             a		:Optional[Any]									=			[]
             for j in range(0   ,    z.shape[3]   ,    _lowerCamelCase       ):
                   a		:Optional[Any]									=			z[:, :, i : i + self.tile_latent_min_size, j : j + self.tile_latent_min_size]
                   a		:Any									=			self.post_quant_conv(_lowerCamelCase       )
                   a		:Tuple									=			self.decoder(_lowerCamelCase       )
                   row.append(_lowerCamelCase       )
             rows.append(_lowerCamelCase       )
       a		:Dict									=			[]
       for i, row in enumerate(_lowerCamelCase       ):
             a		:Tuple									=			[]
             for j, tile in enumerate(_lowerCamelCase       ):
                   # blend the above tile and the left tile
                   # to the current tile and add the current tile to the result row
                   if i > 0:
                         a		:Optional[Any]									=			self.blend_v(rows[i - 1][j]   ,    _lowerCamelCase   ,    _lowerCamelCase       )
                   if j > 0:
                         a		:Union[str, Any]									=			self.blend_h(row[j - 1]   ,    _lowerCamelCase   ,    _lowerCamelCase       )
                   result_row.append(tile[:, :, :row_limit, :row_limit]       )
             result_rows.append(torch.cat(_lowerCamelCase   ,    dim=3       )       )

       a		:Tuple									=			torch.cat(_lowerCamelCase   ,    dim=2       )
       if not return_dict:
             return (dec,)

       return DecoderOutput(sample=_lowerCamelCase       )




 def 			SCREAMING_SNAKE_CASE__							(						self   ,    _lowerCamelCase   ,    _lowerCamelCase = False   ,    _lowerCamelCase = True   ,    _lowerCamelCase = None   ,    ):
       a		:Optional[int]									=			sample
       a		:List[Any]									=			self.encode(_lowerCamelCase       ).latent_dist
       if sample_posterior:
             a		:Union[str, Any]									=			posterior.sample(generator=_lowerCamelCase       )
       else:
             a		:Union[str, Any]									=			posterior.mode()
       a		:int									=			self.decode(_lowerCamelCase       ).sample

       if not return_dict:
             return (dec,)

       return DecoderOutput(sample=_lowerCamelCase       )









import unittest

import numpy as np

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

from ...test_modeling_flax_common import FlaxModelTesterMixin, ids_tensor, random_attention_mask


if is_flax_available():
     import jax.numpy as jnp

     from transformers.models.distilbert.modeling_flax_distilbert import (
         FlaxDistilBertForMaskedLM,
         FlaxDistilBertForMultipleChoice,
         FlaxDistilBertForQuestionAnswering,
         FlaxDistilBertForSequenceClassification,
         FlaxDistilBertForTokenClassification,
         FlaxDistilBertModel,
     )







class 						_snake_case    ( unittest.TestCase				):




 def __init__(						self   ,    _lowerCamelCase   ,    _lowerCamelCase=13   ,    _lowerCamelCase=7   ,    _lowerCamelCase=True   ,    _lowerCamelCase=True   ,    _lowerCamelCase=True   ,    _lowerCamelCase=True   ,    _lowerCamelCase=99   ,    _lowerCamelCase=32   ,    _lowerCamelCase=5   ,    _lowerCamelCase=4   ,    _lowerCamelCase=37   ,    _lowerCamelCase="gelu"   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=0.1   ,    _lowerCamelCase=512   ,    _lowerCamelCase=16   ,    _lowerCamelCase=2   ,    _lowerCamelCase=0.02   ,    _lowerCamelCase=4   ,    ):
       a		:Optional[Any]									=			parent
       a		:str									=			batch_size
       a		:Tuple									=			seq_length
       a		:List[Any]									=			is_training
       a		:Optional[int]									=			use_attention_mask
       a		:List[str]									=			use_token_type_ids
       a		:str									=			use_labels
       a		:Optional[Any]									=			vocab_size
       a		:Optional[int]									=			hidden_size
       a		:Tuple									=			num_hidden_layers
       a		:Union[str, Any]									=			num_attention_heads
       a		:int									=			intermediate_size
       a		:int									=			hidden_act
       a		:int									=			hidden_dropout_prob
       a		:Union[str, Any]									=			attention_probs_dropout_prob
       a		:str									=			max_position_embeddings
       a		:Dict									=			type_vocab_size
       a		:str									=			type_sequence_label_size
       a		:List[str]									=			initializer_range
       a		:Optional[Any]									=			num_choices




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:Tuple									=			ids_tensor([self.batch_size, self.seq_length]   ,    self.vocab_size       )

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

       a		:Any									=			DistilBertConfig(
           vocab_size=self.vocab_size   ,    dim=self.hidden_size   ,    n_layers=self.num_hidden_layers   ,    n_heads=self.num_attention_heads   ,    hidden_dim=self.intermediate_size   ,    hidden_act=self.hidden_act   ,    dropout=self.hidden_dropout_prob   ,    attention_dropout=self.attention_probs_dropout_prob   ,    max_position_embeddings=self.max_position_embeddings   ,    initializer_range=self.initializer_range   ,    tie_weights_=_lowerCamelCase   ,    )

       return config, input_ids, attention_mask




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[str]									=			self.prepare_config_and_inputs()
       a				,							a				,							a		:str									=			config_and_inputs
       a		:List[Any]									=			{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
       return config, inputs_dict







@require_flax
class 						_snake_case    ( _snake_case		,       unittest.TestCase				):
 SCREAMING_SNAKE_CASE__ =			(
     (
         FlaxDistilBertModel,
         FlaxDistilBertForMaskedLM,
         FlaxDistilBertForMultipleChoice,
         FlaxDistilBertForQuestionAnswering,
         FlaxDistilBertForSequenceClassification,
         FlaxDistilBertForTokenClassification,
         FlaxDistilBertForQuestionAnswering,
     )
     if is_flax_available()
     else ()
 )




 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[Any]									=			FlaxDistilBertModelTester(self       )




 @slow
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       for model_class_name in self.all_model_classes:
             a		:int									=			model_class_name.from_pretrained('''distilbert-base-uncased'''       )
             a		:List[str]									=			model(np.ones((1, 1)       )       )
             self.assertIsNotNone(_lowerCamelCase       )







@require_flax
class 						_snake_case    ( unittest.TestCase				):




 @slow
 def 			SCREAMING_SNAKE_CASE__							(						self       ):
       a		:List[str]									=			FlaxDistilBertModel.from_pretrained('''distilbert-base-uncased'''       )
       a		:Optional[Any]									=			np.array([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]       )
       a		:List[Any]									=			np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
       a		:List[str]									=			model(_lowerCamelCase   ,    attention_mask=_lowerCamelCase       )[0]
       a		:Union[str, Any]									=			(1, 11, 768)
       self.assertEqual(output.shape   ,    _lowerCamelCase       )
       a		:int									=			np.array([[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]       )

       self.assertTrue(jnp.allclose(output[:, 1:4, 1:4]   ,    _lowerCamelCase   ,    atol=1e-4       )       )





