
'''simple docstring'''






from collections import OrderedDict
from typing import TYPE_CHECKING, Any, List, Mapping, Optional

from packaging import version


if TYPE_CHECKING:
					from ... import PreTrainedTokenizer, TensorType

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfigWithPast, PatchingSpec
from ...utils import is_torch_available, logging


A			      =      logging.get_logger(__name__)

A			      =      {
    'bigscience/bloom': 'https://huggingface.co/bigscience/bloom/resolve/main/config.json',
    'bigscience/bloom-560m': 'https://huggingface.co/bigscience/bloom-560m/blob/main/config.json',
    'bigscience/bloom-1b1': 'https://huggingface.co/bigscience/bloom-1b1/blob/main/config.json',
    'bigscience/bloom-1b7': 'https://huggingface.co/bigscience/bloom-1b7/blob/main/config.json',
    'bigscience/bloom-3b': 'https://huggingface.co/bigscience/bloom-3b/blob/main/config.json',
    'bigscience/bloom-7b1': 'https://huggingface.co/bigscience/bloom-7b1/blob/main/config.json',
}




class     __snake_case		(   a__):
					_lowerCAmelCase			     =	'''bloom'''
					_lowerCAmelCase			     =	['''past_key_values''']
					_lowerCAmelCase			     =	{
					    '''num_hidden_layers''': '''n_layer''',
					    '''num_attention_heads''': '''n_head''',
					}
					def __init__(     self,    A=25_0880,    A=64,    A=2,    A=8,    A=1e-5,    A=0.02,    A=True,    A=1,    A=2,    A=False,    A=0.0,    A=0.0,    A=1,    A=False,    **A,    ):

									"""simple docstring"""


									lowerCamelCase					:			List[Any]									= vocab_size
									# Backward compatibility with n_embed kwarg
									lowerCamelCase					:			List[str]									= kwargs.pop('n_embed',    A      )
									lowerCamelCase					:			str									= hidden_size if n_embed is None else n_embed
									lowerCamelCase					:			Optional[int]									= n_layer
									lowerCamelCase					:			List[Any]									= n_head
									lowerCamelCase					:			Any									= layer_norm_epsilon
									lowerCamelCase					:			Tuple									= initializer_range
									lowerCamelCase					:			Dict									= use_cache
									lowerCamelCase					:			str									= pretraining_tp
									lowerCamelCase					:			Tuple									= apply_residual_connection_post_layernorm
									lowerCamelCase					:			List[Any]									= hidden_dropout
									lowerCamelCase					:			Any									= attention_dropout

									lowerCamelCase					:			Dict									= bos_token_id
									lowerCamelCase					:			Dict									= eos_token_id
									lowerCamelCase					:			int									= slow_but_exact

									super().__init__(bos_token_id=A,    eos_token_id=A,    **A      )





