




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 			lowerCamelCase_				( unittest.TestCase   ):


 '''simple docstring'''







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



 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_      		=     ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size		)

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

       SCREAMING_SNAKE_CASE_      		=     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  lowerCAmelCase_    (				self    :						Union[str, Any]		):
       SCREAMING_SNAKE_CASE_      		=     self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     config_and_inputs
       SCREAMING_SNAKE_CASE_      		=     {'input_ids': input_ids, 'attention_mask': attention_mask}
       return config, inputs_dict





@require_flax
class 			lowerCamelCase_				( _SCREAMING_SNAKE_CASE			,  unittest.TestCase   ):


 '''simple docstring'''







 lowercase_							       =       (
     (
         FlaxDistilBertModel,
         FlaxDistilBertForMaskedLM,
         FlaxDistilBertForMultipleChoice,
         FlaxDistilBertForQuestionAnswering,
         FlaxDistilBertForSequenceClassification,
         FlaxDistilBertForTokenClassification,
         FlaxDistilBertForQuestionAnswering,
     )
     if is_flax_available()
     else ()
 )



 def  lowerCAmelCase_    (				self    :						Dict		):
       SCREAMING_SNAKE_CASE_      		=     FlaxDistilBertModelTester(self		)




 @slow
 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       for model_class_name in self.all_model_classes:
             SCREAMING_SNAKE_CASE_      		=     model_class_name.from_pretrained('distilbert-base-uncased'		)
             SCREAMING_SNAKE_CASE_      		=     model(np.ones((1, 1)		)		)
             self.assertIsNotNone(_lowerCAmelCase		)





@require_flax
class 			lowerCamelCase_				( unittest.TestCase   ):


 '''simple docstring'''







 @slow
 def  lowerCAmelCase_    (				self    :						List[Any]		):
       SCREAMING_SNAKE_CASE_      		=     FlaxDistilBertModel.from_pretrained('distilbert-base-uncased'		)
       SCREAMING_SNAKE_CASE_      		=     np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]		)
       SCREAMING_SNAKE_CASE_      		=     np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]		)
       SCREAMING_SNAKE_CASE_      		=     model(_lowerCAmelCase							,							attention_mask=_lowerCAmelCase		)[0]
       SCREAMING_SNAKE_CASE_      		=     (1, 11, 768)
       self.assertEqual(output.shape							,							_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     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		)		)





import unittest
from typing import Tuple

import torch

from diffusers.utils import floats_tensor, randn_tensor, torch_all_close, torch_device
from diffusers.utils.testing_utils import require_torch





@require_torch
class 			lowerCamelCase_				:


 '''simple docstring'''







 @property
 def  lowerCAmelCase_    (				self    :						Optional[Any]		):
       return self.get_dummy_input()



 @property
 def  lowerCAmelCase_    (				self    :						Union[str, Any]		):
       if self.block_type == "down":
             return (4, 32, 16, 16)
       elif self.block_type == "mid":
             return (4, 32, 32, 32)
       elif self.block_type == "up":
             return (4, 32, 64, 64)

       raise ValueError(F"'{self.block_type}' is not a supported block_type. Set it to 'up', 'mid', or 'down'."		)



 def  lowerCAmelCase_    (				self    :						List[str]							,							_lowerCAmelCase    :						Optional[Any]=True							,							_lowerCAmelCase    :						str=False							,							_lowerCAmelCase    :						Optional[int]=False							,							_lowerCAmelCase    :						Dict=False							,							):
       SCREAMING_SNAKE_CASE_      		=     4
       SCREAMING_SNAKE_CASE_      		=     32
       SCREAMING_SNAKE_CASE_      		=     (32, 32)

       SCREAMING_SNAKE_CASE_      		=     torch.manual_seed(0		)
       SCREAMING_SNAKE_CASE_      		=     torch.device(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     (batch_size, num_channels) + sizes
       SCREAMING_SNAKE_CASE_      		=     randn_tensor(_lowerCAmelCase							,							generator=_lowerCAmelCase							,							device=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     {'hidden_states': hidden_states}

       if include_temb:
             SCREAMING_SNAKE_CASE_      		=     128
             SCREAMING_SNAKE_CASE_      		=     randn_tensor((batch_size, temb_channels)							,							generator=_lowerCAmelCase							,							device=_lowerCAmelCase		)

       if include_res_hidden_states_tuple:
             SCREAMING_SNAKE_CASE_      		=     torch.manual_seed(1		)
             SCREAMING_SNAKE_CASE_      		=     (randn_tensor(_lowerCAmelCase							,							generator=_lowerCAmelCase							,							device=_lowerCAmelCase		),)

       if include_encoder_hidden_states:
             SCREAMING_SNAKE_CASE_      		=     floats_tensor((batch_size, 32, 32)		).to(_lowerCAmelCase		)

       if include_skip_sample:
             SCREAMING_SNAKE_CASE_      		=     randn_tensor(((batch_size, 3) + sizes)							,							generator=_lowerCAmelCase							,							device=_lowerCAmelCase		)

       return dummy_input



 def  lowerCAmelCase_    (				self    :						Optional[int]		):
       SCREAMING_SNAKE_CASE_      		=     {
           'in_channels': 32,
           'out_channels': 32,
           'temb_channels': 128,
       }
       if self.block_type == "up":
             SCREAMING_SNAKE_CASE_      		=     32

       if self.block_type == "mid":
             init_dict.pop('out_channels'		)

       SCREAMING_SNAKE_CASE_      		=     self.dummy_input
       return init_dict, inputs_dict



 def  lowerCAmelCase_    (				self    :						Dict							,							_lowerCAmelCase    :						Optional[Any]		):
       SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     self.prepare_init_args_and_inputs_for_common()
       SCREAMING_SNAKE_CASE_      		=     self.block_class(**_lowerCAmelCase		)
       unet_block.to(_lowerCAmelCase		)
       unet_block.eval()

       with torch.no_grad():
             SCREAMING_SNAKE_CASE_      		=     unet_block(**_lowerCAmelCase		)

       if isinstance(_lowerCAmelCase							,							_lowerCAmelCase		):
             SCREAMING_SNAKE_CASE_      		=     output[0]

       self.assertEqual(output.shape							,							self.output_shape		)

       SCREAMING_SNAKE_CASE_      		=     output[0, -1, -3:, -3:]
       SCREAMING_SNAKE_CASE_      		=     torch.tensor(_lowerCAmelCase		).to(_lowerCAmelCase		)
       assert torch_all_close(output_slice.flatten()							,							_lowerCAmelCase							,							atol=5E-3		)




 @unittest.skipIf(torch_device == 'mps'							,							'Training is not supported in mps'		)
 def  lowerCAmelCase_    (				self    :						Any		):
       SCREAMING_SNAKE_CASE_		,					SCREAMING_SNAKE_CASE_      		=     self.prepare_init_args_and_inputs_for_common()
       SCREAMING_SNAKE_CASE_      		=     self.block_class(**_lowerCAmelCase		)
       model.to(_lowerCAmelCase		)
       model.train()
       SCREAMING_SNAKE_CASE_      		=     model(**_lowerCAmelCase		)

       if isinstance(_lowerCAmelCase							,							_lowerCAmelCase		):
             SCREAMING_SNAKE_CASE_      		=     output[0]

       SCREAMING_SNAKE_CASE_      		=     torch.device(_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     randn_tensor(output.shape							,							device=_lowerCAmelCase		)
       SCREAMING_SNAKE_CASE_      		=     torch.nn.functional.mse_loss(_lowerCAmelCase							,							_lowerCAmelCase		)
       loss.backward()