





import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_video_inputs


if is_torch_available():
    import torch

if is_vision_available():
    from PIL import Image

    from transformers import VivitImageProcessor
class     SCREAMING_SNAKE_CASE       (unittest.TestCase				):


   def __init__(   self      :    Optional[int]      ,					a      :    Optional[Any]      ,					a      :    int=7      ,					a      :    int=3      ,					a      :    Any=10      ,					a      :    Tuple=18      ,					a      :    List[Any]=30      ,					a      :    List[Any]=400      ,					a      :    str=True      ,					a      :    Optional[int]=None      ,					a      :    List[Any]=True      ,					a      :    List[str]=[0.5, 0.5, 0.5]      ,					a      :    List[str]=[0.5, 0.5, 0.5]      ,					a      :    Tuple=None      ,					)->       List[str]:





     """simple docstring"""







     lowercase__           =  size if size is not None else {"shortest_edge": 18}
     lowercase__           =  crop_size if crop_size is not None else {"height": 18, "width": 18}

     lowercase__           =  parent
     lowercase__           =  batch_size
     lowercase__           =  num_channels
     lowercase__           =  num_frames
     lowercase__           =  image_size
     lowercase__           =  min_resolution
     lowercase__           =  max_resolution
     lowercase__           =  do_resize
     lowercase__           =  size
     lowercase__           =  do_normalize
     lowercase__           =  image_mean
     lowercase__           =  image_std
     lowercase__           =  crop_size


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Optional[Any]	)->       Dict:





     """simple docstring"""







     return {
         "image_mean": self.image_mean,
         "image_std": self.image_std,
         "do_normalize": self.do_normalize,
         "do_resize": self.do_resize,
         "size": self.size,
         "crop_size": self.crop_size,
     }






