



'''simple docstring'''


from ...configuration_utils import PretrainedConfig
from ...utils import logging


_lowerCamelCase	:       Optional[int]      	=	logging.get_logger(__name__)

_lowerCamelCase	:       Tuple      	=	{
    "tanreinama/GPTSAN-2.8B-spout_is_uniform": (
        "https://huggingface.co/tanreinama/GPTSAN-2.8B-spout_is_uniform/resolve/main/config.json"
    ),
}

class  SCREAMING_SNAKE_CASE (  SCREAMING_SNAKE_CASE__		):






    """simple docstring"""





    _SCREAMING_SNAKE_CASE	  =  """gptsan-japanese"""
    _SCREAMING_SNAKE_CASE	  =  [
        """past_key_values""",
    ]
    _SCREAMING_SNAKE_CASE	  =  {
        """hidden_size""": """d_model""",
        """num_attention_heads""": """num_heads""",
        """num_hidden_layers""": """num_layers""",
    }






    def __init__(  self      :					Tuple  ,						UpperCamelCase__      :					Tuple=3_6_0_0_0  ,						UpperCamelCase__      :					str=1_2_8_0  ,						UpperCamelCase__      :					Any=1_0_2_4  ,						UpperCamelCase__      :					Tuple=8_1_9_2  ,						UpperCamelCase__      :					Optional[int]=4_0_9_6  ,						UpperCamelCase__      :					Optional[Any]=1_2_8  ,						UpperCamelCase__      :					int=1_0  ,						UpperCamelCase__      :					Dict=0  ,						UpperCamelCase__      :					Dict=1_6  ,						UpperCamelCase__      :					Tuple=1_6  ,						UpperCamelCase__      :					List[str]=1_2_8  ,						UpperCamelCase__      :					str=0.0  ,						UpperCamelCase__      :					List[str]=1E-5  ,						UpperCamelCase__      :					Union[str, Any]=False  ,						UpperCamelCase__      :					Tuple=0.0  ,						UpperCamelCase__      :					Union[str, Any]="float32"  ,						UpperCamelCase__      :					Union[str, Any]=False  ,						UpperCamelCase__      :					str=False  ,						UpperCamelCase__      :					Union[str, Any]=False  ,						UpperCamelCase__      :					List[Any]=0.0_0_2  ,						UpperCamelCase__      :					Tuple=False  ,						UpperCamelCase__      :					str=True  ,						UpperCamelCase__      :					Tuple=3_5_9_9_8  ,						UpperCamelCase__      :					Optional[Any]=3_5_9_9_5  ,						UpperCamelCase__      :					Union[str, Any]=3_5_9_9_9  ,						**UpperCamelCase__      :					Union[str, Any]  ,						):


       """simple docstring"""







       UpperCamelCase					=  vocab_size
       UpperCamelCase					=  max_position_embeddings
       UpperCamelCase					=  d_model
       UpperCamelCase					=  d_ff
       UpperCamelCase					=  d_ext
       UpperCamelCase					=  d_spout
       UpperCamelCase					=  num_switch_layers
       UpperCamelCase					=  num_ext_layers
       UpperCamelCase					=  num_switch_layers + num_ext_layers
       UpperCamelCase					=  num_heads
       UpperCamelCase					=  num_experts
       UpperCamelCase					=  expert_capacity
       UpperCamelCase					=  dropout_rate
       UpperCamelCase					=  layer_norm_epsilon
       UpperCamelCase					=  router_bias
       UpperCamelCase					=  router_jitter_noise
       UpperCamelCase					=  router_dtype
       UpperCamelCase					=  router_ignore_padding_tokens
       UpperCamelCase					=  output_hidden_states
       UpperCamelCase					=  output_attentions
       UpperCamelCase					=  initializer_factor
       UpperCamelCase					=  output_router_logits
       UpperCamelCase					=  use_cache

       super().__init__(
           separator_token_id=UpperCamelCase__  ,						pad_token_id=UpperCamelCase__  ,						eos_token_id=UpperCamelCase__  ,						**UpperCamelCase__  ,						)


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      lowerCamelCase    (SCREAMING_SNAKE_CASE__    ):
  """simple docstring"""







  lowerCamelCase__   =  '''char'''
  lowerCamelCase__   =  '''bpe'''
  lowerCamelCase__   =  '''wp'''



