import shutil
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_tf_cross_test,
    require_tf,
    require_torch,
    require_torchvision,
    require_vision,
)
from transformers.utils import is_tf_available, is_torch_available, is_vision_available


if is_vision_available():
   from PIL import Image

   from transformers import AutoProcessor, SamImageProcessor, SamProcessor

if is_torch_available():
   import torch

if is_tf_available():
   import tensorflow as tf







@require_vision
@require_torchvision
class __snake_case       (		unittest.TestCase	):




   def 			__a		(					self    :       Optional[int]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   tempfile.mkdtemp()
     SCREAMING_SNAKE_CASE__    							=   SamImageProcessor()
     SCREAMING_SNAKE_CASE__    							=   SamProcessor(_lowercase   )
     processor.save_pretrained(self.tmpdirname   )




   def 			__a		(					self    :       Tuple ,			**_lowercase    :       Optional[int]   ):



     """simple docstring"""






     return AutoProcessor.from_pretrained(self.tmpdirname ,			**_lowercase   ).image_processor




   def 			__a		(					self    :       int   ):



     """simple docstring"""






     shutil.rmtree(self.tmpdirname   )




   def 			__a		(					self    :       int   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   [np.random.randint(2_55 ,			size=(3, 30, 4_00) ,			dtype=np.uinta   )]

     SCREAMING_SNAKE_CASE__    							=   [Image.fromarray(np.moveaxis(_lowercase ,			0 ,			-1   )   ) for x in image_inputs]

     return image_inputs




   def 			__a		(					self    :       List[Any]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   SamProcessor(image_processor=self.get_image_processor()   )
     processor.save_pretrained(self.tmpdirname   )

     SCREAMING_SNAKE_CASE__    							=   self.get_image_processor(do_normalize=_lowercase ,			padding_value=1.0   )

     SCREAMING_SNAKE_CASE__    							=   SamProcessor.from_pretrained(self.tmpdirname ,			do_normalize=_lowercase ,			padding_value=1.0   )

     self.assertEqual(processor.image_processor.to_json_string() ,			image_processor_add_kwargs.to_json_string()   )
     self.assertIsInstance(processor.image_processor ,			_lowercase   )




   def 			__a		(					self    :       Optional[int]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.get_image_processor()

     SCREAMING_SNAKE_CASE__    							=   SamProcessor(image_processor=_lowercase   )

     SCREAMING_SNAKE_CASE__    							=   self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE__    							=   image_processor(_lowercase ,			return_tensors="""np"""   )
     SCREAMING_SNAKE_CASE__    							=   processor(images=_lowercase ,			return_tensors="""np"""   )

     input_feat_extract.pop("""original_sizes"""   )  # pop original_sizes as it is popped in the processor
     input_feat_extract.pop("""reshaped_input_sizes"""   )  # pop original_sizes as it is popped in the processor

     for key in input_feat_extract.keys():
       self.assertAlmostEqual(input_feat_extract[key].sum() ,			input_processor[key].sum() ,			delta=1E-2   )




   @require_torch
   def 			__a		(					self    :       int   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.get_image_processor()

     SCREAMING_SNAKE_CASE__    							=   SamProcessor(image_processor=_lowercase   )
     SCREAMING_SNAKE_CASE__    							=   [torch.ones((1, 3, 5, 5)   )]

     SCREAMING_SNAKE_CASE__    							=   [[17_64, 26_46]]

     SCREAMING_SNAKE_CASE__    							=   [[6_83, 10_24]]
     SCREAMING_SNAKE_CASE__    							=   processor.post_process_masks(_lowercase ,			_lowercase ,			_lowercase   )
     self.assertEqual(masks[0].shape ,			(1, 3, 17_64, 26_46)   )

     SCREAMING_SNAKE_CASE__    							=   processor.post_process_masks(
         _lowercase ,			torch.tensor(_lowercase   ) ,			torch.tensor(_lowercase   )   )
     self.assertEqual(masks[0].shape ,			(1, 3, 17_64, 26_46)   )

     # should also work with np
     SCREAMING_SNAKE_CASE__    							=   [np.ones((1, 3, 5, 5)   )]
     SCREAMING_SNAKE_CASE__    							=   processor.post_process_masks(_lowercase ,			np.array(_lowercase   ) ,			np.array(_lowercase   )   )

     self.assertEqual(masks[0].shape ,			(1, 3, 17_64, 26_46)   )

     SCREAMING_SNAKE_CASE__    							=   [[1, 0], [0, 1]]
     with self.assertRaises(_lowercase   ):
       SCREAMING_SNAKE_CASE__    							=   processor.post_process_masks(_lowercase ,			np.array(_lowercase   ) ,			np.array(_lowercase   )   )







@require_vision
@require_tf
class __snake_case       (		unittest.TestCase	):




   def 			__a		(					self    :       Optional[Any]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   tempfile.mkdtemp()
     SCREAMING_SNAKE_CASE__    							=   SamImageProcessor()
     SCREAMING_SNAKE_CASE__    							=   SamProcessor(_lowercase   )
     processor.save_pretrained(self.tmpdirname   )




   def 			__a		(					self    :       Optional[int] ,			**_lowercase    :       str   ):



     """simple docstring"""






     return AutoProcessor.from_pretrained(self.tmpdirname ,			**_lowercase   ).image_processor




   def 			__a		(					self    :       Optional[Any]   ):



     """simple docstring"""






     shutil.rmtree(self.tmpdirname   )




   def 			__a		(					self    :       List[Any]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   [np.random.randint(2_55 ,			size=(3, 30, 4_00) ,			dtype=np.uinta   )]

     SCREAMING_SNAKE_CASE__    							=   [Image.fromarray(np.moveaxis(_lowercase ,			0 ,			-1   )   ) for x in image_inputs]

     return image_inputs




   def 			__a		(					self    :       Tuple   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   SamProcessor(image_processor=self.get_image_processor()   )
     processor.save_pretrained(self.tmpdirname   )

     SCREAMING_SNAKE_CASE__    							=   self.get_image_processor(do_normalize=_lowercase ,			padding_value=1.0   )

     SCREAMING_SNAKE_CASE__    							=   SamProcessor.from_pretrained(self.tmpdirname ,			do_normalize=_lowercase ,			padding_value=1.0   )

     self.assertEqual(processor.image_processor.to_json_string() ,			image_processor_add_kwargs.to_json_string()   )
     self.assertIsInstance(processor.image_processor ,			_lowercase   )




   def 			__a		(					self    :       List[str]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.get_image_processor()

     SCREAMING_SNAKE_CASE__    							=   SamProcessor(image_processor=_lowercase   )

     SCREAMING_SNAKE_CASE__    							=   self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE__    							=   image_processor(_lowercase ,			return_tensors="""np"""   )
     SCREAMING_SNAKE_CASE__    							=   processor(images=_lowercase ,			return_tensors="""np"""   )

     input_feat_extract.pop("""original_sizes"""   )  # pop original_sizes as it is popped in the processor
     input_feat_extract.pop("""reshaped_input_sizes"""   )  # pop reshaped_input_sizes as it is popped in the processor

     for key in input_feat_extract.keys():
       self.assertAlmostEqual(input_feat_extract[key].sum() ,			input_processor[key].sum() ,			delta=1E-2   )




   @require_tf
   def 			__a		(					self    :       int   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.get_image_processor()

     SCREAMING_SNAKE_CASE__    							=   SamProcessor(image_processor=_lowercase   )
     SCREAMING_SNAKE_CASE__    							=   [tf.ones((1, 3, 5, 5)   )]

     SCREAMING_SNAKE_CASE__    							=   [[17_64, 26_46]]

     SCREAMING_SNAKE_CASE__    							=   [[6_83, 10_24]]
     SCREAMING_SNAKE_CASE__    							=   processor.post_process_masks(_lowercase ,			_lowercase ,			_lowercase ,			return_tensors="""tf"""   )
     self.assertEqual(masks[0].shape ,			(1, 3, 17_64, 26_46)   )

     SCREAMING_SNAKE_CASE__    							=   processor.post_process_masks(
         _lowercase ,			tf.convert_to_tensor(_lowercase   ) ,			tf.convert_to_tensor(_lowercase   ) ,			return_tensors="""tf""" ,			)
     self.assertEqual(masks[0].shape ,			(1, 3, 17_64, 26_46)   )

     # should also work with np
     SCREAMING_SNAKE_CASE__    							=   [np.ones((1, 3, 5, 5)   )]
     SCREAMING_SNAKE_CASE__    							=   processor.post_process_masks(
         _lowercase ,			np.array(_lowercase   ) ,			np.array(_lowercase   ) ,			return_tensors="""tf"""   )

     self.assertEqual(masks[0].shape ,			(1, 3, 17_64, 26_46)   )

     SCREAMING_SNAKE_CASE__    							=   [[1, 0], [0, 1]]
     with self.assertRaises(tf.errors.InvalidArgumentError   ):
       SCREAMING_SNAKE_CASE__    							=   processor.post_process_masks(
           _lowercase ,			np.array(_lowercase   ) ,			np.array(_lowercase   ) ,			return_tensors="""tf"""   )







@require_vision
@require_torchvision
class __snake_case       (		unittest.TestCase	):




   def 			__a		(					self    :       Optional[Any]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   tempfile.mkdtemp()
     SCREAMING_SNAKE_CASE__    							=   SamImageProcessor()
     SCREAMING_SNAKE_CASE__    							=   SamProcessor(_lowercase   )
     processor.save_pretrained(self.tmpdirname   )




   def 			__a		(					self    :       List[Any] ,			**_lowercase    :       Optional[int]   ):



     """simple docstring"""






     return AutoProcessor.from_pretrained(self.tmpdirname ,			**_lowercase   ).image_processor




   def 			__a		(					self    :       Dict   ):



     """simple docstring"""






     shutil.rmtree(self.tmpdirname   )




   def 			__a		(					self    :       List[Any]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   [np.random.randint(2_55 ,			size=(3, 30, 4_00) ,			dtype=np.uinta   )]

     SCREAMING_SNAKE_CASE__    							=   [Image.fromarray(np.moveaxis(_lowercase ,			0 ,			-1   )   ) for x in image_inputs]

     return image_inputs




   @is_pt_tf_cross_test
   def 			__a		(					self    :       List[str]   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.get_image_processor()

     SCREAMING_SNAKE_CASE__    							=   SamProcessor(image_processor=_lowercase   )
     SCREAMING_SNAKE_CASE__    							=   np.random.randint(0 ,			2 ,			size=(1, 3, 5, 5)   ).astype(np.floataa   )
     SCREAMING_SNAKE_CASE__    							=   [tf.convert_to_tensor(_lowercase   )]
     SCREAMING_SNAKE_CASE__    							=   [torch.tensor(_lowercase   )]

     SCREAMING_SNAKE_CASE__    							=   [[17_64, 26_46]]

     SCREAMING_SNAKE_CASE__    							=   [[6_83, 10_24]]
     SCREAMING_SNAKE_CASE__    							=   processor.post_process_masks(
         _lowercase ,			_lowercase ,			_lowercase ,			return_tensors="""tf"""   )
     SCREAMING_SNAKE_CASE__    							=   processor.post_process_masks(
         _lowercase ,			_lowercase ,			_lowercase ,			return_tensors="""pt"""   )

     self.assertTrue(np.all(tf_masks[0].numpy() == pt_masks[0].numpy()   )   )




   @is_pt_tf_cross_test
   def 			__a		(					self    :       int   ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   self.get_image_processor()

     SCREAMING_SNAKE_CASE__    							=   SamProcessor(image_processor=_lowercase   )

     SCREAMING_SNAKE_CASE__    							=   self.prepare_image_inputs()

     SCREAMING_SNAKE_CASE__    							=   image_processor(_lowercase ,			return_tensors="""pt"""   )["""pixel_values"""].numpy()
     SCREAMING_SNAKE_CASE__    							=   processor(images=_lowercase ,			return_tensors="""pt"""   )["""pixel_values"""].numpy()

     SCREAMING_SNAKE_CASE__    							=   image_processor(_lowercase ,			return_tensors="""tf"""   )["""pixel_values"""].numpy()
     SCREAMING_SNAKE_CASE__    							=   processor(images=_lowercase ,			return_tensors="""tf"""   )["""pixel_values"""].numpy()

     self.assertTrue(np.allclose(_lowercase ,			_lowercase   )   )
     self.assertTrue(np.allclose(_lowercase ,			_lowercase   )   )
     self.assertTrue(np.allclose(_lowercase ,			_lowercase   )   )

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


__lowerCamelCase    :				Optional[int]							     =	logging.get_logger(__name__)

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







class __snake_case       (		lowerCamelCase_	):
   lowerCAmelCase_														=							"nllb-moe"
   lowerCAmelCase_														=							["past_key_values"]
   lowerCAmelCase_														=							{"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}




   def __init__(					self    :       int ,			_lowercase    :       Tuple=12_81_12 ,			_lowercase    :       List[Any]=10_24 ,			_lowercase    :       Any=12 ,			_lowercase    :       List[Any]=40_96 ,			_lowercase    :       str=16 ,			_lowercase    :       str=12 ,			_lowercase    :       Optional[int]=40_96 ,			_lowercase    :       List[Any]=16 ,			_lowercase    :       str=0.05 ,			_lowercase    :       Tuple=0.05 ,			_lowercase    :       str=True ,			_lowercase    :       List[str]=True ,			_lowercase    :       Optional[Any]="relu" ,			_lowercase    :       str=10_24 ,			_lowercase    :       Tuple=0.1 ,			_lowercase    :       int=0.1 ,			_lowercase    :       Dict=0.0 ,			_lowercase    :       List[str]=0.02 ,			_lowercase    :       int=2 ,			_lowercase    :       Optional[Any]=True ,			_lowercase    :       List[Any]=False ,			_lowercase    :       List[str]="float32" ,			_lowercase    :       Optional[Any]=False ,			_lowercase    :       str=1_28 ,			_lowercase    :       int=64 ,			_lowercase    :       Optional[int]=4 ,			_lowercase    :       List[str]=4 ,			_lowercase    :       Union[str, Any]=0.0_01 ,			_lowercase    :       List[Any]=0.0_01 ,			_lowercase    :       List[str]="all" ,			_lowercase    :       Optional[Any]=False ,			_lowercase    :       int=False ,			_lowercase    :       Tuple=1.0 ,			_lowercase    :       Optional[int]=0.2 ,			_lowercase    :       Optional[int]=1 ,			_lowercase    :       List[Any]=0 ,			_lowercase    :       List[Any]=2 ,			_lowercase    :       int=False ,			**_lowercase    :       Union[str, Any] ,			):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__    							=   vocab_size
     SCREAMING_SNAKE_CASE__    							=   max_position_embeddings
     SCREAMING_SNAKE_CASE__    							=   d_model
     SCREAMING_SNAKE_CASE__    							=   encoder_ffn_dim
     SCREAMING_SNAKE_CASE__    							=   encoder_layers
     SCREAMING_SNAKE_CASE__    							=   encoder_attention_heads
     SCREAMING_SNAKE_CASE__    							=   decoder_ffn_dim
     SCREAMING_SNAKE_CASE__    							=   decoder_layers
     SCREAMING_SNAKE_CASE__    							=   decoder_attention_heads
     SCREAMING_SNAKE_CASE__    							=   dropout
     SCREAMING_SNAKE_CASE__    							=   attention_dropout
     SCREAMING_SNAKE_CASE__    							=   activation_dropout
     SCREAMING_SNAKE_CASE__    							=   activation_function
     SCREAMING_SNAKE_CASE__    							=   init_std
     SCREAMING_SNAKE_CASE__    							=   encoder_layerdrop
     SCREAMING_SNAKE_CASE__    							=   decoder_layerdrop
     SCREAMING_SNAKE_CASE__    							=   use_cache
     SCREAMING_SNAKE_CASE__    							=   encoder_layers
     SCREAMING_SNAKE_CASE__    							=   scale_embedding  # scale factor will be sqrt(d_model) if True
     SCREAMING_SNAKE_CASE__    							=   router_z_loss_coef
     SCREAMING_SNAKE_CASE__    							=   router_aux_loss_coef
     SCREAMING_SNAKE_CASE__    							=   decoder_sparse_step
     SCREAMING_SNAKE_CASE__    							=   encoder_sparse_step
     SCREAMING_SNAKE_CASE__    							=   num_experts
     SCREAMING_SNAKE_CASE__    							=   expert_capacity
     SCREAMING_SNAKE_CASE__    							=   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}"""   )
     SCREAMING_SNAKE_CASE__    							=   router_dtype

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