@require_torch
@require_vision
class     SCREAMING_SNAKE_CASE       (_a      ,     unittest.TestCase				):
   _UpperCamelCase   :		Union[str, Any]			 =	VivitImageProcessor if is_vision_available() else None


   def 	SCREAMING_SNAKE_CASE_							(   self      :    int	)->       Optional[Any]:





     """simple docstring"""







     lowercase__           =  VivitImageProcessingTester(self	)


   @property
   def 	SCREAMING_SNAKE_CASE_							(   self      :    Optional[Any]	)->       str:





     """simple docstring"""







     return self.image_processor_tester.prepare_image_processor_dict()


   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[str]	)->       str:





     """simple docstring"""







     lowercase__           =  self.image_processing_class(**self.image_processor_dict	)
     self.assertTrue(hasattr(_a      ,					'image_mean'	)	)
     self.assertTrue(hasattr(_a      ,					'image_std'	)	)
     self.assertTrue(hasattr(_a      ,					'do_normalize'	)	)
     self.assertTrue(hasattr(_a      ,					'do_resize'	)	)
     self.assertTrue(hasattr(_a      ,					'do_center_crop'	)	)
     self.assertTrue(hasattr(_a      ,					'size'	)	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[str]	)->       List[Any]:





     """simple docstring"""







     lowercase__           =  self.image_processing_class.from_dict(self.image_processor_dict	)
     self.assertEqual(image_processor.size      ,					{'shortest_edge': 18}	)
     self.assertEqual(image_processor.crop_size      ,					{'height': 18, 'width': 18}	)

     lowercase__           =  self.image_processing_class.from_dict(self.image_processor_dict      ,					size=42      ,					crop_size=84	)
     self.assertEqual(image_processor.size      ,					{'shortest_edge': 42}	)
     self.assertEqual(image_processor.crop_size      ,					{'height': 84, 'width': 84}	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Tuple	)->       List[Any]:





     """simple docstring"""







     lowercase__           =  self.image_processing_class(**self.image_processor_dict	)
     # create random PIL videos
     lowercase__           =  prepare_video_inputs(self.image_processor_tester      ,					equal_resolution=_a	)
     for video in video_inputs:
       self.assertIsInstance(_a      ,					_a	)
       self.assertIsInstance(video[0]      ,					Image.Image	)

     # Test not batched input
     lowercase__           =  image_processing(video_inputs[0]      ,					return_tensors='pt'	).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,					(
             1,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )      ,					)

     # Test batched
     lowercase__           =  image_processing(_a      ,					return_tensors='pt'	).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,					(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )      ,					)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[Any]	)->       Dict:





     """simple docstring"""







     lowercase__           =  self.image_processing_class(**self.image_processor_dict	)
     # create random numpy tensors
     lowercase__           =  prepare_video_inputs(self.image_processor_tester      ,					equal_resolution=_a      ,					numpify=_a	)
     for video in video_inputs:
       self.assertIsInstance(_a      ,					_a	)
       self.assertIsInstance(video[0]      ,					np.ndarray	)

     # Test not batched input
     lowercase__           =  image_processing(video_inputs[0]      ,					return_tensors='pt'	).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,					(
             1,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )      ,					)

     # Test batched
     lowercase__           =  image_processing(_a      ,					return_tensors='pt'	).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,					(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )      ,					)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[Any]	)->       str:





     """simple docstring"""







     lowercase__           =  self.image_processing_class(**self.image_processor_dict	)
     # create random PyTorch tensors
     lowercase__           =  prepare_video_inputs(self.image_processor_tester      ,					equal_resolution=_a      ,					torchify=_a	)
     for video in video_inputs:
       self.assertIsInstance(_a      ,					_a	)
       self.assertIsInstance(video[0]      ,					torch.Tensor	)

     # Test not batched input
     lowercase__           =  image_processing(video_inputs[0]      ,					return_tensors='pt'	).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,					(
             1,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )      ,					)

     # Test batched
     lowercase__           =  image_processing(_a      ,					return_tensors='pt'	).pixel_values
     self.assertEqual(
         encoded_videos.shape      ,					(
             self.image_processor_tester.batch_size,
             self.image_processor_tester.num_frames,
             self.image_processor_tester.num_channels,
             self.image_processor_tester.crop_size['height'],
             self.image_processor_tester.crop_size['width'],
         )      ,					)









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


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





     """simple docstring"""







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


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Union[str, Any]	)->       Union[str, Any]:





     """simple docstring"""







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

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

     lowercase__           =  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_=a      ,					)

     return config, input_ids, attention_mask


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Optional[Any]	)->       Any:





     """simple docstring"""







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






@require_flax
class     SCREAMING_SNAKE_CASE       (UpperCAmelCase      ,     unittest.TestCase				):
   _UpperCamelCase   :		List[Any]			 =	(
       (
           FlaxDistilBertModel,
           FlaxDistilBertForMaskedLM,
           FlaxDistilBertForMultipleChoice,
           FlaxDistilBertForQuestionAnswering,
           FlaxDistilBertForSequenceClassification,
           FlaxDistilBertForTokenClassification,
           FlaxDistilBertForQuestionAnswering,
       )
       if is_flax_available()
       else ()
   )


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Dict	)->       List[str]:





     """simple docstring"""







     lowercase__           =  FlaxDistilBertModelTester(self	)


   @slow
   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[str]	)->       int:





     """simple docstring"""







     for model_class_name in self.all_model_classes:
       lowercase__           =  model_class_name.from_pretrained('distilbert-base-uncased'	)
       lowercase__           =  model(np.ones((1, 1)	)	)
       self.assertIsNotNone(a	)






@require_flax
class     SCREAMING_SNAKE_CASE       (unittest.TestCase				):


   @slow
   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[str]	)->       List[str]:





     """simple docstring"""







     lowercase__           =  FlaxDistilBertModel.from_pretrained('distilbert-base-uncased'	)
     lowercase__           =  np.array([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]	)
     lowercase__           =  np.array([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
     lowercase__           =  model(a      ,					attention_mask=a	)[0]
     lowercase__           =  (1, 11, 768)
     self.assertEqual(output.shape      ,					a	)
     lowercase__           =  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]      ,					a      ,					atol=1E-4	)	)


