





'''simple docstring'''




import math

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


lowercase__    :      Union[str, Any]						=    logging.get_logger(__name__)

lowercase__    :      str						=    {
    '''facebook/data2vec-base-960h''': '''https://huggingface.co/facebook/data2vec-audio-base-960h/resolve/main/config.json''',
    # See all Data2VecAudio models at https://huggingface.co/models?filter=data2vec-audio
}


class  SCREAMING_SNAKE_CASE    (a__							):
     lowerCAmelCase					=       '''data2vec-audio'''


     def __init__(						self						,	_UpperCAmelCase=32						,	_UpperCAmelCase=768						,	_UpperCAmelCase=12						,	_UpperCAmelCase=12						,	_UpperCAmelCase=3072						,	_UpperCAmelCase="gelu"						,	_UpperCAmelCase=0.1						,	_UpperCAmelCase=0.1						,	_UpperCAmelCase=0.1						,	_UpperCAmelCase=0.0						,	_UpperCAmelCase=0.1						,	_UpperCAmelCase=0.1						,	_UpperCAmelCase=0.02						,	_UpperCAmelCase=1e-5						,	_UpperCAmelCase="gelu"						,	_UpperCAmelCase=(512, 512, 512, 512, 512, 512, 512)						,	_UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2)						,	_UpperCAmelCase=(10, 3, 3, 3, 3, 2, 2)						,	_UpperCAmelCase=False						,	_UpperCAmelCase=16						,	_UpperCAmelCase=19						,	_UpperCAmelCase=5						,	_UpperCAmelCase=0.05						,	_UpperCAmelCase=10						,	_UpperCAmelCase=2						,	_UpperCAmelCase=0.0						,	_UpperCAmelCase=10						,	_UpperCAmelCase=0						,	_UpperCAmelCase="sum"						,	_UpperCAmelCase=False						,	_UpperCAmelCase=False						,	_UpperCAmelCase=256						,	_UpperCAmelCase=(512, 512, 512, 512, 1500)						,	_UpperCAmelCase=(5, 3, 3, 1, 1)						,	_UpperCAmelCase=(1, 2, 3, 1, 1)						,	_UpperCAmelCase=512						,	_UpperCAmelCase=0						,	_UpperCAmelCase=1						,	_UpperCAmelCase=2						,	_UpperCAmelCase=False						,	_UpperCAmelCase=3						,	_UpperCAmelCase=2						,	_UpperCAmelCase=3						,	_UpperCAmelCase=None						,	**_UpperCAmelCase						,	):

          '''simple docstring'''






          super().__init__(**_UpperCAmelCase						,	pad_token_id=_UpperCAmelCase						,	bos_token_id=_UpperCAmelCase						,	eos_token_id=_UpperCAmelCase)
          __A						: Tuple      =       hidden_size
          __A						: Optional[Any]      =       feat_extract_activation
          __A						: int      =       list(_UpperCAmelCase)
          __A						: str      =       list(_UpperCAmelCase)
          __A						: List[str]      =       list(_UpperCAmelCase)
          __A						: Optional[Any]      =       conv_bias
          __A						: int      =       num_conv_pos_embeddings
          __A						: Dict      =       num_conv_pos_embedding_groups
          __A						: str      =       conv_pos_kernel_size
          __A						: Optional[Any]      =       len(self.conv_dim)
          __A						: Any      =       num_hidden_layers
          __A						: Optional[int]      =       intermediate_size
          __A						: int      =       hidden_act
          __A						: Any      =       num_attention_heads
          __A						: Any      =       hidden_dropout
          __A						: Optional[Any]      =       attention_dropout
          __A						: Optional[int]      =       activation_dropout
          __A						: int      =       feat_proj_dropout
          __A						: int      =       final_dropout
          __A						: Union[str, Any]      =       layerdrop
          __A						: Tuple      =       layer_norm_eps
          __A						: Union[str, Any]      =       initializer_range
          __A						: Dict      =       vocab_size
          __A						: int      =       use_weighted_layer_sum

          if (
              (len(self.conv_stride) != self.num_feat_extract_layers)
              or (len(self.conv_kernel) != self.num_feat_extract_layers)
              or (len(self.conv_dim) != self.num_feat_extract_layers)
          ):
               raise ValueError(
                   'Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='
                   ' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='
                   F' {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,'
                   F' `len(config.conv_kernel) = {len(self.conv_kernel)}`.')

          # fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
          __A						: List[str]      =       mask_time_prob
          __A						: str      =       mask_time_length
          __A						: Union[str, Any]      =       mask_time_min_masks
          __A						: List[str]      =       mask_feature_prob
          __A						: Optional[int]      =       mask_feature_length
          __A						: Tuple      =       mask_feature_min_masks

          # ctc loss
          __A						: str      =       ctc_loss_reduction
          __A						: Union[str, Any]      =       ctc_zero_infinity

          # adapter
          __A						: int      =       add_adapter
          __A						: Any      =       adapter_kernel_size
          __A						: Union[str, Any]      =       adapter_stride
          __A						: str      =       num_adapter_layers
          __A						: Tuple      =       output_hidden_size or hidden_size

          # SequenceClassification-specific parameter. Feel free to ignore for other classes.
          __A						: int      =       classifier_proj_size

          # XVector-specific parameters. Feel free to ignore for other classes.
          __A						: Any      =       list(_UpperCAmelCase)
          __A						: Optional[int]      =       list(_UpperCAmelCase)
          __A						: Dict      =       list(_UpperCAmelCase)
          __A						: List[str]      =       xvector_output_dim


     @property
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          return math.prod(self.conv_stride)






