





import inspect
import unittest

from transformers import MobileNetVaConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import MobileNetVaForImageClassification, MobileNetVaModel
    from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import MobileNetVaImageProcessor

class     snake_case_		(   a							):






 '''simple docstring'''





 def  __UpperCAmelCase						(  self		)	->       Union[str, Any]:
    UpperCAmelCase__						=self.config_class(**self.inputs_dict		)
    self.parent.assertTrue(hasattr(A_,		"tf_padding"		)		)
    self.parent.assertTrue(hasattr(A_,		"depth_multiplier"		)		)

class     snake_case_		:






 '''simple docstring'''





 def __init__(  self,		A_,		A_=13,		A_=3,		A_=32,		A_=0.25,		A_=8,		A_=True,		A_=1024,		A_=32,		A_="relu6",		A_=0.1,		A_=0.02,		A_=True,		A_=True,		A_=10,		A_=None,		)	->       Any:
    UpperCAmelCase__						=parent
    UpperCAmelCase__						=batch_size
    UpperCAmelCase__						=num_channels
    UpperCAmelCase__						=image_size
    UpperCAmelCase__						=depth_multiplier
    UpperCAmelCase__						=min_depth
    UpperCAmelCase__						=tf_padding
    UpperCAmelCase__						=int(last_hidden_size * depth_multiplier		)
    UpperCAmelCase__						=output_stride
    UpperCAmelCase__						=hidden_act
    UpperCAmelCase__						=classifier_dropout_prob
    UpperCAmelCase__						=use_labels
    UpperCAmelCase__						=is_training
    UpperCAmelCase__						=num_labels
    UpperCAmelCase__						=initializer_range
    UpperCAmelCase__						=scope

 def  __UpperCAmelCase						(  self		)	->       Optional[Any]:
    UpperCAmelCase__						=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]		)

    UpperCAmelCase__						=None
    UpperCAmelCase__						=None
    if self.use_labels:
       UpperCAmelCase__						=ids_tensor([self.batch_size],		self.num_labels		)
       UpperCAmelCase__						=ids_tensor([self.batch_size, self.image_size, self.image_size],		self.num_labels		)

    UpperCAmelCase__						=self.get_config()

    return config, pixel_values, labels, pixel_labels

 def  __UpperCAmelCase						(  self		)	->       str:
    return MobileNetVaConfig(
        num_channels=self.num_channels,		image_size=self.image_size,		depth_multiplier=self.depth_multiplier,		min_depth=self.min_depth,		tf_padding=self.tf_padding,		hidden_act=self.hidden_act,		classifier_dropout_prob=self.classifier_dropout_prob,		initializer_range=self.initializer_range,		)

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_,		A_		)	->       Optional[Any]:
    UpperCAmelCase__						=MobileNetVaModel(config=A_		)
    model.to(A_		)
    model.eval()
    UpperCAmelCase__						=model(A_		)
    self.parent.assertEqual(
        result.last_hidden_state.shape,		(
            self.batch_size,
            self.last_hidden_size,
            self.image_size // self.output_stride,
            self.image_size // self.output_stride,
        ),		)

 def  __UpperCAmelCase						(  self,		A_,		A_,		A_,		A_		)	->       int:
    UpperCAmelCase__						=self.num_labels
    UpperCAmelCase__						=MobileNetVaForImageClassification(A_		)
    model.to(A_		)
    model.eval()
    UpperCAmelCase__						=model(A_,		labels=A_		)
    self.parent.assertEqual(result.logits.shape,		(self.batch_size, self.num_labels)		)






 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    UpperCAmelCase__						=self.prepare_config_and_inputs()
    UpperCAmelCase__			,							UpperCAmelCase__			,							UpperCAmelCase__			,							UpperCAmelCase__						=config_and_inputs
    UpperCAmelCase__						={"pixel_values": pixel_values}
    return config, inputs_dict

