

import warnings

from transformers import AutoTokenizer
from transformers.utils import is_torch_available
from transformers.utils.generic import ExplicitEnum

from ...processing_utils import ProcessorMixin


if is_torch_available():
   import torch



class SCREAMING_SNAKE_CASE     (  a_			):
  """simple docstring"""






  lowerCamelCase   :      int										="char"
  lowerCamelCase   :      Optional[Any]										="bpe"
  lowerCamelCase   :      List[str]										="wp"






__UpperCAmelCase								=			(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)



class SCREAMING_SNAKE_CASE     (  a_			):
  """simple docstring"""






  lowerCamelCase   :      Dict										=["image_processor", "char_tokenizer"]
  lowerCamelCase   :      Tuple										="ViTImageProcessor"
  lowerCamelCase   :      Union[str, Any]										="MgpstrTokenizer"







  def __init__(      self			:   str	,							lowerCAmelCase			:   Tuple=None	,							lowerCAmelCase			:   Optional[Any]=None	,							**lowerCAmelCase			:   List[str]   )							->     Optional[Any]:






     """simple docstring"""







     __lowerCAmelCase  :      Dict    =						None
     if "feature_extractor" in kwargs:
        warnings.warn(
            """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
            """ instead."""	,							lowerCAmelCase	,							)
        __lowerCAmelCase  :      Any    =						kwargs.pop("""feature_extractor"""   )

     __lowerCAmelCase  :      List[str]    =						image_processor if image_processor is not None else feature_extractor
     if image_processor is None:
        raise ValueError("""You need to specify an `image_processor`."""   )
     if tokenizer is None:
        raise ValueError("""You need to specify a `tokenizer`."""   )

     __lowerCAmelCase  :      Any    =						tokenizer
     __lowerCAmelCase  :      Tuple    =						AutoTokenizer.from_pretrained("""gpt2"""   )
     __lowerCAmelCase  :      int    =						AutoTokenizer.from_pretrained("""bert-base-uncased"""   )

     super().__init__(lowerCAmelCase	,							lowerCAmelCase   )







  def __call__(      self			:   Any	,							lowerCAmelCase			:   int=None	,							lowerCAmelCase			:   Union[str, Any]=None	,							lowerCAmelCase			:   Dict=None	,							**lowerCAmelCase			:   int   )							->     Union[str, Any]:






     """simple docstring"""







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

     if images is not None:
        __lowerCAmelCase  :      Union[str, Any]    =						self.image_processor(lowerCAmelCase	,							return_tensors=lowerCAmelCase	,							**lowerCAmelCase   )
     if text is not None:
        __lowerCAmelCase  :      List[Any]    =						self.char_tokenizer(lowerCAmelCase	,							return_tensors=lowerCAmelCase	,							**lowerCAmelCase   )

     if text is None:
        return inputs
     elif images is None:
        return encodings
     else:
        __lowerCAmelCase  :      Any    =						encodings["""input_ids"""]
        return inputs







  def 	SCREAMING_SNAKE_CASE   (      self			:   Tuple	,							lowerCAmelCase			:   Dict   )							->     Dict:






     """simple docstring"""







     __lowerCAmelCase   ,__lowerCAmelCase   ,__lowerCAmelCase  :      Optional[int]    =						sequences
     __lowerCAmelCase  :      Union[str, Any]    =						char_preds.size(0   )

     __lowerCAmelCase   ,__lowerCAmelCase  :      Dict    =						self._decode_helper(lowerCAmelCase	,							"""char"""   )
     __lowerCAmelCase   ,__lowerCAmelCase  :      int    =						self._decode_helper(lowerCAmelCase	,							"""bpe"""   )
     __lowerCAmelCase   ,__lowerCAmelCase  :      Any    =						self._decode_helper(lowerCAmelCase	,							"""wp"""   )

     __lowerCAmelCase  :      Dict    =						[]
     __lowerCAmelCase  :      int    =						[]
     for i in range(lowerCAmelCase   ):
        __lowerCAmelCase  :      Any    =						[char_scores[i], bpe_scores[i], wp_scores[i]]
        __lowerCAmelCase  :      Optional[int]    =						[char_strs[i], bpe_strs[i], wp_strs[i]]
        __lowerCAmelCase  :      List[Any]    =						scores.index(max(lowerCAmelCase   )   )
        final_strs.append(strs[max_score_index]   )
        final_scores.append(scores[max_score_index]   )

     __lowerCAmelCase  :      Optional[int]    =						{}
     __lowerCAmelCase  :      Dict    =						final_strs
     __lowerCAmelCase  :      List[str]    =						final_scores
     __lowerCAmelCase  :      str    =						char_strs
     __lowerCAmelCase  :      Union[str, Any]    =						bpe_strs
     __lowerCAmelCase  :      Dict    =						wp_strs
     return out







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[Any]	,							lowerCAmelCase			:   List[str]	,							lowerCAmelCase			:   Dict   )							->     List[str]:






     """simple docstring"""







     if format == DecodeType.CHARACTER:
        __lowerCAmelCase  :      Dict    =						self.char_decode
        __lowerCAmelCase  :      Dict    =						1
        __lowerCAmelCase  :      Any    =						"""[s]"""
     elif format == DecodeType.BPE:
        __lowerCAmelCase  :      Dict    =						self.bpe_decode
        __lowerCAmelCase  :      List[Any]    =						2
        __lowerCAmelCase  :      int    =						"""#"""
     elif format == DecodeType.WORDPIECE:
        __lowerCAmelCase  :      str    =						self.wp_decode
        __lowerCAmelCase  :      Optional[Any]    =						1_02
        __lowerCAmelCase  :      Any    =						"""[SEP]"""
     else:
        raise ValueError(f'''Format {format} is not supported.'''   )

     __lowerCAmelCase   ,__lowerCAmelCase  :      Optional[int]    =						[], []
     __lowerCAmelCase  :      Optional[int]    =						pred_logits.size(0   )
     __lowerCAmelCase  :      Optional[int]    =						pred_logits.size(1   )
     __lowerCAmelCase   ,__lowerCAmelCase  :      Union[str, Any]    =						pred_logits.topk(1	,							dim=-1	,							largest=lowerCAmelCase	,							sorted=lowerCAmelCase   )
     __lowerCAmelCase  :      str    =						preds_index.view(-1	,							lowerCAmelCase   )[:, 1:]
     __lowerCAmelCase  :      Optional[int]    =						decoder(lowerCAmelCase   )
     __lowerCAmelCase   ,__lowerCAmelCase  :      Union[str, Any]    =						torch.nn.functional.softmax(lowerCAmelCase	,							dim=2   ).max(dim=2   )
     __lowerCAmelCase  :      int    =						preds_max_prob[:, 1:]

     for index in range(lowerCAmelCase   ):
        __lowerCAmelCase  :      Union[str, Any]    =						preds_str[index].find(lowerCAmelCase   )
        __lowerCAmelCase  :      Optional[Any]    =						preds_str[index][:pred_eos]
        __lowerCAmelCase  :      Dict    =						preds_index[index].cpu().tolist()
        __lowerCAmelCase  :      Tuple    =						pred_index.index(lowerCAmelCase   ) if eos_token in pred_index else -1
        __lowerCAmelCase  :      Optional[int]    =						preds_max_prob[index][: pred_eos_index + 1]
        __lowerCAmelCase  :      Optional[Any]    =						pred_max_prob.cumprod(dim=0   )[-1] if pred_max_prob.nelement() != 0 else 0.0
        dec_strs.append(lowerCAmelCase   )
        conf_scores.append(lowerCAmelCase   )

     return dec_strs, conf_scores







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[str]	,							lowerCAmelCase			:   List[Any]   )							->     Any:






     """simple docstring"""







     __lowerCAmelCase  :      Dict    =						[seq.replace(""" """	,							""""""   ) for seq in self.char_tokenizer.batch_decode(lowerCAmelCase   )]
     return decode_strs







  def 	SCREAMING_SNAKE_CASE   (      self			:   int	,							lowerCAmelCase			:   Dict   )							->     Dict:






     """simple docstring"""







     return self.bpe_tokenizer.batch_decode(lowerCAmelCase   )







  def 	SCREAMING_SNAKE_CASE   (      self			:   Dict	,							lowerCAmelCase			:   Optional[Any]   )							->     List[str]:






     """simple docstring"""







     __lowerCAmelCase  :      Tuple    =						[seq.replace(""" """	,							""""""   ) for seq in self.wp_tokenizer.batch_decode(lowerCAmelCase   )]
     return decode_strs





