



'''simple docstring'''





import warnings
from contextlib import contextmanager

from ....processing_utils import ProcessorMixin







class        UpperCAmelCase_			(			_a      ):
    """simple docstring"""







    lowercase   =						"MCTCTFeatureExtractor"
    lowercase   =						"AutoTokenizer"




    def __init__(       self   :	Tuple					,	snake_case_   :	Any					,	snake_case_   :	Union[str, Any]		):
          super().__init__(snake_case_					,	snake_case_		)
          snake_case__    :       List[Any]       		= self.feature_extractor
          snake_case__    :       List[Any]       		= False




    def __call__(       self   :	List[Any]					,	*snake_case_   :	Tuple					,	**snake_case_   :	Dict		):
          # For backward compatibility
          if self._in_target_context_manager:
                return self.current_processor(*snake_case_					,	**snake_case_		)

          if "raw_speech" in kwargs:
                warnings.warn("""Using `raw_speech` as a keyword argument is deprecated. Use `audio` instead."""		)
                snake_case__    :       Tuple       		= kwargs.pop("""raw_speech"""		)
          else:
                snake_case__    :       Optional[int]       		= kwargs.pop("""audio"""					,	snake_case_		)
          snake_case__    :       List[str]       		= kwargs.pop("""sampling_rate"""					,	snake_case_		)
          snake_case__    :       Optional[int]       		= kwargs.pop("""text"""					,	snake_case_		)
          if len(snake_case_		) > 0:
                snake_case__    :       Union[str, Any]       		= args[0]
                snake_case__    :       Optional[int]       		= 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:
                snake_case__    :       Optional[Any]       		= self.feature_extractor(snake_case_					,	*snake_case_					,	sampling_rate=snake_case_					,	**snake_case_		)
          if text is not None:
                snake_case__    :       Tuple       		= self.tokenizer(snake_case_					,	**snake_case_		)

          if text is None:
                return inputs
          elif audio is None:
                return encodings
          else:
                snake_case__    :       Union[str, Any]       		= encodings["""input_ids"""]
                return inputs




    def lowerCamelCase			(       self   :	Union[str, Any]					,	*snake_case_   :	str					,	**snake_case_   :	Dict		):
          return self.tokenizer.batch_decode(*snake_case_					,	**snake_case_		)




    def lowerCamelCase			(       self   :	List[Any]					,	*snake_case_   :	Optional[int]					,	**snake_case_   :	Dict		):
          # For backward compatibility
          if self._in_target_context_manager:
                return self.current_processor.pad(*snake_case_					,	**snake_case_		)

          snake_case__    :       Optional[int]       		= kwargs.pop("""input_features"""					,	snake_case_		)
          snake_case__    :       Dict       		= kwargs.pop("""labels"""					,	snake_case_		)
          if len(snake_case_		) > 0:
                snake_case__    :       Optional[int]       		= args[0]
                snake_case__    :       Tuple       		= args[1:]

          if input_features is not None:
                snake_case__    :       Union[str, Any]       		= self.feature_extractor.pad(snake_case_					,	*snake_case_					,	**snake_case_		)
          if labels is not None:
                snake_case__    :       Union[str, Any]       		= self.tokenizer.pad(snake_case_					,	**snake_case_		)

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




    def lowerCamelCase			(       self   :	Any					,	*snake_case_   :	List[str]					,	**snake_case_   :	Dict		):
          return self.tokenizer.decode(*snake_case_					,	**snake_case_		)




    @contextmanager
    def lowerCamelCase			(       self   :	str		):
          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."""		)
          snake_case__    :       str       		= True
          snake_case__    :       Optional[int]       		= self.tokenizer
          yield
          snake_case__    :       List[Any]       		= self.feature_extractor
          snake_case__    :       Union[str, Any]       		= False





'''simple docstring'''





import baseaa
import io
import json
import os
from copy import deepcopy

from ..optimizer import AcceleratedOptimizer
from ..scheduler import AcceleratedScheduler







class        UpperCAmelCase_			:
    """simple docstring"""







    def __init__(       self   :	Tuple					,	snake_case_   :	List[str]		):
          if isinstance(snake_case_					,	snake_case_		):
                # Don't modify user's data should they want to reuse it (e.g. in tests), because once we
                # modified it, it will not be accepted here again, since `auto` values would have been overridden
                snake_case__    :       Optional[Any]       		= deepcopy(snake_case_		)
          elif os.path.exists(snake_case_		):
                with io.open(snake_case_					,	"""r"""					,	encoding="""utf-8"""		) as f:
                      snake_case__    :       Union[str, Any]       		= json.load(snake_case_		)
          else:
                try:
                      snake_case__    :       List[Any]       		= baseaa.urlsafe_baadecode(snake_case_		).decode("""utf-8"""		)
                      snake_case__    :       Optional[int]       		= json.loads(snake_case_		)
                except (UnicodeDecodeError, AttributeError, ValueError):
                      raise ValueError(
                          f"Expected a string path to an existing deepspeed config, or a dictionary, or a base64 encoded string. Received: {config_file_or_dict}"		)

          snake_case__    :       str       		= config

          self.set_stage_and_offload()




    def lowerCamelCase			(       self   :	int		):
          # zero stage - this is done as early as possible, before model is created, to allow
          # ``is_deepspeed_zero3_enabled`` query and getting to the early deepspeed config object
          # during ``zero.Init()`` which needs to know the dtype, and some other hparams.
          snake_case__    :       Union[str, Any]       		= self.get_value("""zero_optimization.stage"""					,	-1		)

          # offload
          snake_case__    :       Optional[Any]       		= False
          if self.is_zeroa() or self.is_zeroa():
                snake_case__    :       int       		= set(["""cpu""", """nvme"""]		)
                snake_case__    :       Any       		= set(
                    [
                        self.get_value("""zero_optimization.offload_optimizer.device"""		),
                        self.get_value("""zero_optimization.offload_param.device"""		),
                    ]		)
                if len(offload_devices & offload_devices_valid		) > 0:
                      snake_case__    :       List[Any]       		= True




    def lowerCamelCase			(       self   :	Any					,	snake_case_   :	Optional[Any]		):
          snake_case__    :       Optional[Any]       		= self.config

          # find the config node of interest if it exists
          snake_case__    :       List[Any]       		= ds_key_long.split("""."""		)
          snake_case__    :       Tuple       		= nodes.pop()
          for node in nodes:
                snake_case__    :       Optional[Any]       		= config.get(snake_case_		)
                if config is None:
                      return None, ds_key

          return config, ds_key




    def lowerCamelCase			(       self   :	List[Any]					,	snake_case_   :	int					,	snake_case_   :	List[str]=None		):
          snake_case__		,							snake_case__    :       Dict       		= self.find_config_node(snake_case_		)
          if config is None:
                return default
          return config.get(snake_case_					,	snake_case_		)




    def lowerCamelCase			(       self   :	Dict					,	snake_case_   :	Optional[int]					,	snake_case_   :	int=False		):
          snake_case__    :       Dict       		= self.config

          # find the config node of interest if it exists
          snake_case__    :       str       		= ds_key_long.split("""."""		)
          for node in nodes:
                snake_case__    :       Union[str, Any]       		= config
                snake_case__    :       List[Any]       		= config.get(snake_case_		)
                if config is None:
                      if must_exist:
                            raise ValueError(f"Can't find {ds_key_long} entry in the config: {self.config}"		)
                      else:
                            return

        # if found remove it
          if parent_config is not None:
                parent_config.pop(snake_case_		)




    def lowerCamelCase			(       self   :	str					,	snake_case_   :	Optional[Any]		):
          snake_case__    :       Dict       		= self.get_value(snake_case_		)
          return False if value is None else bool(snake_case_		)




    def lowerCamelCase			(       self   :	List[str]					,	snake_case_   :	Union[str, Any]		):
          snake_case__    :       Optional[int]       		= self.get_value(snake_case_		)
          return False if value is None else not bool(snake_case_		)




    def lowerCamelCase			(       self   :	Optional[Any]		):
          return self._stage == 2




    def lowerCamelCase			(       self   :	Dict		):
          return self._stage == 3




    def lowerCamelCase			(       self   :	List[Any]		):
          return self._offload







class        UpperCAmelCase_			:
    """simple docstring"""







    def __init__(       self   :	Any					,	snake_case_   :	List[Any]		):
          snake_case__    :       Tuple       		= engine




    def lowerCamelCase			(       self   :	Dict					,	snake_case_   :	Optional[Any]					,	**snake_case_   :	int		):
          # runs backpropagation and handles mixed precision
          self.engine.backward(snake_case_					,	**snake_case_		)

          # Deepspeed's `engine.step` performs the following operations:
          # - gradient accumulation check
          # - gradient clipping
          # - optimizer step
          # - zero grad
          # - checking overflow
          # - lr_scheduler step (only if engine.lr_scheduler is not None)
          self.engine.step()
          # and this plugin overrides the above calls with no-ops when Accelerate runs under
          # Deepspeed, but allows normal functionality for non-Deepspeed cases thus enabling a simple
          # training loop that works transparently under many training regimes.







class        UpperCAmelCase_			(			_a      ):
    """simple docstring"""







    def __init__(       self   :	Tuple					,	snake_case_   :	Any		):
          super().__init__(snake_case_					,	device_placement=snake_case_					,	scaler=snake_case_		)
          snake_case__    :       Optional[int]       		= hasattr(self.optimizer					,	"""overflow"""		)




    def lowerCamelCase			(       self   :	int					,	snake_case_   :	Optional[Any]=None		):
          pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed




    def lowerCamelCase			(       self   :	List[Any]		):
          pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed




    @property
    def lowerCamelCase			(       self   :	List[str]		):
          if self.__has_overflow__:
                return self.optimizer.overflow
          return False







class        UpperCAmelCase_			(			_a      ):
    """simple docstring"""







    def __init__(       self   :	Tuple					,	snake_case_   :	Optional[Any]					,	snake_case_   :	Tuple		):
          super().__init__(snake_case_					,	snake_case_		)




    def lowerCamelCase			(       self   :	Optional[int]		):
          pass  # `accelerator.backward(loss)` is doing that automatically. Therefore, its implementation is not needed







class        UpperCAmelCase_			:
    """simple docstring"""







    def __init__(       self   :	Optional[int]					,	snake_case_   :	Dict					,	snake_case_   :	int=0.001					,	snake_case_   :	Optional[Any]=0					,	**snake_case_   :	List[Any]		):
          snake_case__    :       int       		= params
          snake_case__    :       Optional[Any]       		= lr
          snake_case__    :       Any       		= weight_decay
          snake_case__    :       Optional[Any]       		= kwargs







class        UpperCAmelCase_			:
    """simple docstring"""







    def __init__(       self   :	Dict					,	snake_case_   :	str					,	snake_case_   :	Optional[int]=None					,	snake_case_   :	Any=0					,	**snake_case_   :	List[str]		):
          snake_case__    :       List[Any]       		= optimizer
          snake_case__    :       List[Any]       		= total_num_steps
          snake_case__    :       List[Any]       		= warmup_num_steps
          snake_case__    :       Optional[Any]       		= kwargs
