




import glob
import os
from typing import Dict, List, Union

import torch

from diffusers.utils import is_safetensors_available


if is_safetensors_available():
   import safetensors.torch

from huggingface_hub import snapshot_download

from diffusers import DiffusionPipeline, __version__
from diffusers.schedulers.scheduling_utils import SCHEDULER_CONFIG_NAME
from diffusers.utils import CONFIG_NAME, DIFFUSERS_CACHE, ONNX_WEIGHTS_NAME, WEIGHTS_NAME

class   UpperCAmelCase    (      a_ ):






   def __init__(						self :							List[Any]  )->      Optional[int]:
     self.register_to_config()
     super().__init__()






   def 			_a    (						self :							List[Any]    ,       UpperCamelCase_ :							Any    ,       UpperCamelCase_ :							Any  )->      List[str]:
     if dicta == dicta:
       return True
     else:
       _lowercase:			Tuple      =     self._remove_meta_keys(UpperCamelCase_  )
       _lowercase:			Any      =     self._remove_meta_keys(UpperCamelCase_  )
       if configa == configa:
         print(f'Warning !: Mismatch in keys {meta_keysa} and {meta_keysa}.'  )
         return True
     return False






   def 			_a    (						self :							Any    ,       UpperCamelCase_ :							Dict  )->      str:
     _lowercase:			List[str]      =     []
     _lowercase:			Optional[Any]      =     config_dict.copy()
     for key in config_dict.keys():
       if key.startswith('_'  ):
         temp_dict.pop(UpperCamelCase_  )
         meta_keys.append(UpperCamelCase_  )
     return (temp_dict, meta_keys)






   @torch.no_grad()
   def 			_a    (						self :							str    ,       UpperCamelCase_ :							List[Union[str, os.PathLike]]    ,       **UpperCamelCase_ :							Optional[Any]  )->      Any:
     _lowercase:			List[Any]      =     kwargs.pop('cache_dir'    ,       UpperCamelCase_  )
     _lowercase:			Any      =     kwargs.pop('resume_download'    ,       UpperCamelCase_  )
     _lowercase:			Optional[int]      =     kwargs.pop('force_download'    ,       UpperCamelCase_  )
     _lowercase:			Union[str, Any]      =     kwargs.pop('proxies'    ,       UpperCamelCase_  )
     _lowercase:			Union[str, Any]      =     kwargs.pop('local_files_only'    ,       UpperCamelCase_  )
     _lowercase:			Union[str, Any]      =     kwargs.pop('use_auth_token'    ,       UpperCamelCase_  )
     _lowercase:			Optional[int]      =     kwargs.pop('revision'    ,       UpperCamelCase_  )
     _lowercase:			Any      =     kwargs.pop('torch_dtype'    ,       UpperCamelCase_  )
     _lowercase:			Any      =     kwargs.pop('device_map'    ,       UpperCamelCase_  )

     _lowercase:			List[str]      =     kwargs.pop('alpha'    ,       0.5  )
     _lowercase:			List[Any]      =     kwargs.pop('interp'    ,       UpperCamelCase_  )

     print('Received list'    ,       UpperCamelCase_  )
     print(f'Combining with alpha={alpha}, interpolation mode={interp}'  )

     _lowercase:			Any      =     len(UpperCamelCase_  )
     # Ignore result from model_index_json comparision of the two checkpoints
     _lowercase:			Dict      =     kwargs.pop('force'    ,       UpperCamelCase_  )

     # If less than 2 checkpoints, nothing to merge. If more than 3, not supported for now.
     if checkpoint_count > 3 or checkpoint_count < 2:
       raise ValueError(
           'Received incorrect number of checkpoints to merge. Ensure that either 2 or 3 checkpoints are being'
           ' passed.'  )

     print('Received the right number of checkpoints'  )
     # chkpt0, chkpt1 = pretrained_model_name_or_path_list[0:2]
     # chkpt2 = pretrained_model_name_or_path_list[2] if checkpoint_count == 3 else None

     # Validate that the checkpoints can be merged
     # Step 1: Load the model config and compare the checkpoints. We'll compare the model_index.json first while ignoring the keys starting with '_'
     _lowercase:			List[Any]      =     []
     for pretrained_model_name_or_path in pretrained_model_name_or_path_list:
       _lowercase:			Tuple      =     DiffusionPipeline.load_config(
           UpperCamelCase_    ,       cache_dir=UpperCamelCase_    ,       resume_download=UpperCamelCase_    ,       force_download=UpperCamelCase_    ,       proxies=UpperCamelCase_    ,       local_files_only=UpperCamelCase_    ,       use_auth_token=UpperCamelCase_    ,       revision=UpperCamelCase_    ,       )
       config_dicts.append(UpperCamelCase_  )

     _lowercase:			Tuple      =     True
     for idx in range(1    ,       len(UpperCamelCase_  )  ):
       comparison_result &= self._compare_model_configs(config_dicts[idx - 1]    ,       config_dicts[idx]  )
       if not force and comparison_result is False:
         raise ValueError('Incompatible checkpoints. Please check model_index.json for the models.'  )
         print(config_dicts[0]    ,       config_dicts[1]  )
     print('Compatible model_index.json files found'  )
     # Step 2: Basic Validation has succeeded. Let's download the models and save them into our local files.
     _lowercase:			Union[str, Any]      =     []
     for pretrained_model_name_or_path, config_dict in zip(UpperCamelCase_    ,       UpperCamelCase_  ):
       _lowercase:			str      =     [k for k in config_dict.keys() if not k.startswith('_'  )]
       _lowercase:			List[Any]      =     [os.path.join(UpperCamelCase_    ,       '*'  ) for k in folder_names]
       allow_patterns += [
           WEIGHTS_NAME,
           SCHEDULER_CONFIG_NAME,
           CONFIG_NAME,
           ONNX_WEIGHTS_NAME,
           DiffusionPipeline.config_name,
       ]
       _lowercase:			Dict      =     config_dict.get('_class_name'  )
       _lowercase:			Optional[int]      =     {'diffusers': __version__, 'pipeline_class': requested_pipeline_class}

       _lowercase:			Any      =     (
           pretrained_model_name_or_path
           if os.path.isdir(UpperCamelCase_  )
           else snapshot_download(
               UpperCamelCase_    ,       cache_dir=UpperCamelCase_    ,       resume_download=UpperCamelCase_    ,       proxies=UpperCamelCase_    ,       local_files_only=UpperCamelCase_    ,       use_auth_token=UpperCamelCase_    ,       revision=UpperCamelCase_    ,       allow_patterns=UpperCamelCase_    ,       user_agent=UpperCamelCase_    ,       )
       )
       print('Cached Folder'    ,       UpperCamelCase_  )
       cached_folders.append(UpperCamelCase_  )

     # Step 3:-
     # Load the first checkpoint as a diffusion pipeline and modify its module state_dict in place
     _lowercase:			int      =     DiffusionPipeline.from_pretrained(
         cached_folders[0]    ,       torch_dtype=UpperCamelCase_    ,       device_map=UpperCamelCase_  )
     final_pipe.to(self.device  )

     _lowercase:			str      =     None
     if len(UpperCamelCase_  ) > 2:
       _lowercase:			Union[str, Any]      =     os.path.join(cached_folders[2]  )

     if interp == "sigmoid":
       _lowercase:			Any      =     CheckpointMergerPipeline.sigmoid
     elif interp == "inv_sigmoid":
       _lowercase:			Tuple      =     CheckpointMergerPipeline.inv_sigmoid
     elif interp == "add_diff":
       _lowercase:			List[Any]      =     CheckpointMergerPipeline.add_difference
     else:
       _lowercase:			Tuple      =     CheckpointMergerPipeline.weighted_sum

     # Find each module's state dict.
     for attr in final_pipe.config.keys():
       if not attr.startswith('_'  ):
         _lowercase:			Dict      =     os.path.join(cached_folders[1]    ,       UpperCamelCase_  )
         if os.path.exists(UpperCamelCase_  ):
           _lowercase:			Any      =     [
               *glob.glob(os.path.join(UpperCamelCase_    ,       '*.safetensors'  )  ),
               *glob.glob(os.path.join(UpperCamelCase_    ,       '*.bin'  )  ),
           ]
           _lowercase:			Optional[int]      =     files[0] if len(UpperCamelCase_  ) > 0 else None
         if len(UpperCamelCase_  ) < 3:
           _lowercase:			Tuple      =     None
         else:
           _lowercase:			int      =     os.path.join(cached_folders[2]    ,       UpperCamelCase_  )
           if os.path.exists(UpperCamelCase_  ):
             _lowercase:			Dict      =     [
                 *glob.glob(os.path.join(UpperCamelCase_    ,       '*.safetensors'  )  ),
                 *glob.glob(os.path.join(UpperCamelCase_    ,       '*.bin'  )  ),
             ]
             _lowercase:			Optional[int]      =     files[0] if len(UpperCamelCase_  ) > 0 else None
                # For an attr if both checkpoint_path_1 and 2 are None, ignore.
                # If atleast one is present, deal with it according to interp method, of course only if the state_dict keys match.
         if checkpoint_path_a is None and checkpoint_path_a is None:
           print(f'Skipping {attr}: not present in 2nd or 3d model'  )
           continue
         try:
           _lowercase:			Tuple      =     getattr(UpperCamelCase_    ,       UpperCamelCase_  )
           if isinstance(UpperCamelCase_    ,       UpperCamelCase_  ):  # ignore requires_safety_checker boolean
             continue
           _lowercase:			Optional[int]      =     getattr(UpperCamelCase_    ,       'state_dict'  )
           _lowercase:			Union[str, Any]      =     theta_a()

           _lowercase:			Optional[int]      =     getattr(UpperCamelCase_    ,       'load_state_dict'  )
           _lowercase:			Dict      =     (
               safetensors.torch.load_file(UpperCamelCase_  )
               if (is_safetensors_available() and checkpoint_path_a.endswith('.safetensors'  ))
               else torch.load(UpperCamelCase_    ,       map_location='cpu'  )
           )
           _lowercase:			List[str]      =     None
           if checkpoint_path_a:
             _lowercase:			int      =     (
                 safetensors.torch.load_file(UpperCamelCase_  )
                 if (is_safetensors_available() and checkpoint_path_a.endswith('.safetensors'  ))
                 else torch.load(UpperCamelCase_    ,       map_location='cpu'  )
             )

           if not theta_a.keys() == theta_a.keys():
             print(f'Skipping {attr}: key mismatch'  )
             continue
           if theta_a and not theta_a.keys() == theta_a.keys():
             print(f'Skipping {attr}:y mismatch'  )
         except Exception as e:
           print(f'Skipping {attr} do to an unexpected error: {str(UpperCamelCase_  )}'  )
           continue
         print(f'MERGING {attr}'  )

         for key in theta_a.keys():
           if theta_a:
             _lowercase:			Union[str, Any]      =     theta_func(theta_a[key]    ,       theta_a[key]    ,       theta_a[key]    ,       UpperCamelCase_  )
           else:
             _lowercase:			Dict      =     theta_func(theta_a[key]    ,       theta_a[key]    ,       UpperCamelCase_    ,       UpperCamelCase_  )

         del theta_a
         del theta_a
         update_theta_a(UpperCamelCase_  )

         del theta_a
     return final_pipe






   @staticmethod
   def 			_a    (						UpperCamelCase_ :							Optional[int]    ,       UpperCamelCase_ :							Dict    ,       UpperCamelCase_ :							str    ,       UpperCamelCase_ :							Optional[Any]  )->      Dict:
     return ((1 - alpha) * thetaa) + (alpha * thetaa)






   @staticmethod
   def 			_a    (						UpperCamelCase_ :							Any    ,       UpperCamelCase_ :							Union[str, Any]    ,       UpperCamelCase_ :							Union[str, Any]    ,       UpperCamelCase_ :							Dict  )->      str:
     _lowercase:			Union[str, Any]      =     alpha * alpha * (3 - (2 * alpha))
     return thetaa + ((thetaa - thetaa) * alpha)






   @staticmethod
   def 			_a    (						UpperCamelCase_ :							Union[str, Any]    ,       UpperCamelCase_ :							Optional[Any]    ,       UpperCamelCase_ :							int    ,       UpperCamelCase_ :							int  )->      List[str]:
     import math

     _lowercase:			str      =     0.5 - math.sin(math.asin(1.0 - 2.0 * alpha  ) / 3.0  )
     return thetaa + ((thetaa - thetaa) * alpha)






   @staticmethod
   def 			_a    (						UpperCamelCase_ :							int    ,       UpperCamelCase_ :							Any    ,       UpperCamelCase_ :							str    ,       UpperCamelCase_ :							Tuple  )->      int:
     return thetaa + (thetaa - thetaa) * (1.0 - alpha)

