

'''simple docstring'''




import functools
import operator

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


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

_a					:			Optional[Any]							=    {
    'microsoft/unispeech-sat-base-100h-libri-ft': (
        'https://huggingface.co/microsoft/unispeech-sat-base-100h-libri-ft/resolve/main/config.json'
    ),
    # See all UniSpeechSat models at https://huggingface.co/models?filter=unispeech_sat
}



class 							lowercase_						(		a			):

  '''simple docstring'''





  __lowerCAmelCase       :			Union[str, Any]     	= "unispeech-sat"
  def __init__(  self ,			a_=3_2 ,			a_=7_6_8 ,			a_=1_2 ,			a_=1_2 ,			a_=3_0_7_2 ,			a_="gelu" ,			a_=0.1 ,			a_=0.1 ,			a_=0.1 ,			a_=0.0 ,			a_=0.0 ,			a_=0.1 ,			a_=0.1 ,			a_=0.02 ,			a_=1E-5 ,			a_="group" ,			a_="gelu" ,			a_=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2, 5_1_2) ,			a_=(5, 2, 2, 2, 2, 2, 2) ,			a_=(1_0, 3, 3, 3, 3, 2, 2) ,			a_=False ,			a_=1_2_8 ,			a_=1_6 ,			a_=False ,			a_=True ,			a_=0.05 ,			a_=1_0 ,			a_=2 ,			a_=0.0 ,			a_=1_0 ,			a_=0 ,			a_=3_2_0 ,			a_=2 ,			a_=0.1 ,			a_=1_0_0 ,			a_=2_5_6 ,			a_=2_5_6 ,			a_=0.1 ,			a_="mean" ,			a_=False ,			a_=False ,			a_=2_5_6 ,			a_=(5_1_2, 5_1_2, 5_1_2, 5_1_2, 1_5_0_0) ,			a_=(5, 3, 3, 1, 1) ,			a_=(1, 2, 3, 1, 1) ,			a_=5_1_2 ,			a_=0 ,			a_=1 ,			a_=2 ,			a_=5_0_4 ,			**a_ ,			)				->					Optional[Any]:


    """simple docstring"""
    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_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    				=						vocab_size
    UpperCAmelCase    				=						num_clusters
    UpperCAmelCase    				=						do_stable_layer_norm
    UpperCAmelCase    				=						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
    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
    UpperCAmelCase    				=						mask_feature_min_masks

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

    # ctc loss
    UpperCAmelCase    				=						ctc_loss_reduction
    UpperCAmelCase    				=						ctc_zero_infinity

    # 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     snake_case_      (  self  )				->					Union[str, Any]:


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





'''simple docstring'''




import subprocess
import sys

from transformers import BertConfig, BertModel, BertTokenizer, pipeline
from transformers.testing_utils import TestCasePlus, require_torch



