


import shutil
import tempfile
import unittest

import numpy as np
import pytest

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


if is_torch_available():
	from transformers.pytorch_utils import is_torch_greater_or_equal_than_1_11
else:
	A :	str												=     False

if is_vision_available():
	from PIL import Image

	from transformers import (
	    AutoProcessor,
	    PixaStructImageProcessor,
	    PixaStructProcessor,
	    PreTrainedTokenizerFast,
	    TaTokenizer,
	)



@unittest.skipIf(
    not is_torch_greater_or_equal_than_1_11				,						reason='''`Pix2StructImageProcessor` requires `torch>=1.11.0`.'''				,						)
@require_vision
@require_torch
class 							A					( unittest.TestCase							):
					'''simple docstring'''






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




							"""simple docstring"""


							lowercase__      =	tempfile.mkdtemp()

							lowercase__      =	PixaStructImageProcessor()
							lowercase__      =	TaTokenizer.from_pretrained("""t5-small"""	)

							lowercase__      =	PixaStructProcessor(_UpperCAmelCase   ,      _UpperCAmelCase	)

							processor.save_pretrained(self.tmpdirname	)

					def 						lowerCamelCase__  (self					:		Tuple   ,      **_UpperCAmelCase					:		int	)		->     Optional[Any]:




							"""simple docstring"""


							return AutoProcessor.from_pretrained(self.tmpdirname   ,      **_UpperCAmelCase	).tokenizer

					def 						lowerCamelCase__  (self					:		Union[str, Any]   ,      **_UpperCAmelCase					:		List[str]	)		->     Union[str, Any]:




							"""simple docstring"""


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

					def 						lowerCamelCase__  (self					:		Optional[int]	)		->     List[str]:




							"""simple docstring"""


							shutil.rmtree(self.tmpdirname	)

					def 						lowerCamelCase__  (self					:		List[Any]	)		->     List[Any]:




							"""simple docstring"""


							lowercase__      =	[np.random.randint(255   ,      size=(3, 30, 400)   ,      dtype=np.uinta	)]

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

							return image_inputs

					def 						lowerCamelCase__  (self					:		Tuple	)		->     Any:




							"""simple docstring"""


							lowercase__      =	PixaStructProcessor(tokenizer=self.get_tokenizer()   ,      image_processor=self.get_image_processor()	)
							processor.save_pretrained(self.tmpdirname	)

							lowercase__      =	self.get_tokenizer(bos_token="""(BOS)"""   ,      eos_token="""(EOS)"""	)
							lowercase__      =	self.get_image_processor(do_normalize=_UpperCAmelCase   ,      padding_value=1.0	)

							lowercase__      =	PixaStructProcessor.from_pretrained(
							    self.tmpdirname   ,      bos_token="""(BOS)"""   ,      eos_token="""(EOS)"""   ,      do_normalize=_UpperCAmelCase   ,      padding_value=1.0	)

							self.assertEqual(processor.tokenizer.get_vocab()   ,      tokenizer_add_kwargs.get_vocab()	)
							self.assertIsInstance(processor.tokenizer   ,      _UpperCAmelCase	)

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

					def 						lowerCamelCase__  (self					:		str	)		->     Optional[int]:




							"""simple docstring"""


							lowercase__      =	self.get_image_processor()
							lowercase__      =	self.get_tokenizer()

							lowercase__      =	PixaStructProcessor(tokenizer=_UpperCAmelCase   ,      image_processor=_UpperCAmelCase	)

							lowercase__      =	self.prepare_image_inputs()

							lowercase__      =	image_processor(_UpperCAmelCase   ,      return_tensors="""np"""	)
							lowercase__      =	processor(images=_UpperCAmelCase   ,      return_tensors="""np"""	)

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

					def 						lowerCamelCase__  (self					:		str	)		->     Optional[int]:




							"""simple docstring"""


							lowercase__      =	self.get_image_processor()
							lowercase__      =	self.get_tokenizer()

							lowercase__      =	PixaStructProcessor(tokenizer=_UpperCAmelCase   ,      image_processor=_UpperCAmelCase	)

							lowercase__      =	"""lower newer"""

							lowercase__      =	processor(text=_UpperCAmelCase	)

							lowercase__      =	tokenizer(_UpperCAmelCase   ,      return_token_type_ids=_UpperCAmelCase   ,      add_special_tokens=_UpperCAmelCase	)

							for key in encoded_tok.keys():
									self.assertListEqual(encoded_tok[key]   ,      encoded_processor[key]	)

					def 						lowerCamelCase__  (self					:		int	)		->     List[str]:




							"""simple docstring"""


							lowercase__      =	self.get_image_processor()
							lowercase__      =	self.get_tokenizer()

							lowercase__      =	PixaStructProcessor(tokenizer=_UpperCAmelCase   ,      image_processor=_UpperCAmelCase	)

							lowercase__      =	"""lower newer"""
							lowercase__      =	self.prepare_image_inputs()

							lowercase__      =	processor(text=_UpperCAmelCase   ,      images=_UpperCAmelCase	)

							self.assertListEqual(
							    list(inputs.keys()	)   ,      ["""flattened_patches""", """attention_mask""", """decoder_attention_mask""", """decoder_input_ids"""]	)

							# test if it raises when no input is passed
							with pytest.raises(_UpperCAmelCase	):
									processor()

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




							"""simple docstring"""


							lowercase__      =	self.get_image_processor()
							lowercase__      =	self.get_tokenizer()

							lowercase__      =	PixaStructProcessor(tokenizer=_UpperCAmelCase   ,      image_processor=_UpperCAmelCase	)

							lowercase__      =	"""lower newer"""
							lowercase__      =	self.prepare_image_inputs()

							lowercase__      =	processor(text=_UpperCAmelCase   ,      images=_UpperCAmelCase	)

							lowercase__      =	[512, 1024, 2048, 4096]
							lowercase__      =	[770, 770, 770, 770]
							# with text
							for i, max_patch in enumerate(_UpperCAmelCase	):
									lowercase__      =	processor(text=_UpperCAmelCase   ,      images=_UpperCAmelCase   ,      max_patches=_UpperCAmelCase	)
									self.assertEqual(inputs["""flattened_patches"""][0].shape[0]   ,      _UpperCAmelCase	)
									self.assertEqual(inputs["""flattened_patches"""][0].shape[1]   ,      expected_hidden_size[i]	)

							# without text input
							for i, max_patch in enumerate(_UpperCAmelCase	):
									lowercase__      =	processor(images=_UpperCAmelCase   ,      max_patches=_UpperCAmelCase	)
									self.assertEqual(inputs["""flattened_patches"""][0].shape[0]   ,      _UpperCAmelCase	)
									self.assertEqual(inputs["""flattened_patches"""][0].shape[1]   ,      expected_hidden_size[i]	)

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




							"""simple docstring"""


							lowercase__      =	self.get_image_processor()
							lowercase__      =	self.get_tokenizer()

							lowercase__      =	PixaStructProcessor(tokenizer=_UpperCAmelCase   ,      image_processor=_UpperCAmelCase	)

							lowercase__      =	[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

							lowercase__      =	processor.batch_decode(_UpperCAmelCase	)
							lowercase__      =	tokenizer.batch_decode(_UpperCAmelCase	)

							self.assertListEqual(_UpperCAmelCase   ,      _UpperCAmelCase	)


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




							"""simple docstring"""


							lowercase__      =	self.get_image_processor()
							lowercase__      =	self.get_tokenizer()

							lowercase__      =	PixaStructProcessor(tokenizer=_UpperCAmelCase   ,      image_processor=_UpperCAmelCase	)

							lowercase__      =	"""lower newer"""
							lowercase__      =	self.prepare_image_inputs()

							lowercase__      =	processor(text=_UpperCAmelCase   ,      images=_UpperCAmelCase	)

							# For now the processor supports only ["flattened_patches", "input_ids", "attention_mask", "decoder_attention_mask"]
							self.assertListEqual(
							    list(inputs.keys()	)   ,      ["""flattened_patches""", """attention_mask""", """decoder_attention_mask""", """decoder_input_ids"""]	)

							lowercase__      =	processor(text=_UpperCAmelCase	)

							# For now the processor supports only ["flattened_patches", "input_ids", "attention_mask", "decoder_attention_mask"]
							self.assertListEqual(list(inputs.keys()	)   ,      ["""input_ids""", """attention_mask"""]	)





import copy

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


A :	Optional[Any]												=     logging.get_logger(__name__)

A :	Union[str, Any]												=     {
    'facebook/sam-vit-huge': 'https://huggingface.co/facebook/sam-vit-huge/resolve/main/config.json',
    'facebook/sam-vit-large': 'https://huggingface.co/facebook/sam-vit-large/resolve/main/config.json',
    'facebook/sam-vit-base': 'https://huggingface.co/facebook/sam-vit-base/resolve/main/config.json',
}



class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self					:		Dict   ,      _UpperCAmelCase					:		List[str]=256   ,      _UpperCAmelCase					:		Tuple=1024   ,      _UpperCAmelCase					:		List[str]=16   ,      _UpperCAmelCase					:		Optional[Any]=16   ,      _UpperCAmelCase					:		Union[str, Any]=4   ,      _UpperCAmelCase					:		str="gelu"   ,      _UpperCAmelCase					:		List[str]=1E-6   ,      **_UpperCAmelCase					:		List[Any]   ,      )		->     Tuple:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)
							lowercase__      =	hidden_size
							lowercase__      =	image_size
							lowercase__      =	patch_size
							lowercase__      =	image_size // patch_size
							lowercase__      =	mask_input_channels
							lowercase__      =	num_point_embeddings
							lowercase__      =	hidden_act
							lowercase__      =	layer_norm_eps






