



"""simple docstring"""


import copy

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


snake_case_								=			logging.get_logger(__name__)

snake_case_								=			{
    """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  ,    lowercase_					:List[Any]=2_56  ,    lowercase_					:Dict=10_24  ,    lowercase_					:List[str]=16  ,    lowercase_					:List[str]=16  ,    lowercase_					:Optional[int]=4  ,    lowercase_					:str="gelu"  ,    lowercase_					:Optional[int]=1E-6  ,    **lowercase_					:Optional[Any]  ,    )							->     Union[str, Any]:
												super().__init__(**lowercase_   )
												UpperCAmelCase			 =					hidden_size
												UpperCAmelCase			 =					image_size
												UpperCAmelCase			 =					patch_size
												UpperCAmelCase			 =					image_size // patch_size
												UpperCAmelCase			 =					mask_input_channels
												UpperCAmelCase			 =					num_point_embeddings
												UpperCAmelCase			 =					hidden_act
												UpperCAmelCase			 =					layer_norm_eps







class  A_     (	_UpperCAmelCase							):



						"""simple docstring"""






						def __init__(   self					:int  ,    lowercase_					:Optional[int]=2_56  ,    lowercase_					:Tuple="relu"  ,    lowercase_					:Optional[int]=20_48  ,    lowercase_					:Tuple=2  ,    lowercase_					:Dict=8  ,    lowercase_					:List[str]=2  ,    lowercase_					:str=3  ,    lowercase_					:List[str]=3  ,    lowercase_					:Any=2_56  ,    lowercase_					:Union[str, Any]=1E-6  ,    **lowercase_					:List[Any]  ,    )							->     List[Any]:
												super().__init__(**lowercase_   )
												UpperCAmelCase			 =					hidden_size
												UpperCAmelCase			 =					hidden_act
												UpperCAmelCase			 =					mlp_dim
												UpperCAmelCase			 =					num_hidden_layers
												UpperCAmelCase			 =					num_attention_heads
												UpperCAmelCase			 =					attention_downsample_rate
												UpperCAmelCase			 =					num_multimask_outputs
												UpperCAmelCase			 =					iou_head_depth
												UpperCAmelCase			 =					iou_head_hidden_dim
												UpperCAmelCase			 =					layer_norm_eps







class  A_     (	_UpperCAmelCase							):



						"""simple docstring"""






						def __init__(   self					:List[str]  ,    lowercase_					:str=7_68  ,    lowercase_					:int=2_56  ,    lowercase_					:str=12  ,    lowercase_					:Tuple=12  ,    lowercase_					:str=3  ,    lowercase_					:str=10_24  ,    lowercase_					:Dict=16  ,    lowercase_					:List[str]="gelu"  ,    lowercase_					:int=1E-06  ,    lowercase_					:Dict=0.0  ,    lowercase_					:int=1E-10  ,    lowercase_					:Any=True  ,    lowercase_					:List[Any]=4.0  ,    lowercase_					:str=True  ,    lowercase_					:Any=True  ,    lowercase_					:Union[str, Any]=14  ,    lowercase_					:List[Any]=[2, 5, 8, 11]  ,    lowercase_					:Optional[int]=1_28  ,    lowercase_					:str=None  ,    **lowercase_					:Any  ,    )							->     List[str]:
												super().__init__(**lowercase_   )

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







class  A_     (	_UpperCAmelCase							):



						"""simple docstring"""
						__UpperCamelCase													=				"""sam"""
						__UpperCamelCase													=				True






						def __init__(   self					:Union[str, Any]  ,    lowercase_					:Tuple=None  ,    lowercase_					:Optional[Any]=None  ,    lowercase_					:Union[str, Any]=None  ,    lowercase_					:Union[str, Any]=0.02  ,    **lowercase_					:Dict  ,    )							->     Any:
												super().__init__(**lowercase_   )
												UpperCAmelCase			 =					vision_config if vision_config is not None else {}
												UpperCAmelCase			 =					prompt_encoder_config if prompt_encoder_config is not None else {}
												UpperCAmelCase			 =					mask_decoder_config if mask_decoder_config is not None else {}

												if isinstance(lowercase_  ,    lowercase_   ):
																		UpperCAmelCase			 =					vision_config.to_dict()
												if isinstance(lowercase_  ,    lowercase_   ):
																		UpperCAmelCase			 =					prompt_encoder_config.to_dict()
												if isinstance(lowercase_  ,    lowercase_   ):
																		UpperCAmelCase			 =					mask_decoder_config.to_dict()

												UpperCAmelCase			 =					SamVisionConfig(**lowercase_   )
												UpperCAmelCase			 =					SamPromptEncoderConfig(**lowercase_   )
												UpperCAmelCase			 =					SamMaskDecoderConfig(**lowercase_   )
												UpperCAmelCase			 =					initializer_range






						def       UpperCAmelCase__	(   self					:List[str]   )							->     Any:
												UpperCAmelCase			 =					copy.deepcopy(self.__dict__   )
												UpperCAmelCase			 =					self.vision_config.to_dict()
												UpperCAmelCase			 =					self.prompt_encoder_config.to_dict()
												UpperCAmelCase			 =					self.mask_decoder_config.to_dict()
												UpperCAmelCase			 =					self.__class__.model_type
												return output








import itertools
import random
import unittest

import numpy as np

from transformers import EncodecFeatureExtractor
from transformers.testing_utils import require_torch
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
     import torch


lowercase_									=    random.Random()
def 		_snake_case(       SCREAMING_SNAKE_CASE__     :      Optional[int]       ,     SCREAMING_SNAKE_CASE__     :      List[str]=1.0       ,     SCREAMING_SNAKE_CASE__     :      List[Any]=None       ,     SCREAMING_SNAKE_CASE__     :      List[Any]=None )				-> List[str]:





    '''simple docstring'''






    if rng is None:
        A__        =  global_rng

    A__        =  []
    for batch_idx in range(shape[0] ):
        values.append([] )
        for _ in range(shape[1] ):
            values[-1].append(rng.random() * scale )

    return values


@require_torch
class 					A		(				unittest.TestCase					):







    """simple docstring"""





    def __init__( self					:			Any,lowercase_					:			Any,lowercase_					:			Tuple=7,lowercase_					:			Union[str, Any]=4_0_0,lowercase_					:			Union[str, Any]=2_0_0_0,lowercase_					:			int=1,lowercase_					:			Optional[Any]=0.0,lowercase_					:			Optional[int]=2_4_0_0_0,lowercase_					:			List[str]=True,)->       Any:





        '''simple docstring'''







        A__        =  parent
        A__        =  batch_size
        A__        =  min_seq_length
        A__        =  max_seq_length
        A__        =  (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
        A__        =  feature_size
        A__        =  padding_value
        A__        =  sampling_rate
        A__        =  return_attention_mask


    def 		snake_case__	( self					:			str						)->       List[str]:





        '''simple docstring'''







        return {
            "feature_size": self.feature_size,
            "padding_value": self.padding_value,
            "sampling_rate": self.sampling_rate,
            "return_attention_mask": self.return_attention_mask,
        }


    def 		snake_case__	( self					:			Optional[Any],lowercase_					:			str=False,lowercase_					:			Tuple=False						)->       List[str]:





        '''simple docstring'''







        def _flatten(lowercase_					:			Any						):
            return list(itertools.chain(*lowercase_						)						)

        if equal_length:
            A__        =  floats_list((self.batch_size, self.max_seq_length)						)
        else:
            # make sure that inputs increase in size
            A__        =  [
                _flatten(floats_list((x, self.feature_size)						)						)
                for x in range(self.min_seq_length,self.max_seq_length,self.seq_length_diff						)
            ]

        if numpify:
            A__        =  [np.asarray(lowercase_						) for x in audio_inputs]

        return audio_inputs




@require_torch
class 					A		(				_UpperCAmelCase       ,				unittest.TestCase					):







    """simple docstring"""





    lowerCamelCase						      =				EncodecFeatureExtractor


    def 		snake_case__	( self					:			List[str]						)->       List[Any]:





        '''simple docstring'''







        A__        =  EnCodecFeatureExtractionTester(self						)


    def 		snake_case__	( self					:			Optional[Any]						)->       List[str]:





        '''simple docstring'''







        A__        =  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
        # create three inputs of length 800, 1000, and 1200
        A__        =  [floats_list((1, x)						)[0] for x in range(8_0_0,1_4_0_0,2_0_0						)]
        A__        =  [np.asarray(lowercase_						) for audio_input in audio_inputs]

        # Test not batched input
        A__        =  feat_extract(audio_inputs[0],return_tensors='np'						).input_values
        A__        =  feat_extract(np_audio_inputs[0],return_tensors='np'						).input_values
        self.assertTrue(np.allclose(lowercase_,lowercase_,atol=1E-3						)						)

        # Test batched
        A__        =  feat_extract(lowercase_,padding=lowercase_,return_tensors='np'						).input_values
        A__        =  feat_extract(lowercase_,padding=lowercase_,return_tensors='np'						).input_values
        for enc_seq_a, enc_seq_a in zip(lowercase_,lowercase_						):
            self.assertTrue(np.allclose(lowercase_,lowercase_,atol=1E-3						)						)


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





        '''simple docstring'''







        A__        =  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()						)
        A__        =  np.random.rand(1_0_0						).astype(np.floataa						)
        A__        =  np_audio_inputs.tolist()

        for inputs in [py_audio_inputs, np_audio_inputs]:
            A__        =  feature_extractor.pad([{'input_values': inputs}],return_tensors='np'						)
            self.assertTrue(np_processed.input_values.dtype == np.floataa						)
            A__        =  feature_extractor.pad([{'input_values': inputs}],return_tensors='pt'						)
            self.assertTrue(pt_processed.input_values.dtype == torch.floataa						)


    def 		snake_case__	( self					:			int,lowercase_					:			Tuple						)->       Dict:





        '''simple docstring'''







        from datasets import load_dataset

        A__        =  load_dataset('hf-internal-testing/librispeech_asr_dummy','clean',split='validation'						)
        # automatic decoding with librispeech
        A__        =  ds.sort('id'						).select(range(lowercase_						)						)[:num_samples]['audio']

        return [x["array"] for x in audio_samples]


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





        '''simple docstring'''







        A__        =  torch.tensor(
            [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03,
             3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03,
             2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04,
             4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03,
             7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04,
             4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03]						)
        # fmt: on
        A__        =  self._load_datasamples(1						)
        A__        =  EncodecFeatureExtractor()
        A__        =  feature_extractor(lowercase_,return_tensors='pt'						).input_values
        self.assertEquals(input_values.shape,(1, 1, 9_3_6_8_0)						)
        self.assertTrue(torch.allclose(input_values[0, 0, :3_0],lowercase_,atol=1E-6						)						)


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





        '''simple docstring'''







        A__        =  torch.tensor(
            [2.3_804E-03, 2.0_752E-03, 1.9_836E-03, 2.1_057E-03, 1.6_174E-03,
             3.0_518E-04, 9.1_553E-05, 3.3_569E-04, 9.7_656E-04, 1.8_311E-03,
             2.0_142E-03, 2.1_057E-03, 1.7_395E-03, 4.5_776E-04, -3.9_673E-04,
             4.5_776E-04, 1.0_071E-03, 9.1_553E-05, 4.8_828E-04, 1.1_597E-03,
             7.3_242E-04, 9.4_604E-04, 1.8_005E-03, 1.8_311E-03, 8.8_501E-04,
             4.2_725E-04, 4.8_828E-04, 7.3_242E-04, 1.0_986E-03, 2.1_057E-03]						)
        # fmt: on
        A__        =  self._load_datasamples(1						)
        A__        =  [np.tile(input_audio[0][None],reps=(2, 1)						)]
        input_audio[0][1] *= 0.5
        A__        =  EncodecFeatureExtractor(feature_size=2						)
        A__        =  feature_extractor(lowercase_,return_tensors='pt'						).input_values
        self.assertEquals(input_values.shape,(1, 2, 9_3_6_8_0)						)
        self.assertTrue(torch.allclose(input_values[0, 0, :3_0],lowercase_,atol=1E-6						)						)
        self.assertTrue(torch.allclose(input_values[0, 1, :3_0],EXPECTED_INPUT_VALUES * 0.5,atol=1E-6						)						)


    def 		snake_case__	( self					:			Any						)->       int:





        '''simple docstring'''







        A__        =  self._load_datasamples(2						)
        # would be easier if the stride was like
        A__        =  EncodecFeatureExtractor(feature_size=1,chunk_length_s=1,overlap=0.01						)

        # pad and trunc raise an error ?
        with self.assertRaisesRegex(
            lowercase_,'^Both padding and truncation were set. Make sure you only set one.$',):
            A__        =  feature_extractor(
                lowercase_,padding='max_length',truncation=lowercase_,return_tensors='pt'						).input_values

        # truncate to chunk
        A__        =  feature_extractor(lowercase_,truncation=lowercase_,return_tensors='pt'						).input_values
        self.assertEquals(truncated_outputs.shape,(2, 1, 7_1_5_2_0)						)  # 2 chunks

        # force truncate to max_length
        A__        =  feature_extractor(
            lowercase_,truncation=lowercase_,max_length=4_8_0_0_0,return_tensors='pt'						).input_values
        self.assertEquals(truncated_outputs.shape,(2, 1, 4_8_0_0_0)						)

        # pad to chunk
        A__        =  feature_extractor(lowercase_,padding=lowercase_,return_tensors='pt'						).input_values
        self.assertEquals(padded_outputs.shape,(2, 1, 9_5_2_8_0)						)

        # pad to chunk
        A__        =  feature_extractor(lowercase_,return_tensors='pt'						).input_values
        self.assertEquals(truncated_outputs.shape,(2, 1, 9_5_2_8_0)						)

        # force pad to max length
        A__        =  feature_extractor(
            lowercase_,padding='max_length',max_length=1_0_0_0_0_0,return_tensors='pt'						).input_values
        self.assertEquals(truncated_outputs.shape,(2, 1, 1_0_0_0_0_0)						)

        # force no pad
        with self.assertRaisesRegex(
            lowercase_,'^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$',):
            A__        =  feature_extractor(lowercase_,padding=lowercase_,return_tensors='pt'						).input_values

        A__        =  feature_extractor(input_audio[0],padding=lowercase_,return_tensors='pt'						).input_values
        self.assertEquals(truncated_outputs.shape,(1, 1, 9_3_6_8_0)						)

        # no pad if no chunk_length_s
        A__        =  None
        with self.assertRaisesRegex(
            lowercase_,'^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$',):
            A__        =  feature_extractor(lowercase_,padding=lowercase_,return_tensors='pt'						).input_values

        A__        =  feature_extractor(input_audio[0],padding=lowercase_,return_tensors='pt'						).input_values
        self.assertEquals(truncated_outputs.shape,(1, 1, 9_3_6_8_0)						)

        # no pad if no overlap
        A__        =  2
        A__        =  None
        with self.assertRaisesRegex(
            lowercase_,'^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$',):
            A__        =  feature_extractor(lowercase_,padding=lowercase_,return_tensors='pt'						).input_values

        A__        =  feature_extractor(input_audio[0],padding=lowercase_,return_tensors='pt'						).input_values
        self.assertEquals(truncated_outputs.shape,(1, 1, 9_3_6_8_0)						)

