


"""simple docstring"""





import os
from shutil import copyfile
from typing import List, Optional, Tuple

from tokenizers import processors

from ...tokenization_utils import AddedToken, BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import is_sentencepiece_available, logging


if is_sentencepiece_available():
  from .tokenization_mbartaa import MBartaaTokenizer
else:
  SCREAMING_SNAKE_CASE				:     str          =	None


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

SCREAMING_SNAKE_CASE				:     Optional[int]          =	{'''vocab_file''': '''sentencepiece.bpe.model''', '''tokenizer_file''': '''tokenizer.json'''}

SCREAMING_SNAKE_CASE				:     Optional[Any]          =	{
    '''vocab_file''': {
        '''facebook/mbart-large-50-one-to-many-mmt''': (
            '''https://huggingface.co/facebook/mbart-large-50-one-to-many-mmt/resolve/main/sentencepiece.bpe.model'''
        ),
    },
    '''tokenizer_file''': {
        '''facebook/mbart-large-50-one-to-many-mmt''': (
            '''https://huggingface.co/facebook/mbart-large-50-one-to-many-mmt/resolve/main/tokenizer.json'''
        ),
    },
}

SCREAMING_SNAKE_CASE				:     List[Any]          =	{
    '''facebook/mbart-large-50-one-to-many-mmt''': 1_0_2_4,
}

# fmt: off
SCREAMING_SNAKE_CASE				:     Optional[int]          =	['''ar_AR''', '''cs_CZ''', '''de_DE''', '''en_XX''', '''es_XX''', '''et_EE''', '''fi_FI''', '''fr_XX''', '''gu_IN''', '''hi_IN''', '''it_IT''', '''ja_XX''', '''kk_KZ''', '''ko_KR''', '''lt_LT''', '''lv_LV''', '''my_MM''', '''ne_NP''', '''nl_XX''', '''ro_RO''', '''ru_RU''', '''si_LK''', '''tr_TR''', '''vi_VN''', '''zh_CN''', '''af_ZA''', '''az_AZ''', '''bn_IN''', '''fa_IR''', '''he_IL''', '''hr_HR''', '''id_ID''', '''ka_GE''', '''km_KH''', '''mk_MK''', '''ml_IN''', '''mn_MN''', '''mr_IN''', '''pl_PL''', '''ps_AF''', '''pt_XX''', '''sv_SE''', '''sw_KE''', '''ta_IN''', '''te_IN''', '''th_TH''', '''tl_XX''', '''uk_UA''', '''ur_PK''', '''xh_ZA''', '''gl_ES''', '''sl_SI''']