class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self					:		List[str]   ,      _UpperCAmelCase					:		Optional[int]=256   ,      _UpperCAmelCase					:		int="relu"   ,      _UpperCAmelCase					:		Union[str, Any]=2048   ,      _UpperCAmelCase					:		int=2   ,      _UpperCAmelCase					:		Optional[Any]=8   ,      _UpperCAmelCase					:		Any=2   ,      _UpperCAmelCase					:		Tuple=3   ,      _UpperCAmelCase					:		List[str]=3   ,      _UpperCAmelCase					:		List[str]=256   ,      _UpperCAmelCase					:		Tuple=1E-6   ,      **_UpperCAmelCase					:		int   ,      )		->     Tuple:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)
							lowercase__      =	hidden_size
							lowercase__      =	hidden_act
							lowercase__      =	mlp_dim
							lowercase__      =	num_hidden_layers
							lowercase__      =	num_attention_heads
							lowercase__      =	attention_downsample_rate
							lowercase__      =	num_multimask_outputs
							lowercase__      =	iou_head_depth
							lowercase__      =	iou_head_hidden_dim
							lowercase__      =	layer_norm_eps






class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					def __init__(self					:		Optional[int]   ,      _UpperCAmelCase					:		List[Any]=768   ,      _UpperCAmelCase					:		Union[str, Any]=256   ,      _UpperCAmelCase					:		Union[str, Any]=12   ,      _UpperCAmelCase					:		Optional[Any]=12   ,      _UpperCAmelCase					:		Tuple=3   ,      _UpperCAmelCase					:		Optional[int]=1024   ,      _UpperCAmelCase					:		str=16   ,      _UpperCAmelCase					:		Tuple="gelu"   ,      _UpperCAmelCase					:		Any=1E-0_6   ,      _UpperCAmelCase					:		Any=0.0   ,      _UpperCAmelCase					:		str=1E-1_0   ,      _UpperCAmelCase					:		Tuple=True   ,      _UpperCAmelCase					:		Tuple=4.0   ,      _UpperCAmelCase					:		Optional[Any]=True   ,      _UpperCAmelCase					:		Optional[int]=True   ,      _UpperCAmelCase					:		Optional[Any]=14   ,      _UpperCAmelCase					:		Optional[int]=[2, 5, 8, 11]   ,      _UpperCAmelCase					:		List[Any]=128   ,      _UpperCAmelCase					:		int=None   ,      **_UpperCAmelCase					:		int   ,      )		->     Dict:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)

							lowercase__      =	hidden_size
							lowercase__      =	output_channels
							lowercase__      =	num_hidden_layers
							lowercase__      =	num_attention_heads
							lowercase__      =	num_channels
							lowercase__      =	image_size
							lowercase__      =	patch_size
							lowercase__      =	hidden_act
							lowercase__      =	layer_norm_eps
							lowercase__      =	attention_dropout
							lowercase__      =	initializer_range
							lowercase__      =	qkv_bias
							lowercase__      =	mlp_ratio
							lowercase__      =	use_abs_pos
							lowercase__      =	use_rel_pos
							lowercase__      =	window_size
							lowercase__      =	global_attn_indexes
							lowercase__      =	num_pos_feats
							lowercase__      =	int(hidden_size * mlp_ratio	) if mlp_dim is None else mlp_dim