import importlib.util
import json
import os
import warnings
from dataclasses import dataclass, field

import torch

from ..training_args import TrainingArguments
from ..utils import cached_property, is_sagemaker_dp_enabled, logging


__UpperCAmelCase								=			logging.get_logger(__name__)





def      snake_case_				()      ->     str:
   # Get the sagemaker specific mp parameters from smp_options variable.
   __lowerCAmelCase  :      List[Any]    =						os.getenv("""SM_HP_MP_PARAMETERS""" ,       """{}"""					)
   try:
      # Parse it and check the field "partitions" is included, it is required for model parallel.
      __lowerCAmelCase  :      Any    =						json.loads(__A					)
      if "partitions" not in smp_options:
         return False
   except json.JSONDecodeError:
      return False

   # Get the sagemaker specific framework parameters from mpi_options variable.
   __lowerCAmelCase  :      Any    =						os.getenv("""SM_FRAMEWORK_PARAMS""" ,       """{}"""					)
   try:
      # Parse it and check the field "sagemaker_distributed_dataparallel_enabled".
      __lowerCAmelCase  :      Union[str, Any]    =						json.loads(__A					)
      if not mpi_options.get("""sagemaker_mpi_enabled""" ,       __A					):
         return False
   except json.JSONDecodeError:
      return False
   # Lastly, check if the `smdistributed` module is present.
   return importlib.util.find_spec("""smdistributed"""					) is not None


