


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





      def __init__(		self						:					Union[str, Any]					,       __lowerCamelCase						:					int					,       __lowerCamelCase						:					Tuple=7					,       __lowerCamelCase						:					int=3					,       __lowerCamelCase						:					List[str]=1_0					,       __lowerCamelCase						:					Any=1_8					,       __lowerCamelCase						:					str=3_0					,       __lowerCamelCase						:					Dict=4_0_0					,       __lowerCamelCase						:					Optional[Any]=True					,       __lowerCamelCase						:					Any=None					,       __lowerCamelCase						:					str=True					,       __lowerCamelCase						:					List[str]=[0.5, 0.5, 0.5]					,       __lowerCamelCase						:					Any=[0.5, 0.5, 0.5]					,       __lowerCamelCase						:					str=None					,       )    ->   List[Any]:






         """simple docstring"""







         UpperCAmelCase							=     size if size is not None else {"""shortest_edge""": 1_8}
         UpperCAmelCase							=     crop_size if crop_size is not None else {"""height""": 1_8, """width""": 1_8}

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





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






         """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   __lowercase				(  __snake_case ,					unittest.TestCase  ):
      UpperCamelCase								=       VivitImageProcessor if is_vision_available() else None





      def        _lowercase				(		self						:					Dict       )    ->   Dict:






         """simple docstring"""







         UpperCAmelCase							=     VivitImageProcessingTester(self       )





      @property
      def        _lowercase				(		self						:					Optional[int]       )    ->   Any:






         """simple docstring"""







         return self.image_processor_tester.prepare_image_processor_dict()





      def        _lowercase				(		self						:					Optional[int]       )    ->   Tuple:






         """simple docstring"""







         UpperCAmelCase							=     self.image_processing_class(**self.image_processor_dict       )
         self.assertTrue(hasattr(__lowerCamelCase					,       """image_mean"""       )       )
         self.assertTrue(hasattr(__lowerCamelCase					,       """image_std"""       )       )
         self.assertTrue(hasattr(__lowerCamelCase					,       """do_normalize"""       )       )
         self.assertTrue(hasattr(__lowerCamelCase					,       """do_resize"""       )       )
         self.assertTrue(hasattr(__lowerCamelCase					,       """do_center_crop"""       )       )
         self.assertTrue(hasattr(__lowerCamelCase					,       """size"""       )       )





      def        _lowercase				(		self						:					str       )    ->   Tuple:






         """simple docstring"""







         UpperCAmelCase							=     self.image_processing_class.from_dict(self.image_processor_dict       )
         self.assertEqual(image_processor.size					,       {"""shortest_edge""": 1_8}       )
         self.assertEqual(image_processor.crop_size					,       {"""height""": 1_8, """width""": 1_8}       )

         UpperCAmelCase							=     self.image_processing_class.from_dict(self.image_processor_dict					,       size=4_2					,       crop_size=8_4       )
         self.assertEqual(image_processor.size					,       {"""shortest_edge""": 4_2}       )
         self.assertEqual(image_processor.crop_size					,       {"""height""": 8_4, """width""": 8_4}       )





      def        _lowercase				(		self						:					str       )    ->   str:






         """simple docstring"""







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

         # Test not batched input
         UpperCAmelCase							=     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
         UpperCAmelCase							=     image_processing(__lowerCamelCase					,       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        _lowercase				(		self						:					List[str]       )    ->   List[Any]:






         """simple docstring"""







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

         # Test not batched input
         UpperCAmelCase							=     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
         UpperCAmelCase							=     image_processing(__lowerCamelCase					,       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        _lowercase				(		self						:					List[Any]       )    ->   Any:






         """simple docstring"""







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

         # Test not batched input
         UpperCAmelCase							=     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
         UpperCAmelCase							=     image_processing(__lowerCamelCase					,       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"""],
             )					,       )







from ...configuration_utils import PretrainedConfig
from ...utils import logging


__a     =			logging.get_logger(__name__)

__a     =			{
    """facebook/nllb-moe-54B""": """https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json""",
}