class       __lowerCamelCase	(   __lowerCamelCase    ):
   __UpperCamelCase									=   VOCAB_FILES_NAMES
   __UpperCamelCase									=   PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
   __UpperCamelCase									=   PRETRAINED_VOCAB_FILES_MAP
   __UpperCamelCase									=   ['input_ids', 'attention_mask']
   __UpperCamelCase									=   MBartaaTokenizer

   __UpperCamelCase									=   []
   __UpperCamelCase									=   []







   def __init__(self       ,	lowerCamelCase=None       ,	lowerCamelCase=None       ,	lowerCamelCase=None       ,	lowerCamelCase=None       ,	lowerCamelCase="</s>"       ,	lowerCamelCase="</s>"       ,	lowerCamelCase="<s>"       ,	lowerCamelCase="<unk>"       ,	lowerCamelCase="<pad>"       ,	lowerCamelCase="<mask>"       ,	**lowerCamelCase       ,	):




      '''simple docstring'''




      _lowerCAmelCase					     =		AddedToken(lowerCamelCase       ,	lstrip=lowerCamelCase       ,	rstrip=lowerCamelCase				) if isinstance(lowerCamelCase       ,	lowerCamelCase				) else mask_token

      _lowerCAmelCase					     =		kwargs.get("""additional_special_tokens"""       ,	[]				)
      kwargs["additional_special_tokens"] += [
          code for code in FAIRSEQ_LANGUAGE_CODES if code not in kwargs["additional_special_tokens"]
      ]

      super().__init__(
          lowerCamelCase       ,	src_lang=lowerCamelCase       ,	tgt_lang=lowerCamelCase       ,	tokenizer_file=lowerCamelCase       ,	eos_token=lowerCamelCase       ,	sep_token=lowerCamelCase       ,	cls_token=lowerCamelCase       ,	unk_token=lowerCamelCase       ,	pad_token=lowerCamelCase       ,	mask_token=lowerCamelCase       ,	**lowerCamelCase       ,	)

      _lowerCAmelCase					     =		vocab_file
      _lowerCAmelCase					     =		False if not self.vocab_file else True

      _lowerCAmelCase					     =		{
          lang_code: self.convert_tokens_to_ids(lowerCamelCase				) for lang_code in FAIRSEQ_LANGUAGE_CODES
      }

      _lowerCAmelCase					     =		src_lang if src_lang is not None else """en_XX"""
      _lowerCAmelCase					     =		tgt_lang
      _lowerCAmelCase					     =		self.lang_code_to_id[self._src_lang]
      self.set_src_lang_special_tokens(self._src_lang				)







   @property
   def 							A__						(self				):




      '''simple docstring'''




      return self._src_lang







   @src_lang.setter
   def 							A__						(self       ,	lowerCamelCase				):




      '''simple docstring'''




      _lowerCAmelCase					     =		new_src_lang
      self.set_src_lang_special_tokens(self._src_lang				)







   def 							A__						(self       ,	lowerCamelCase       ,	lowerCamelCase = None				):




      '''simple docstring'''




      if token_ids_a is None:
         return self.prefix_tokens + token_ids_a + self.suffix_tokens
      # We don't expect to process pairs, but leave the pair logic for API consistency
      return self.prefix_tokens + token_ids_a + token_ids_a + self.suffix_tokens







   def 							A__						(self       ,	lowerCamelCase       ,	lowerCamelCase = "en_XX"       ,	lowerCamelCase = None       ,	lowerCamelCase = "ro_RO"       ,	**lowerCamelCase       ,	):




      '''simple docstring'''




      _lowerCAmelCase					     =		src_lang
      _lowerCAmelCase					     =		tgt_lang
      return super().prepare_seqaseq_batch(lowerCamelCase       ,	lowerCamelCase       ,	**lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      return self.set_src_lang_special_tokens(self.src_lang				)







   def 							A__						(self				):




      '''simple docstring'''




      return self.set_tgt_lang_special_tokens(self.tgt_lang				)







   def 							A__						(self       ,	lowerCamelCase				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.convert_tokens_to_ids(lowerCamelCase				)
      _lowerCAmelCase					     =		[self.cur_lang_code_id]
      _lowerCAmelCase					     =		[self.eos_token_id]

      _lowerCAmelCase					     =		self.convert_ids_to_tokens(self.prefix_tokens				)
      _lowerCAmelCase					     =		self.convert_ids_to_tokens(self.suffix_tokens				)

      _lowerCAmelCase					     =		processors.TemplateProcessing(
          single=prefix_tokens_str + ["""$A"""] + suffix_tokens_str       ,	pair=prefix_tokens_str + ["""$A""", """$B"""] + suffix_tokens_str       ,	special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str       ,	self.prefix_tokens + self.suffix_tokens				)				)       ,	)







   def 							A__						(self       ,	lowerCamelCase				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.convert_tokens_to_ids(lowerCamelCase				)
      _lowerCAmelCase					     =		[self.cur_lang_code_id]
      _lowerCAmelCase					     =		[self.eos_token_id]

      _lowerCAmelCase					     =		self.convert_ids_to_tokens(self.prefix_tokens				)
      _lowerCAmelCase					     =		self.convert_ids_to_tokens(self.suffix_tokens				)

      _lowerCAmelCase					     =		processors.TemplateProcessing(
          single=prefix_tokens_str + ["""$A"""] + suffix_tokens_str       ,	pair=prefix_tokens_str + ["""$A""", """$B"""] + suffix_tokens_str       ,	special_tokens=list(zip(prefix_tokens_str + suffix_tokens_str       ,	self.prefix_tokens + self.suffix_tokens				)				)       ,	)







   def 							A__						(self       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase       ,	**lowerCamelCase				):




      '''simple docstring'''




      if src_lang is None or tgt_lang is None:
         raise ValueError("""Translation requires a `src_lang` and a `tgt_lang` for this model"""				)
      _lowerCAmelCase					     =		src_lang
      _lowerCAmelCase					     =		self(lowerCamelCase       ,	add_special_tokens=lowerCamelCase       ,	return_tensors=lowerCamelCase       ,	**lowerCamelCase				)
      _lowerCAmelCase					     =		self.convert_tokens_to_ids(lowerCamelCase				)
      _lowerCAmelCase					     =		tgt_lang_id
      return inputs







   def 							A__						(self       ,	lowerCamelCase       ,	lowerCamelCase = None				):




      '''simple docstring'''




      if not self.can_save_slow_tokenizer:
         raise ValueError(
             """Your fast tokenizer does not have the necessary information to save the vocabulary for a slow """
             """tokenizer."""				)

      if not os.path.isdir(lowerCamelCase				):
         logger.error(f"""Vocabulary path ({save_directory}) should be a directory"""				)
         return
      _lowerCAmelCase					     =		os.path.join(
          lowerCamelCase       ,	(filename_prefix + """-""" if filename_prefix else """""") + VOCAB_FILES_NAMES["""vocab_file"""]				)

      if os.path.abspath(self.vocab_file				) != os.path.abspath(lowerCamelCase				):
         copyfile(self.vocab_file       ,	lowerCamelCase				)

      return (out_vocab_file,)




"""simple docstring"""





import torch

from diffusers import HeunDiscreteScheduler
from diffusers.utils import torch_device

from .test_schedulers import SchedulerCommonTest


class       __lowerCamelCase	(   __lowercase    ):
   __UpperCamelCase									=   (HeunDiscreteScheduler,)
   __UpperCamelCase									=   10







   def 							A__						(self       ,	**lowerCamelCase				):




      '''simple docstring'''




      _lowerCAmelCase					     =		{
          """num_train_timesteps""": 1_100,
          """beta_start""": 0.0001,
          """beta_end""": 0.02,
          """beta_schedule""": """linear""",
      }

      config.update(**lowerCamelCase				)
      return config







   def 							A__						(self				):




      '''simple docstring'''




      for timesteps in [10, 50, 100, 1_000]:
         self.check_over_configs(num_train_timesteps=lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      for beta_start, beta_end in zip([0.0_0001, 0.0001, 0.001]       ,	[0.0002, 0.002, 0.02]				):
         self.check_over_configs(beta_start=lowerCamelCase       ,	beta_end=lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      for schedule in ["linear", "scaled_linear", "exp"]:
         self.check_over_configs(beta_schedule=lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      for clip_sample_range in [1.0, 2.0, 3.0]:
         self.check_over_configs(clip_sample_range=lowerCamelCase       ,	clip_sample=lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      for prediction_type in ["epsilon", "v_prediction", "sample"]:
         self.check_over_configs(prediction_type=lowerCamelCase				)







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.scheduler_classes[0]
      _lowerCAmelCase					     =		self.get_scheduler_config()
      _lowerCAmelCase					     =		scheduler_class(**lowerCamelCase				)

      scheduler.set_timesteps(self.num_inference_steps				)

      _lowerCAmelCase					     =		self.dummy_model()
      _lowerCAmelCase					     =		self.dummy_sample_deter * scheduler.init_noise_sigma
      _lowerCAmelCase					     =		sample.to(lowerCamelCase				)

      for i, t in enumerate(scheduler.timesteps				):
         _lowerCAmelCase					     =		scheduler.scale_model_input(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		model(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		scheduler.step(lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				)
         _lowerCAmelCase					     =		output.prev_sample

      _lowerCAmelCase					     =		torch.sum(torch.abs(lowerCamelCase				)				)
      _lowerCAmelCase					     =		torch.mean(torch.abs(lowerCamelCase				)				)

      if torch_device in ["cpu", "mps"]:
         assert abs(result_sum.item() - 0.1233				) < 1e-2
         assert abs(result_mean.item() - 0.0002				) < 1e-3
      else:
         # CUDA
         assert abs(result_sum.item() - 0.1233				) < 1e-2
         assert abs(result_mean.item() - 0.0002				) < 1e-3







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.scheduler_classes[0]
      _lowerCAmelCase					     =		self.get_scheduler_config(prediction_type="""v_prediction"""				)
      _lowerCAmelCase					     =		scheduler_class(**lowerCamelCase				)

      scheduler.set_timesteps(self.num_inference_steps				)

      _lowerCAmelCase					     =		self.dummy_model()
      _lowerCAmelCase					     =		self.dummy_sample_deter * scheduler.init_noise_sigma
      _lowerCAmelCase					     =		sample.to(lowerCamelCase				)

      for i, t in enumerate(scheduler.timesteps				):
         _lowerCAmelCase					     =		scheduler.scale_model_input(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		model(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		scheduler.step(lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				)
         _lowerCAmelCase					     =		output.prev_sample

      _lowerCAmelCase					     =		torch.sum(torch.abs(lowerCamelCase				)				)
      _lowerCAmelCase					     =		torch.mean(torch.abs(lowerCamelCase				)				)

      if torch_device in ["cpu", "mps"]:
         assert abs(result_sum.item() - 4.6934e-07				) < 1e-2
         assert abs(result_mean.item() - 6.1112e-10				) < 1e-3
      else:
         # CUDA
         assert abs(result_sum.item() - 4.693428650170972e-07				) < 1e-2
         assert abs(result_mean.item() - 0.0002				) < 1e-3







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.scheduler_classes[0]
      _lowerCAmelCase					     =		self.get_scheduler_config()
      _lowerCAmelCase					     =		scheduler_class(**lowerCamelCase				)

      scheduler.set_timesteps(self.num_inference_steps       ,	device=lowerCamelCase				)

      _lowerCAmelCase					     =		self.dummy_model()
      _lowerCAmelCase					     =		self.dummy_sample_deter.to(lowerCamelCase				) * scheduler.init_noise_sigma

      for t in scheduler.timesteps:
         _lowerCAmelCase					     =		scheduler.scale_model_input(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		model(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		scheduler.step(lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				)
         _lowerCAmelCase					     =		output.prev_sample

      _lowerCAmelCase					     =		torch.sum(torch.abs(lowerCamelCase				)				)
      _lowerCAmelCase					     =		torch.mean(torch.abs(lowerCamelCase				)				)

      if str(lowerCamelCase				).startswith("""cpu"""				):
         # The following sum varies between 148 and 156 on mps. Why?
         assert abs(result_sum.item() - 0.1233				) < 1e-2
         assert abs(result_mean.item() - 0.0002				) < 1e-3
      elif str(lowerCamelCase				).startswith("""mps"""				):
         # Larger tolerance on mps
         assert abs(result_mean.item() - 0.0002				) < 1e-2
      else:
         # CUDA
         assert abs(result_sum.item() - 0.1233				) < 1e-2
         assert abs(result_mean.item() - 0.0002				) < 1e-3







   def 							A__						(self				):




      '''simple docstring'''




      _lowerCAmelCase					     =		self.scheduler_classes[0]
      _lowerCAmelCase					     =		self.get_scheduler_config()
      _lowerCAmelCase					     =		scheduler_class(**lowerCamelCase       ,	use_karras_sigmas=lowerCamelCase				)

      scheduler.set_timesteps(self.num_inference_steps       ,	device=lowerCamelCase				)

      _lowerCAmelCase					     =		self.dummy_model()
      _lowerCAmelCase					     =		self.dummy_sample_deter.to(lowerCamelCase				) * scheduler.init_noise_sigma
      _lowerCAmelCase					     =		sample.to(lowerCamelCase				)

      for t in scheduler.timesteps:
         _lowerCAmelCase					     =		scheduler.scale_model_input(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		model(lowerCamelCase       ,	lowerCamelCase				)

         _lowerCAmelCase					     =		scheduler.step(lowerCamelCase       ,	lowerCamelCase       ,	lowerCamelCase				)
         _lowerCAmelCase					     =		output.prev_sample

      _lowerCAmelCase					     =		torch.sum(torch.abs(lowerCamelCase				)				)
      _lowerCAmelCase					     =		torch.mean(torch.abs(lowerCamelCase				)				)

      assert abs(result_sum.item() - 0.0_0015				) < 1e-2
      assert abs(result_mean.item() - 1.9869554535034695e-07				) < 1e-2