@require_torch
class     snake_case_		(   a,		a,		unittest.TestCase							):






 '''simple docstring'''





 __UpperCamelCase       =      (MobileNetVaModel, MobileNetVaForImageClassification) if is_torch_available() else ()
 __UpperCamelCase       =      (
     {'feature-extraction': MobileNetVaModel, 'image-classification': MobileNetVaForImageClassification}
     if is_torch_available()
     else {}
 )

 __UpperCamelCase       =      False
 __UpperCamelCase       =      False
 __UpperCamelCase       =      False
 __UpperCamelCase       =      False

 def  __UpperCAmelCase						(  self		)	->       Any:
    UpperCAmelCase__						=MobileNetVaModelTester(self		)
    UpperCAmelCase__						=MobileNetVaConfigTester(self,		config_class=A_,		has_text_modality=A_		)

 def  __UpperCAmelCase						(  self		)	->       List[str]:
    self.config_tester.run_common_tests()

 @unittest.skip(reason="MobileNetV1 does not use inputs_embeds"		)
 def  __UpperCAmelCase						(  self		)	->       str:
    pass

 @unittest.skip(reason="MobileNetV1 does not support input and output embeddings"		)
 def  __UpperCAmelCase						(  self		)	->       Optional[int]:
    pass

 @unittest.skip(reason="MobileNetV1 does not output attentions"		)
 def  __UpperCAmelCase						(  self		)	->       Optional[int]:
    pass

 def  __UpperCAmelCase						(  self		)	->       int:
    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       UpperCAmelCase__						=model_class(A_		)
       UpperCAmelCase__						=inspect.signature(model.forward		)
       # signature.parameters is an OrderedDict => so arg_names order is deterministic
       UpperCAmelCase__						=[*signature.parameters.keys()]

       UpperCAmelCase__						=["pixel_values"]
       self.assertListEqual(arg_names[:1],		A_		)

 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_model(*A_		)

 def  __UpperCAmelCase						(  self		)	->       str:
    def check_hidden_states_output(A_,		A_,		A_		):
       UpperCAmelCase__						=model_class(A_		)
       model.to(A_		)
       model.eval()

       with torch.no_grad():
          UpperCAmelCase__						=model(**self._prepare_for_class(A_,		A_		)		)

       UpperCAmelCase__						=outputs.hidden_states

       UpperCAmelCase__						=26
       self.assertEqual(len(A_		),		A_		)

    UpperCAmelCase__			,							UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs_for_common()

    for model_class in self.all_model_classes:
       UpperCAmelCase__						=True
       check_hidden_states_output(A_,		A_,		A_		)

       # check that output_hidden_states also work using config
       del inputs_dict["output_hidden_states"]
       UpperCAmelCase__						=True

       check_hidden_states_output(A_,		A_,		A_		)

 def  __UpperCAmelCase						(  self		)	->       Any:
    UpperCAmelCase__						=self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_for_image_classification(*A_		)






 @slow
 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    for model_name in MOBILENET_V1_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       UpperCAmelCase__						=MobileNetVaModel.from_pretrained(A_		)
       self.assertIsNotNone(A_		)






def 				_UpperCAmelCase			(    ):




   '''simple docstring'''

   UpperCAmelCase__						=Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"     )
   return image




@require_torch
@require_vision
class     snake_case_		(   unittest.TestCase							):






 '''simple docstring'''





 @cached_property
 def  __UpperCAmelCase						(  self		)	->       Dict:
    return (
        MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v1_1.0_224"		) if is_vision_available() else None
    )






 @slow
 def  __UpperCAmelCase						(  self		)	->       Any:
    UpperCAmelCase__						=MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v1_1.0_224"		).to(A_		)

    UpperCAmelCase__						=self.default_image_processor
    UpperCAmelCase__						=prepare_img()
    UpperCAmelCase__						=image_processor(images=A_,		return_tensors="pt"		).to(A_		)

    # forward pass
    with torch.no_grad():
       UpperCAmelCase__						=model(**A_		)

    # verify the logits
    UpperCAmelCase__						=torch.Size((1, 1001)		)
    self.assertEqual(outputs.logits.shape,		A_		)

    UpperCAmelCase__						=torch.tensor([-4.17_39, -1.12_33, 3.12_05]		).to(A_		)

    self.assertTrue(torch.allclose(outputs.logits[0, :3],		A_,		atol=1E-4		)		)










import functools
import operator

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


UpperCamelCase_												=	logging.get_logger(__name__)

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

