
'''simple docstring'''



import inspect
import warnings

import torch

from .state import AcceleratorState, GradientState
from .utils import DistributedType, honor_type, is_tpu_available


if is_tpu_available(check_device=False):
				import torch_xla.core.xla_model as xm





def 	_A						(							lowercase__						,						lowercase__		):
						if isinstance(lowercase__						,						(list, tuple)		):
												return honor_type(lowercase__						,						(move_to_device(lowercase__						,						lowercase__		) for t in state)		)
						elif isinstance(lowercase__						,						lowercase__		):
												return type(lowercase__		)({k: move_to_device(lowercase__						,						lowercase__		) for k, v in state.items()}		)
						elif isinstance(lowercase__						,						torch.Tensor		):
												return state.to(lowercase__		)
						return state







class      A							(    torch.optim.Optimizer			):



			def __init__(   self    ,       lowerCamelCase__    ,       lowerCamelCase__=True    ,       lowerCamelCase__=None    )     ->						List[Any]:




									'''simple docstring'''



									lowercase__								=    optimizer
									lowercase__								=    scaler
									lowercase__								=    AcceleratorState()
									lowercase__								=    GradientState()
									lowercase__								=    device_placement
									lowercase__								=    False
									lowercase__								=    None

									# Handle device placement
									if device_placement:
															lowercase__								=    self.optimizer.state_dict()
															if self.accelerator_state.distributed_type == DistributedType.TPU:
																					xm.send_cpu_data_to_device(lowerCamelCase__    ,       self.accelerator_state.device    )
															else:
																					lowercase__								=    move_to_device(lowerCamelCase__    ,       self.accelerator_state.device    )
															self.optimizer.load_state_dict(lowerCamelCase__    )



			@property
			def A__	(   self    )     ->						Any:




									'''simple docstring'''



									return self.optimizer.state



			@state.setter
			def A__	(   self    ,       lowerCamelCase__    )     ->						List[str]:




									'''simple docstring'''



									lowercase__								=    state



			@property
			def A__	(   self    )     ->						Optional[Any]:




									'''simple docstring'''



									return self.optimizer.param_groups



			@param_groups.setter
			def A__	(   self    ,       lowerCamelCase__    )     ->						str:




									'''simple docstring'''



									lowercase__								=    param_groups



			@property
			def A__	(   self    )     ->						Optional[Any]:




									'''simple docstring'''



									return self.optimizer.defaults



			@defaults.setter
			def A__	(   self    ,       lowerCamelCase__    )     ->						Dict:




									'''simple docstring'''



									lowercase__								=    defaults



			def A__	(   self    ,       lowerCamelCase__    )     ->						List[str]:




									'''simple docstring'''



									self.optimizer.add_param_group(lowerCamelCase__    )



			def A__	(   self    ,       lowerCamelCase__    )     ->						Tuple:




									'''simple docstring'''



									if self.accelerator_state.distributed_type == DistributedType.TPU and self.device_placement:
															xm.send_cpu_data_to_device(lowerCamelCase__    ,       self.accelerator_state.device    )
									self.optimizer.load_state_dict(lowerCamelCase__    )



			def A__	(   self    )     ->						Union[str, Any]:




									'''simple docstring'''



									return self.optimizer.state_dict()



			def A__	(   self    ,       lowerCamelCase__=None    )     ->						Optional[Any]:




									'''simple docstring'''



									if self.gradient_state.sync_gradients:
															lowercase__								=    """set_to_none""" in inspect.signature(self.optimizer.zero_grad    ).parameters
															if accept_arg:
																					if set_to_none is None:
																											lowercase__								=    False
																					self.optimizer.zero_grad(set_to_none=lowerCamelCase__    )
															else:
																					if set_to_none is not None:
																											raise ValueError("""`set_to_none` for Optimizer.zero_grad` is not supported by this optimizer."""    )
																					self.optimizer.zero_grad()



			def A__	(   self    ,       lowerCamelCase__=None    )     ->						Dict:




									'''simple docstring'''



									if self.gradient_state.sync_gradients:
															if self.accelerator_state.distributed_type == DistributedType.TPU:
																					lowercase__								=    {"""closure""": closure} if closure is not None else {}
																					xm.optimizer_step(self.optimizer    ,       optimizer_args=lowerCamelCase__    )
															elif self.scaler is not None:
																					lowercase__								=    False
																					if self._last_scale is None:
																											# `get_scale` is an async operation requiring full synchronization
																											# on CPU and GPUs before finishing. As a result, we store away
																											# the prior one to reduce the call overhead
																											lowercase__								=    self.scaler.get_scale()
																											lowercase__								=    True
																					self.scaler.step(self.optimizer    ,       lowerCamelCase__    )
																					self.scaler.update()
																					lowercase__								=    self.scaler.get_scale()
																					if not new_scale:
																											# If we reduced the loss scale, it means the optimizer step was skipped because of gradient overflow.
																											lowercase__								=    scale_after < self._last_scale
																					lowercase__								=    scale_after

															else:
																					self.optimizer.step(lowerCamelCase__    )



			def A__	(   self    ,       lowerCamelCase__    )     ->						List[str]:




									'''simple docstring'''



									for param_group in self.optimizer.param_groups:
															lowercase__								=    [parameters_map.get(lowerCamelCase__    ,       lowerCamelCase__    ) for p in param_group["""params"""]]



			@property
			def A__	(   self    )     ->						Tuple:




									'''simple docstring'''



									warnings.warn(
									    """The `is_overflow` property is deprecated and will be removed in version 1.0 of Accelerate use """
									    """`optimizer.step_was_skipped` instead."""    ,       lowerCamelCase__    ,       )
									return self._is_overflow



			@property
			def A__	(   self    )     ->						Union[str, Any]:




									'''simple docstring'''



									return self._is_overflow



			def __getstate__(   self    )     ->						int:




									'''simple docstring'''



									return self.__dict__.copy()



			def __setstate__(   self    ,       lowerCamelCase__    )     ->						List[Any]:




									'''simple docstring'''



									self.__dict__.update(lowerCamelCase__    )








