



import functools
import operator

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


__lowerCamelCase								=      logging.get_logger(__name__)

__lowerCamelCase								=      {
    '''facebook/hubert-base-ls960''': '''https://huggingface.co/facebook/hubert-base-ls960/resolve/main/config.json''',
    # See all Hubert models at https://huggingface.co/models?filter=hubert
}


class 	snake_case_			(SCREAMING_SNAKE_CASE__	):




       """simple docstring"""






       _lowerCamelCase           =					"""hubert"""



       def __init__(  self  ,lowercase=32  ,lowercase=768  ,lowercase=12  ,lowercase=12  ,lowercase=3072  ,lowercase="gelu"  ,lowercase=0.1  ,lowercase=0.1  ,lowercase=0.1  ,lowercase=True  ,lowercase=0.0  ,lowercase=0.1  ,lowercase=0.1  ,lowercase=0.02  ,lowercase=1E-5  ,lowercase="group"  ,lowercase="gelu"  ,lowercase=(512, 512, 512, 512, 512, 512, 512)  ,lowercase=(5, 2, 2, 2, 2, 2, 2)  ,lowercase=(10, 3, 3, 3, 3, 2, 2)  ,lowercase=False  ,lowercase=128  ,lowercase=16  ,lowercase=False  ,lowercase=True  ,lowercase=0.05  ,lowercase=10  ,lowercase=2  ,lowercase=0.0  ,lowercase=10  ,lowercase=0  ,lowercase="sum"  ,lowercase=False  ,lowercase=False  ,lowercase=256  ,lowercase=0  ,lowercase=1  ,lowercase=2  ,**lowercase  ,):

             """simple docstring"""





             super().__init__(**_UpperCAmelCase  ,pad_token_id=_UpperCAmelCase  ,bos_token_id=_UpperCAmelCase  ,eos_token_id=_UpperCAmelCase)
             UpperCAmelCase_	:						Optional[int]					   =		hidden_size
             UpperCAmelCase_	:						int					   =		feat_extract_norm
             UpperCAmelCase_	:						int					   =		feat_extract_activation
             UpperCAmelCase_	:						List[Any]					   =		list(_UpperCAmelCase)
             UpperCAmelCase_	:						Dict					   =		list(_UpperCAmelCase)
             UpperCAmelCase_	:						Optional[Any]					   =		list(_UpperCAmelCase)
             UpperCAmelCase_	:						Dict					   =		conv_bias
             UpperCAmelCase_	:						Optional[int]					   =		num_conv_pos_embeddings
             UpperCAmelCase_	:						List[str]					   =		num_conv_pos_embedding_groups
             UpperCAmelCase_	:						int					   =		len(self.conv_dim)
             UpperCAmelCase_	:						Optional[int]					   =		num_hidden_layers
             UpperCAmelCase_	:						List[str]					   =		intermediate_size
             UpperCAmelCase_	:						Union[str, Any]					   =		hidden_act
             UpperCAmelCase_	:						Optional[int]					   =		num_attention_heads
             UpperCAmelCase_	:						Any					   =		hidden_dropout
             UpperCAmelCase_	:						Any					   =		attention_dropout
             UpperCAmelCase_	:						Dict					   =		activation_dropout
             UpperCAmelCase_	:						Dict					   =		feat_proj_layer_norm
             UpperCAmelCase_	:						Optional[Any]					   =		feat_proj_dropout
             UpperCAmelCase_	:						int					   =		final_dropout
             UpperCAmelCase_	:						List[str]					   =		layerdrop
             UpperCAmelCase_	:						Optional[int]					   =		layer_norm_eps
             UpperCAmelCase_	:						Tuple					   =		initializer_range
             UpperCAmelCase_	:						Optional[Any]					   =		vocab_size
             UpperCAmelCase_	:						Dict					   =		do_stable_layer_norm
             UpperCAmelCase_	:						str					   =		use_weighted_layer_sum
             UpperCAmelCase_	:						Optional[int]					   =		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_	:						str					   =		apply_spec_augment
             UpperCAmelCase_	:						List[str]					   =		mask_time_prob
             UpperCAmelCase_	:						Any					   =		mask_time_length
             UpperCAmelCase_	:						Optional[int]					   =		mask_time_min_masks
             UpperCAmelCase_	:						str					   =		mask_feature_prob
             UpperCAmelCase_	:						List[str]					   =		mask_feature_length
             UpperCAmelCase_	:						Union[str, Any]					   =		mask_feature_min_masks

             # ctc loss
             UpperCAmelCase_	:						Any					   =		ctc_loss_reduction
             UpperCAmelCase_	:						Optional[int]					   =		ctc_zero_infinity



       @property
       def 			A_ (  self):

             """simple docstring"""





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






'''simple docstring'''