class     snake_case_		(   a							):






 '''simple docstring'''





 __UpperCamelCase       =      'wavlm'

 def __init__(  self,		A_=32,		A_=768,		A_=12,		A_=12,		A_=3072,		A_="gelu",		A_=0.1,		A_=0.1,		A_=0.1,		A_=0.0,		A_=0.1,		A_=0.1,		A_=0.02,		A_=1E-5,		A_="group",		A_="gelu",		A_=(512, 512, 512, 512, 512, 512, 512),		A_=(5, 2, 2, 2, 2, 2, 2),		A_=(10, 3, 3, 3, 3, 2, 2),		A_=False,		A_=128,		A_=16,		A_=320,		A_=800,		A_=False,		A_=True,		A_=0.05,		A_=10,		A_=2,		A_=0.0,		A_=10,		A_=320,		A_=2,		A_=0.1,		A_=100,		A_=256,		A_=256,		A_=0.1,		A_="mean",		A_=False,		A_=False,		A_=256,		A_=(512, 512, 512, 512, 1500),		A_=(5, 3, 3, 1, 1),		A_=(1, 2, 3, 1, 1),		A_=512,		A_=80,		A_=0,		A_=1,		A_=2,		A_=False,		A_=3,		A_=2,		A_=3,		A_=None,		**A_,		)	->       Union[str, Any]:
    super().__init__(**A_,		pad_token_id=A_,		bos_token_id=A_,		eos_token_id=A_		)
    UpperCAmelCase__						=hidden_size
    UpperCAmelCase__						=feat_extract_norm
    UpperCAmelCase__						=feat_extract_activation
    UpperCAmelCase__						=list(A_		)
    UpperCAmelCase__						=list(A_		)
    UpperCAmelCase__						=list(A_		)
    UpperCAmelCase__						=conv_bias
    UpperCAmelCase__						=num_buckets
    UpperCAmelCase__						=max_bucket_distance
    UpperCAmelCase__						=num_conv_pos_embeddings
    UpperCAmelCase__						=num_conv_pos_embedding_groups
    UpperCAmelCase__						=len(self.conv_dim		)
    UpperCAmelCase__						=num_hidden_layers
    UpperCAmelCase__						=intermediate_size
    UpperCAmelCase__						=hidden_act
    UpperCAmelCase__						=num_attention_heads
    UpperCAmelCase__						=hidden_dropout
    UpperCAmelCase__						=attention_dropout
    UpperCAmelCase__						=activation_dropout
    UpperCAmelCase__						=feat_proj_dropout
    UpperCAmelCase__						=final_dropout
    UpperCAmelCase__						=layerdrop
    UpperCAmelCase__						=layer_norm_eps
    UpperCAmelCase__						=initializer_range
    UpperCAmelCase__						=num_ctc_classes
    UpperCAmelCase__						=vocab_size
    UpperCAmelCase__						=do_stable_layer_norm
    UpperCAmelCase__						=use_weighted_layer_sum
    UpperCAmelCase__						=classifier_proj_size

    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
    UpperCAmelCase__						=apply_spec_augment
    UpperCAmelCase__						=mask_time_prob
    UpperCAmelCase__						=mask_time_length
    UpperCAmelCase__						=mask_time_min_masks
    UpperCAmelCase__						=mask_feature_prob
    UpperCAmelCase__						=mask_feature_length

    # parameters for pretraining with codevector quantized representations
    UpperCAmelCase__						=num_codevectors_per_group
    UpperCAmelCase__						=num_codevector_groups
    UpperCAmelCase__						=contrastive_logits_temperature
    UpperCAmelCase__						=num_negatives
    UpperCAmelCase__						=codevector_dim
    UpperCAmelCase__						=proj_codevector_dim
    UpperCAmelCase__						=diversity_loss_weight

    # ctc loss
    UpperCAmelCase__						=ctc_loss_reduction
    UpperCAmelCase__						=ctc_zero_infinity

    # adapter
    UpperCAmelCase__						=add_adapter
    UpperCAmelCase__						=adapter_kernel_size
    UpperCAmelCase__						=adapter_stride
    UpperCAmelCase__						=num_adapter_layers
    UpperCAmelCase__						=output_hidden_size or hidden_size

    # SequenceClassification-specific parameter. Feel free to ignore for other classes.
    UpperCAmelCase__						=classifier_proj_size

    # XVector-specific parameters. Feel free to ignore for other classes.
    UpperCAmelCase__						=list(A_		)
    UpperCAmelCase__						=list(A_		)
    UpperCAmelCase__						=list(A_		)
    UpperCAmelCase__						=xvector_output_dim






 @property
 def  __UpperCAmelCase						(  self		)	->       List[Any]:
    return functools.reduce(operator.mul,		self.conv_stride,		1		)