class     __snake_case		(   a__):
					_lowerCAmelCase			     =	version.parse('''1.12''')
					def __init__(     self,    A,    A = "default",    A = None,    A = False,    ):

									"""simple docstring"""


									super().__init__(A,    task=A,    patching_specs=A,    use_past=A      )
									if not getattr(self._config,    'pad_token_id',    A      ):
													# TODO: how to do that better?
													lowerCamelCase					:			int									= 0
					@property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									lowerCamelCase					:			str									= OrderedDict({'input_ids': {0: 'batch', 1: 'sequence'}}      )
									if self.use_past:
													# BLOOM stores values on dynamic axis 2. For more details see: https://github.com/huggingface/transformers/pull/18344
													self.fill_with_past_key_values_(A,    direction='inputs',    inverted_values_shape=A      )
													lowerCamelCase					:			Union[str, Any]									= {0: 'batch', 1: 'past_sequence + sequence'}
									else:
													lowerCamelCase					:			Optional[int]									= {0: 'batch', 1: 'sequence'}

									return common_inputs
					@property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return self._config.n_layer
					@property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return self._config.n_head
					@property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return 1e-3
					def     UpperCAmelCase_							(     self,    A,    A = -1,    A = -1,    A = False,    A = None,    ):

									"""simple docstring"""


									lowerCamelCase					:			Optional[int]									= super(A,    self      ).generate_dummy_inputs(
									    A,    batch_size=A,    seq_length=A,    is_pair=A,    framework=A      )

									# We need to order the input in the way they appears in the forward()
									lowerCamelCase					:			Optional[Any]									= OrderedDict({'input_ids': common_inputs['input_ids']}      )

									# Need to add the past_keys
									if self.use_past:
													if not is_torch_available():
																	raise ValueError('Cannot generate dummy past_keys inputs without PyTorch installed.'      )
													else:
																	import torch

																	lowerCamelCase			, lowerCamelCase					:			Tuple									= common_inputs['input_ids'].shape
																	# Not using the same length for past_key_values
																	lowerCamelCase					:			Any									= seqlen + 2
																	lowerCamelCase					:			int									= self._config.hidden_size // self.num_attention_heads
																	lowerCamelCase					:			List[Any]									= (
																	    batch * self.num_attention_heads,
																	    head_dim,
																	    past_key_values_length,
																	)
																	lowerCamelCase					:			List[str]									= (
																	    batch * self.num_attention_heads,
																	    past_key_values_length,
																	    head_dim,
																	)
																	lowerCamelCase					:			Optional[Any]									= [
																	    (torch.zeros(A      ), torch.zeros(A      )) for _ in range(self.num_layers      )
																	]

									lowerCamelCase					:			Any									= common_inputs['attention_mask']
									if self.use_past:
													lowerCamelCase					:			str									= ordered_inputs['attention_mask'].dtype
													lowerCamelCase					:			str									= torch.cat(
													    [ordered_inputs['attention_mask'], torch.ones(A,    A,    dtype=A      )],    dim=1      )

									return ordered_inputs




					@property
					def     UpperCAmelCase_							(     self      ):

									"""simple docstring"""


									return 13


'''simple docstring'''






import json
import os
from typing import Optional

import numpy as np

from ...feature_extraction_utils import BatchFeature
from ...processing_utils import ProcessorMixin
from ...utils import logging
from ...utils.hub import get_file_from_repo
from ..auto import AutoTokenizer


A			      =      logging.get_logger(__name__)




