


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


__snake_case            =   logging.get_logger(__name__)




class    UpperCAmelCase					(       __snake_case    ):
			lowercase						= """AutoTokenizer"""
			lowercase						= ["""tokenizer"""]

			lowercase						= {
			    """semantic_prompt""": 1,
			    """coarse_prompt""": 2,
			    """fine_prompt""": 2,
			}




			def __init__(						self							:		Dict       ,       __magic_name__							:		Optional[Any]       ,       __magic_name__							:		Dict=None						):







						"""simple docstring"""







						super().__init__(__magic_name__						)

						UpperCamelCase         =					speaker_embeddings




			@classmethod
			def   lowerCamelCase_					(						cls							:		List[Any]       ,       __magic_name__							:		List[Any]       ,       __magic_name__							:		List[Any]="speaker_embeddings_path.json"       ,       **__magic_name__							:		Optional[int]						):







						"""simple docstring"""







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

						UpperCamelCase         =					AutoTokenizer.from_pretrained(__magic_name__       ,       **__magic_name__						)

						return cls(tokenizer=__magic_name__       ,       speaker_embeddings=__magic_name__						)




			def   lowerCamelCase_					(						self							:		Dict       ,       __magic_name__							:		Union[str, Any]       ,       __magic_name__							:		int="speaker_embeddings_path.json"       ,       __magic_name__							:		List[str]="speaker_embeddings"       ,       __magic_name__							:		bool = False       ,       **__magic_name__							:		Optional[Any]       ,       ):







						"""simple docstring"""







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

									UpperCamelCase         =					{}

									UpperCamelCase         =					save_directory

									for prompt_key in self.speaker_embeddings:
												if prompt_key != "repo_or_path":
															UpperCamelCase         =					self._load_voice_preset(__magic_name__						)

															UpperCamelCase         =					{}
															for key in self.speaker_embeddings[prompt_key]:
																		np.save(
																		    os.path.join(
																		        embeddings_dict["""repo_or_path"""]       ,       __magic_name__       ,       F'{prompt_key}_{key}'						)       ,       voice_preset[key]       ,       allow_pickle=__magic_name__       ,       )
																		UpperCamelCase         =					os.path.join(__magic_name__       ,       F'{prompt_key}_{key}.npy'						)

															UpperCamelCase         =					tmp_dict

									with open(os.path.join(__magic_name__       ,       __magic_name__						)       ,       """w"""						) as fp:
												json.dump(__magic_name__       ,       __magic_name__						)

						super().save_pretrained(__magic_name__       ,       __magic_name__       ,       **__magic_name__						)




			def   lowerCamelCase_					(						self							:		int       ,       __magic_name__							:		str = None       ,       **__magic_name__							:		str						):







						"""simple docstring"""







						UpperCamelCase         =					self.speaker_embeddings[voice_preset]

						UpperCamelCase         =					{}
						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}].'						)

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

									UpperCamelCase         =					np.load(__magic_name__						)

						return voice_preset_dict




			def   lowerCamelCase_					(						self							:		str       ,       __magic_name__							:		Optional[dict] = 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							:		Optional[int]       ,       __magic_name__							:		Tuple=None       ,       __magic_name__							:		Optional[Any]=None       ,       __magic_name__							:		str="pt"       ,       __magic_name__							:		str=2_5_6       ,       __magic_name__							:		Optional[Any]=False       ,       __magic_name__							:		Dict=True       ,       __magic_name__							:		Union[str, Any]=False       ,       **__magic_name__							:		Dict       ,       ):







						"""simple docstring"""







						if voice_preset is not None and not isinstance(__magic_name__       ,       __magic_name__						):
									if (
									    isinstance(__magic_name__       ,       __magic_name__						)
									    and self.speaker_embeddings is not None
									    and voice_preset in self.speaker_embeddings
									):
												UpperCamelCase         =					self._load_voice_preset(__magic_name__						)

									else:
												if isinstance(__magic_name__       ,       __magic_name__						) and not voice_preset.endswith(""".npz"""						):
															UpperCamelCase         =					voice_preset + """.npz"""

												UpperCamelCase         =					np.load(__magic_name__						)

						if voice_preset is not None:
									self._validate_voice_preset_dict(__magic_name__       ,       **__magic_name__						)
									UpperCamelCase         =					BatchFeature(data=__magic_name__       ,       tensor_type=__magic_name__						)

						UpperCamelCase         =					self.tokenizer(
						    __magic_name__       ,       return_tensors=__magic_name__       ,       padding="""max_length"""       ,       max_length=__magic_name__       ,       return_attention_mask=__magic_name__       ,       return_token_type_ids=__magic_name__       ,       add_special_tokens=__magic_name__       ,       **__magic_name__       ,       )

						if voice_preset is not None:
									UpperCamelCase         =					voice_preset

						return encoded_text