class 							A					( UpperCAmelCase__							):
					'''simple docstring'''






					A__    =				'''sam'''
					A__    =				True

					def __init__(self					:		List[str]   ,      _UpperCAmelCase					:		Any=None   ,      _UpperCAmelCase					:		List[Any]=None   ,      _UpperCAmelCase					:		List[Any]=None   ,      _UpperCAmelCase					:		List[Any]=0.02   ,      **_UpperCAmelCase					:		Optional[Any]   ,      )		->     Dict:




							"""simple docstring"""


							super().__init__(**_UpperCAmelCase	)
							lowercase__      =	vision_config if vision_config is not None else {}
							lowercase__      =	prompt_encoder_config if prompt_encoder_config is not None else {}
							lowercase__      =	mask_decoder_config if mask_decoder_config is not None else {}

							if isinstance(_UpperCAmelCase   ,      _UpperCAmelCase	):
									lowercase__      =	vision_config.to_dict()
							if isinstance(_UpperCAmelCase   ,      _UpperCAmelCase	):
									lowercase__      =	prompt_encoder_config.to_dict()
							if isinstance(_UpperCAmelCase   ,      _UpperCAmelCase	):
									lowercase__      =	mask_decoder_config.to_dict()

							lowercase__      =	SamVisionConfig(**_UpperCAmelCase	)
							lowercase__      =	SamPromptEncoderConfig(**_UpperCAmelCase	)
							lowercase__      =	SamMaskDecoderConfig(**_UpperCAmelCase	)
							lowercase__      =	initializer_range


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




							"""simple docstring"""


							lowercase__      =	copy.deepcopy(self.__dict__	)
							lowercase__      =	self.vision_config.to_dict()
							lowercase__      =	self.prompt_encoder_config.to_dict()
							lowercase__      =	self.mask_decoder_config.to_dict()
							lowercase__      =	self.__class__.model_type
							return output