class     __snake_case		(   a__):
					_lowerCAmelCase			     =	'''AutoTokenizer'''
					_lowerCAmelCase			     =	['''tokenizer''']

					_lowerCAmelCase			     =	{
					    '''semantic_prompt''': 1,
					    '''coarse_prompt''': 2,
					    '''fine_prompt''': 2,
					}
					def __init__(     self,    A,    A=None      ):

									"""simple docstring"""


									super().__init__(A      )

									lowerCamelCase					:			Union[str, Any]									= speaker_embeddings
					@classmethod
					def     UpperCAmelCase_							(     cls,    A,    A="speaker_embeddings_path.json",    **A      ):

									"""simple docstring"""


									if speaker_embeddings_dict_path is not None:
													lowerCamelCase					:			List[Any]									= get_file_from_repo(
													    A,    A,    subfolder=kwargs.pop('subfolder',    A      ),    cache_dir=kwargs.pop('cache_dir',    A      ),    force_download=kwargs.pop('force_download',    A      ),    proxies=kwargs.pop('proxies',    A      ),    resume_download=kwargs.pop('resume_download',    A      ),    local_files_only=kwargs.pop('local_files_only',    A      ),    use_auth_token=kwargs.pop('use_auth_token',    A      ),    revision=kwargs.pop('revision',    A      ),    )
													if speaker_embeddings_path is None:
																	logger.warning(
																	    F'''`{os.path.join(A,    A      )}` does not exists
                    , no preloaded speaker embeddings will be used - Make sure to provide a correct path to the json
                    dictionnary if wanted, otherwise set `speaker_embeddings_dict_path=None`.'''      )
																	lowerCamelCase					:			Union[str, Any]									= None
													else:
																	with open(A      ) as speaker_embeddings_json:
																					lowerCamelCase					:			Union[str, Any]									= json.load(A      )
									else:
													lowerCamelCase					:			Dict									= None

									lowerCamelCase					:			int									= AutoTokenizer.from_pretrained(A,    **A      )

									return cls(tokenizer=A,    speaker_embeddings=A      )
					def     UpperCAmelCase_							(     self,    A,    A="speaker_embeddings_path.json",    A="speaker_embeddings",    A = False,    **A,    ):

									"""simple docstring"""


									if self.speaker_embeddings is not None:
													os.makedirs(os.path.join(A,    A,    'v2'      ),    exist_ok=A      )

													lowerCamelCase					:			Any									= {}

													lowerCamelCase					:			Tuple									= save_directory

													for prompt_key in self.speaker_embeddings:
																	if prompt_key != "repo_or_path":
																					lowerCamelCase					:			Optional[int]									= self._load_voice_preset(A      )

																					lowerCamelCase					:			Optional[Any]									= {}
																					for key in self.speaker_embeddings[prompt_key]:
																									np.save(
																									    os.path.join(
																									        embeddings_dict['repo_or_path'],    A,    F'''{prompt_key}_{key}'''      ),    voice_preset[key],    allow_pickle=A,    )
																									lowerCamelCase					:			Tuple									= os.path.join(A,    F'''{prompt_key}_{key}.npy'''      )

																					lowerCamelCase					:			Optional[Any]									= tmp_dict

													with open(os.path.join(A,    A      ),    'w'      ) as fp:
																	json.dump(A,    A      )

									super().save_pretrained(A,    A,    **A      )
					def     UpperCAmelCase_							(     self,    A = None,    **A      ):

									"""simple docstring"""


									lowerCamelCase					:			List[str]									= self.speaker_embeddings[voice_preset]

									lowerCamelCase					:			Optional[Any]									= {}
									for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]:
													if key not in voice_preset_paths:
																	raise ValueError(
																	    F'''Voice preset unrecognized, missing {key} as a key in self.speaker_embeddings[{voice_preset}].'''      )

													lowerCamelCase					:			Optional[Any]									= get_file_from_repo(
													    self.speaker_embeddings.get('repo_or_path',    '/'      ),    voice_preset_paths[key],    subfolder=kwargs.pop('subfolder',    A      ),    cache_dir=kwargs.pop('cache_dir',    A      ),    force_download=kwargs.pop('force_download',    A      ),    proxies=kwargs.pop('proxies',    A      ),    resume_download=kwargs.pop('resume_download',    A      ),    local_files_only=kwargs.pop('local_files_only',    A      ),    use_auth_token=kwargs.pop('use_auth_token',    A      ),    revision=kwargs.pop('revision',    A      ),    )
													if path is None:
																	raise ValueError(
																	    F'''`{os.path.join(self.speaker_embeddings.get('repo_or_path',    '/'      ),    voice_preset_paths[key]      )}` does not exists
                    , no preloaded voice preset will be used - Make sure to provide correct paths to the {voice_preset}
                    embeddings.'''      )

													lowerCamelCase					:			Tuple									= np.load(A      )

									return voice_preset_dict
					def     UpperCAmelCase_							(     self,    A = None      ):

									"""simple docstring"""


									for key in ["semantic_prompt", "coarse_prompt", "fine_prompt"]:
													if key not in voice_preset:
																	raise ValueError(F'''Voice preset unrecognized, missing {key} as a key.'''      )

													if not isinstance(voice_preset[key],    np.ndarray      ):
																	raise ValueError(F'''{key} voice preset must be a {str(self.preset_shape[key]      )}D ndarray.'''      )

													if len(voice_preset[key].shape      ) != self.preset_shape[key]:
																	raise ValueError(F'''{key} voice preset must be a {str(self.preset_shape[key]      )}D ndarray.'''      )




					def __call__(     self,    A=None,    A=None,    A="pt",    A=256,    A=False,    A=True,    A=False,    **A,    ):

									"""simple docstring"""


									if voice_preset is not None and not isinstance(A,    A      ):
													if (
													    isinstance(A,    A      )
													    and self.speaker_embeddings is not None
													    and voice_preset in self.speaker_embeddings
													):
																	lowerCamelCase					:			Dict									= self._load_voice_preset(A      )

													else:
																	if isinstance(A,    A      ) and not voice_preset.endswith('.npz'      ):
																					lowerCamelCase					:			List[Any]									= voice_preset + '.npz'

																	lowerCamelCase					:			Tuple									= np.load(A      )

									if voice_preset is not None:
													self._validate_voice_preset_dict(A,    **A      )
													lowerCamelCase					:			List[Any]									= BatchFeature(data=A,    tensor_type=A      )

									lowerCamelCase					:			str									= self.tokenizer(
									    A,    return_tensors=A,    padding='max_length',    max_length=A,    return_attention_mask=A,    return_token_type_ids=A,    add_special_tokens=A,    **A,    )

									if voice_preset is not None:
													lowerCamelCase					:			Dict									= voice_preset

									return encoded_text