import logging
import os

import quant_trainer
import torch
from torch.utils.data import DataLoader

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput


__snake_case            =   logging.getLogger(__name__)

if is_torch_tpu_available(check_device=False):
			import torch_xla.core.xla_model as xm
			import torch_xla.debug.metrics as met




class    UpperCAmelCase					(       __snake_case    ):




			def __init__(						self							:		Dict       ,       *__magic_name__							:		Union[str, Any]       ,       __magic_name__							:		Any=None       ,       __magic_name__							:		Union[str, Any]=None       ,       __magic_name__							:		Any=None       ,       **__magic_name__							:		Optional[Any]						):







						"""simple docstring"""







						super().__init__(*__magic_name__       ,       **__magic_name__						)
						UpperCamelCase         =					eval_examples
						UpperCamelCase         =					post_process_function
						UpperCamelCase         =					quant_trainer_args
						UpperCamelCase         =					1_2_8  # default number of calibration samples




			def   lowerCamelCase_					(						self							:		str       ,       __magic_name__							:		List[Any]=None						):







						"""simple docstring"""







						if calib_dataset is None and self.calib_dataset is None:
									raise ValueError("""Trainer: calibration requires an calib_dataset."""						)
						UpperCamelCase         =					calib_dataset if calib_dataset is not None else self.calib_dataset

						UpperCamelCase         =					self._remove_unused_columns(__magic_name__       ,       description="""Calibration"""						)

						return DataLoader(
						    __magic_name__       ,       batch_size=self.args.eval_batch_size       ,       collate_fn=self.data_collator       ,       drop_last=self.args.dataloader_drop_last       ,       num_workers=self.args.dataloader_num_workers       ,       pin_memory=self.args.dataloader_pin_memory       ,       shuffle=__magic_name__       ,       )




			def   lowerCamelCase_					(						self							:		Tuple       ,       __magic_name__							:		List[str]=None						):







						"""simple docstring"""







						UpperCamelCase         =					self.train_dataset if calib_dataset is None else calib_dataset
						UpperCamelCase         =					self.get_calib_dataloader(__magic_name__						)

						UpperCamelCase         =					self.model
						quant_trainer.configure_model(__magic_name__       ,       self.quant_trainer_args       ,       calib=__magic_name__						)
						model.eval()
						quant_trainer.enable_calibration(__magic_name__						)

						logger.info("""***** Running calibration *****"""						)
						logger.info(F'  Num examples = {self.calib_num}'						)
						logger.info(F'  Batch size = {calib_dataloader.batch_size}'						)

						for step, inputs in enumerate(__magic_name__						):
									# Prediction step
									UpperCamelCase  ,  UpperCamelCase  ,  UpperCamelCase         =					self.prediction_step(__magic_name__       ,       __magic_name__       ,       prediction_loss_only=__magic_name__						)
									if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
												break

						quant_trainer.finish_calibration(__magic_name__       ,       self.quant_trainer_args						)
						UpperCamelCase         =					model




			def   lowerCamelCase_					(						self							:		Any       ,       __magic_name__							:		Optional[int]=None       ,       __magic_name__							:		Optional[Any]=None       ,       __magic_name__							:		Union[str, Any]=None       ,       __magic_name__							:		str = "eval"						):







						"""simple docstring"""







						UpperCamelCase         =					self.eval_dataset if eval_dataset is None else eval_dataset
						UpperCamelCase         =					self.get_eval_dataloader(__magic_name__						)
						UpperCamelCase         =					self.eval_examples if eval_examples is None else eval_examples

						# Temporarily disable metric computation, we will do it in the loop here.
						UpperCamelCase         =					self.compute_metrics
						UpperCamelCase         =					None
						UpperCamelCase         =					self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
						try:
									UpperCamelCase         =					eval_loop(
									    __magic_name__       ,       description="""Evaluation"""       ,       prediction_loss_only=True if compute_metrics is None else None       ,       ignore_keys=__magic_name__       ,       )
						finally:
									UpperCamelCase         =					compute_metrics

						if self.post_process_function is not None and self.compute_metrics is not None:
									UpperCamelCase         =					self.post_process_function(__magic_name__       ,       __magic_name__       ,       output.predictions						)
									UpperCamelCase         =					self.compute_metrics(__magic_name__						)

									# Prefix all keys with metric_key_prefix + '_'
									for key in list(metrics.keys()						):
												if not key.startswith(F'{metric_key_prefix}_'						):
															UpperCamelCase         =					metrics.pop(__magic_name__						)

									self.log(__magic_name__						)
						else:
									UpperCamelCase         =					{}

						if self.args.tpu_metrics_debug or self.args.debug:
									# tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
									xm.master_print(met.metrics_report()						)

						UpperCamelCase         =					self.callback_handler.on_evaluate(self.args       ,       self.state       ,       self.control       ,       __magic_name__						)
						return metrics




			def   lowerCamelCase_					(						self							:		Dict       ,       __magic_name__							:		List[str]       ,       __magic_name__							:		List[Any]       ,       __magic_name__							:		List[Any]=None       ,       __magic_name__							:		str = "test"						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_test_dataloader(__magic_name__						)

						# Temporarily disable metric computation, we will do it in the loop here.
						UpperCamelCase         =					self.compute_metrics
						UpperCamelCase         =					None
						UpperCamelCase         =					self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
						try:
									UpperCamelCase         =					eval_loop(
									    __magic_name__       ,       description="""Prediction"""       ,       prediction_loss_only=True if compute_metrics is None else None       ,       ignore_keys=__magic_name__       ,       )
						finally:
									UpperCamelCase         =					compute_metrics

						if self.post_process_function is None or self.compute_metrics is None:
									return output

						UpperCamelCase         =					self.post_process_function(__magic_name__       ,       __magic_name__       ,       output.predictions       ,       """predict"""						)
						UpperCamelCase         =					self.compute_metrics(__magic_name__						)

						# Prefix all keys with metric_key_prefix + '_'
						for key in list(metrics.keys()						):
									if not key.startswith(F'{metric_key_prefix}_'						):
												UpperCamelCase         =					metrics.pop(__magic_name__						)

						return PredictionOutput(predictions=predictions.predictions       ,       label_ids=predictions.label_ids       ,       metrics=__magic_name__						)





			def   lowerCamelCase_					(						self							:		List[str]       ,       __magic_name__							:		Optional[Any]="./"						):







						"""simple docstring"""







						UpperCamelCase         =					self.eval_dataset
						UpperCamelCase         =					self.get_eval_dataloader(__magic_name__						)

						UpperCamelCase         =					next(iter(__magic_name__						)						)

						# saving device - to make it consistent
						UpperCamelCase         =					torch.device("""cuda""" if torch.cuda.is_available() else """cpu"""						)

						# convert to tuple
						UpperCamelCase         =					tuple(v.to(__magic_name__						) for k, v in batch.items()						)

						logger.info("""Converting model to be onnx compatible"""						)
						from pytorch_quantization.nn import TensorQuantizer

						UpperCamelCase         =					True

						UpperCamelCase         =					self.model.to(__magic_name__						)

						model.eval()
						model.float()

						UpperCamelCase         =					model.module if hasattr(__magic_name__       ,       """module"""						) else model
						quant_trainer.configure_model(__magic_name__       ,       self.quant_trainer_args						)

						UpperCamelCase         =					os.path.join(__magic_name__       ,       """model.onnx"""						)
						logger.info(F'exporting model to {output_model_file}'						)

						UpperCamelCase         =					{0: """batch_size""", 1: """seq_len"""}

						torch.onnx.export(
						    __magic_name__       ,       __magic_name__       ,       __magic_name__       ,       export_params=__magic_name__       ,       opset_version=1_3       ,       do_constant_folding=__magic_name__       ,       input_names=["""input_ids""", """attention_mask""", """token_type_ids"""]       ,       output_names=["""output_start_logits""", """output_end_logits"""]       ,       dynamic_axes={
						        """input_ids""": axes,
						        """attention_mask""": axes,
						        """token_type_ids""": axes,
						        """output_start_logits""": axes,
						        """output_end_logits""": axes,
						    }       ,       verbose=__magic_name__       ,       )
						logger.info("""onnx export finished"""						)




