




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

						def     A__						(		self   :Tuple ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								=tempfile.mkdtemp()
										__magic_name__	:	Optional[int]								=SamImageProcessor()
										__magic_name__	:	int								=SamProcessor(__snake_case )
										processor.save_pretrained(self.tmpdirname )

						def     A__						(		self   :Optional[int]    , **__snake_case   :Any ):
										'''simple docstring'''

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

						def     A__						(		self   :str ):
										'''simple docstring'''

										shutil.rmtree(self.tmpdirname )

						def     A__						(		self   :Dict ):
										'''simple docstring'''

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

										__magic_name__	:	List[Any]								=[Image.fromarray(np.moveaxis(__snake_case    , 0    , -1 ) ) for x in image_inputs]

										return image_inputs

						def     A__						(		self   :List[str] ):
										'''simple docstring'''

										__magic_name__	:	Optional[int]								=SamProcessor(image_processor=self.get_image_processor() )
										processor.save_pretrained(self.tmpdirname )

										__magic_name__	:	Dict								=self.get_image_processor(do_normalize=__snake_case    , padding_value=1.0 )

										__magic_name__	:	Any								=SamProcessor.from_pretrained(self.tmpdirname    , do_normalize=__snake_case    , padding_value=1.0 )

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

						def     A__						(		self   :List[Any] ):
										'''simple docstring'''

										__magic_name__	:	Tuple								=self.get_image_processor()

										__magic_name__	:	str								=SamProcessor(image_processor=__snake_case )

										__magic_name__	:	Union[str, Any]								=self.prepare_image_inputs()

										__magic_name__	:	List[str]								=image_processor(__snake_case    , return_tensors="""np""" )
										__magic_name__	:	str								=processor(images=__snake_case    , 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'''

										__magic_name__	:	Union[str, Any]								=self.get_image_processor()

										__magic_name__	:	Union[str, Any]								=SamProcessor(image_processor=__snake_case )
										__magic_name__	:	List[str]								=[torch.ones((1, 3, 5, 5) )]

										__magic_name__	:	Union[str, Any]								=[[17_64, 26_46]]

										__magic_name__	:	List[Any]								=[[6_83, 10_24]]
										__magic_name__	:	str								=processor.post_process_masks(__snake_case    , __snake_case    , __snake_case )
										self.assertEqual(masks[0].shape    , (1, 3, 17_64, 26_46) )

										__magic_name__	:	int								=processor.post_process_masks(
										    __snake_case    , torch.tensor(__snake_case )    , torch.tensor(__snake_case ) )
										self.assertEqual(masks[0].shape    , (1, 3, 17_64, 26_46) )

										# should also work with np
										__magic_name__	:	Tuple								=[np.ones((1, 3, 5, 5) )]
										__magic_name__	:	Tuple								=processor.post_process_masks(__snake_case    , np.array(__snake_case )    , np.array(__snake_case ) )

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

										__magic_name__	:	Union[str, Any]								=[[1, 0], [0, 1]]
										with self.assertRaises(__snake_case ):
														__magic_name__	:	Dict								=processor.post_process_masks(__snake_case    , np.array(__snake_case )    , np.array(__snake_case ) )




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

						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										__magic_name__	:	Any								=tempfile.mkdtemp()
										__magic_name__	:	Union[str, Any]								=SamImageProcessor()
										__magic_name__	:	int								=SamProcessor(__snake_case )
										processor.save_pretrained(self.tmpdirname )

						def     A__						(		self   :Any    , **__snake_case   :Optional[int] ):
										'''simple docstring'''

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

						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										shutil.rmtree(self.tmpdirname )

						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										__magic_name__	:	List[str]								=[np.random.randint(2_55    , size=(3, 30, 4_00)    , dtype=np.uinta )]

										__magic_name__	:	int								=[Image.fromarray(np.moveaxis(__snake_case    , 0    , -1 ) ) for x in image_inputs]

										return image_inputs

						def     A__						(		self   :Dict ):
										'''simple docstring'''

										__magic_name__	:	List[Any]								=SamProcessor(image_processor=self.get_image_processor() )
										processor.save_pretrained(self.tmpdirname )

										__magic_name__	:	Optional[int]								=self.get_image_processor(do_normalize=__snake_case    , padding_value=1.0 )

										__magic_name__	:	List[Any]								=SamProcessor.from_pretrained(self.tmpdirname    , do_normalize=__snake_case    , padding_value=1.0 )

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

						def     A__						(		self   :Optional[Any] ):
										'''simple docstring'''

										__magic_name__	:	Optional[int]								=self.get_image_processor()

										__magic_name__	:	Union[str, Any]								=SamProcessor(image_processor=__snake_case )

										__magic_name__	:	Optional[Any]								=self.prepare_image_inputs()

										__magic_name__	:	Union[str, Any]								=image_processor(__snake_case    , return_tensors="""np""" )
										__magic_name__	:	Dict								=processor(images=__snake_case    , 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   :Tuple ):
										'''simple docstring'''

										__magic_name__	:	Optional[Any]								=self.get_image_processor()

										__magic_name__	:	List[str]								=SamProcessor(image_processor=__snake_case )
										__magic_name__	:	int								=[tf.ones((1, 3, 5, 5) )]

										__magic_name__	:	Union[str, Any]								=[[17_64, 26_46]]

										__magic_name__	:	str								=[[6_83, 10_24]]
										__magic_name__	:	List[str]								=processor.post_process_masks(__snake_case    , __snake_case    , __snake_case    , return_tensors="""tf""" )
										self.assertEqual(masks[0].shape    , (1, 3, 17_64, 26_46) )

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

										# should also work with np
										__magic_name__	:	Any								=[np.ones((1, 3, 5, 5) )]
										__magic_name__	:	List[Any]								=processor.post_process_masks(
										    __snake_case    , np.array(__snake_case )    , np.array(__snake_case )    , return_tensors="""tf""" )

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

										__magic_name__	:	Tuple								=[[1, 0], [0, 1]]
										with self.assertRaises(tf.errors.InvalidArgumentError ):
														__magic_name__	:	Optional[Any]								=processor.post_process_masks(
														    __snake_case    , np.array(__snake_case )    , np.array(__snake_case )    , return_tensors="""tf""" )




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

						def     A__						(		self   :Tuple ):
										'''simple docstring'''

										__magic_name__	:	Dict								=tempfile.mkdtemp()
										__magic_name__	:	Union[str, Any]								=SamImageProcessor()
										__magic_name__	:	Optional[Any]								=SamProcessor(__snake_case )
										processor.save_pretrained(self.tmpdirname )

						def     A__						(		self   :Optional[Any]    , **__snake_case   :List[Any] ):
										'''simple docstring'''

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

						def     A__						(		self   :Dict ):
										'''simple docstring'''

										shutil.rmtree(self.tmpdirname )

						def     A__						(		self   :Tuple ):
										'''simple docstring'''

										__magic_name__	:	List[str]								=[np.random.randint(2_55    , size=(3, 30, 4_00)    , dtype=np.uinta )]

										__magic_name__	:	List[str]								=[Image.fromarray(np.moveaxis(__snake_case    , 0    , -1 ) ) for x in image_inputs]

										return image_inputs

						@is_pt_tf_cross_test
						def     A__						(		self   :Optional[int] ):
										'''simple docstring'''

										__magic_name__	:	Union[str, Any]								=self.get_image_processor()

										__magic_name__	:	str								=SamProcessor(image_processor=__snake_case )
										__magic_name__	:	int								=np.random.randint(0    , 2    , size=(1, 3, 5, 5) ).astype(np.floataa )
										__magic_name__	:	List[Any]								=[tf.convert_to_tensor(__snake_case )]
										__magic_name__	:	Optional[int]								=[torch.tensor(__snake_case )]

										__magic_name__	:	Union[str, Any]								=[[17_64, 26_46]]

										__magic_name__	:	Optional[Any]								=[[6_83, 10_24]]
										__magic_name__	:	int								=processor.post_process_masks(
										    __snake_case    , __snake_case    , __snake_case    , return_tensors="""tf""" )
										__magic_name__	:	Tuple								=processor.post_process_masks(
										    __snake_case    , __snake_case    , __snake_case    , 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'''

										__magic_name__	:	str								=self.get_image_processor()

										__magic_name__	:	List[Any]								=SamProcessor(image_processor=__snake_case )

										__magic_name__	:	List[Any]								=self.prepare_image_inputs()

										__magic_name__	:	Optional[int]								=image_processor(__snake_case    , return_tensors="""pt""" )["""pixel_values"""].numpy()
										__magic_name__	:	Union[str, Any]								=processor(images=__snake_case    , return_tensors="""pt""" )["""pixel_values"""].numpy()

										__magic_name__	:	Union[str, Any]								=image_processor(__snake_case    , return_tensors="""tf""" )["""pixel_values"""].numpy()
										__magic_name__	:	int								=processor(images=__snake_case    , return_tensors="""tf""" )["""pixel_values"""].numpy()

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










from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxSeqaSeqConfigWithPast
from ...utils import logging


UpperCAmelCase_		:				str									=     logging.get_logger(__name__)

UpperCAmelCase_		:				Tuple									=     {
    "t5-small": "https://huggingface.co/t5-small/resolve/main/config.json",
    "t5-base": "https://huggingface.co/t5-base/resolve/main/config.json",
    "t5-large": "https://huggingface.co/t5-large/resolve/main/config.json",
    "t5-3b": "https://huggingface.co/t5-3b/resolve/main/config.json",
    "t5-11b": "https://huggingface.co/t5-11b/resolve/main/config.json",
}




class 		__A      (			UpperCamelCase__			):
						UpperCamelCase     						=					"""t5"""
						UpperCamelCase     						=					["""past_key_values"""]
						UpperCamelCase     						=					{"""hidden_size""": """d_model""", """num_attention_heads""": """num_heads""", """num_hidden_layers""": """num_layers"""}

						def __init__(		self   :Optional[Any]    , __snake_case   :Optional[Any]=3_21_28    , __snake_case   :Optional[int]=5_12    , __snake_case   :str=64    , __snake_case   :int=20_48    , __snake_case   :Union[str, Any]=6    , __snake_case   :Union[str, Any]=None    , __snake_case   :Union[str, Any]=8    , __snake_case   :List[str]=32    , __snake_case   :Dict=1_28    , __snake_case   :List[Any]=0.1    , __snake_case   :int=1E-6    , __snake_case   :List[str]=1.0    , __snake_case   :Optional[int]="relu"    , __snake_case   :List[str]=True    , __snake_case   :Union[str, Any]=True    , __snake_case   :Union[str, Any]=0    , __snake_case   :str=1    , **__snake_case   :int    , ):
										'''simple docstring'''

										__magic_name__	:	str								=vocab_size
										__magic_name__	:	str								=d_model
										__magic_name__	:	Optional[Any]								=d_kv
										__magic_name__	:	int								=d_ff
										__magic_name__	:	Union[str, Any]								=num_layers
										__magic_name__	:	Tuple								=(
										    num_decoder_layers if num_decoder_layers is not None else self.num_layers
										)  # default = symmetry
										__magic_name__	:	List[str]								=num_heads
										__magic_name__	:	Optional[Any]								=relative_attention_num_buckets
										__magic_name__	:	Union[str, Any]								=relative_attention_max_distance
										__magic_name__	:	Dict								=dropout_rate
										__magic_name__	:	Dict								=layer_norm_epsilon
										__magic_name__	:	List[str]								=initializer_factor
										__magic_name__	:	Any								=feed_forward_proj
										__magic_name__	:	Optional[int]								=use_cache

										__magic_name__	:	Tuple								=self.feed_forward_proj.split("""-""" )
										__magic_name__	:	int								=act_info[-1]
										__magic_name__	:	int								=act_info[0] == """gated"""

										if len(__snake_case ) > 1 and act_info[0] != "gated" or len(__snake_case ) > 2:
														raise ValueError(
														    f"`feed_forward_proj`: {feed_forward_proj} is not a valid activation function of the dense layer."
														    """Please make sure `feed_forward_proj` is of the format `gated-{ACT_FN}` or `{ACT_FN}`, e.g. """
														    """'gated-gelu' or 'relu'""" )

										# for backwards compatibility
										if feed_forward_proj == "gated-gelu":
														__magic_name__	:	str								="""gelu_new"""

										super().__init__(
										    pad_token_id=__snake_case    , eos_token_id=__snake_case    , is_encoder_decoder=__snake_case    , **__snake_case    , )




class 		__A      (			UpperCamelCase__			):

						@property
						def     A__						(		self   :List[Any] ):
										'''simple docstring'''

										__magic_name__	:	int								={
										    """input_ids""": {0: """batch""", 1: """encoder_sequence"""},
										    """attention_mask""": {0: """batch""", 1: """encoder_sequence"""},
										}
										if self.use_past:
														__magic_name__	:	List[str]								="""past_encoder_sequence + sequence"""
														__magic_name__	:	List[Any]								={0: """batch"""}
														__magic_name__	:	Union[str, Any]								={0: """batch""", 1: """past_decoder_sequence + sequence"""}
										else:
														__magic_name__	:	int								={0: """batch""", 1: """decoder_sequence"""}
														__magic_name__	:	Tuple								={0: """batch""", 1: """decoder_sequence"""}

										if self.use_past:
														self.fill_with_past_key_values_(__snake_case    , direction="""inputs""" )

										return common_inputs


						@property
						def     A__						(		self   :Union[str, Any] ):
										'''simple docstring'''

										return 13




