





'''simple docstring'''

from dataclasses import dataclass, field
from typing import Tuple

from ..utils import cached_property, is_torch_available, is_torch_tpu_available, logging, requires_backends
from .benchmark_args_utils import BenchmarkArguments


if is_torch_available():
			import torch

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


__lowerCamelCase : List[str]											=			logging.get_logger(__name__)

@dataclass
class       UpperCAmelCase (						_lowercase  ):
		UpperCAmelCase					:	Any 			=		[
		    '''no_inference''',
		    '''no_cuda''',
		    '''no_tpu''',
		    '''no_speed''',
		    '''no_memory''',
		    '''no_env_print''',
		    '''no_multi_process''',
		]




		def __init__(self      :	int							,			**A__      :	Optional[Any]				)					-> Dict:
						for deprecated_arg in self.deprecated_args:
										if deprecated_arg in kwargs:
														lowercase         = deprecated_arg[3:]
														setattr(self							,			A__							,			not kwargs.pop(A__				)				)
														logger.warning(
														    f'{deprecated_arg} is depreciated. Please use --no_{positive_arg} or'
														    f' {positive_arg}={kwargs[positive_arg]}'				)

						lowercase         = kwargs.pop("torchscript"							,			self.torchscript				)
						lowercase         = kwargs.pop("torch_xla_tpu_print_metrics"							,			self.torch_xla_tpu_print_metrics				)
						lowercase         = kwargs.pop("fp16_opt_level"							,			self.fpaa_opt_level				)
						super().__init__(**A__				)

		UpperCAmelCase					:	bool 			=		field(default=_lowercase			,       metadata={'''help''': '''Trace the models using torchscript'''}  )
		UpperCAmelCase					:	bool 			=		field(default=_lowercase			,       metadata={'''help''': '''Print Xla/PyTorch tpu metrics'''}  )
		UpperCAmelCase					:	str 			=		field(
		    default='''O1'''			,       metadata={
		        '''help''': (
		            '''For fp16: Apex AMP optimization level selected in [\'O0\', \'O1\', \'O2\', and \'O3\']. '''
		            '''See details at https://nvidia.github.io/apex/amp.html'''
		        )
		    }			,       )




		@cached_property
		def 				UpperCAmelCase__   (self      :	str				)					-> Tuple["torch.device", int]:
						requires_backends(self							,			["torch"]				)
						logger.info("PyTorch: setting up devices"				)
						if not self.cuda:
										lowercase         = torch.device("cpu"				)
										lowercase         = 0
						elif is_torch_tpu_available():
										lowercase         = xm.xla_device()
										lowercase         = 0
						else:
										lowercase         = torch.device("cuda" if torch.cuda.is_available() else "cpu"				)
										lowercase         = torch.cuda.device_count()
						return device, n_gpu




		@property
		def 				UpperCAmelCase__   (self      :	int				)					-> List[str]:
						return is_torch_tpu_available() and self.tpu




		@property
		def 				UpperCAmelCase__   (self      :	List[str]				)					-> int:
						requires_backends(self							,			["torch"]				)
						# TODO(PVP): currently only single GPU is supported
						return torch.cuda.current_device()




		@property
		def 				UpperCAmelCase__   (self      :	Dict				)					-> "torch.device":
						requires_backends(self							,			["torch"]				)
						return self._setup_devices[0]




		@property
		def 				UpperCAmelCase__   (self      :	Dict				)					-> int:
						requires_backends(self							,			["torch"]				)
						return self._setup_devices[1]





		@property
		def 				UpperCAmelCase__   (self      :	Any				)					-> Optional[int]:
						return self.n_gpu > 0











'''simple docstring'''

import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin

class       UpperCAmelCase (						_lowercase  ):
		UpperCAmelCase					:	Optional[Any] 			=		'''MCTCTFeatureExtractor'''
		UpperCAmelCase					:	Tuple 			=		'''AutoTokenizer'''




		def __init__(self      :	int							,			A__      :	Tuple							,			A__      :	Union[str, Any]				)					-> Dict:
						super().__init__(A__							,			A__				)
						lowercase         = self.feature_extractor
						lowercase         = False




		def __call__(self      :	Tuple							,			*A__      :	str							,			**A__      :	Dict				)					-> int:
						# For backward compatibility
						if self._in_target_context_manager:
										return self.current_processor(*A__							,			**A__				)

						if "raw_speech" in kwargs:
										warnings.warn("Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."				)
										lowercase         = kwargs.pop("raw_speech"				)
						else:
										lowercase         = kwargs.pop("audio"							,			A__				)
						lowercase         = kwargs.pop("sampling_rate"							,			A__				)
						lowercase         = kwargs.pop("text"							,			A__				)
						if len(A__				) > 0:
										lowercase         = args[0]
										lowercase         = args[1:]

						if audio is None and text is None:
										raise ValueError("You need to specify either an `audio` or `text` input to process."				)

						if audio is not None:
										lowercase         = self.feature_extractor(A__							,			*A__							,			sampling_rate=A__							,			**A__				)
						if text is not None:
										lowercase         = self.tokenizer(A__							,			**A__				)

						if text is None:
										return inputs
						elif audio is None:
										return encodings
						else:
										lowercase         = encodings["input_ids"]
										return inputs




		def 				UpperCAmelCase__   (self      :	Tuple							,			*A__      :	str							,			**A__      :	str				)					-> str:
						return self.tokenizer.batch_decode(*A__							,			**A__				)




		def 				UpperCAmelCase__   (self      :	Any							,			*A__      :	List[Any]							,			**A__      :	List[str]				)					-> Tuple:
						# For backward compatibility
						if self._in_target_context_manager:
										return self.current_processor.pad(*A__							,			**A__				)

						lowercase         = kwargs.pop("input_features"							,			A__				)
						lowercase         = kwargs.pop("labels"							,			A__				)
						if len(A__				) > 0:
										lowercase         = args[0]
										lowercase         = args[1:]

						if input_features is not None:
										lowercase         = self.feature_extractor.pad(A__							,			*A__							,			**A__				)
						if labels is not None:
										lowercase         = self.tokenizer.pad(A__							,			**A__				)

						if labels is None:
										return input_features
						elif input_features is None:
										return labels
						else:
										lowercase         = labels["input_ids"]
										return input_features




		def 				UpperCAmelCase__   (self      :	Tuple							,			*A__      :	Optional[int]							,			**A__      :	Optional[int]				)					-> Tuple:
						return self.tokenizer.decode(*A__							,			**A__				)





		@contextmanager
		def 				UpperCAmelCase__   (self      :	Optional[Any]				)					-> Union[str, Any]:
						warnings.warn(
						    "`as_target_processor` is deprecated and will be removed in v5 of Transformers. You can process your "
						    "labels by using the argument `text` of the regular `__call__` method (either in the same call as "
						    "your audio inputs, or in a separate call."				)
						lowercase         = True
						lowercase         = self.tokenizer
						yield
						lowercase         = self.feature_extractor
						lowercase         = False