'''simple docstring'''




import hashlib
import unittest
from typing import Dict

import numpy as np

from transformers import (
    MODEL_FOR_MASK_GENERATION_MAPPING,
    TF_MODEL_FOR_MASK_GENERATION_MAPPING,
    is_vision_available,
    pipeline,
)
from transformers.pipelines import MaskGenerationPipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)


if is_vision_available():
  from PIL import Image
else:


  class  SCREAMING_SNAKE_CASE    :


       @staticmethod
       def 							SCREAMING_SNAKE_CASE  (						*_UpperCAmelCase						,	**_UpperCAmelCase):

            '''simple docstring'''






            pass






def        _lowerCAmelCase			(  __snake_case  : Image			)     ->     str:
     __A						: Dict      =       hashlib.mda(image.tobytes()			)
     return m.hexdigest()[:10]






def        _lowerCAmelCase			(  __snake_case  : Image			)     ->     Dict:
     __A						: Dict      =       np.array(__snake_case			)
     __A						: List[Any]      =       npimg.shape
     return {"hash": hashimage(__snake_case			), "shape": shape}







@is_pipeline_test
@require_vision
@require_torch
class  SCREAMING_SNAKE_CASE    (unittest.TestCase							):
     lowerCAmelCase					=       dict(
         (list(MODEL_FOR_MASK_GENERATION_MAPPING.items()							) if MODEL_FOR_MASK_GENERATION_MAPPING else [])							)
     lowerCAmelCase					=       dict(
         (list(TF_MODEL_FOR_MASK_GENERATION_MAPPING.items()							) if TF_MODEL_FOR_MASK_GENERATION_MAPPING else [])							)


     def 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase						,	_UpperCAmelCase						,	_UpperCAmelCase):

          '''simple docstring'''






          __A						: Any      =       MaskGenerationPipeline(model=_UpperCAmelCase						,	image_processor=_UpperCAmelCase)
          return image_segmenter, [
              "./tests/fixtures/tests_samples/COCO/000000039769.png",
              "./tests/fixtures/tests_samples/COCO/000000039769.png",
          ]


     def 							SCREAMING_SNAKE_CASE  (						self						,	_UpperCAmelCase						,	_UpperCAmelCase):

          '''simple docstring'''






          pass


     @require_tf
     @unittest.skip('Image segmentation not implemented in TF')
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          pass


     @slow
     @require_torch
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: Optional[int]      =       pipeline('mask-generation'						,	model='facebook/sam-vit-huge')

          __A						: Tuple      =       image_segmenter('http://images.cocodataset.org/val2017/000000039769.jpg'						,	points_per_batch=256)

          # Shortening by hashing
          __A						: int      =       []
          for i, o in enumerate(outputs['masks']):
               new_outupt += [{"mask": mask_to_test_readable(_UpperCAmelCase), "scores": outputs["scores"][i]}]

          # fmt: off
          self.assertEqual(
              nested_simplify(_UpperCAmelCase						,	decimals=4)						,	[
                  {'mask': {'hash': '115ad19f5f', 'shape': (480, 640)}, 'scores': 1.0444},
                  {'mask': {'hash': '6affa964c6', 'shape': (480, 640)}, 'scores': 1.021},
                  {'mask': {'hash': 'dfe28a0388', 'shape': (480, 640)}, 'scores': 1.0167},
                  {'mask': {'hash': 'c0a5f4a318', 'shape': (480, 640)}, 'scores': 1.0132},
                  {'mask': {'hash': 'fe8065c197', 'shape': (480, 640)}, 'scores': 1.0053},
                  {'mask': {'hash': 'e2d0b7a0b7', 'shape': (480, 640)}, 'scores': 0.9967},
                  {'mask': {'hash': '453c7844bd', 'shape': (480, 640)}, 'scores': 0.993},
                  {'mask': {'hash': '3d44f2926d', 'shape': (480, 640)}, 'scores': 0.9909},
                  {'mask': {'hash': '64033ddc3f', 'shape': (480, 640)}, 'scores': 0.9879},
                  {'mask': {'hash': '801064ff79', 'shape': (480, 640)}, 'scores': 0.9834},
                  {'mask': {'hash': '6172f276ef', 'shape': (480, 640)}, 'scores': 0.9716},
                  {'mask': {'hash': 'b49e60e084', 'shape': (480, 640)}, 'scores': 0.9612},
                  {'mask': {'hash': 'a811e775fd', 'shape': (480, 640)}, 'scores': 0.9599},
                  {'mask': {'hash': 'a6a8ebcf4b', 'shape': (480, 640)}, 'scores': 0.9552},
                  {'mask': {'hash': '9d8257e080', 'shape': (480, 640)}, 'scores': 0.9532},
                  {'mask': {'hash': '32de6454a8', 'shape': (480, 640)}, 'scores': 0.9516},
                  {'mask': {'hash': 'af3d4af2c8', 'shape': (480, 640)}, 'scores': 0.9499},
                  {'mask': {'hash': '3c6db475fb', 'shape': (480, 640)}, 'scores': 0.9483},
                  {'mask': {'hash': 'c290813fb9', 'shape': (480, 640)}, 'scores': 0.9464},
                  {'mask': {'hash': 'b6f0b8f606', 'shape': (480, 640)}, 'scores': 0.943},
                  {'mask': {'hash': '92ce16bfdf', 'shape': (480, 640)}, 'scores': 0.943},
                  {'mask': {'hash': 'c749b25868', 'shape': (480, 640)}, 'scores': 0.9408},
                  {'mask': {'hash': 'efb6cab859', 'shape': (480, 640)}, 'scores': 0.9335},
                  {'mask': {'hash': '1ff2eafb30', 'shape': (480, 640)}, 'scores': 0.9326},
                  {'mask': {'hash': '788b798e24', 'shape': (480, 640)}, 'scores': 0.9262},
                  {'mask': {'hash': 'abea804f0e', 'shape': (480, 640)}, 'scores': 0.8999},
                  {'mask': {'hash': '7b9e8ddb73', 'shape': (480, 640)}, 'scores': 0.8986},
                  {'mask': {'hash': 'cd24047c8a', 'shape': (480, 640)}, 'scores': 0.8984},
                  {'mask': {'hash': '6943e6bcbd', 'shape': (480, 640)}, 'scores': 0.8873},
                  {'mask': {'hash': 'b5f47c9191', 'shape': (480, 640)}, 'scores': 0.8871}
              ]						,	)
          # fmt: on


     @require_torch
     @slow
     def 							SCREAMING_SNAKE_CASE  (						self):

          '''simple docstring'''






          __A						: int      =       'facebook/sam-vit-huge'
          __A						: Optional[Any]      =       pipeline('mask-generation'						,	model=_UpperCAmelCase)

          __A						: int      =       image_segmenter(
              'http://images.cocodataset.org/val2017/000000039769.jpg'						,	pred_iou_thresh=1						,	points_per_batch=256)

          # Shortening by hashing
          __A						: int      =       []
          for i, o in enumerate(outputs['masks']):
               new_outupt += [{"mask": mask_to_test_readable(_UpperCAmelCase), "scores": outputs["scores"][i]}]

          self.assertEqual(
              nested_simplify(_UpperCAmelCase						,	decimals=4)						,	[
                  {'mask': {'hash': '115ad19f5f', 'shape': (480, 640)}, 'scores': 1.0444},
                  {'mask': {'hash': '6affa964c6', 'shape': (480, 640)}, 'scores': 1.0210},
                  {'mask': {'hash': 'dfe28a0388', 'shape': (480, 640)}, 'scores': 1.0167},
                  {'mask': {'hash': 'c0a5f4a318', 'shape': (480, 640)}, 'scores': 1.0132},
                  {'mask': {'hash': 'fe8065c197', 'shape': (480, 640)}, 'scores': 1.0053},
              ]						,	)