

"""simple docstring"""




import unittest

from transformers import SpeechaTextaConfig
from transformers.testing_utils import is_torch_available, require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers.models.speech_to_text_a.modeling_speech_to_text_a import (
	    SpeechaTextaDecoder,
	    SpeechaTextaForCausalLM,
	)





@require_torch
class 			__UpperCamelCase :


		def __init__(self :		str     ,     __SCREAMING_SNAKE_CASE :		List[Any]     ,     __SCREAMING_SNAKE_CASE :		Optional[Any]=9_9     ,     __SCREAMING_SNAKE_CASE :		int=1_3     ,     __SCREAMING_SNAKE_CASE :		Any=1_6     ,     __SCREAMING_SNAKE_CASE :		List[Any]=7     ,     __SCREAMING_SNAKE_CASE :		Optional[Any]=True     ,     __SCREAMING_SNAKE_CASE :		Tuple=True     ,     __SCREAMING_SNAKE_CASE :		int=True     ,     __SCREAMING_SNAKE_CASE :		Tuple=False     ,     __SCREAMING_SNAKE_CASE :		Optional[Any]=True     ,     __SCREAMING_SNAKE_CASE :		Dict=2     ,     __SCREAMING_SNAKE_CASE :		Tuple=3_2     ,     __SCREAMING_SNAKE_CASE :		List[Any]=4     ,     __SCREAMING_SNAKE_CASE :		List[Any]=4     ,     __SCREAMING_SNAKE_CASE :		Optional[Any]=3_0     ,     __SCREAMING_SNAKE_CASE :		Union[str, Any]=0     ,     __SCREAMING_SNAKE_CASE :		Tuple=1     ,     __SCREAMING_SNAKE_CASE :		List[Any]=2     ,     __SCREAMING_SNAKE_CASE :		Union[str, Any]=None     ,     ):
							A			=					parent
							A			=					batch_size
							A			=					decoder_seq_length
							# For common tests
							A			=					self.decoder_seq_length
							A			=					is_training
							A			=					use_attention_mask
							A			=					use_labels

							A			=					vocab_size
							A			=					d_model
							A			=					d_model
							A			=					decoder_layers
							A			=					decoder_layers
							A			=					decoder_ffn_dim
							A			=					decoder_attention_heads
							A			=					decoder_attention_heads
							A			=					eos_token_id
							A			=					bos_token_id
							A			=					pad_token_id
							A			=					decoder_start_token_id
							A			=					use_cache
							A			=					max_position_embeddings

							A			=					None
							A			=					decoder_seq_length
							A			=					2
							A			=					1


		def 					SCREAMING_SNAKE_CASE__   (self :		str):
							A			=					ids_tensor([self.batch_size, self.decoder_seq_length]     ,     self.vocab_size)

							A			=					None
							if self.use_attention_mask:
												A			=					ids_tensor([self.batch_size, self.decoder_seq_length]     ,     vocab_size=2)

							A			=					None
							if self.use_labels:
												A			=					ids_tensor([self.batch_size, self.decoder_seq_length]     ,     self.vocab_size)

							A			=					SpeechaTextaConfig(
							    vocab_size=self.vocab_size     ,     d_model=self.d_model     ,     decoder_layers=self.decoder_layers     ,     decoder_ffn_dim=self.decoder_ffn_dim     ,     decoder_attention_heads=self.decoder_attention_heads     ,     eos_token_id=self.eos_token_id     ,     bos_token_id=self.bos_token_id     ,     use_cache=self.use_cache     ,     pad_token_id=self.pad_token_id     ,     decoder_start_token_id=self.decoder_start_token_id     ,     max_position_embeddings=self.max_position_embeddings     ,     )

							return (
							    config,
							    input_ids,
							    attention_mask,
							    lm_labels,
							)


		def 					SCREAMING_SNAKE_CASE__   (self :		Dict     ,     __SCREAMING_SNAKE_CASE :		List[str]     ,     __SCREAMING_SNAKE_CASE :		Union[str, Any]     ,     __SCREAMING_SNAKE_CASE :		Optional[Any]     ,     __SCREAMING_SNAKE_CASE :		Any     ,     ):
							A			=					True
							A			=					SpeechaTextaDecoder(config=__SCREAMING_SNAKE_CASE).to(__SCREAMING_SNAKE_CASE).eval()
							A			=					input_ids[:2]

							input_ids[input_ids == 0] += 1
							# first forward pass
							A			=					model(__SCREAMING_SNAKE_CASE     ,     use_cache=__SCREAMING_SNAKE_CASE)
							A			=					model(__SCREAMING_SNAKE_CASE)
							A			=					model(__SCREAMING_SNAKE_CASE     ,     use_cache=__SCREAMING_SNAKE_CASE)

							self.parent.assertTrue(len(__SCREAMING_SNAKE_CASE) == len(__SCREAMING_SNAKE_CASE))
							self.parent.assertTrue(len(__SCREAMING_SNAKE_CASE) == len(__SCREAMING_SNAKE_CASE) + 1)

							A			=					outputs["past_key_values"]

							# create hypothetical next token and extent to next_input_ids
							A			=					ids_tensor((2, 1)     ,     config.vocab_size - 1) + 1

							# append to next input_ids and
							A			=					torch.cat([input_ids, next_tokens]     ,     dim=-1)

							print(__SCREAMING_SNAKE_CASE)

							A			=					model(__SCREAMING_SNAKE_CASE)["last_hidden_state"]
							A			=					model(__SCREAMING_SNAKE_CASE     ,     past_key_values=__SCREAMING_SNAKE_CASE)["last_hidden_state"]

							# select random slice
							A			=					ids_tensor((1,)     ,     output_from_past.shape[-1]).item()
							A			=					output_from_no_past[:, next_input_ids.shape[-1] - 1, random_slice_idx].detach()
							A			=					output_from_past[:, 0, random_slice_idx].detach()

							# test that outputs are equal for slice
							assert torch.allclose(__SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE     ,     atol=1E-3)


		def 					SCREAMING_SNAKE_CASE__   (self :		Dict):
							A			=					self.prepare_config_and_inputs()
							(
							    (
							    A
							)			,    (
							    A
							)			,    (
							    A
							)			,    (
							    A
							)			,    
							)			=					config_and_inputs

							A			=					{
							    "input_ids": input_ids,
							    "attention_mask": attention_mask,
							}
							return config, inputs_dict





