





"""simple docstring"""





import functools
import operator

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


__A											=						logging.get_logger(__name__)

__A											=						{
    """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    _lowerCAmelCase  (	a			):
       """simple docstring"""




       __magic_name__	:Dict						    =   """wavlm"""






       def __init__(       self       ,					__UpperCAmelCase=3_2       ,					__UpperCAmelCase=7_6_8       ,					__UpperCAmelCase=1_2       ,					__UpperCAmelCase=1_2       ,					__UpperCAmelCase=3_0_7_2       ,					__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="group"       ,					__UpperCAmelCase="gelu"       ,					__UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2)       ,					__UpperCAmelCase=(5, 2, 2, 2, 2, 2, 2)       ,					__UpperCAmelCase=(1_0, 3, 3, 3, 3, 2, 2)       ,					__UpperCAmelCase=False       ,					__UpperCAmelCase=1_2_8       ,					__UpperCAmelCase=1_6       ,					__UpperCAmelCase=3_2_0       ,					__UpperCAmelCase=8_0_0       ,					__UpperCAmelCase=False       ,					__UpperCAmelCase=True       ,					__UpperCAmelCase=0.05       ,					__UpperCAmelCase=1_0       ,					__UpperCAmelCase=2       ,					__UpperCAmelCase=0.0       ,					__UpperCAmelCase=1_0       ,					__UpperCAmelCase=3_2_0       ,					__UpperCAmelCase=2       ,					__UpperCAmelCase=0.1       ,					__UpperCAmelCase=1_0_0       ,					__UpperCAmelCase=2_5_6       ,					__UpperCAmelCase=2_5_6       ,					__UpperCAmelCase=0.1       ,					__UpperCAmelCase="mean"       ,					__UpperCAmelCase=False       ,					__UpperCAmelCase=False       ,					__UpperCAmelCase=2_5_6       ,					__UpperCAmelCase=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0)       ,					__UpperCAmelCase=(5, 3, 3, 1, 1)       ,					__UpperCAmelCase=(1, 2, 3, 1, 1)       ,					__UpperCAmelCase=5_1_2       ,					__UpperCAmelCase=8_0       ,					__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 )
         lowerCAmelCase__     :int											=				hidden_size
         lowerCAmelCase__     :Optional[Any]											=				feat_extract_norm
         lowerCAmelCase__     :List[str]											=				feat_extract_activation
         lowerCAmelCase__     :List[Any]											=				list(__UpperCAmelCase )
         lowerCAmelCase__     :int											=				list(__UpperCAmelCase )
         lowerCAmelCase__     :List[str]											=				list(__UpperCAmelCase )
         lowerCAmelCase__     :str											=				conv_bias
         lowerCAmelCase__     :Any											=				num_buckets
         lowerCAmelCase__     :Optional[Any]											=				max_bucket_distance
         lowerCAmelCase__     :str											=				num_conv_pos_embeddings
         lowerCAmelCase__     :int											=				num_conv_pos_embedding_groups
         lowerCAmelCase__     :Dict											=				len(self.conv_dim )
         lowerCAmelCase__     :int											=				num_hidden_layers
         lowerCAmelCase__     :int											=				intermediate_size
         lowerCAmelCase__     :List[str]											=				hidden_act
         lowerCAmelCase__     :Any											=				num_attention_heads
         lowerCAmelCase__     :Optional[Any]											=				hidden_dropout
         lowerCAmelCase__     :Union[str, Any]											=				attention_dropout
         lowerCAmelCase__     :Tuple											=				activation_dropout
         lowerCAmelCase__     :Union[str, Any]											=				feat_proj_dropout
         lowerCAmelCase__     :Optional[Any]											=				final_dropout
         lowerCAmelCase__     :int											=				layerdrop
         lowerCAmelCase__     :Union[str, Any]											=				layer_norm_eps
         lowerCAmelCase__     :int											=				initializer_range
         lowerCAmelCase__     :Any											=				num_ctc_classes
         lowerCAmelCase__     :int											=				vocab_size
         lowerCAmelCase__     :Dict											=				do_stable_layer_norm
         lowerCAmelCase__     :Tuple											=				use_weighted_layer_sum
         lowerCAmelCase__     :Dict											=				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
         lowerCAmelCase__     :str											=				apply_spec_augment
         lowerCAmelCase__     :Any											=				mask_time_prob
         lowerCAmelCase__     :Union[str, Any]											=				mask_time_length
         lowerCAmelCase__     :List[Any]											=				mask_time_min_masks
         lowerCAmelCase__     :Dict											=				mask_feature_prob
         lowerCAmelCase__     :Union[str, Any]											=				mask_feature_length

         # parameters for pretraining with codevector quantized representations
         lowerCAmelCase__     :Optional[Any]											=				num_codevectors_per_group
         lowerCAmelCase__     :List[str]											=				num_codevector_groups
         lowerCAmelCase__     :Union[str, Any]											=				contrastive_logits_temperature
         lowerCAmelCase__     :List[str]											=				num_negatives
         lowerCAmelCase__     :Union[str, Any]											=				codevector_dim
         lowerCAmelCase__     :Optional[Any]											=				proj_codevector_dim
         lowerCAmelCase__     :Optional[int]											=				diversity_loss_weight

         # ctc loss
         lowerCAmelCase__     :Any											=				ctc_loss_reduction
         lowerCAmelCase__     :str											=				ctc_zero_infinity

         # adapter
         lowerCAmelCase__     :Optional[int]											=				add_adapter
         lowerCAmelCase__     :Tuple											=				adapter_kernel_size
         lowerCAmelCase__     :Tuple											=				adapter_stride
         lowerCAmelCase__     :List[Any]											=				num_adapter_layers
         lowerCAmelCase__     :Dict											=				output_hidden_size or hidden_size

         # SequenceClassification-specific parameter. Feel free to ignore for other classes.
         lowerCAmelCase__     :Optional[Any]											=				classifier_proj_size

         # XVector-specific parameters. Feel free to ignore for other classes.
         lowerCAmelCase__     :int											=				list(__UpperCAmelCase )
         lowerCAmelCase__     :Tuple											=				list(__UpperCAmelCase )
         lowerCAmelCase__     :Any											=				list(__UpperCAmelCase )
         lowerCAmelCase__     :Any											=				xvector_output_dim






       @property
       def 	snake_case		(       self ):



         '''simple docstring'''

         return functools.reduce(operator.mul       ,					self.conv_stride       ,					1 )







"""simple docstring"""





import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
  from PIL import Image

  from transformers import (
      AutoProcessor,
      BertTokenizerFast,
      BlipImageProcessor,
      GPTaTokenizer,
      InstructBlipProcessor,
      PreTrainedTokenizerFast,
  )






@require_vision
class    _lowerCAmelCase  (	unittest.TestCase			):
       """simple docstring"""




       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Any											=				tempfile.mkdtemp()

         lowerCAmelCase__     :List[Any]											=				BlipImageProcessor()
         lowerCAmelCase__     :Union[str, Any]											=				GPTaTokenizer.from_pretrained('hf-internal-testing/tiny-random-GPT2Model' )
         lowerCAmelCase__     :Union[str, Any]											=				BertTokenizerFast.from_pretrained('hf-internal-testing/tiny-random-bert' )

         lowerCAmelCase__     :List[str]											=				InstructBlipProcessor(__UpperCAmelCase       ,					__UpperCAmelCase       ,					__UpperCAmelCase )

         processor.save_pretrained(self.tmpdirname )






       def 	snake_case		(       self       ,					**__UpperCAmelCase ):



         '''simple docstring'''

         return AutoProcessor.from_pretrained(self.tmpdirname       ,					**__UpperCAmelCase ).tokenizer






       def 	snake_case		(       self       ,					**__UpperCAmelCase ):



         '''simple docstring'''

         return AutoProcessor.from_pretrained(self.tmpdirname       ,					**__UpperCAmelCase ).image_processor






       def 	snake_case		(       self       ,					**__UpperCAmelCase ):



         '''simple docstring'''

         return AutoProcessor.from_pretrained(self.tmpdirname       ,					**__UpperCAmelCase ).qformer_tokenizer






       def 	snake_case		(       self ):



         '''simple docstring'''

         shutil.rmtree(self.tmpdirname )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				[np.random.randint(2_5_5       ,					size=(3, 3_0, 4_0_0)       ,					dtype=np.uinta )]

         lowerCAmelCase__     :Dict											=				[Image.fromarray(np.moveaxis(__UpperCAmelCase       ,					0       ,					-1 ) ) for x in image_inputs]

         return image_inputs






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :List[str]											=				InstructBlipProcessor(
             tokenizer=self.get_tokenizer()       ,					image_processor=self.get_image_processor()       ,					qformer_tokenizer=self.get_qformer_tokenizer()       ,					)
         processor.save_pretrained(self.tmpdirname )

         lowerCAmelCase__     :Tuple											=				self.get_tokenizer(bos_token='(BOS)'       ,					eos_token='(EOS)' )
         lowerCAmelCase__     :str											=				self.get_image_processor(do_normalize=__UpperCAmelCase       ,					padding_value=1.0 )

         lowerCAmelCase__     :Dict											=				InstructBlipProcessor.from_pretrained(
             self.tmpdirname       ,					bos_token='(BOS)'       ,					eos_token='(EOS)'       ,					do_normalize=__UpperCAmelCase       ,					padding_value=1.0 )

         self.assertEqual(processor.tokenizer.get_vocab()       ,					tokenizer_add_kwargs.get_vocab() )
         self.assertIsInstance(processor.tokenizer       ,					__UpperCAmelCase )

         self.assertEqual(processor.image_processor.to_json_string()       ,					image_processor_add_kwargs.to_json_string() )
         self.assertIsInstance(processor.image_processor       ,					__UpperCAmelCase )
         self.assertIsInstance(processor.qformer_tokenizer       ,					__UpperCAmelCase )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				self.get_image_processor()
         lowerCAmelCase__     :List[Any]											=				self.get_tokenizer()
         lowerCAmelCase__     :Optional[int]											=				self.get_qformer_tokenizer()

         lowerCAmelCase__     :str											=				InstructBlipProcessor(
             tokenizer=__UpperCAmelCase       ,					image_processor=__UpperCAmelCase       ,					qformer_tokenizer=__UpperCAmelCase )

         lowerCAmelCase__     :Dict											=				self.prepare_image_inputs()

         lowerCAmelCase__     :List[str]											=				image_processor(__UpperCAmelCase       ,					return_tensors='np' )
         lowerCAmelCase__     :Optional[Any]											=				processor(images=__UpperCAmelCase       ,					return_tensors='np' )

         for key in input_feat_extract.keys():
           self.assertAlmostEqual(input_feat_extract[key].sum()       ,					input_processor[key].sum()       ,					delta=1E-2 )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Union[str, Any]											=				self.get_image_processor()
         lowerCAmelCase__     :Optional[int]											=				self.get_tokenizer()
         lowerCAmelCase__     :Optional[int]											=				self.get_qformer_tokenizer()

         lowerCAmelCase__     :Any											=				InstructBlipProcessor(
             tokenizer=__UpperCAmelCase       ,					image_processor=__UpperCAmelCase       ,					qformer_tokenizer=__UpperCAmelCase )

         lowerCAmelCase__     :Optional[int]											=				'lower newer'

         lowerCAmelCase__     :Dict											=				processor(text=__UpperCAmelCase )

         lowerCAmelCase__     :Union[str, Any]											=				tokenizer(__UpperCAmelCase       ,					return_token_type_ids=__UpperCAmelCase )
         lowerCAmelCase__     :Tuple											=				qformer_tokenizer(__UpperCAmelCase       ,					return_token_type_ids=__UpperCAmelCase )

         for key in encoded_tokens.keys():
           self.assertListEqual(encoded_tokens[key]       ,					encoded_processor[key] )

         for key in encoded_tokens_qformer.keys():
           self.assertListEqual(encoded_tokens_qformer[key]       ,					encoded_processor['qformer_' + key] )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Optional[Any]											=				self.get_image_processor()
         lowerCAmelCase__     :Optional[int]											=				self.get_tokenizer()
         lowerCAmelCase__     :str											=				self.get_qformer_tokenizer()

         lowerCAmelCase__     :Dict											=				InstructBlipProcessor(
             tokenizer=__UpperCAmelCase       ,					image_processor=__UpperCAmelCase       ,					qformer_tokenizer=__UpperCAmelCase )

         lowerCAmelCase__     :Union[str, Any]											=				'lower newer'
         lowerCAmelCase__     :Dict											=				self.prepare_image_inputs()

         lowerCAmelCase__     :Tuple											=				processor(text=__UpperCAmelCase       ,					images=__UpperCAmelCase )

         self.assertListEqual(
             list(inputs.keys() )       ,					['input_ids', 'attention_mask', 'qformer_input_ids', 'qformer_attention_mask', 'pixel_values']       ,					)

         # test if it raises when no input is passed
         with pytest.raises(__UpperCAmelCase ):
           processor()






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Tuple											=				self.get_image_processor()
         lowerCAmelCase__     :Dict											=				self.get_tokenizer()
         lowerCAmelCase__     :Optional[int]											=				self.get_qformer_tokenizer()

         lowerCAmelCase__     :int											=				InstructBlipProcessor(
             tokenizer=__UpperCAmelCase       ,					image_processor=__UpperCAmelCase       ,					qformer_tokenizer=__UpperCAmelCase )

         lowerCAmelCase__     :List[str]											=				[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

         lowerCAmelCase__     :Tuple											=				processor.batch_decode(__UpperCAmelCase )
         lowerCAmelCase__     :Optional[int]											=				tokenizer.batch_decode(__UpperCAmelCase )

         self.assertListEqual(__UpperCAmelCase       ,					__UpperCAmelCase )






       def 	snake_case		(       self ):



         '''simple docstring'''

         lowerCAmelCase__     :Tuple											=				self.get_image_processor()
         lowerCAmelCase__     :Optional[int]											=				self.get_tokenizer()
         lowerCAmelCase__     :Dict											=				self.get_qformer_tokenizer()

         lowerCAmelCase__     :Optional[Any]											=				InstructBlipProcessor(
             tokenizer=__UpperCAmelCase       ,					image_processor=__UpperCAmelCase       ,					qformer_tokenizer=__UpperCAmelCase )

         lowerCAmelCase__     :Optional[int]											=				'lower newer'
         lowerCAmelCase__     :Optional[int]											=				self.prepare_image_inputs()

         lowerCAmelCase__     :int											=				processor(text=__UpperCAmelCase       ,					images=__UpperCAmelCase )

         self.assertListEqual(
             list(inputs.keys() )       ,					['input_ids', 'attention_mask', 'qformer_input_ids', 'qformer_attention_mask', 'pixel_values']       ,					)