A   :     Tuple       =				(DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)


class      lowerCamelCase    (SCREAMING_SNAKE_CASE__    ):
  """simple docstring"""







  lowerCamelCase__   =  ['''image_processor''', '''char_tokenizer''']
  lowerCamelCase__   =  '''ViTImageProcessor'''
  lowerCamelCase__   =  '''MgpstrTokenizer'''







  def __init__(							self       :				Optional[Any]     ,   __magic_name__       :				Tuple=None     ,   __magic_name__       :				int=None     ,   **__magic_name__       :				Optional[Any]      )     ->		str:
        SCREAMING_SNAKE_CASE_      =     None
        if "feature_extractor" in kwargs:
              warnings.warn(
                  "The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"
                  " instead."     ,   __magic_name__     ,   )
              SCREAMING_SNAKE_CASE_      =     kwargs.pop("feature_extractor"      )

        SCREAMING_SNAKE_CASE_      =     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`."      )

        SCREAMING_SNAKE_CASE_      =     tokenizer
        SCREAMING_SNAKE_CASE_      =     AutoTokenizer.from_pretrained("gpt2"      )
        SCREAMING_SNAKE_CASE_      =     AutoTokenizer.from_pretrained("bert-base-uncased"      )

        super().__init__(__magic_name__     ,   __magic_name__      )







  def __call__(							self       :				Dict     ,   __magic_name__       :				Union[str, Any]=None     ,   __magic_name__       :				Union[str, Any]=None     ,   __magic_name__       :				Dict=None     ,   **__magic_name__       :				Tuple      )     ->		int:
        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:
              SCREAMING_SNAKE_CASE_      =     self.image_processor(__magic_name__     ,   return_tensors=__magic_name__     ,   **__magic_name__      )
        if text is not None:
              SCREAMING_SNAKE_CASE_      =     self.char_tokenizer(__magic_name__     ,   return_tensors=__magic_name__     ,   **__magic_name__      )

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







  def       __A   (							self       :				Tuple     ,   __magic_name__       :				int      )     ->		Any:
        SCREAMING_SNAKE_CASE_   ,       SCREAMING_SNAKE_CASE_   ,       SCREAMING_SNAKE_CASE_      =     sequences
        SCREAMING_SNAKE_CASE_      =     char_preds.size(0      )

        SCREAMING_SNAKE_CASE_   ,       SCREAMING_SNAKE_CASE_      =     self._decode_helper(__magic_name__     ,   "char"      )
        SCREAMING_SNAKE_CASE_   ,       SCREAMING_SNAKE_CASE_      =     self._decode_helper(__magic_name__     ,   "bpe"      )
        SCREAMING_SNAKE_CASE_   ,       SCREAMING_SNAKE_CASE_      =     self._decode_helper(__magic_name__     ,   "wp"      )

        SCREAMING_SNAKE_CASE_      =     []
        SCREAMING_SNAKE_CASE_      =     []
        for i in range(__magic_name__      ):
              SCREAMING_SNAKE_CASE_      =     [char_scores[i], bpe_scores[i], wp_scores[i]]
              SCREAMING_SNAKE_CASE_      =     [char_strs[i], bpe_strs[i], wp_strs[i]]
              SCREAMING_SNAKE_CASE_      =     scores.index(max(__magic_name__      )      )
              final_strs.append(strs[max_score_index]      )
              final_scores.append(scores[max_score_index]      )

        SCREAMING_SNAKE_CASE_      =     {}
        SCREAMING_SNAKE_CASE_      =     final_strs
        SCREAMING_SNAKE_CASE_      =     final_scores
        SCREAMING_SNAKE_CASE_      =     char_strs
        SCREAMING_SNAKE_CASE_      =     bpe_strs
        SCREAMING_SNAKE_CASE_      =     wp_strs
        return out







  def       __A   (							self       :				int     ,   __magic_name__       :				List[Any]     ,   __magic_name__       :				str      )     ->		Any:
        if format == DecodeType.CHARACTER:
              SCREAMING_SNAKE_CASE_      =     self.char_decode
              SCREAMING_SNAKE_CASE_      =     1
              SCREAMING_SNAKE_CASE_      =     "[s]"
        elif format == DecodeType.BPE:
              SCREAMING_SNAKE_CASE_      =     self.bpe_decode
              SCREAMING_SNAKE_CASE_      =     2
              SCREAMING_SNAKE_CASE_      =     "#"
        elif format == DecodeType.WORDPIECE:
              SCREAMING_SNAKE_CASE_      =     self.wp_decode
              SCREAMING_SNAKE_CASE_      =     102
              SCREAMING_SNAKE_CASE_      =     "[SEP]"
        else:
              raise ValueError(F'''Format {format} is not supported.'''      )

        SCREAMING_SNAKE_CASE_   ,       SCREAMING_SNAKE_CASE_      =     [], []
        SCREAMING_SNAKE_CASE_      =     pred_logits.size(0      )
        SCREAMING_SNAKE_CASE_      =     pred_logits.size(1      )
        SCREAMING_SNAKE_CASE_   ,       SCREAMING_SNAKE_CASE_      =     pred_logits.topk(1     ,   dim=-1     ,   largest=__magic_name__     ,   sorted=__magic_name__      )
        SCREAMING_SNAKE_CASE_      =     preds_index.view(-1     ,   __magic_name__      )[:, 1:]
        SCREAMING_SNAKE_CASE_      =     decoder(__magic_name__      )
        SCREAMING_SNAKE_CASE_   ,       SCREAMING_SNAKE_CASE_      =     torch.nn.functional.softmax(__magic_name__     ,   dim=2      ).max(dim=2      )
        SCREAMING_SNAKE_CASE_      =     preds_max_prob[:, 1:]

        for index in range(__magic_name__      ):
              SCREAMING_SNAKE_CASE_      =     preds_str[index].find(__magic_name__      )
              SCREAMING_SNAKE_CASE_      =     preds_str[index][:pred_eos]
              SCREAMING_SNAKE_CASE_      =     preds_index[index].cpu().tolist()
              SCREAMING_SNAKE_CASE_      =     pred_index.index(__magic_name__      ) if eos_token in pred_index else -1
              SCREAMING_SNAKE_CASE_      =     preds_max_prob[index][: pred_eos_index + 1]
              SCREAMING_SNAKE_CASE_      =     pred_max_prob.cumprod(dim=0      )[-1] if pred_max_prob.nelement() != 0 else 0.0
              dec_strs.append(__magic_name__      )
              conf_scores.append(__magic_name__      )

        return dec_strs, conf_scores







  def       __A   (							self       :				Any     ,   __magic_name__       :				Dict      )     ->		List[str]:
        SCREAMING_SNAKE_CASE_      =     [seq.replace(" "     ,   ""      ) for seq in self.char_tokenizer.batch_decode(__magic_name__      )]
        return decode_strs







  def       __A   (							self       :				Any     ,   __magic_name__       :				Union[str, Any]      )     ->		Tuple:
        return self.bpe_tokenizer.batch_decode(__magic_name__      )







  def       __A   (							self       :				str     ,   __magic_name__       :				Dict      )     ->		List[Any]:
        SCREAMING_SNAKE_CASE_      =     [seq.replace(" "     ,   ""      ) for seq in self.wp_tokenizer.batch_decode(__magic_name__      )]
        return decode_strs