import json
import os
from functools import lru_cache
from typing import TYPE_CHECKING, List, Optional, Tuple, Union

import numpy as np
import regex as re

from ...utils import is_tf_available, is_torch_available, logging


if TYPE_CHECKING:
 if is_torch_available():
  import torch
 if is_tf_available():
  import tensorflow as tf

from ...tokenization_utils import AddedToken, PreTrainedTokenizer


UpperCAmelCase      		=					logging.get_logger(__name__)

UpperCAmelCase      		=					{
    '''vocab_file''': '''vocab.json''',
    '''merges_file''': '''merges.txt''',
}

UpperCAmelCase      		=					{
    '''vocab_file''': {
        '''Salesforce/codegen-350M-mono''': '''https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/vocab.json''',
    },
    '''merges_file''': {
        '''Salesforce/codegen-350M-mono''': '''https://huggingface.co/Salesforce/codegen-350M-mono/resolve/main/merges.txt''',
    },
}

UpperCAmelCase      		=					{
    '''Salesforce/codegen-350M-mono''': 20_48,
}


@lru_cache()
def SCREAMING_SNAKE_CASE					(  ):
       _UpperCamelCase  :							Union[str, Any]	    =	(
           list(range(ord('!'					)		,    ord('~'					) + 1					)					) + list(range(ord('¡'					)		,    ord('¬'					) + 1					)					) + list(range(ord('®'					)		,    ord('ÿ'					) + 1					)					)
       )
       _UpperCamelCase  :							List[Any]	    =	bs[:]
       _UpperCamelCase  :							Optional[Any]	    =	0
       for b in range(2**8					):
              if b not in bs:
                     bs.append(lowercase_					)
                     cs.append(2**8 + n					)
                     n += 1
       _UpperCamelCase  :							int	    =	[chr(lowercase_					) for n in cs]
       return dict(zip(lowercase_		,    lowercase_					)					)