'''simple docstring'''



import json
import os
import shutil
import tempfile
import unittest

from transformers.models.wavaveca import WavaVecaCTCTokenizer, WavaVecaFeatureExtractor, WavaVecaProcessor
from transformers.models.wavaveca.tokenization_wavaveca import VOCAB_FILES_NAMES
from transformers.utils import FEATURE_EXTRACTOR_NAME

from .test_feature_extraction_wavaveca import floats_list





class      A							(    unittest.TestCase			):



			def A__	(   self    )     ->						Union[str, Any]:




									'''simple docstring'''



									lowercase__								=    """<pad> <s> </s> <unk> | E T A O N I H S R D L U M W C F G Y P B V K ' X J Q Z""".split(""" """    )
									lowercase__								=    dict(zip(lowerCamelCase__    ,       range(len(lowerCamelCase__    )    )    )    )

									lowercase__								=    {
									    """pad_token""": """<pad>""",
									    """unk_token""": """<unk>""",
									    """bos_token""": """<s>""",
									    """eos_token""": """</s>""",
									}
									lowercase__								=    {
									    """feature_size""": 1,
									    """padding_value""": 0.0,
									    """sampling_rate""": 16_000,
									    """return_attention_mask""": False,
									    """do_normalize""": True,
									}

									lowercase__								=    tempfile.mkdtemp()
									lowercase__								=    os.path.join(self.tmpdirname    ,       VOCAB_FILES_NAMES["""vocab_file"""]    )
									lowercase__								=    os.path.join(self.tmpdirname    ,       lowerCamelCase__    )
									with open(self.vocab_file    ,       """w"""    ,       encoding="""utf-8"""    ) as fp:
															fp.write(json.dumps(lowerCamelCase__    ) + """\n"""    )

									with open(self.feature_extraction_file    ,       """w"""    ,       encoding="""utf-8"""    ) as fp:
															fp.write(json.dumps(lowerCamelCase__    ) + """\n"""    )



			def A__	(   self    ,       **lowerCamelCase__    )     ->						int:




									'''simple docstring'''



									lowercase__								=    self.add_kwargs_tokens_map.copy()
									kwargs.update(lowerCamelCase__    )
									return WavaVecaCTCTokenizer.from_pretrained(self.tmpdirname    ,       **lowerCamelCase__    )



			def A__	(   self    ,       **lowerCamelCase__    )     ->						Dict:




									'''simple docstring'''



									return WavaVecaFeatureExtractor.from_pretrained(self.tmpdirname    ,       **lowerCamelCase__    )



			def A__	(   self    )     ->						Dict:




									'''simple docstring'''



									shutil.rmtree(self.tmpdirname    )



			def A__	(   self    )     ->						str:




									'''simple docstring'''



									lowercase__								=    self.get_tokenizer()
									lowercase__								=    self.get_feature_extractor()

									lowercase__								=    WavaVecaProcessor(tokenizer=lowerCamelCase__    ,       feature_extractor=lowerCamelCase__    )

									processor.save_pretrained(self.tmpdirname    )
									lowercase__								=    WavaVecaProcessor.from_pretrained(self.tmpdirname    )

									self.assertEqual(processor.tokenizer.get_vocab()    ,       tokenizer.get_vocab()    )
									self.assertIsInstance(processor.tokenizer    ,       lowerCamelCase__    )

									self.assertEqual(processor.feature_extractor.to_json_string()    ,       feature_extractor.to_json_string()    )
									self.assertIsInstance(processor.feature_extractor    ,       lowerCamelCase__    )



			def A__	(   self    )     ->						Tuple:




									'''simple docstring'''



									lowercase__								=    WavaVecaProcessor(tokenizer=self.get_tokenizer()    ,       feature_extractor=self.get_feature_extractor()    )
									processor.save_pretrained(self.tmpdirname    )

									lowercase__								=    self.get_tokenizer(bos_token="""(BOS)"""    ,       eos_token="""(EOS)"""    )
									lowercase__								=    self.get_feature_extractor(do_normalize=lowerCamelCase__    ,       padding_value=1.0    )

									lowercase__								=    WavaVecaProcessor.from_pretrained(
									    self.tmpdirname    ,       bos_token="""(BOS)"""    ,       eos_token="""(EOS)"""    ,       do_normalize=lowerCamelCase__    ,       padding_value=1.0    )

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

									self.assertEqual(processor.feature_extractor.to_json_string()    ,       feature_extractor_add_kwargs.to_json_string()    )
									self.assertIsInstance(processor.feature_extractor    ,       lowerCamelCase__    )



			def A__	(   self    )     ->						List[str]:




									'''simple docstring'''



									lowercase__								=    self.get_feature_extractor()
									lowercase__								=    self.get_tokenizer()

									lowercase__								=    WavaVecaProcessor(tokenizer=lowerCamelCase__    ,       feature_extractor=lowerCamelCase__    )

									lowercase__								=    floats_list((3, 1_000)    )

									lowercase__								=    feature_extractor(lowerCamelCase__    ,       return_tensors="""np"""    )
									lowercase__								=    processor(lowerCamelCase__    ,       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 A__	(   self    )     ->						Optional[Any]:




									'''simple docstring'''



									lowercase__								=    self.get_feature_extractor()
									lowercase__								=    self.get_tokenizer()

									lowercase__								=    WavaVecaProcessor(tokenizer=lowerCamelCase__    ,       feature_extractor=lowerCamelCase__    )

									lowercase__								=    """This is a test string"""

									lowercase__								=    processor(text=lowerCamelCase__    )

									lowercase__								=    tokenizer(lowerCamelCase__    )

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



			def A__	(   self    )     ->						List[Any]:




									'''simple docstring'''



									lowercase__								=    self.get_feature_extractor()
									lowercase__								=    self.get_tokenizer()

									lowercase__								=    WavaVecaProcessor(tokenizer=lowerCamelCase__    ,       feature_extractor=lowerCamelCase__    )

									lowercase__								=    [[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

									lowercase__								=    processor.batch_decode(lowerCamelCase__    )
									lowercase__								=    tokenizer.batch_decode(lowerCamelCase__    )

									self.assertListEqual(lowerCamelCase__    ,       lowerCamelCase__    )



			def A__	(   self    )     ->						List[str]:




									'''simple docstring'''



									lowercase__								=    self.get_feature_extractor()
									lowercase__								=    self.get_tokenizer()

									lowercase__								=    WavaVecaProcessor(tokenizer=lowerCamelCase__    ,       feature_extractor=lowerCamelCase__    )

									self.assertListEqual(
									    processor.model_input_names    ,       feature_extractor.model_input_names    ,       msg="""`processor` and `feature_extractor` model input names do not match"""    ,       )






