




import re
import time
from typing import Optional

import IPython.display as disp

from ..trainer_callback import TrainerCallback
from ..trainer_utils import IntervalStrategy, has_length

def 			a   (_lowerCAmelCase     ):
							SCREAMING_SNAKE_CASE_   	=    int(_lowerCAmelCase     )
							SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_	,	SCREAMING_SNAKE_CASE_   	=    t // 3_6_0_0, (t // 6_0) % 6_0, t % 6_0
							return F"{h}:{m:02d}:{s:02d}" if h != 0 else F"{m:02d}:{s:02d}"

def 			a   (_lowerCAmelCase  ,    _lowerCAmelCase  ,    _lowerCAmelCase  ,    _lowerCAmelCase  ,    _lowerCAmelCase=3_0_0     ):
							# docstyle-ignore
							return F"\n    <div>\n      {prefix}\n      <progress value='{value}' max='{total}' style='width:{width}px; height:20px; vertical-align: middle;'></progress>\n      {label}\n    </div>\n    "

def 			a   (_lowerCAmelCase     ):
							SCREAMING_SNAKE_CASE_   	=    '''<table border="1" class="dataframe">\n'''
							html_code += """  <thead>\n <tr style="text-align: left;">\n"""
							for i in items[0]:
														html_code += F"      <th>{i}</th>\n"
							html_code += "    </tr>\n  </thead>\n  <tbody>\n"
							for line in items[1:]:
														html_code += "    <tr>\n"
														for elt in line:
																					SCREAMING_SNAKE_CASE_   	=    F"{elt:.6f}" if isinstance(_lowerCAmelCase  ,    _lowerCAmelCase     ) else str(_lowerCAmelCase     )
																					html_code += F"      <td>{elt}</td>\n"
														html_code += "    </tr>\n"
							html_code += "  </tbody>\n</table><p>"
							return html_code






