



"""simple docstring"""




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


_UpperCamelCase:					Any     				=	random.Random()


def 	lowercase__  (      _UpperCAmelCase						,   _UpperCAmelCase=1.0						,   _UpperCAmelCase=None						,   _UpperCAmelCase=None					)  ->      int:






      '''simple docstring'''


      if rng is None:
            lowercase	:   int											= global_rng

      lowercase	:   Tuple											= []
      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  ):


   def __init__(				self   :				Optional[Any],  lowerCAmelCase   :				str,  lowerCAmelCase   :				List[str]=7,  lowerCAmelCase   :				int=400,  lowerCAmelCase   :				Tuple=2000,  lowerCAmelCase   :				Optional[Any]=1,  lowerCAmelCase   :				Optional[int]=0.0,  lowerCAmelCase   :				Dict=24000,  lowerCAmelCase   :				Dict=True,  )     ->							Any:
         lowercase	:   List[str]											= parent
         lowercase	:   List[str]											= batch_size
         lowercase	:   List[str]											= min_seq_length
         lowercase	:   Optional[int]											= max_seq_length
         lowercase	:   int											= (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
         lowercase	:   int											= feature_size
         lowercase	:   List[str]											= padding_value
         lowercase	:   Optional[Any]											= sampling_rate
         lowercase	:   Optional[int]											= return_attention_mask


   def 							lowercase					(				self   :				Optional[int]      )     ->							Optional[Any]:
         return {
             "feature_size": self.feature_size,
             "padding_value": self.padding_value,
             "sampling_rate": self.sampling_rate,
             "return_attention_mask": self.return_attention_mask,
         }






   def 							lowercase					(				self   :				List[Any],  lowerCAmelCase   :				List[Any]=False,  lowerCAmelCase   :				Dict=False      )     ->							List[str]:
         def _flatten(lowerCAmelCase   :				Optional[int]      ):
               return list(itertools.chain(*lowerCamelCase_      )      )

         if equal_length:
               lowercase	:   Optional[int]											= floats_list((self.batch_size, self.max_seq_length)      )
         else:
               # make sure that inputs increase in size
               lowercase	:   List[Any]											= [
                   _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:
               lowercase	:   str											= [np.asarray(lowerCamelCase_      ) for x in audio_inputs]

         return audio_inputs




@require_torch
class 	a__		(    __snake_case,	unittest.TestCase  ):
   _lowerCamelCase       				=					EncodecFeatureExtractor


   def 							lowercase					(				self   :				Union[str, Any]      )     ->							Union[str, Any]:
         lowercase	:   List[str]											= EnCodecFeatureExtractionTester(self      )


   def 							lowercase					(				self   :				List[Any]      )     ->							Dict:
         # Tests that all call wrap to encode_plus and batch_encode_plus
         lowercase	:   List[Any]											= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
         # create three inputs of length 800, 1000, and 1200
         lowercase	:   str											= [floats_list((1, x)      )[0] for x in range(800,  1400,  200      )]
         lowercase	:   int											= [np.asarray(lowerCamelCase_      ) for audio_input in audio_inputs]

         # Test not batched input
         lowercase	:   Union[str, Any]											= feat_extract(audio_inputs[0],  return_tensors='np'      ).input_values
         lowercase	:   Dict											= feat_extract(np_audio_inputs[0],  return_tensors='np'      ).input_values
         self.assertTrue(np.allclose(lowerCamelCase_,  lowerCamelCase_,  atol=1e-3      )      )

         # Test batched
         lowercase	:   Tuple											= feat_extract(lowerCamelCase_,  padding=lowerCamelCase_,  return_tensors='np'      ).input_values
         lowercase	:   Any											= feat_extract(lowerCamelCase_,  padding=lowerCamelCase_,  return_tensors='np'      ).input_values
         for enc_seq_a, enc_seq_a in zip(lowerCamelCase_,  lowerCamelCase_      ):
               self.assertTrue(np.allclose(lowerCamelCase_,  lowerCamelCase_,  atol=1e-3      )      )


   def 							lowercase					(				self   :				str      )     ->							List[str]:
         lowercase	:   Union[str, Any]											= self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()      )
         lowercase	:   Union[str, Any]											= np.random.rand(100      ).astype(np.floataa      )
         lowercase	:   Union[str, Any]											= np_audio_inputs.tolist()

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


   def 							lowercase					(				self   :				List[str],  lowerCAmelCase   :				Union[str, Any]      )     ->							Optional[Any]:
         from datasets import load_dataset

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

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


   def 							lowercase					(				self   :				Optional[int]      )     ->							Union[str, Any]:
         # fmt: off
         lowercase	:   Dict											= torch.tensor(
             [2.3804e-03, 2.0752e-03, 1.9836e-03, 2.1057e-03, 1.6174e-03,
              3.0518e-04, 9.1553e-05, 3.3569e-04, 9.7656e-04, 1.8311e-03,
              2.0142e-03, 2.1057e-03, 1.7395e-03, 4.5776e-04, -3.9673e-04,
              4.5776e-04, 1.0071e-03, 9.1553e-05, 4.8828e-04, 1.1597e-03,
              7.3242e-04, 9.4604e-04, 1.8005e-03, 1.8311e-03, 8.8501e-04,
              4.2725e-04, 4.8828e-04, 7.3242e-04, 1.0986e-03, 2.1057e-03]      )
         # fmt: on
         lowercase	:   Optional[Any]											= self._load_datasamples(1      )
         lowercase	:   Union[str, Any]											= EncodecFeatureExtractor()
         lowercase	:   List[Any]											= feature_extractor(lowerCamelCase_,  return_tensors='pt'      ).input_values
         self.assertEquals(input_values.shape,  (1, 1, 93680)      )
         self.assertTrue(torch.allclose(input_values[0, 0, :30],  lowerCamelCase_,  atol=1e-6      )      )


   def 							lowercase					(				self   :				List[str]      )     ->							Dict:
         # fmt: off
         lowercase	:   Optional[int]											= torch.tensor(
             [2.3804e-03, 2.0752e-03, 1.9836e-03, 2.1057e-03, 1.6174e-03,
              3.0518e-04, 9.1553e-05, 3.3569e-04, 9.7656e-04, 1.8311e-03,
              2.0142e-03, 2.1057e-03, 1.7395e-03, 4.5776e-04, -3.9673e-04,
              4.5776e-04, 1.0071e-03, 9.1553e-05, 4.8828e-04, 1.1597e-03,
              7.3242e-04, 9.4604e-04, 1.8005e-03, 1.8311e-03, 8.8501e-04,
              4.2725e-04, 4.8828e-04, 7.3242e-04, 1.0986e-03, 2.1057e-03]      )
         # fmt: on
         lowercase	:   Tuple											= self._load_datasamples(1      )
         lowercase	:   List[Any]											= [np.tile(input_audio[0][None],  reps=(2, 1)      )]
         input_audio[0][1] *= 0.5
         lowercase	:   int											= EncodecFeatureExtractor(feature_size=2      )
         lowercase	:   int											= feature_extractor(lowerCamelCase_,  return_tensors='pt'      ).input_values
         self.assertEquals(input_values.shape,  (1, 2, 93680)      )
         self.assertTrue(torch.allclose(input_values[0, 0, :30],  lowerCamelCase_,  atol=1e-6      )      )
         self.assertTrue(torch.allclose(input_values[0, 1, :30],  EXPECTED_INPUT_VALUES * 0.5,  atol=1e-6      )      )






   def 							lowercase					(				self   :				Optional[int]      )     ->							List[str]:
         lowercase	:   Dict											= self._load_datasamples(2      )
         # would be easier if the stride was like
         lowercase	:   Any											= EncodecFeatureExtractor(feature_size=1,  chunk_length_s=1,  overlap=0.01      )

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

         # truncate to chunk
         lowercase	:   str											= feature_extractor(lowerCamelCase_,  truncation=lowerCamelCase_,  return_tensors='pt'      ).input_values
         self.assertEquals(truncated_outputs.shape,  (2, 1, 71520)      )  # 2 chunks

         # force truncate to max_length
         lowercase	:   Optional[Any]											= feature_extractor(
             lowerCamelCase_,  truncation=lowerCamelCase_,  max_length=48000,  return_tensors='pt'      ).input_values
         self.assertEquals(truncated_outputs.shape,  (2, 1, 48000)      )

         # pad to chunk
         lowercase	:   Dict											= feature_extractor(lowerCamelCase_,  padding=lowerCamelCase_,  return_tensors='pt'      ).input_values
         self.assertEquals(padded_outputs.shape,  (2, 1, 95280)      )

         # pad to chunk
         lowercase	:   Optional[Any]											= feature_extractor(lowerCamelCase_,  return_tensors='pt'      ).input_values
         self.assertEquals(truncated_outputs.shape,  (2, 1, 95280)      )

         # force pad to max length
         lowercase	:   str											= feature_extractor(
             lowerCamelCase_,  padding='max_length',  max_length=100000,  return_tensors='pt'      ).input_values
         self.assertEquals(truncated_outputs.shape,  (2, 1, 100000)      )

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

         lowercase	:   Any											= feature_extractor(input_audio[0],  padding=lowerCamelCase_,  return_tensors='pt'      ).input_values
         self.assertEquals(truncated_outputs.shape,  (1, 1, 93680)      )

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

         lowercase	:   Any											= feature_extractor(input_audio[0],  padding=lowerCamelCase_,  return_tensors='pt'      ).input_values
         self.assertEquals(truncated_outputs.shape,  (1, 1, 93680)      )

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

         lowercase	:   Optional[Any]											= feature_extractor(input_audio[0],  padding=lowerCamelCase_,  return_tensors='pt'      ).input_values
         self.assertEquals(truncated_outputs.shape,  (1, 1, 93680)      )





from ...configuration_utils import PretrainedConfig
from ...utils import is_detectrona_available, logging


UpperCamelCase_									=   logging.get_logger(__name__)

UpperCamelCase_									=   {
    '''layoutlmv2-base-uncased''': '''https://huggingface.co/microsoft/layoutlmv2-base-uncased/resolve/main/config.json''',
    '''layoutlmv2-large-uncased''': '''https://huggingface.co/microsoft/layoutlmv2-large-uncased/resolve/main/config.json''',
    # See all LayoutLMv2 models at https://huggingface.co/models?filter=layoutlmv2
}

# soft dependency
if is_detectrona_available():
      import detectrona







class       _snake_case				(     __snake_case ):






   '''simple docstring'''






   A__      : List[Any]       =						"layoutlmv2"


   def __init__(    self:     Tuple  ,lowerCamelCase_:     Dict=30522  ,lowerCamelCase_:     str=768  ,lowerCamelCase_:     Any=12  ,lowerCamelCase_:     Tuple=12  ,lowerCamelCase_:     int=3072  ,lowerCamelCase_:     int="gelu"  ,lowerCamelCase_:     Dict=0.1  ,lowerCamelCase_:     List[Any]=0.1  ,lowerCamelCase_:     List[Any]=512  ,lowerCamelCase_:     Tuple=2  ,lowerCamelCase_:     Dict=0.0_2  ,lowerCamelCase_:     Dict=1e-12  ,lowerCamelCase_:     Dict=0  ,lowerCamelCase_:     List[str]=1024  ,lowerCamelCase_:     Optional[Any]=128  ,lowerCamelCase_:     List[Any]=32  ,lowerCamelCase_:     List[Any]=True  ,lowerCamelCase_:     Dict=256  ,lowerCamelCase_:     int=64  ,lowerCamelCase_:     str=True  ,lowerCamelCase_:     Tuple=[7, 7, 256]  ,lowerCamelCase_:     Union[str, Any]=128  ,lowerCamelCase_:     Any=128  ,lowerCamelCase_:     int=True  ,lowerCamelCase_:     Union[str, Any]=True  ,lowerCamelCase_:     Optional[int]=False  ,lowerCamelCase_:     List[str]=None  ,**lowerCamelCase_:     str  ,)	->      Any:
       super().__init__(
           vocab_size=lowerCamelCase_  ,hidden_size=lowerCamelCase_  ,num_hidden_layers=lowerCamelCase_  ,num_attention_heads=lowerCamelCase_  ,intermediate_size=lowerCamelCase_  ,hidden_act=lowerCamelCase_  ,hidden_dropout_prob=lowerCamelCase_  ,attention_probs_dropout_prob=lowerCamelCase_  ,max_position_embeddings=lowerCamelCase_  ,type_vocab_size=lowerCamelCase_  ,initializer_range=lowerCamelCase_  ,layer_norm_eps=lowerCamelCase_  ,pad_token_id=lowerCamelCase_  ,**lowerCamelCase_  ,)
       UpperCAmelCase_     :       Optional[int]  		=					max_ad_position_embeddings
       UpperCAmelCase_     :       Optional[int]  		=					max_rel_pos
       UpperCAmelCase_     :       Optional[int]  		=					rel_pos_bins
       UpperCAmelCase_     :       List[str]  		=					fast_qkv
       UpperCAmelCase_     :       int  		=					max_rel_ad_pos
       UpperCAmelCase_     :       int  		=					rel_ad_pos_bins
       UpperCAmelCase_     :       Optional[Any]  		=					convert_sync_batchnorm
       UpperCAmelCase_     :       str  		=					image_feature_pool_shape
       UpperCAmelCase_     :       List[Any]  		=					coordinate_size
       UpperCAmelCase_     :       int  		=					shape_size
       UpperCAmelCase_     :       Optional[Any]  		=					has_relative_attention_bias
       UpperCAmelCase_     :       str  		=					has_spatial_attention_bias
       UpperCAmelCase_     :       Tuple  		=					has_visual_segment_embedding
       UpperCAmelCase_     :       Optional[Any]  		=					(
           detectrona_config_args if detectrona_config_args is not None else self.get_default_detectrona_config()
       )


   @classmethod
   def       A__    (    self:     Any							)	->      Dict:
       return {
           "MODEL.MASK_ON": True,
           "MODEL.PIXEL_STD": [5_7.3_7_5, 5_7.1_2_0, 5_8.3_9_5],
           "MODEL.BACKBONE.NAME": "build_resnet_fpn_backbone",
           "MODEL.FPN.IN_FEATURES": ["res2", "res3", "res4", "res5"],
           "MODEL.ANCHOR_GENERATOR.SIZES": [[32], [64], [128], [256], [512]],
           "MODEL.RPN.IN_FEATURES": ["p2", "p3", "p4", "p5", "p6"],
           "MODEL.RPN.PRE_NMS_TOPK_TRAIN": 2000,
           "MODEL.RPN.PRE_NMS_TOPK_TEST": 1000,
           "MODEL.RPN.POST_NMS_TOPK_TRAIN": 1000,
           "MODEL.POST_NMS_TOPK_TEST": 1000,
           "MODEL.ROI_HEADS.NAME": "StandardROIHeads",
           "MODEL.ROI_HEADS.NUM_CLASSES": 5,
           "MODEL.ROI_HEADS.IN_FEATURES": ["p2", "p3", "p4", "p5"],
           "MODEL.ROI_BOX_HEAD.NAME": "FastRCNNConvFCHead",
           "MODEL.ROI_BOX_HEAD.NUM_FC": 2,
           "MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION": 14,
           "MODEL.ROI_MASK_HEAD.NAME": "MaskRCNNConvUpsampleHead",
           "MODEL.ROI_MASK_HEAD.NUM_CONV": 4,
           "MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION": 7,
           "MODEL.RESNETS.DEPTH": 101,
           "MODEL.RESNETS.SIZES": [[32], [64], [128], [256], [512]],
           "MODEL.RESNETS.ASPECT_RATIOS": [[0.5, 1.0, 2.0]],
           "MODEL.RESNETS.OUT_FEATURES": ["res2", "res3", "res4", "res5"],
           "MODEL.RESNETS.NUM_GROUPS": 32,
           "MODEL.RESNETS.WIDTH_PER_GROUP": 8,
           "MODEL.RESNETS.STRIDE_IN_1X1": False,
       }






   def       A__    (    self:     Tuple							)	->      int:
       UpperCAmelCase_     :       Any  		=					detectrona.config.get_cfg()
       for k, v in self.detectrona_config_args.items():
           UpperCAmelCase_     :       Optional[int]  		=					k.split("""."""							)
           UpperCAmelCase_     :       List[str]  		=					detectrona_config
           for attribute in attributes[:-1]:
               UpperCAmelCase_     :       Optional[Any]  		=					getattr(lowerCamelCase_  ,lowerCamelCase_							)
           setattr(lowerCamelCase_  ,attributes[-1]  ,lowerCamelCase_							)

       return detectrona_config