import os
import pickle
import unittest
from collections import UserDict, namedtuple

import torch

from accelerate.test_utils.testing import require_cuda, require_torch_min_version
from accelerate.test_utils.training import RegressionModel
from accelerate.utils import (
    convert_outputs_to_fpaa,
    extract_model_from_parallel,
    find_device,
    listify,
    patch_environment,
    recursively_apply,
    send_to_device,
)


_lowerCAmelCase       =   namedtuple('''ExampleNamedTuple''', '''a b c''')



class    A						(					unittest.TestCase						):




				'''simple docstring'''





				def 			a_			(self     )      ->			Tuple:
							__UpperCamelCase :     Optional[int]										=  torch.randn(5					,    2     )
							__UpperCamelCase :     List[str]										=  torch.device("cuda"     ) if torch.cuda.is_available() else torch.device("cpu"     )

							__UpperCamelCase :     Optional[Any]										=  send_to_device(_UpperCAmelCase					,    _UpperCAmelCase     )
							self.assertTrue(torch.equal(resulta.cpu()					,    _UpperCAmelCase     )     )

							__UpperCamelCase :     Any										=  send_to_device((tensor, [tensor, tensor], 1)					,    _UpperCAmelCase     )
							self.assertIsInstance(_UpperCAmelCase					,    _UpperCAmelCase     )
							self.assertTrue(torch.equal(resulta[0].cpu()					,    _UpperCAmelCase     )     )
							self.assertIsInstance(resulta[1]					,    _UpperCAmelCase     )
							self.assertTrue(torch.equal(resulta[1][0].cpu()					,    _UpperCAmelCase     )     )
							self.assertTrue(torch.equal(resulta[1][1].cpu()					,    _UpperCAmelCase     )     )
							self.assertEqual(resulta[2]					,    1     )

							__UpperCamelCase :     Optional[int]										=  send_to_device({"a": tensor, "b": [tensor, tensor], "c": 1}					,    _UpperCAmelCase     )
							self.assertIsInstance(_UpperCAmelCase					,    _UpperCAmelCase     )
							self.assertTrue(torch.equal(resulta["a"].cpu()					,    _UpperCAmelCase     )     )
							self.assertIsInstance(resulta["b"]					,    _UpperCAmelCase     )
							self.assertTrue(torch.equal(resulta["b"][0].cpu()					,    _UpperCAmelCase     )     )
							self.assertTrue(torch.equal(resulta["b"][1].cpu()					,    _UpperCAmelCase     )     )
							self.assertEqual(resulta["c"]					,    1     )

							__UpperCamelCase :     Optional[Any]										=  send_to_device(ExampleNamedTuple(a=_UpperCAmelCase					,    b=[tensor, tensor]					,    c=1     )					,    _UpperCAmelCase     )
							self.assertIsInstance(_UpperCAmelCase					,    _UpperCAmelCase     )
							self.assertTrue(torch.equal(resulta.a.cpu()					,    _UpperCAmelCase     )     )
							self.assertIsInstance(resulta.b					,    _UpperCAmelCase     )
							self.assertTrue(torch.equal(resulta.b[0].cpu()					,    _UpperCAmelCase     )     )
							self.assertTrue(torch.equal(resulta.b[1].cpu()					,    _UpperCAmelCase     )     )
							self.assertEqual(resulta.c					,    1     )

							__UpperCamelCase :     Dict										=  send_to_device(UserDict({"a": tensor, "b": [tensor, tensor], "c": 1}     )					,    _UpperCAmelCase     )
							self.assertIsInstance(_UpperCAmelCase					,    _UpperCAmelCase     )
							self.assertTrue(torch.equal(resulta["a"].cpu()					,    _UpperCAmelCase     )     )
							self.assertIsInstance(resulta["b"]					,    _UpperCAmelCase     )
							self.assertTrue(torch.equal(resulta["b"][0].cpu()					,    _UpperCAmelCase     )     )
							self.assertTrue(torch.equal(resulta["b"][1].cpu()					,    _UpperCAmelCase     )     )
							self.assertEqual(resulta["c"]					,    1     )
				def 			a_			(self     )      ->			Optional[Any]:
							with self.assertRaises(_UpperCAmelCase     ) as cm:
										__UpperCamelCase :     int										=  recursively_apply(torch.tensor					,    (torch.tensor(1     ), 1)					,    error_on_other_type=_UpperCAmelCase     )
							self.assertEqual(
							    str(cm.exception     )					,    "Unsupported types (<class 'int'>) passed to `tensor`. Only nested list/tuple/dicts of objects that are valid for `is_torch_tensor` should be passed."					,    )
				def 			a_			(self     )      ->			Union[str, Any]:
							__UpperCamelCase :     Dict										=  torch.tensor([1, 2, 3, 4, 5]     )
							self.assertEqual(listify(_UpperCAmelCase     )					,    [1, 2, 3, 4, 5]     )

							__UpperCamelCase :     int										=  torch.tensor([[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]     )
							self.assertEqual(listify(_UpperCAmelCase     )					,    [[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]]     )

							__UpperCamelCase :     Dict										=  torch.tensor([[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]]     )
							self.assertEqual(
							    listify(_UpperCAmelCase     )					,    [[[1, 2, 3, 4, 5], [6, 7, 8, 9, 1_0]], [[1_1, 1_2, 1_3, 1_4, 1_5], [1_6, 1_7, 1_8, 1_9, 2_0]]]     )
				def 			a_			(self     )      ->			Optional[Any]:
							with patch_environment(aa=1					,    BB=2     ):
										self.assertEqual(os.environ.get("AA"     )					,    "1"     )
										self.assertEqual(os.environ.get("BB"     )					,    "2"     )

							self.assertNotIn("AA"					,    os.environ     )
							self.assertNotIn("BB"					,    os.environ     )
				def 			a_			(self     )      ->			Dict:
							__UpperCamelCase :     Tuple										=  RegressionModel()
							__UpperCamelCase :     List[str]										=  model.forward
							__UpperCamelCase :     Any										=  convert_outputs_to_fpaa(model.forward     )
							__UpperCamelCase :     List[str]										=  extract_model_from_parallel(_UpperCAmelCase					,    keep_fpaa_wrapper=_UpperCAmelCase     )
							__UpperCamelCase :     Dict										=  pickle.dumps(_UpperCAmelCase     )
				@require_cuda
				def 			a_			(self     )      ->			str:
							__UpperCamelCase :     List[str]										=  RegressionModel()
							__UpperCamelCase :     List[Any]										=  model.forward
							__UpperCamelCase :     List[str]										=  torch.cuda.amp.autocast(dtype=torch.floataa     )(model.forward     )
							__UpperCamelCase :     str										=  convert_outputs_to_fpaa(model.forward     )
							__UpperCamelCase :     int										=  extract_model_from_parallel(_UpperCAmelCase					,    keep_fpaa_wrapper=_UpperCAmelCase     )
							__UpperCamelCase :     int										=  pickle.dumps(_UpperCAmelCase     )
				@require_cuda
				@require_torch_min_version(version="2.0"     )
				def 			a_			(self     )      ->			Tuple:
							__UpperCamelCase :     List[Any]										=  RegressionModel()
							__UpperCamelCase :     Tuple										=  model.forward
							__UpperCamelCase :     Optional[int]										=  torch.cuda.amp.autocast(dtype=torch.floataa     )(model.forward     )
							__UpperCamelCase :     List[Any]										=  convert_outputs_to_fpaa(model.forward     )
							__UpperCamelCase :     Union[str, Any]										=  torch.compile(model.forward					,    backend="inductor"     )
							__UpperCamelCase :     int										=  torch.randn(4					,    1_0     ).cuda()
							__UpperCamelCase :     int										=  model(_UpperCAmelCase     )
				def 			a_			(self     )      ->			Union[str, Any]:
							__UpperCamelCase :     List[Any]										=  RegressionModel()
							# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
							__UpperCamelCase :     Optional[Any]										=  torch.nn.parallel.DataParallel(_UpperCAmelCase     )
							__UpperCamelCase :     Union[str, Any]										=  extract_model_from_parallel(_UpperCAmelCase     )

							self.assertEqual(_UpperCAmelCase					,    _UpperCAmelCase     )
				@require_torch_min_version(version="2.0"     )
				def 			a_			(self     )      ->			Union[str, Any]:
							__UpperCamelCase :     int										=  RegressionModel()
							__UpperCamelCase :     List[str]										=  torch.compile(_UpperCAmelCase     )

							# could also do a test with DistributedDataParallel, but difficult to run on CPU or single GPU
							__UpperCamelCase :     Union[str, Any]										=  torch.nn.parallel.DataParallel(_UpperCAmelCase     )
							__UpperCamelCase :     List[str]										=  torch.compile(_UpperCAmelCase     )
							__UpperCamelCase :     Optional[int]										=  extract_model_from_parallel(_UpperCAmelCase     )

							self.assertEqual(compiled_model._orig_mod					,    compiled_model_unwrapped._orig_mod     )





				def 			a_			(self     )      ->			str:
							self.assertEqual(find_device([1, "a", torch.tensor([1, 2, 3]     )]     )					,    torch.device("cpu"     )     )
							self.assertEqual(find_device({"a": 1, "b": torch.tensor([1, 2, 3]     )}     )					,    torch.device("cpu"     )     )
							self.assertIsNone(find_device([1, "a"]     )     )