if is_sagemaker_model_parallel_available():
   import smdistributed.modelparallel.torch as smp

   smp.init()



@dataclass
class SCREAMING_SNAKE_CASE     (  a_			):
  """simple docstring"""






  lowerCamelCase   :      str										=field(
      default=""       ,							metadata={"help": "Used by the SageMaker launcher to send mp-specific args. Ignored in SageMakerTrainer"}       ,							)







  def 	SCREAMING_SNAKE_CASE   (      self			:   List[Any]   )							->     Optional[int]:






     """simple docstring"""







     super().__post_init__()
     warnings.warn(
         """`SageMakerTrainingArguments` is deprecated and will be removed in v5 of Transformers. You can use """
         """`TrainingArguments` instead."""	,							lowerCAmelCase	,							)







  @cached_property
  def 	SCREAMING_SNAKE_CASE   (      self			:   Any   )							->     "torch.device":






     """simple docstring"""







     logger.info("""PyTorch: setting up devices"""   )
     if torch.distributed.is_available() and torch.distributed.is_initialized() and self.local_rank == -1:
        logger.warning(
            """torch.distributed process group is initialized, but local_rank == -1. """
            """In order to use Torch DDP, launch your script with `python -m torch.distributed.launch"""   )
     if self.no_cuda:
        __lowerCAmelCase  :      Optional[Any]    =						torch.device("""cpu"""   )
        __lowerCAmelCase  :      Union[str, Any]    =						0
     elif is_sagemaker_model_parallel_available():
        __lowerCAmelCase  :      int    =						smp.local_rank()
        __lowerCAmelCase  :      str    =						torch.device("""cuda"""	,							lowerCAmelCase   )
        __lowerCAmelCase  :      Tuple    =						1
     elif is_sagemaker_dp_enabled():
        import smdistributed.dataparallel.torch.torch_smddp  # noqa: F401

        torch.distributed.init_process_group(backend="""smddp"""	,							timeout=self.ddp_timeout_delta   )
        __lowerCAmelCase  :      List[str]    =						int(os.getenv("""SMDATAPARALLEL_LOCAL_RANK"""   )   )
        __lowerCAmelCase  :      Tuple    =						torch.device("""cuda"""	,							self.local_rank   )
        __lowerCAmelCase  :      Optional[int]    =						1
     elif self.local_rank == -1:
        # if n_gpu is > 1 we'll use nn.DataParallel.
        # If you only want to use a specific subset of GPUs use `CUDA_VISIBLE_DEVICES=0`
        # Explicitly set CUDA to the first (index 0) CUDA device, otherwise `set_device` will
        # trigger an error that a device index is missing. Index 0 takes into account the
        # GPUs available in the environment, so `CUDA_VISIBLE_DEVICES=1,2` with `cuda:0`
        # will use the first GPU in that env, i.e. GPU#1
        __lowerCAmelCase  :      List[Any]    =						torch.device("""cuda:0""" if torch.cuda.is_available() else """cpu"""   )
        # Sometimes the line in the postinit has not been run before we end up here, so just checking we're not at
        # the default value.
        __lowerCAmelCase  :      Optional[int]    =						torch.cuda.device_count()
     else:
        # Here, we'll use torch.distributed.
        # Initializes the distributed backend which will take care of synchronizing nodes/GPUs
        if not torch.distributed.is_initialized():
           torch.distributed.init_process_group(backend="""nccl"""	,							timeout=self.ddp_timeout_delta   )
        __lowerCAmelCase  :      List[str]    =						torch.device("""cuda"""	,							self.local_rank   )
        __lowerCAmelCase  :      int    =						1

     if device.type == "cuda":
        torch.cuda.set_device(lowerCAmelCase   )

     return device







  @property
  def 	SCREAMING_SNAKE_CASE   (      self			:   int   )							->     Any:






     """simple docstring"""







     if is_sagemaker_model_parallel_available():
        return smp.dp_size()

     return super().world_size







  @property
  def 	SCREAMING_SNAKE_CASE   (      self			:   Optional[int]   )							->     Tuple:






     """simple docstring"""







     return not is_sagemaker_model_parallel_available()







  @property
  def 	SCREAMING_SNAKE_CASE   (      self			:   int   )							->     Tuple:






     """simple docstring"""







     return False