class   __lowercase				(  __snake_case  ):
      UpperCamelCase								=       '''nllb-moe'''
      UpperCamelCase								=       ['''past_key_values''']
      UpperCamelCase								=       {'''num_attention_heads''': '''encoder_attention_heads''', '''hidden_size''': '''d_model'''}





      def __init__(		self						:					Optional[int]					,       __lowerCamelCase						:					Optional[Any]=1_2_8_1_1_2					,       __lowerCamelCase						:					Dict=1_0_2_4					,       __lowerCamelCase						:					Optional[int]=1_2					,       __lowerCamelCase						:					Union[str, Any]=4_0_9_6					,       __lowerCamelCase						:					List[str]=1_6					,       __lowerCamelCase						:					List[str]=1_2					,       __lowerCamelCase						:					int=4_0_9_6					,       __lowerCamelCase						:					Tuple=1_6					,       __lowerCamelCase						:					str=0.05					,       __lowerCamelCase						:					List[str]=0.05					,       __lowerCamelCase						:					List[Any]=True					,       __lowerCamelCase						:					Tuple=True					,       __lowerCamelCase						:					str="relu"					,       __lowerCamelCase						:					Dict=1_0_2_4					,       __lowerCamelCase						:					List[str]=0.1					,       __lowerCamelCase						:					Optional[int]=0.1					,       __lowerCamelCase						:					List[Any]=0.0					,       __lowerCamelCase						:					Optional[Any]=0.02					,       __lowerCamelCase						:					Dict=2					,       __lowerCamelCase						:					Union[str, Any]=True					,       __lowerCamelCase						:					Any=False					,       __lowerCamelCase						:					Tuple="float32"					,       __lowerCamelCase						:					Any=False					,       __lowerCamelCase						:					Optional[int]=1_2_8					,       __lowerCamelCase						:					List[str]=6_4					,       __lowerCamelCase						:					List[Any]=4					,       __lowerCamelCase						:					Tuple=4					,       __lowerCamelCase						:					str=0.001					,       __lowerCamelCase						:					Optional[int]=0.001					,       __lowerCamelCase						:					Tuple="all"					,       __lowerCamelCase						:					Any=False					,       __lowerCamelCase						:					Optional[int]=False					,       __lowerCamelCase						:					List[str]=1.0					,       __lowerCamelCase						:					Dict=0.2					,       __lowerCamelCase						:					Union[str, Any]=1					,       __lowerCamelCase						:					int=0					,       __lowerCamelCase						:					Dict=2					,       __lowerCamelCase						:					int=False					,       **__lowerCamelCase						:					str					,       )    ->   int:






         """simple docstring"""







         UpperCAmelCase							=     vocab_size
         UpperCAmelCase							=     max_position_embeddings
         UpperCAmelCase							=     d_model
         UpperCAmelCase							=     encoder_ffn_dim
         UpperCAmelCase							=     encoder_layers
         UpperCAmelCase							=     encoder_attention_heads
         UpperCAmelCase							=     decoder_ffn_dim
         UpperCAmelCase							=     decoder_layers
         UpperCAmelCase							=     decoder_attention_heads
         UpperCAmelCase							=     dropout
         UpperCAmelCase							=     attention_dropout
         UpperCAmelCase							=     activation_dropout
         UpperCAmelCase							=     activation_function
         UpperCAmelCase							=     init_std
         UpperCAmelCase							=     encoder_layerdrop
         UpperCAmelCase							=     decoder_layerdrop
         UpperCAmelCase							=     use_cache
         UpperCAmelCase							=     encoder_layers
         UpperCAmelCase							=     scale_embedding  # scale factor will be sqrt(d_model) if True
         UpperCAmelCase							=     router_z_loss_coef
         UpperCAmelCase							=     router_aux_loss_coef
         UpperCAmelCase							=     decoder_sparse_step
         UpperCAmelCase							=     encoder_sparse_step
         UpperCAmelCase							=     num_experts
         UpperCAmelCase							=     expert_capacity
         UpperCAmelCase							=     router_bias
         if router_dtype not in ["float32", "float16", "bfloat16"]:
            raise ValueError(F"""`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}"""       )
         UpperCAmelCase							=     router_dtype

         UpperCAmelCase							=     router_ignore_padding_tokens
         UpperCAmelCase							=     batch_prioritized_routing
         UpperCAmelCase							=     second_expert_policy
         UpperCAmelCase							=     normalize_router_prob_before_dropping
         UpperCAmelCase							=     moe_eval_capacity_token_fraction
         UpperCAmelCase							=     moe_token_dropout
         UpperCAmelCase							=     output_router_logits
         super().__init__(
             pad_token_id=__lowerCamelCase					,       bos_token_id=__lowerCamelCase					,       eos_token_id=__lowerCamelCase					,       is_encoder_decoder=__lowerCamelCase					,       decoder_start_token_id=__lowerCamelCase					,       **__lowerCamelCase					,       )