@require_torch
class 			__UpperCamelCase (   _A     ,		_A     ,		_A     ,		unittest.TestCase						):
		SCREAMING_SNAKE_CASE								=    (SpeechaTextaDecoder, SpeechaTextaForCausalLM) if is_torch_available() else ()
		SCREAMING_SNAKE_CASE								=    (SpeechaTextaForCausalLM,) if is_torch_available() else ()
		SCREAMING_SNAKE_CASE								=    {"text-generation": SpeechaTextaForCausalLM} if is_torch_available() else {}
		SCREAMING_SNAKE_CASE								=    True
		SCREAMING_SNAKE_CASE								=    False


		def 					SCREAMING_SNAKE_CASE__   (self :		Tuple     ,     ):
							A			=					SpeechaTextaStandaloneDecoderModelTester(self     ,     is_training=__SCREAMING_SNAKE_CASE)
							A			=					ConfigTester(self     ,     config_class=__SCREAMING_SNAKE_CASE)


		def 					SCREAMING_SNAKE_CASE__   (self :		List[str]):
							pass


		def 					SCREAMING_SNAKE_CASE__   (self :		List[str]):
							pass


		def 					SCREAMING_SNAKE_CASE__   (self :		str):
							pass


		def 					SCREAMING_SNAKE_CASE__   (self :		Union[str, Any]):
							self.config_tester.run_common_tests()


		def 					SCREAMING_SNAKE_CASE__   (self :		List[Any]):
							A			=					self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_decoder_model_past(*__SCREAMING_SNAKE_CASE)


		def 					SCREAMING_SNAKE_CASE__   (self :		Any):
							return








"""simple docstring"""




import copy

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


__A     :    str    =		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 			__UpperCamelCase (   _A						):


		def __init__(self :		int     ,     __SCREAMING_SNAKE_CASE :		Optional[int]=2_5_6     ,     __SCREAMING_SNAKE_CASE :		List[str]=1_0_2_4     ,     __SCREAMING_SNAKE_CASE :		Optional[int]=1_6     ,     __SCREAMING_SNAKE_CASE :		Optional[int]=1_6     ,     __SCREAMING_SNAKE_CASE :		List[str]=4     ,     __SCREAMING_SNAKE_CASE :		List[Any]="gelu"     ,     __SCREAMING_SNAKE_CASE :		str=1E-6     ,     **__SCREAMING_SNAKE_CASE :		Optional[int]     ,     ):
							super().__init__(**__SCREAMING_SNAKE_CASE)
							A			=					hidden_size
							A			=					image_size
							A			=					patch_size
							A			=					image_size // patch_size
							A			=					mask_input_channels
							A			=					num_point_embeddings
							A			=					hidden_act
							A			=					layer_norm_eps