class  __magic_name__						:
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							Union[str, Any]	      =					5
				SCREAMING_SNAKE_CASE__					:							int	      =					0.2


				def __init__(							self:   str		,					_lowerCamelCase:   int		,					_lowerCamelCase:   Optional[str] = None		,					_lowerCamelCase:   bool = True		,					_lowerCamelCase:   Optional["NotebookTrainingTracker"] = None		,					_lowerCamelCase:   int = 3_00		,					)     ->   Union[str, Any]:
											SCREAMING_SNAKE_CASE_   	=    total
											SCREAMING_SNAKE_CASE_   	=    '''''' if prefix is None else prefix
											SCREAMING_SNAKE_CASE_   	=    leave
											SCREAMING_SNAKE_CASE_   	=    parent
											SCREAMING_SNAKE_CASE_   	=    width
											SCREAMING_SNAKE_CASE_   	=    None
											SCREAMING_SNAKE_CASE_   	=    None
											SCREAMING_SNAKE_CASE_   	=    None


				def     _A							(							self:   List[Any]		,					_lowerCamelCase:   int		,					_lowerCamelCase:   bool = False		,					_lowerCamelCase:   str = None		)     ->   Dict:
											SCREAMING_SNAKE_CASE_   	=    value
											if comment is not None:
																		SCREAMING_SNAKE_CASE_   	=    comment
											if self.last_value is None:
																		SCREAMING_SNAKE_CASE_   	=    SCREAMING_SNAKE_CASE_   	=    time.time()
																		SCREAMING_SNAKE_CASE_   	=    SCREAMING_SNAKE_CASE_   	=    value
																		SCREAMING_SNAKE_CASE_   	=    SCREAMING_SNAKE_CASE_   	=    None
																		SCREAMING_SNAKE_CASE_   	=    self.warmup
																		SCREAMING_SNAKE_CASE_   	=    1
																		self.update_bar(_lowerCamelCase		)
											elif value <= self.last_value and not force_update:
																		return
											elif force_update or self.first_calls > 0 or value >= min(self.last_value + self.wait_for		,					self.total		):
																		if self.first_calls > 0:
																									self.first_calls -= 1
																		SCREAMING_SNAKE_CASE_   	=    time.time()
																		SCREAMING_SNAKE_CASE_   	=    current_time - self.start_time
																		# We could have value = self.start_value if the update is called twixe with the same start value.
																		if value > self.start_value:
																									SCREAMING_SNAKE_CASE_   	=    self.elapsed_time / (value - self.start_value)
																		else:
																									SCREAMING_SNAKE_CASE_   	=    None
																		if value >= self.total:
																									SCREAMING_SNAKE_CASE_   	=    self.total
																									SCREAMING_SNAKE_CASE_   	=    None
																									if not self.leave:
																																self.close()
																		elif self.average_time_per_item is not None:
																									SCREAMING_SNAKE_CASE_   	=    self.average_time_per_item * (self.total - value)
																		self.update_bar(_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    value
																		SCREAMING_SNAKE_CASE_   	=    current_time
																		if self.average_time_per_item is None:
																									SCREAMING_SNAKE_CASE_   	=    1
																		else:
																									SCREAMING_SNAKE_CASE_   	=    max(int(self.update_every / self.average_time_per_item		)		,					1		)


				def     _A							(							self:   int		,					_lowerCamelCase:   Any		,					_lowerCamelCase:   List[str]=None		)     ->   Optional[int]:
											SCREAMING_SNAKE_CASE_   	=    ''' ''' * (len(str(self.total		)		) - len(str(_lowerCamelCase		)		)) + str(_lowerCamelCase		)
											if self.elapsed_time is None:
																		SCREAMING_SNAKE_CASE_   	=    f"[{spaced_value}/{self.total} : < :"
											elif self.predicted_remaining is None:
																		SCREAMING_SNAKE_CASE_   	=    f"[{spaced_value}/{self.total} {format_time(self.elapsed_time		)}"
											else:
																		SCREAMING_SNAKE_CASE_   	=    (
																		    f"[{spaced_value}/{self.total} {format_time(self.elapsed_time		)} <"
																		    f" {format_time(self.predicted_remaining		)}"
																		)
																		self.label += f", {1/self.average_time_per_item:.2f} it/s"
											self.label += "]" if self.comment is None or len(self.comment		) == 0 else f", {self.comment}]"
											self.display()


				def     _A							(							self:   List[Any]		)     ->   Optional[int]:
											SCREAMING_SNAKE_CASE_   	=    html_progress_bar(self.value		,					self.total		,					self.prefix		,					self.label		,					self.width		)
											if self.parent is not None:
																		# If this is a child bar, the parent will take care of the display.
																		self.parent.display()
																		return
											if self.output is None:
																		SCREAMING_SNAKE_CASE_   	=    disp.display(disp.HTML(self.html_code		)		,					display_id=_lowerCamelCase		)
											else:
																		self.output.update(disp.HTML(self.html_code		)		)





				def     _A							(							self:   List[str]		)     ->   List[Any]:
											if self.parent is None and self.output is not None:
																		self.output.update(disp.HTML(''''''		)		)






class  __magic_name__						(  __UpperCAmelCase):
				'''simple docstring'''







				def __init__(							self:   Dict		,					_lowerCamelCase:   Union[str, Any]		,					_lowerCamelCase:   Optional[int]=None		)     ->   Optional[int]:
											super().__init__(_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    None if column_names is None else [column_names]
											SCREAMING_SNAKE_CASE_   	=    None


				def     _A							(							self:   Optional[int]		)     ->   List[Any]:
											SCREAMING_SNAKE_CASE_   	=    html_progress_bar(self.value		,					self.total		,					self.prefix		,					self.label		,					self.width		)
											if self.inner_table is not None:
																		self.html_code += text_to_html_table(self.inner_table		)
											if self.child_bar is not None:
																		self.html_code += self.child_bar.html_code
											if self.output is None:
																		SCREAMING_SNAKE_CASE_   	=    disp.display(disp.HTML(self.html_code		)		,					display_id=_lowerCamelCase		)
											else:
																		self.output.update(disp.HTML(self.html_code		)		)


				def     _A							(							self:   List[Any]		,					_lowerCamelCase:   Any		)     ->   Union[str, Any]:
											if self.inner_table is None:
																		SCREAMING_SNAKE_CASE_   	=    [list(values.keys()		), list(values.values()		)]
											else:
																		SCREAMING_SNAKE_CASE_   	=    self.inner_table[0]
																		if len(self.inner_table		) == 1:
																									# We give a chance to update the column names at the first iteration
																									for key in values.keys():
																																if key not in columns:
																																							columns.append(_lowerCamelCase		)
																									SCREAMING_SNAKE_CASE_   	=    columns
																		self.inner_table.append([values[c] for c in columns]		)


				def     _A							(							self:   List[Any]		,					_lowerCamelCase:   Optional[Any]		,					_lowerCamelCase:   Union[str, Any]=None		,					_lowerCamelCase:   Any=3_00		)     ->   Tuple:
											SCREAMING_SNAKE_CASE_   	=    NotebookProgressBar(_lowerCamelCase		,					prefix=_lowerCamelCase		,					parent=self		,					width=_lowerCamelCase		)
											return self.child_bar





				def     _A							(							self:   Union[str, Any]		)     ->   Optional[int]:
											SCREAMING_SNAKE_CASE_   	=    None
											self.display()






class  __magic_name__						(  __UpperCAmelCase):
				'''simple docstring'''







				def __init__(							self:   Tuple		)     ->   Any:
											SCREAMING_SNAKE_CASE_   	=    None
											SCREAMING_SNAKE_CASE_   	=    None
											SCREAMING_SNAKE_CASE_   	=    False


				def     _A							(							self:   int		,					_lowerCamelCase:   Union[str, Any]		,					_lowerCamelCase:   Union[str, Any]		,					_lowerCamelCase:   Optional[int]		,					**_lowerCamelCase:   List[Any]		)     ->   List[str]:
											SCREAMING_SNAKE_CASE_   	=    '''Epoch''' if args.evaluation_strategy == IntervalStrategy.EPOCH else '''Step'''
											SCREAMING_SNAKE_CASE_   	=    0
											SCREAMING_SNAKE_CASE_   	=    0
											SCREAMING_SNAKE_CASE_   	=    [self.first_column] + ['''Training Loss''']
											if args.evaluation_strategy != IntervalStrategy.NO:
																		column_names.append('''Validation Loss'''		)
											SCREAMING_SNAKE_CASE_   	=    NotebookTrainingTracker(state.max_steps		,					_lowerCamelCase		)


				def     _A							(							self:   str		,					_lowerCamelCase:   str		,					_lowerCamelCase:   Tuple		,					_lowerCamelCase:   str		,					**_lowerCamelCase:   Optional[int]		)     ->   Any:
											SCREAMING_SNAKE_CASE_   	=    int(state.epoch		) if int(state.epoch		) == state.epoch else f"{state.epoch:.2f}"
											self.training_tracker.update(
											    state.global_step + 1		,					comment=f"Epoch {epoch}/{state.num_train_epochs}"		,					force_update=self._force_next_update		,					)
											SCREAMING_SNAKE_CASE_   	=    False


				def     _A							(							self:   Optional[Any]		,					_lowerCamelCase:   Optional[Any]		,					_lowerCamelCase:   List[str]		,					_lowerCamelCase:   int		,					_lowerCamelCase:   Dict=None		,					**_lowerCamelCase:   Optional[Any]		)     ->   Any:
											if not has_length(_lowerCamelCase		):
																		return
											if self.prediction_bar is None:
																		if self.training_tracker is not None:
																									SCREAMING_SNAKE_CASE_   	=    self.training_tracker.add_child(len(_lowerCamelCase		)		)
																		else:
																									SCREAMING_SNAKE_CASE_   	=    NotebookProgressBar(len(_lowerCamelCase		)		)
																		self.prediction_bar.update(1		)
											else:
																		self.prediction_bar.update(self.prediction_bar.value + 1		)


				def     _A							(							self:   int		,					_lowerCamelCase:   Optional[int]		,					_lowerCamelCase:   Optional[Any]		,					_lowerCamelCase:   int		,					**_lowerCamelCase:   Union[str, Any]		)     ->   Tuple:
											if self.prediction_bar is not None:
																		self.prediction_bar.close()
											SCREAMING_SNAKE_CASE_   	=    None


				def     _A							(							self:   Any		,					_lowerCamelCase:   Union[str, Any]		,					_lowerCamelCase:   Union[str, Any]		,					_lowerCamelCase:   Union[str, Any]		,					_lowerCamelCase:   Dict=None		,					**_lowerCamelCase:   str		)     ->   List[str]:
											# Only for when there is no evaluation
											if args.evaluation_strategy == IntervalStrategy.NO and "loss" in logs:
																		SCREAMING_SNAKE_CASE_   	=    {'''Training Loss''': logs['''loss''']}
																		# First column is necessarily Step sine we're not in epoch eval strategy
																		SCREAMING_SNAKE_CASE_   	=    state.global_step
																		self.training_tracker.write_line(_lowerCamelCase		)


				def     _A							(							self:   str		,					_lowerCamelCase:   List[Any]		,					_lowerCamelCase:   Union[str, Any]		,					_lowerCamelCase:   Any		,					_lowerCamelCase:   Dict=None		,					**_lowerCamelCase:   List[Any]		)     ->   List[Any]:
											if self.training_tracker is not None:
																		SCREAMING_SNAKE_CASE_   	=    {'''Training Loss''': '''No log''', '''Validation Loss''': '''No log'''}
																		for log in reversed(state.log_history		):
																									if "loss" in log:
																																SCREAMING_SNAKE_CASE_   	=    log['''loss''']
																																break

																		if self.first_column == "Epoch":
																									SCREAMING_SNAKE_CASE_   	=    int(state.epoch		)
																		else:
																									SCREAMING_SNAKE_CASE_   	=    state.global_step
																		SCREAMING_SNAKE_CASE_   	=    '''eval'''
																		for k in metrics:
																									if k.endswith('''_loss'''		):
																																SCREAMING_SNAKE_CASE_   	=    re.sub(R'''\_loss$'''		,					''''''		,					_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    metrics.pop('''total_flos'''		,					_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    metrics.pop('''epoch'''		,					_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    metrics.pop(f"{metric_key_prefix}_runtime"		,					_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    metrics.pop(f"{metric_key_prefix}_samples_per_second"		,					_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    metrics.pop(f"{metric_key_prefix}_steps_per_second"		,					_lowerCamelCase		)
																		SCREAMING_SNAKE_CASE_   	=    metrics.pop(f"{metric_key_prefix}_jit_compilation_time"		,					_lowerCamelCase		)
																		for k, v in metrics.items():
																									if k == f"{metric_key_prefix}_loss":
																																SCREAMING_SNAKE_CASE_   	=    v
																									else:
																																SCREAMING_SNAKE_CASE_   	=    k.split('''_'''		)
																																SCREAMING_SNAKE_CASE_   	=    ''' '''.join([part.capitalize() for part in splits[1:]]		)
																																SCREAMING_SNAKE_CASE_   	=    v
																		self.training_tracker.write_line(_lowerCamelCase		)
																		self.training_tracker.remove_child()
																		SCREAMING_SNAKE_CASE_   	=    None
																		# Evaluation takes a long time so we should force the next update.
																		SCREAMING_SNAKE_CASE_   	=    True





				def     _A							(							self:   int		,					_lowerCamelCase:   Union[str, Any]		,					_lowerCamelCase:   int		,					_lowerCamelCase:   Any		,					**_lowerCamelCase:   List[str]		)     ->   int:
											self.training_tracker.update(
											    state.global_step		,					comment=f"Epoch {int(state.epoch		)}/{state.num_train_epochs}"		,					force_update=_lowerCamelCase		)
											SCREAMING_SNAKE_CASE_   	=    None








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


__SCREAMING_SNAKE_CASE 						=logging.get_logger(__name__)

__SCREAMING_SNAKE_CASE 						={
    """google/realm-cc-news-pretrained-embedder""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-encoder""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-scorer""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-openqa""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json"""
    ),
    """google/realm-orqa-nq-openqa""": """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json""",
    """google/realm-orqa-nq-reader""": """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json""",
    """google/realm-orqa-wq-openqa""": """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json""",
    """google/realm-orqa-wq-reader""": """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json""",
    # See all REALM models at https://huggingface.co/models?filter=realm
}






class  __magic_name__						(  __UpperCAmelCase):
				'''simple docstring'''







				SCREAMING_SNAKE_CASE__					:							int	      =					"realm"


				def __init__(							self:   Tuple		,					_lowerCamelCase:   Union[str, Any]=3_05_22		,					_lowerCamelCase:   Tuple=7_68		,					_lowerCamelCase:   str=1_28		,					_lowerCamelCase:   str=12		,					_lowerCamelCase:   int=12		,					_lowerCamelCase:   Union[str, Any]=8		,					_lowerCamelCase:   Optional[Any]=30_72		,					_lowerCamelCase:   str="gelu_new"		,					_lowerCamelCase:   str=0.1		,					_lowerCamelCase:   Union[str, Any]=0.1		,					_lowerCamelCase:   Optional[int]=5_12		,					_lowerCamelCase:   Union[str, Any]=2		,					_lowerCamelCase:   int=0.02		,					_lowerCamelCase:   Tuple=1E-12		,					_lowerCamelCase:   List[Any]=2_56		,					_lowerCamelCase:   Any=10		,					_lowerCamelCase:   Optional[Any]=1E-3		,					_lowerCamelCase:   Any=5		,					_lowerCamelCase:   List[str]=3_20		,					_lowerCamelCase:   List[str]=13_35_37_18		,					_lowerCamelCase:   str=50_00		,					_lowerCamelCase:   str=1		,					_lowerCamelCase:   str=0		,					_lowerCamelCase:   Dict=2		,					**_lowerCamelCase:   Tuple		,					):
											super().__init__(pad_token_id=_lowerCamelCase		,					bos_token_id=_lowerCamelCase		,					eos_token_id=_lowerCamelCase		,					**_lowerCamelCase		)

											# Common config
											SCREAMING_SNAKE_CASE_   	=    vocab_size
											SCREAMING_SNAKE_CASE_   	=    max_position_embeddings
											SCREAMING_SNAKE_CASE_   	=    hidden_size
											SCREAMING_SNAKE_CASE_   	=    retriever_proj_size
											SCREAMING_SNAKE_CASE_   	=    num_hidden_layers
											SCREAMING_SNAKE_CASE_   	=    num_attention_heads
											SCREAMING_SNAKE_CASE_   	=    num_candidates
											SCREAMING_SNAKE_CASE_   	=    intermediate_size
											SCREAMING_SNAKE_CASE_   	=    hidden_act
											SCREAMING_SNAKE_CASE_   	=    hidden_dropout_prob
											SCREAMING_SNAKE_CASE_   	=    attention_probs_dropout_prob
											SCREAMING_SNAKE_CASE_   	=    initializer_range
											SCREAMING_SNAKE_CASE_   	=    type_vocab_size
											SCREAMING_SNAKE_CASE_   	=    layer_norm_eps

											# Reader config
											SCREAMING_SNAKE_CASE_   	=    span_hidden_size
											SCREAMING_SNAKE_CASE_   	=    max_span_width
											SCREAMING_SNAKE_CASE_   	=    reader_layer_norm_eps
											SCREAMING_SNAKE_CASE_   	=    reader_beam_size
											SCREAMING_SNAKE_CASE_   	=    reader_seq_len

											# Retrieval config
											SCREAMING_SNAKE_CASE_   	=    num_block_records
											SCREAMING_SNAKE_CASE_   	=    searcher_beam_size


