


import random
from typing import List, Optional, Union

import numpy as np
import torch

from ..state import AcceleratorState
from .constants import CUDA_DISTRIBUTED_TYPES
from .dataclasses import DistributedType, RNGType
from .imports import is_npu_available, is_tpu_available, is_xpu_available


if is_tpu_available(check_device=False):
       import torch_xla.core.xla_model as xm
def       UpperCamelCase__     (  SCREAMING_SNAKE_CASE__			,       SCREAMING_SNAKE_CASE__ = False						):
 if device_specific:
  seed += AcceleratorState().process_index
 random.seed(_UpperCAmelCase						)
 np.random.seed(_UpperCAmelCase						)
 torch.manual_seed(_UpperCAmelCase						)
 if is_xpu_available():
  torch.xpu.manual_seed_all(_UpperCAmelCase						)
 elif is_npu_available():
  torch.npu.manual_seed_all(_UpperCAmelCase						)
 else:
  torch.cuda.manual_seed_all(_UpperCAmelCase						)
 # ^^ safe to call this function even if cuda is not available
 if is_tpu_available():
  xm.set_rng_state(_UpperCAmelCase						)
def       UpperCamelCase__     (  SCREAMING_SNAKE_CASE__ = None			,       SCREAMING_SNAKE_CASE__ = None						):
 # Get the proper rng state
 if rng_type == RNGType.TORCH:
  __lowerCamelCase		:							str							=						torch.get_rng_state()
 elif rng_type == RNGType.CUDA:
  __lowerCamelCase		:							List[str]							=						torch.cuda.get_rng_state()
 elif rng_type == RNGType.XLA:
  assert is_tpu_available(), "Can't synchronize XLA seeds on an environment without TPUs."
  __lowerCamelCase		:							Union[str, Any]							=						torch.tensor(xm.get_rng_state()						)
 elif rng_type == RNGType.NPU:
  assert is_npu_available(), "Can't synchronize NPU seeds on an environment without NPUs."
  __lowerCamelCase		:							Tuple							=						torch.npu.get_rng_state()
 elif rng_type == RNGType.XPU:
  assert is_xpu_available(), "Can't synchronize XPU seeds on an environment without XPUs."
  __lowerCamelCase		:							str							=						torch.xpu.get_rng_state()
 elif rng_type == RNGType.GENERATOR:
  assert generator is not None, "Need a generator to synchronize its seed."
  __lowerCamelCase		:							Union[str, Any]							=						generator.get_state()

 # Broadcast the rng state from device 0 to other devices
 __lowerCamelCase		:							Union[str, Any]							=						AcceleratorState()
 if state.distributed_type == DistributedType.TPU:
  __lowerCamelCase		:							List[Any]							=						rng_state.to(xm.xla_device()						)
  xm.collective_broadcast([rng_state]						)
  xm.mark_step()
  __lowerCamelCase		:							List[str]							=						rng_state.cpu()
 elif (
     state.distributed_type in CUDA_DISTRIBUTED_TYPES
     or state.distributed_type == DistributedType.MULTI_NPU
     or state.distributed_type == DistributedType.MULTI_XPU
 ):
  __lowerCamelCase		:							List[str]							=						rng_state.to(state.device						)
  torch.distributed.broadcast(_UpperCAmelCase			,       0						)
  __lowerCamelCase		:							Optional[Any]							=						rng_state.cpu()
 elif state.distributed_type == DistributedType.MULTI_CPU:
  torch.distributed.broadcast(_UpperCAmelCase			,       0						)

 # Set the broadcast rng state
 if rng_type == RNGType.TORCH:
  torch.set_rng_state(_UpperCAmelCase						)
 elif rng_type == RNGType.CUDA:
  torch.cuda.set_rng_state(_UpperCAmelCase						)
 elif rng_type == RNGType.NPU:
  torch.npu.set_rng_state(_UpperCAmelCase						)
 elif rng_type == RNGType.XPU:
  torch.xpu.set_rng_state(_UpperCAmelCase						)
 elif rng_type == RNGType.XLA:
  xm.set_rng_state(rng_state.item()						)
 elif rng_type == RNGType.GENERATOR:
  generator.set_state(_UpperCAmelCase						)



def       UpperCamelCase__     (  SCREAMING_SNAKE_CASE__			,       SCREAMING_SNAKE_CASE__ = None						):
 for rng_type in rng_types:
  synchronize_rng_state(RNGType(_UpperCAmelCase						)			,       generator=_UpperCAmelCase						)








"""simple docstring"""
import warnings
from contextlib import contextmanager

from ...processing_utils import ProcessorMixin







class 	a				(					a__   ):
 snake_case__    	=    '''AutoFeatureExtractor'''
 snake_case__    	=    '''Speech2Text2Tokenizer'''



 def __init__(			self							,    _snake_case							,    _snake_case			):







  """simple docstring"""






  super().__init__(_snake_case							,    _snake_case			)
  lowerCAmelCase  							=      self.feature_extractor
  lowerCAmelCase  							=      False



 def __call__(			self							,    *_snake_case							,    **_snake_case			):







  """simple docstring"""






  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.'			)
   lowerCAmelCase  							=      kwargs.pop('raw_speech'			)
  else:
   lowerCAmelCase  							=      kwargs.pop('audio'							,    _snake_case			)
  lowerCAmelCase  							=      kwargs.pop('sampling_rate'							,    _snake_case			)
  lowerCAmelCase  							=      kwargs.pop('text'							,    _snake_case			)
  if len(_snake_case			) > 0:
   lowerCAmelCase  							=      args[0]
   lowerCAmelCase  							=      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:
   lowerCAmelCase  							=      self.feature_extractor(_snake_case							,    *_snake_case							,    sampling_rate=_snake_case							,    **_snake_case			)
  if text is not None:
   lowerCAmelCase  							=      self.tokenizer(_snake_case							,    **_snake_case			)

  if text is None:
   return inputs
  elif audio is None:
   return encodings
  else:
   lowerCAmelCase  							=      encodings['input_ids']
   return inputs



 def 					UpperCamelCase__   (			self							,    *_snake_case							,    **_snake_case			):







  """simple docstring"""






  return self.tokenizer.batch_decode(*_snake_case							,    **_snake_case			)



 def 					UpperCamelCase__   (			self							,    *_snake_case							,    **_snake_case			):







  """simple docstring"""






  return self.tokenizer.decode(*_snake_case							,    **_snake_case			)



 @contextmanager
 def 					UpperCamelCase__   (			self			):







  """simple docstring"""






  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.'			)
  lowerCAmelCase  							=      True
  lowerCAmelCase  							=      self.tokenizer
  yield
  lowerCAmelCase  							=      self.feature_extractor
  lowerCAmelCase  							=      False