class 			__UpperCamelCase (   _A						):


		def __init__(self :		List[Any]     ,     __SCREAMING_SNAKE_CASE :		Tuple=2_5_6     ,     __SCREAMING_SNAKE_CASE :		str="relu"     ,     __SCREAMING_SNAKE_CASE :		List[Any]=2_0_4_8     ,     __SCREAMING_SNAKE_CASE :		Tuple=2     ,     __SCREAMING_SNAKE_CASE :		Any=8     ,     __SCREAMING_SNAKE_CASE :		Union[str, Any]=2     ,     __SCREAMING_SNAKE_CASE :		Optional[int]=3     ,     __SCREAMING_SNAKE_CASE :		List[Any]=3     ,     __SCREAMING_SNAKE_CASE :		Tuple=2_5_6     ,     __SCREAMING_SNAKE_CASE :		int=1E-6     ,     **__SCREAMING_SNAKE_CASE :		int     ,     ):
							super().__init__(**__SCREAMING_SNAKE_CASE)
							A			=					hidden_size
							A			=					hidden_act
							A			=					mlp_dim
							A			=					num_hidden_layers
							A			=					num_attention_heads
							A			=					attention_downsample_rate
							A			=					num_multimask_outputs
							A			=					iou_head_depth
							A			=					iou_head_hidden_dim
							A			=					layer_norm_eps





class 			__UpperCamelCase (   _A						):


		def __init__(self :		List[str]     ,     __SCREAMING_SNAKE_CASE :		List[Any]=7_6_8     ,     __SCREAMING_SNAKE_CASE :		int=2_5_6     ,     __SCREAMING_SNAKE_CASE :		Tuple=1_2     ,     __SCREAMING_SNAKE_CASE :		str=1_2     ,     __SCREAMING_SNAKE_CASE :		Any=3     ,     __SCREAMING_SNAKE_CASE :		Dict=1_0_2_4     ,     __SCREAMING_SNAKE_CASE :		str=1_6     ,     __SCREAMING_SNAKE_CASE :		Tuple="gelu"     ,     __SCREAMING_SNAKE_CASE :		int=1E-06     ,     __SCREAMING_SNAKE_CASE :		Dict=0.0     ,     __SCREAMING_SNAKE_CASE :		str=1E-10     ,     __SCREAMING_SNAKE_CASE :		List[str]=True     ,     __SCREAMING_SNAKE_CASE :		Union[str, Any]=4.0     ,     __SCREAMING_SNAKE_CASE :		str=True     ,     __SCREAMING_SNAKE_CASE :		Any=True     ,     __SCREAMING_SNAKE_CASE :		Optional[int]=1_4     ,     __SCREAMING_SNAKE_CASE :		Tuple=[2, 5, 8, 1_1]     ,     __SCREAMING_SNAKE_CASE :		str=1_2_8     ,     __SCREAMING_SNAKE_CASE :		Optional[int]=None     ,     **__SCREAMING_SNAKE_CASE :		Optional[Any]     ,     ):
							super().__init__(**__SCREAMING_SNAKE_CASE)

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





class 			__UpperCamelCase (   _A						):
		SCREAMING_SNAKE_CASE								=    "sam"
		SCREAMING_SNAKE_CASE								=    True


		def __init__(self :		Optional[int]     ,     __SCREAMING_SNAKE_CASE :		Dict=None     ,     __SCREAMING_SNAKE_CASE :		Union[str, Any]=None     ,     __SCREAMING_SNAKE_CASE :		Optional[int]=None     ,     __SCREAMING_SNAKE_CASE :		List[Any]=0.0_2     ,     **__SCREAMING_SNAKE_CASE :		Optional[Any]     ,     ):
							super().__init__(**__SCREAMING_SNAKE_CASE)
							A			=					vision_config if vision_config is not None else {}
							A			=					prompt_encoder_config if prompt_encoder_config is not None else {}
							A			=					mask_decoder_config if mask_decoder_config is not None else {}

							if isinstance(__SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE):
												A			=					vision_config.to_dict()
							if isinstance(__SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE):
												A			=					prompt_encoder_config.to_dict()
							if isinstance(__SCREAMING_SNAKE_CASE     ,     __SCREAMING_SNAKE_CASE):
												A			=					mask_decoder_config.to_dict()

							A			=					SamVisionConfig(**__SCREAMING_SNAKE_CASE)
							A			=					SamPromptEncoderConfig(**__SCREAMING_SNAKE_CASE)
							A			=					SamMaskDecoderConfig(**__SCREAMING_SNAKE_CASE)
							A			=					initializer_range


		def 					SCREAMING_SNAKE_CASE__   (self :		List[str]):
							A			=					copy.deepcopy(self.__dict__)
							A			=					self.vision_config.to_dict()
							A			=					self.prompt_encoder_config.to_dict()
							A			=					self.mask_decoder_config.to_dict()
							A			=					self.__class__.model_type
							return output