def SCREAMING_SNAKE_CASE					(  lowercase_:						Optional[int]					):
       _UpperCamelCase  :							Any	    =	set()
       _UpperCamelCase  :							str	    =	word[0]
       for char in word[1:]:
              pairs.add((prev_char, char)					)
              _UpperCamelCase  :							Optional[int]	    =	char
       return pairs





class  __lowerCAmelCase	(							a_    ):



 '''simple docstring'''
 _lowerCamelCase    :						int					     =			VOCAB_FILES_NAMES
 _lowerCamelCase    :						str					     =			PRETRAINED_VOCAB_FILES_MAP
 _lowerCamelCase    :						str					     =			PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 _lowerCamelCase    :						Dict					     =			['input_ids', 'attention_mask']

 def __init__(      self							:		List[str]							,      lowerCamelCase							:		List[str]							,      lowerCamelCase							:		Tuple							,      lowerCamelCase							:		Union[str, Any]="replace"							,      lowerCamelCase							:		List[str]="<|endoftext|>"							,      lowerCamelCase							:		Union[str, Any]="<|endoftext|>"							,      lowerCamelCase							:		int="<|endoftext|>"							,      lowerCamelCase							:		str=None							,      lowerCamelCase							:		int=False							,      lowerCamelCase							:		Optional[int]=False							,      **lowerCamelCase							:		List[str]							,      )			->				Any:
        _UpperCamelCase  :							List[Any]	    =	AddedToken(lowerCamelCase							,      lstrip=lowerCamelCase							,      rstrip=lowerCamelCase	) if isinstance(lowerCamelCase							,      lowerCamelCase	) else bos_token
        _UpperCamelCase  :							Union[str, Any]	    =	AddedToken(lowerCamelCase							,      lstrip=lowerCamelCase							,      rstrip=lowerCamelCase	) if isinstance(lowerCamelCase							,      lowerCamelCase	) else eos_token
        _UpperCamelCase  :							str	    =	AddedToken(lowerCamelCase							,      lstrip=lowerCamelCase							,      rstrip=lowerCamelCase	) if isinstance(lowerCamelCase							,      lowerCamelCase	) else unk_token
        _UpperCamelCase  :							List[str]	    =	AddedToken(lowerCamelCase							,      lstrip=lowerCamelCase							,      rstrip=lowerCamelCase	) if isinstance(lowerCamelCase							,      lowerCamelCase	) else pad_token
        super().__init__(
            errors=lowerCamelCase							,      unk_token=lowerCamelCase							,      bos_token=lowerCamelCase							,      eos_token=lowerCamelCase							,      pad_token=lowerCamelCase							,      add_prefix_space=lowerCamelCase							,      add_bos_token=lowerCamelCase							,      **lowerCamelCase							,      )
        _UpperCamelCase  :							str	    =	add_bos_token

        with open(lowerCamelCase							,      encoding='utf-8'	) as vocab_handle:
               _UpperCamelCase  :							Tuple	    =	json.load(lowerCamelCase	)
        _UpperCamelCase  :							List[str]	    =	{v: k for k, v in self.encoder.items()}
        _UpperCamelCase  :							Any	    =	errors  # how to handle errors in decoding
        _UpperCamelCase  :							int	    =	bytes_to_unicode()
        _UpperCamelCase  :							Union[str, Any]	    =	{v: k for k, v in self.byte_encoder.items()}
        with open(lowerCamelCase							,      encoding='utf-8'	) as merges_handle:
               _UpperCamelCase  :							Optional[Any]	    =	merges_handle.read().split('\n'	)[1:-1]
        _UpperCamelCase  :							Optional[Any]	    =	[tuple(merge.split()	) for merge in bpe_merges]
        _UpperCamelCase  :							List[str]	    =	dict(zip(lowerCamelCase							,      range(len(lowerCamelCase	)	)	)	)
        _UpperCamelCase  :							Tuple	    =	{}
        _UpperCamelCase  :							Union[str, Any]	    =	add_prefix_space

        # Should have added re.IGNORECASE so BPE merges can happen for capitalized versions of contractions
        _UpperCamelCase  :							Union[str, Any]	    =	re.compile(r'\'s|\'t|\'re|\'ve|\'m|\'ll|\'d| ?\p{L}+| ?\p{N}+| ?[^\s\p{L}\p{N}]+|\s+(?!\S)|\s+'	)

 @property
 def        __snake_case       (      self							:		Union[str, Any]	)			->				Any:
        return len(self.encoder	)

 def        __snake_case       (      self							:		Dict	)			->				int:
        return dict(self.encoder							,      **self.added_tokens_encoder	)

 def        __snake_case       (      self							:		List[Any]							,      lowerCamelCase							:		List[str]	)			->				Dict:
        if token in self.cache:
               return self.cache[token]
        _UpperCamelCase  :							List[str]	    =	tuple(lowerCamelCase	)
        _UpperCamelCase  :							Optional[Any]	    =	get_pairs(lowerCamelCase	)

        if not pairs:
               return token

        while True:
               _UpperCamelCase  :							Optional[int]	    =	min(lowerCamelCase							,      key=lambda lowerCamelCase	: self.bpe_ranks.get(lowerCamelCase							,      float('inf'	)	)	)
               if bigram not in self.bpe_ranks:
                      break
               _UpperCamelCase    ,   _UpperCamelCase  :							List[Any]	    =	bigram
               _UpperCamelCase  :							Optional[Any]	    =	[]
               _UpperCamelCase  :							str	    =	0
               while i < len(lowerCamelCase	):
                      try:
                             _UpperCamelCase  :							Optional[int]	    =	word.index(lowerCamelCase							,      lowerCamelCase	)
                      except ValueError:
                             new_word.extend(word[i:]	)
                             break
                      else:
                             new_word.extend(word[i:j]	)
                             _UpperCamelCase  :							Tuple	    =	j

                      if word[i] == first and i < len(lowerCamelCase	) - 1 and word[i + 1] == second:
                             new_word.append(first + second	)
                             i += 2
                      else:
                             new_word.append(word[i]	)
                             i += 1
               _UpperCamelCase  :							int	    =	tuple(lowerCamelCase	)
               _UpperCamelCase  :							List[Any]	    =	new_word
               if len(lowerCamelCase	) == 1:
                      break
               else:
                      _UpperCamelCase  :							Any	    =	get_pairs(lowerCamelCase	)
        _UpperCamelCase  :							Dict	    =	' '.join(lowerCamelCase	)
        _UpperCamelCase  :							int	    =	word
        return word

 def        __snake_case       (      self							:		str							,      lowerCamelCase							:		Dict							,      lowerCamelCase							:		Tuple=None	)			->				Dict:
        if self.add_bos_token:
               _UpperCamelCase  :							Dict	    =	[self.bos_token_id]
        else:
               _UpperCamelCase  :							Union[str, Any]	    =	[]

        _UpperCamelCase  :							str	    =	bos_token_ids + token_ids_a

        if token_ids_a is None:
               return output

        return output + bos_token_ids + token_ids_a

 def        __snake_case       (      self							:		int							,      lowerCamelCase							:		int	)			->				Optional[Any]:
        _UpperCamelCase  :							List[str]	    =	[]
        for token in re.findall(self.pat							,      lowerCamelCase	):
               _UpperCamelCase  :							Optional[int]	    =	''.join(
                   self.byte_encoder[b] for b in token.encode('utf-8'	)	)  # Maps all our bytes to unicode strings, avoiding control tokens of the BPE (spaces in our case)
               bpe_tokens.extend(bpe_token for bpe_token in self.bpe(lowerCamelCase	).split(' '	)	)
        return bpe_tokens

 def        __snake_case       (      self							:		Any							,      lowerCamelCase							:		Union[str, Any]	)			->				Any:
        return self.encoder.get(lowerCamelCase							,      self.encoder.get(self.unk_token	)	)

 def        __snake_case       (      self							:		int							,      lowerCamelCase							:		List[Any]	)			->				Union[str, Any]:
        return self.decoder.get(lowerCamelCase	)

 def        __snake_case       (      self							:		List[str]							,      lowerCamelCase							:		Any	)			->				Optional[Any]:
        _UpperCamelCase  :							int	    =	''.join(lowerCamelCase	)
        _UpperCamelCase  :							Union[str, Any]	    =	bytearray([self.byte_decoder[c] for c in text]	).decode('utf-8'							,      errors=self.errors	)
        return text

 def        __snake_case       (      self							:		Dict							,      lowerCamelCase							:		str							,      lowerCamelCase							:		Optional[str] = None	)			->				Tuple[str]:
        if not os.path.isdir(lowerCamelCase	):
               logger.error(f"Vocabulary path ({save_directory}) should be a directory"	)
               return
        _UpperCamelCase  :							str	    =	os.path.join(
            lowerCamelCase							,      (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['vocab_file']	)
        _UpperCamelCase  :							Optional[Any]	    =	os.path.join(
            lowerCamelCase							,      (filename_prefix + '-' if filename_prefix else '') + VOCAB_FILES_NAMES['merges_file']	)

        with open(lowerCamelCase							,      'w'							,      encoding='utf-8'	) as f:
               f.write(json.dumps(self.encoder							,      indent=2							,      sort_keys=lowerCamelCase							,      ensure_ascii=lowerCamelCase	) + '\n'	)

        _UpperCamelCase  :							Optional[Any]	    =	0
        with open(lowerCamelCase							,      'w'							,      encoding='utf-8'	) as writer:
               writer.write('#version: 0.2\n'	)
               for bpe_tokens, token_index in sorted(self.bpe_ranks.items()							,      key=lambda lowerCamelCase	: kv[1]	):
                      if index != token_index:
                             logger.warning(
                                 f"Saving vocabulary to {merge_file}: BPE merge indices are not consecutive."
                                 ' Please check that the tokenizer is not corrupted!'	)
                             _UpperCamelCase  :							str	    =	token_index
                      writer.write(' '.join(lowerCamelCase	) + '\n'	)
                      index += 1

        return vocab_file, merge_file

 def        __snake_case       (      self							:		List[str]							,      lowerCamelCase							:		Union[str, Any]							,      lowerCamelCase							:		int=False							,      **lowerCamelCase							:		int	)			->				List[Any]:
        _UpperCamelCase  :							Dict	    =	kwargs.pop('add_prefix_space'							,      self.add_prefix_space	)
        if is_split_into_words or add_prefix_space:
               _UpperCamelCase  :							Any	    =	' ' + text
        return (text, kwargs)

 def        __snake_case       (      self							:		str							,      lowerCamelCase							:		Union[int, List[int], "np.ndarray", "torch.Tensor", "tf.Tensor"]							,      lowerCamelCase							:		bool = False							,      lowerCamelCase							:		bool = None							,      lowerCamelCase							:		Optional[List[str]] = None							,      **lowerCamelCase							:		List[str]							,      )			->				str:
        _UpperCamelCase  :							Optional[int]	    =	super()._decode(
            token_ids=lowerCamelCase							,      skip_special_tokens=lowerCamelCase							,      clean_up_tokenization_spaces=lowerCamelCase							,      **lowerCamelCase							,      )

        if truncate_before_pattern is not None and len(lowerCamelCase	) > 0:
               _UpperCamelCase  :							Optional[Any]	    =	self.truncate(lowerCamelCase							,      lowerCamelCase	)

        return decoded_text

 def        __snake_case       (      self							:		Optional[int]							,      lowerCamelCase							:		Union[str, Any]							,      lowerCamelCase							:		Any	)			->				Optional[int]:
        def find_re(lowerCamelCase							:		Any							,      lowerCamelCase							:		List[Any]							,      lowerCamelCase							:		str	):
               _UpperCamelCase  :							Optional[int]	    =	pattern.search(lowerCamelCase							,      lowerCamelCase	)
               return m.start() if m else -1

        _UpperCamelCase  :							Dict	    =	[re.compile(lowerCamelCase							,      re.MULTILINE	) for pattern in truncate_before_pattern]

        _UpperCamelCase  :							Union[str, Any]	    =	list(re.finditer('^print'							,      lowerCamelCase							,      re.MULTILINE	)	)

        if len(lowerCamelCase	) > 1:
               _UpperCamelCase  :							Tuple	    =	completion[: prints[1].start()]

        _UpperCamelCase  :							Union[str, Any]	    =	list(re.finditer('^def'							,      lowerCamelCase							,      re.MULTILINE	)	)

        if len(lowerCamelCase	) > 1:
               _UpperCamelCase  :							List[str]	    =	completion[: defs[1].start()]

        _UpperCamelCase  :							Tuple	    =	0

        _UpperCamelCase  :							List[str]	    =	[
            pos for pos in [find_re(lowerCamelCase							,      lowerCamelCase							,      lowerCamelCase	) for terminal in terminals] if pos != -1
        ]

        if len(lowerCamelCase	) > 0:
               return completion[: min(lowerCamelCase	)]
        else:
               return completion