class 							lowercase_						(		a			):

  '''simple docstring'''





  @require_torch
  def     snake_case_      (  self  )				->					Optional[Any]:


    """simple docstring"""
    # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
    # `transformers` is loaded, and it's too late for inside pytest - so we are changing it
    # while running an external program

    # python one-liner segments

    # this must be loaded before socket.socket is monkey-patched
    UpperCAmelCase    				=						'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

    UpperCAmelCase    				=						'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

    UpperCAmelCase    				=						'\nimport socket\ndef offline_socket(*args, **kwargs): raise RuntimeError("Offline mode is enabled, we shouldn\'t access internet")\nsocket.socket = offline_socket\n        '

    # Force fetching the files so that we can use the cache
    UpperCAmelCase    				=						'hf-internal-testing/tiny-random-bert'
    BertConfig.from_pretrained(a_  )
    BertModel.from_pretrained(a_  )
    BertTokenizer.from_pretrained(a_  )
    pipeline(task='fill-mask' ,			model=a_  )

    # baseline - just load from_pretrained with normal network
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, run, mock]  )]

    # should succeed
    UpperCAmelCase    				=						self.get_env()
    # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
    UpperCAmelCase    				=						'1'
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )
  @require_torch
  def     snake_case_      (  self  )				->					Union[str, Any]:


    """simple docstring"""
    # python one-liner segments
    # this must be loaded before socket.socket is monkey-patched
    UpperCAmelCase    				=						'\nfrom transformers import BertConfig, BertModel, BertTokenizer, pipeline\n        '

    UpperCAmelCase    				=						'\nmname = "hf-internal-testing/tiny-random-bert"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nBertTokenizer.from_pretrained(mname)\npipe = pipeline(task="fill-mask", model=mname)\nprint("success")\n        '

    UpperCAmelCase    				=						'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Faking flaky internet")\nsocket.socket = offline_socket\n        '

    # Force fetching the files so that we can use the cache
    UpperCAmelCase    				=						'hf-internal-testing/tiny-random-bert'
    BertConfig.from_pretrained(a_  )
    BertModel.from_pretrained(a_  )
    BertTokenizer.from_pretrained(a_  )
    pipeline(task='fill-mask' ,			model=a_  )

    # baseline - just load from_pretrained with normal network
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, run, mock]  )]

    # should succeed
    UpperCAmelCase    				=						self.get_env()
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )
  @require_torch
  def     snake_case_      (  self  )				->					List[str]:


    """simple docstring"""
    # this test is a bit tricky since TRANSFORMERS_OFFLINE can only be changed before
    # `transformers` is loaded, and it's too late for inside pytest - so we are changing it
    # while running an external program

    # python one-liner segments

    # this must be loaded before socket.socket is monkey-patched
    UpperCAmelCase    				=						'\nfrom transformers import BertConfig, BertModel, BertTokenizer\n        '

    UpperCAmelCase    				=						'\nmname = "hf-internal-testing/tiny-random-bert-sharded"\nBertConfig.from_pretrained(mname)\nBertModel.from_pretrained(mname)\nprint("success")\n        '

    UpperCAmelCase    				=						'\nimport socket\ndef offline_socket(*args, **kwargs): raise ValueError("Offline mode is enabled")\nsocket.socket = offline_socket\n        '

    # baseline - just load from_pretrained with normal network
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, run]  )]

    # should succeed
    UpperCAmelCase    				=						self.get_env()
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )

    # next emulate no network
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, mock, run]  )]

    # Doesn't fail anymore since the model is in the cache due to other tests, so commenting this.
    # env["TRANSFORMERS_OFFLINE"] = "0"
    # result = subprocess.run(cmd, env=env, check=False, capture_output=True)
    # self.assertEqual(result.returncode, 1, result.stderr)

    # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
    UpperCAmelCase    				=						'1'
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )
  @require_torch
  def     snake_case_      (  self  )				->					Optional[Any]:


    """simple docstring"""
    UpperCAmelCase    				=						'\nfrom transformers import pipeline\n        '
    UpperCAmelCase    				=						'\nmname = "hf-internal-testing/tiny-random-bert"\npipe = pipeline(model=mname)\n        '

    UpperCAmelCase    				=						'\nimport socket\ndef offline_socket(*args, **kwargs): raise socket.error("Offline mode is enabled")\nsocket.socket = offline_socket\n        '
    UpperCAmelCase    				=						self.get_env()
    UpperCAmelCase    				=						'1'
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, mock, run]  )]
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			1 ,			result.stderr  )
    self.assertIn(
        'You cannot infer task automatically within `pipeline` when using offline mode' ,			result.stderr.decode().replace('\n' ,			''  ) ,			)





  @require_torch
  def     snake_case_      (  self  )				->					str:


    """simple docstring"""
    UpperCAmelCase    				=						'\nfrom transformers import AutoModel\n        '
    UpperCAmelCase    				=						'\nmname = "hf-internal-testing/test_dynamic_model"\nAutoModel.from_pretrained(mname, trust_remote_code=True)\nprint("success")\n        '

    # baseline - just load from_pretrained with normal network
    UpperCAmelCase    				=						[sys.executable, '-c', '\n'.join([load, run]  )]

    # should succeed
    UpperCAmelCase    				=						self.get_env()
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )

    # should succeed as TRANSFORMERS_OFFLINE=1 tells it to use local files
    UpperCAmelCase    				=						'1'
    UpperCAmelCase    				=						subprocess.run(a_ ,			env=a_ ,			check=a_ ,			capture_output=a_  )
    self.assertEqual(result.returncode ,			0 ,			result.stderr  )
    self.assertIn('success' ,			result.stdout.decode()  )


