

'''simple docstring'''





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


UpperCamelCase_    						=       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 		__SCREAMING_SNAKE_CASE							(			lowercase__      ):







				def __init__(       self     :		Optional[int]		,					*UpperCAmelCase__     :		Any		,					UpperCAmelCase__     :		int=None		,					UpperCAmelCase__     :		Tuple=None		,					UpperCAmelCase__     :		Optional[int]=None		,					**UpperCAmelCase__     :		Tuple   ):
										'''simple docstring'''
										super().__init__(*UpperCAmelCase__		,					**UpperCAmelCase__   )
										lowercase :							Any    		=eval_examples
										lowercase :							Optional[int]    		=post_process_function
										lowercase :							List[Any]    		=quant_trainer_args
										lowercase :							Optional[int]    		=128  # default number of calibration samples







				def  lowerCamelCase_	(       self     :		str		,					UpperCAmelCase__     :		Dict=None   ):
										'''simple docstring'''
										if calib_dataset is None and self.calib_dataset is None:
																raise ValueError('''Trainer: calibration requires an calib_dataset.'''   )
										lowercase :							Tuple    		=calib_dataset if calib_dataset is not None else self.calib_dataset

										lowercase :							Union[str, Any]    		=self._remove_unused_columns(UpperCAmelCase__		,					description='''Calibration'''   )

										return DataLoader(
										    UpperCAmelCase__		,					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=UpperCAmelCase__		,					)







				def  lowerCamelCase_	(       self     :		Optional[int]		,					UpperCAmelCase__     :		List[Any]=None   ):
										'''simple docstring'''
										lowercase :							Optional[int]    		=self.train_dataset if calib_dataset is None else calib_dataset
										lowercase :							List[Any]    		=self.get_calib_dataloader(UpperCAmelCase__   )

										lowercase :							Optional[int]    		=self.model
										quant_trainer.configure_model(UpperCAmelCase__		,					self.quant_trainer_args		,					calib=UpperCAmelCase__   )
										model.eval()
										quant_trainer.enable_calibration(UpperCAmelCase__   )

										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(UpperCAmelCase__   ):
																# Prediction step
																lowercase		,	lowercase		,	lowercase :							Optional[int]    		=self.prediction_step(UpperCAmelCase__		,					UpperCAmelCase__		,					prediction_loss_only=UpperCAmelCase__   )
																if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
																						break

										quant_trainer.finish_calibration(UpperCAmelCase__		,					self.quant_trainer_args   )
										lowercase :							int    		=model







				def  lowerCamelCase_	(       self     :		int		,					UpperCAmelCase__     :		str=None		,					UpperCAmelCase__     :		List[str]=None		,					UpperCAmelCase__     :		Tuple=None		,					UpperCAmelCase__     :		str = "eval"   ):
										'''simple docstring'''
										lowercase :							Tuple    		=self.eval_dataset if eval_dataset is None else eval_dataset
										lowercase :							str    		=self.get_eval_dataloader(UpperCAmelCase__   )
										lowercase :							str    		=self.eval_examples if eval_examples is None else eval_examples

										# Temporarily disable metric computation, we will do it in the loop here.
										lowercase :							int    		=self.compute_metrics
										lowercase :							Optional[Any]    		=None
										lowercase :							Tuple    		=self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
										try:
																lowercase :							Any    		=eval_loop(
																    UpperCAmelCase__		,					description='''Evaluation'''		,					prediction_loss_only=True if compute_metrics is None else None		,					ignore_keys=UpperCAmelCase__		,					)
										finally:
																lowercase :							Dict    		=compute_metrics

										if self.post_process_function is not None and self.compute_metrics is not None:
																lowercase :							int    		=self.post_process_function(UpperCAmelCase__		,					UpperCAmelCase__		,					output.predictions   )
																lowercase :							Union[str, Any]    		=self.compute_metrics(UpperCAmelCase__   )

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

																self.log(UpperCAmelCase__   )
										else:
																lowercase :							int    		={}

										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()   )

										lowercase :							List[Any]    		=self.callback_handler.on_evaluate(self.args		,					self.state		,					self.control		,					UpperCAmelCase__   )
										return metrics







				def  lowerCamelCase_	(       self     :		List[str]		,					UpperCAmelCase__     :		Union[str, Any]		,					UpperCAmelCase__     :		Dict		,					UpperCAmelCase__     :		Union[str, Any]=None		,					UpperCAmelCase__     :		str = "test"   ):
										'''simple docstring'''
										lowercase :							str    		=self.get_test_dataloader(UpperCAmelCase__   )

										# Temporarily disable metric computation, we will do it in the loop here.
										lowercase :							List[Any]    		=self.compute_metrics
										lowercase :							List[Any]    		=None
										lowercase :							Optional[Any]    		=self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
										try:
																lowercase :							Any    		=eval_loop(
																    UpperCAmelCase__		,					description='''Prediction'''		,					prediction_loss_only=True if compute_metrics is None else None		,					ignore_keys=UpperCAmelCase__		,					)
										finally:
																lowercase :							Union[str, Any]    		=compute_metrics

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

										lowercase :							str    		=self.post_process_function(UpperCAmelCase__		,					UpperCAmelCase__		,					output.predictions		,					'''predict'''   )
										lowercase :							Dict    		=self.compute_metrics(UpperCAmelCase__   )

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

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







				def  lowerCamelCase_	(       self     :		Optional[Any]		,					UpperCAmelCase__     :		int="./"   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=self.eval_dataset
										lowercase :							Any    		=self.get_eval_dataloader(UpperCAmelCase__   )

										lowercase :							Tuple    		=next(iter(UpperCAmelCase__   )   )

										# saving device - to make it consistent
										lowercase :							int    		=torch.device('''cuda''' if torch.cuda.is_available() else '''cpu'''   )

										# convert to tuple
										lowercase :							Optional[Any]    		=tuple(v.to(UpperCAmelCase__   ) for k, v in batch.items()   )

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

										lowercase :							List[Any]    		=True

										lowercase :							Optional[int]    		=self.model.to(UpperCAmelCase__   )

										model.eval()
										model.float()

										lowercase :							List[Any]    		=model.module if hasattr(UpperCAmelCase__		,					'''module'''   ) else model
										quant_trainer.configure_model(UpperCAmelCase__		,					self.quant_trainer_args   )

										lowercase :							List[str]    		=os.path.join(UpperCAmelCase__		,					'''model.onnx'''   )
										logger.info(F'''exporting model to {output_model_file}'''   )

										lowercase :							Dict    		={0: '''batch_size''', 1: '''seq_len'''}

										torch.onnx.export(
										    UpperCAmelCase__		,					UpperCAmelCase__		,					UpperCAmelCase__		,					export_params=UpperCAmelCase__		,					opset_version=13		,					do_constant_folding=UpperCAmelCase__		,					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=UpperCAmelCase__		,					)
										logger.info('''onnx export finished'''   )








'''simple docstring'''





from dataclasses import asdict, dataclass
from typing import Optional

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


UpperCamelCase_    						=       logging.get_logger(__name__)

# TODO Update this
UpperCamelCase_    						=       {
    """facebook/esm-1b""": """https://huggingface.co/facebook/esm-1b/resolve/main/config.json""",
    # See all ESM models at https://huggingface.co/models?filter=esm
}





class 		__SCREAMING_SNAKE_CASE							(			lowercase__      ):
				lowerCamelCase_		       =    'esm'







				def __init__(       self     :		Optional[Any]		,					UpperCAmelCase__     :		str=None		,					UpperCAmelCase__     :		Dict=None		,					UpperCAmelCase__     :		Any=None		,					UpperCAmelCase__     :		int=768		,					UpperCAmelCase__     :		Union[str, Any]=12		,					UpperCAmelCase__     :		List[Any]=12		,					UpperCAmelCase__     :		Optional[Any]=3072		,					UpperCAmelCase__     :		Dict=0.1		,					UpperCAmelCase__     :		int=0.1		,					UpperCAmelCase__     :		int=1026		,					UpperCAmelCase__     :		Tuple=0.02		,					UpperCAmelCase__     :		Optional[int]=1E-12		,					UpperCAmelCase__     :		List[Any]="absolute"		,					UpperCAmelCase__     :		int=True		,					UpperCAmelCase__     :		Dict=None		,					UpperCAmelCase__     :		str=False		,					UpperCAmelCase__     :		int=False		,					UpperCAmelCase__     :		Optional[Any]=None		,					UpperCAmelCase__     :		List[str]=None		,					**UpperCAmelCase__     :		int		,					):
										'''simple docstring'''
										super().__init__(pad_token_id=UpperCAmelCase__		,					mask_token_id=UpperCAmelCase__		,					**UpperCAmelCase__   )

										lowercase :							Any    		=vocab_size
										lowercase :							List[Any]    		=hidden_size
										lowercase :							Any    		=num_hidden_layers
										lowercase :							Optional[Any]    		=num_attention_heads
										lowercase :							Tuple    		=intermediate_size
										lowercase :							int    		=hidden_dropout_prob
										lowercase :							Dict    		=attention_probs_dropout_prob
										lowercase :							Optional[int]    		=max_position_embeddings
										lowercase :							Union[str, Any]    		=initializer_range
										lowercase :							Tuple    		=layer_norm_eps
										lowercase :							Union[str, Any]    		=position_embedding_type
										lowercase :							List[Any]    		=use_cache
										lowercase :							Dict    		=emb_layer_norm_before
										lowercase :							Optional[Any]    		=token_dropout
										lowercase :							Union[str, Any]    		=is_folding_model
										if is_folding_model:
																if esmfold_config is None:
																						logger.info('''No esmfold_config supplied for folding model, using default values.'''   )
																						lowercase :							Any    		=EsmFoldConfig()
																elif isinstance(UpperCAmelCase__		,					UpperCAmelCase__   ):
																						lowercase :							Optional[int]    		=EsmFoldConfig(**UpperCAmelCase__   )
																lowercase :							Union[str, Any]    		=esmfold_config
																if vocab_list is None:
																						logger.warning('''No vocab_list supplied for folding model, assuming the ESM-2 vocabulary!'''   )
																						lowercase :							int    		=get_default_vocab_list()
																else:
																						lowercase :							Tuple    		=vocab_list
										else:
																lowercase :							Union[str, Any]    		=None
																lowercase :							Dict    		=None
										if self.esmfold_config is not None and getattr(self.esmfold_config		,					'''use_esm_attn_map'''		,					UpperCAmelCase__   ):
																raise ValueError('''The HuggingFace port of ESMFold does not support use_esm_attn_map at this time!'''   )







				def  lowerCamelCase_	(       self     :		Union[str, Any]   ):
										'''simple docstring'''
										lowercase :							Union[str, Any]    		=super().to_dict()
										if isinstance(self.esmfold_config		,					UpperCAmelCase__   ):
																lowercase :							Optional[Any]    		=self.esmfold_config.to_dict()
										return output





@dataclass
class 		__SCREAMING_SNAKE_CASE							:
				lowerCamelCase_		       =    None
				lowerCamelCase_		       =    True
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    0

				lowerCamelCase_		       =    True
				lowerCamelCase_		       =    False

				lowerCamelCase_		       =    1_28
				lowerCamelCase_		       =    None







				def  lowerCamelCase_	(       self     :		Tuple   ):
										'''simple docstring'''
										if self.trunk is None:
																lowercase :							str    		=TrunkConfig()
										elif isinstance(self.trunk		,					UpperCAmelCase__   ):
																lowercase :							int    		=TrunkConfig(**self.trunk   )







				def  lowerCamelCase_	(       self     :		List[Any]   ):
										'''simple docstring'''
										lowercase :							str    		=asdict(self   )
										lowercase :							Union[str, Any]    		=self.trunk.to_dict()
										return output





@dataclass
class 		__SCREAMING_SNAKE_CASE							:
				lowerCamelCase_		       =    48
				lowerCamelCase_		       =    10_24
				lowerCamelCase_		       =    1_28
				lowerCamelCase_		       =    32
				lowerCamelCase_		       =    32
				lowerCamelCase_		       =    32
				lowerCamelCase_		       =    0
				lowerCamelCase_		       =    0
				lowerCamelCase_		       =    False
				lowerCamelCase_		       =    4
				lowerCamelCase_		       =    1_28
				lowerCamelCase_		       =    None







				def  lowerCamelCase_	(       self     :		List[str]   ):
										'''simple docstring'''
										if self.structure_module is None:
																lowercase :							Any    		=StructureModuleConfig()
										elif isinstance(self.structure_module		,					UpperCAmelCase__   ):
																lowercase :							Union[str, Any]    		=StructureModuleConfig(**self.structure_module   )

										if self.max_recycles <= 0:
																raise ValueError(F'''`max_recycles` should be positive, got {self.max_recycles}.'''   )
										if self.sequence_state_dim % self.sequence_state_dim != 0:
																raise ValueError(
																    '''`sequence_state_dim` should be a round multiple of `sequence_state_dim`, got'''
																    F''' {self.sequence_state_dim} and {self.sequence_state_dim}.'''   )
										if self.pairwise_state_dim % self.pairwise_state_dim != 0:
																raise ValueError(
																    '''`pairwise_state_dim` should be a round multiple of `pairwise_state_dim`, got'''
																    F''' {self.pairwise_state_dim} and {self.pairwise_state_dim}.'''   )

										lowercase :							str    		=self.sequence_state_dim // self.sequence_head_width
										lowercase :							int    		=self.pairwise_state_dim // self.pairwise_head_width

										if self.sequence_state_dim != sequence_num_heads * self.sequence_head_width:
																raise ValueError(
																    '''`sequence_state_dim` should be equal to `sequence_num_heads * sequence_head_width, got'''
																    F''' {self.sequence_state_dim} != {sequence_num_heads} * {self.sequence_head_width}.'''   )
										if self.pairwise_state_dim != pairwise_num_heads * self.pairwise_head_width:
																raise ValueError(
																    '''`pairwise_state_dim` should be equal to `pairwise_num_heads * pairwise_head_width, got'''
																    F''' {self.pairwise_state_dim} != {pairwise_num_heads} * {self.pairwise_head_width}.'''   )
										if self.pairwise_state_dim % 2 != 0:
																raise ValueError(F'''`pairwise_state_dim` should be even, got {self.pairwise_state_dim}.'''   )

										if self.dropout >= 0.4:
																raise ValueError(F'''`dropout` should not be greater than 0.4, got {self.dropout}.'''   )







				def  lowerCamelCase_	(       self     :		Any   ):
										'''simple docstring'''
										lowercase :							List[Any]    		=asdict(self   )
										lowercase :							Any    		=self.structure_module.to_dict()
										return output





@dataclass
class 		__SCREAMING_SNAKE_CASE							:
				lowerCamelCase_		       =    3_84
				lowerCamelCase_		       =    1_28
				lowerCamelCase_		       =    16
				lowerCamelCase_		       =    1_28
				lowerCamelCase_		       =    12
				lowerCamelCase_		       =    4
				lowerCamelCase_		       =    8
				lowerCamelCase_		       =    0.1
				lowerCamelCase_		       =    8
				lowerCamelCase_		       =    1
				lowerCamelCase_		       =    2
				lowerCamelCase_		       =    7
				lowerCamelCase_		       =    10
				lowerCamelCase_		       =    1E-8
				lowerCamelCase_		       =    1E5







				def  lowerCamelCase_	(       self     :		Dict   ):
										'''simple docstring'''
										return asdict(self   )






def 				_lowerCAmelCase			(   )				->  Optional[int]:
						return (
						    "<cls>",
						    "<pad>",
						    "<eos>",
						    "<unk>",
						    "L",
						    "A",
						    "G",
						    "V",
						    "S",
						    "E",
						    "R",
						    "T",
						    "I",
						    "D",
						    "P",
						    "K",
						    "Q",
						    "N",
						    "F",
						    "Y",
						    "M",
						    "H",
						    "W",
						    "C",
						    "X",
						    "B",
						    "U",
						    "Z",
						    "O",
						    ".",
						    "-",
						    "<null_1>",
						    "<mask>",
						)





