






import argparse
from collections import defaultdict

import yaml


A__	:			int			   = 'docs/source/en/_toctree.yml'






def   a			(						lowerCamelCase_				):






				'''simple docstring'''
				lowercase__    =			defaultdict(lowerCamelCase_				)
				lowercase__    =			[]
				lowercase__    =			[]
				for doc in doc_list:
								if "local" in doc:
												counts[doc["local"]] += 1

								if doc["title"].lower() == "overview":
												overview_doc.append({'''local''': doc['''local'''], '''title''': doc['''title''']}				)
								else:
												new_doc_list.append(lowerCamelCase_				)

				lowercase__    =			new_doc_list
				lowercase__    =			[key for key, value in counts.items() if value > 1]

				lowercase__    =			[]
				for duplicate_key in duplicates:
								lowercase__    =			list({doc['''title'''] for doc in doc_list if doc['''local'''] == duplicate_key}				)
								if len(lowerCamelCase_				) > 1:
												raise ValueError(
												    F"""{duplicate_key} is present several times in the documentation table of content at """
												    '''`docs/source/en/_toctree.yml` with different *Title* values. Choose one of those and remove the '''
												    '''others.'''				)
								# Only add this once
								new_doc.append({'''local''': duplicate_key, '''title''': titles[0]}				)

				# Add none duplicate-keys
				new_doc.extend([doc for doc in doc_list if '''local''' not in counts or counts[doc['''local''']] == 1]				)
				lowercase__    =			sorted(lowerCamelCase_		,   key=lambda lowerCamelCase_				: s["title"].lower()				)

				# "overview" gets special treatment and is always first
				if len(lowerCamelCase_				) > 1:
								raise ValueError('''{doc_list} has two \'overview\' docs which is not allowed.'''				)

				overview_doc.extend(lowerCamelCase_				)

				# Sort
				return overview_doc






def   a			(						lowerCamelCase_=False				):






				'''simple docstring'''
				with open(lowerCamelCase_		,   encoding='''utf-8'''				) as f:
								lowercase__    =			yaml.safe_load(f.read()				)

				# Get to the API doc
				lowercase__    =			0
				while content[api_idx]["title"] != "API":
								api_idx += 1
				lowercase__    =			content[api_idx]['''sections''']

				# Then to the model doc
				lowercase__    =			0
				while api_doc[scheduler_idx]["title"] != "Schedulers":
								scheduler_idx += 1

				lowercase__    =			api_doc[scheduler_idx]['''sections''']
				lowercase__    =			clean_doc_toc(lowerCamelCase_				)

				lowercase__    =			False
				if new_scheduler_doc != scheduler_doc:
								lowercase__    =			True
								if overwrite:
												lowercase__    =			new_scheduler_doc

				if diff:
								if overwrite:
												lowercase__    =			api_doc
												with open(lowerCamelCase_		,   '''w'''		,   encoding='''utf-8'''				) as f:
																f.write(yaml.dump(lowerCamelCase_		,   allow_unicode=lowerCamelCase_				)				)
								else:
												raise ValueError(
												    '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.'''				)






def   a			(						lowerCamelCase_=False				):






				'''simple docstring'''
				with open(lowerCamelCase_		,   encoding='''utf-8'''				) as f:
								lowercase__    =			yaml.safe_load(f.read()				)

				# Get to the API doc
				lowercase__    =			0
				while content[api_idx]["title"] != "API":
								api_idx += 1
				lowercase__    =			content[api_idx]['''sections''']

				# Then to the model doc
				lowercase__    =			0
				while api_doc[pipeline_idx]["title"] != "Pipelines":
								pipeline_idx += 1

				lowercase__    =			False
				lowercase__    =			api_doc[pipeline_idx]['''sections''']
				lowercase__    =			[]

				# sort sub pipeline docs
				for pipeline_doc in pipeline_docs:
								if "section" in pipeline_doc:
												lowercase__    =			pipeline_doc['''section''']
												lowercase__    =			clean_doc_toc(lowerCamelCase_				)
												if overwrite:
																lowercase__    =			new_sub_pipeline_doc
								new_pipeline_docs.append(lowerCamelCase_				)

				# sort overall pipeline doc
				lowercase__    =			clean_doc_toc(lowerCamelCase_				)

				if new_pipeline_docs != pipeline_docs:
								lowercase__    =			True
								if overwrite:
												lowercase__    =			new_pipeline_docs

				if diff:
								if overwrite:
												lowercase__    =			api_doc
												with open(lowerCamelCase_		,   '''w'''		,   encoding='''utf-8'''				) as f:
																f.write(yaml.dump(lowerCamelCase_		,   allow_unicode=lowerCamelCase_				)				)
								else:
												raise ValueError(
												    '''The model doc part of the table of content is not properly sorted, run `make style` to fix this.'''				)


if __name__ == "__main__":
							A__	:			List[str]			   = argparse.ArgumentParser()
							parser.add_argument('--fix_and_overwrite', action='store_true', help='Whether to fix inconsistencies.')
							A__	:			Optional[int]			   = parser.parse_args()

							check_scheduler_doc(args.fix_and_overwrite)
							check_pipeline_doc(args.fix_and_overwrite)












import math
import time

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


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

				"""simple docstring"""




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

								'''simple docstring'''
								super().__init__(*lowerCamelCase,  **lowerCamelCase  )
								lowercase__    =			eval_examples
								lowercase__    =			post_process_function




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

								'''simple docstring'''
								lowercase__    =			self.eval_dataset if eval_dataset is None else eval_dataset
								lowercase__    =			self.get_eval_dataloader(lowerCamelCase  )
								lowercase__    =			self.eval_examples if eval_examples is None else eval_examples

								# Temporarily disable metric computation, we will do it in the loop here.
								lowercase__    =			self.compute_metrics
								lowercase__    =			None
								lowercase__    =			self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
								lowercase__    =			time.time()
								try:
												lowercase__    =			eval_loop(
												    lowerCamelCase,  description='''Evaluation''',  prediction_loss_only=True if compute_metrics is None else None,  ignore_keys=lowerCamelCase,  metric_key_prefix=lowerCamelCase,  )
								finally:
												lowercase__    =			compute_metrics
								lowercase__    =			self.args.eval_batch_size * self.args.world_size
								if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics:
												start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""]
								output.metrics.update(
								    speed_metrics(
								        lowerCamelCase,  lowerCamelCase,  num_samples=output.num_samples,  num_steps=math.ceil(output.num_samples / total_batch_size  ),  )  )
								if self.post_process_function is not None and self.compute_metrics is not None and self.args.should_save:
												# Only the main node write the results by default
												lowercase__    =			self.post_process_function(lowerCamelCase,  lowerCamelCase,  output.predictions  )
												lowercase__    =			self.compute_metrics(lowerCamelCase  )

												# Prefix all keys with metric_key_prefix + '_'
												for key in list(metrics.keys()  ):
																if not key.startswith(F"""{metric_key_prefix}_"""  ):
																				lowercase__    =			metrics.pop(lowerCamelCase  )
												metrics.update(output.metrics  )
								else:
												lowercase__    =			output.metrics

								if self.args.should_log:
												# Only the main node log the results by default
												self.log(lowerCamelCase  )

								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__    =			self.callback_handler.on_evaluate(self.args,  self.state,  self.control,  lowerCamelCase  )
								return metrics




				def 				lowercase__   (     self						:    List[Any],  lowerCamelCase						:    Any,  lowerCamelCase						:    Dict,  lowerCamelCase						:    int=None,  lowerCamelCase						:    str = "test"  ):

								'''simple docstring'''
								lowercase__    =			self.get_test_dataloader(lowerCamelCase  )

								# Temporarily disable metric computation, we will do it in the loop here.
								lowercase__    =			self.compute_metrics
								lowercase__    =			None
								lowercase__    =			self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
								lowercase__    =			time.time()
								try:
												lowercase__    =			eval_loop(
												    lowerCamelCase,  description='''Prediction''',  prediction_loss_only=True if compute_metrics is None else None,  ignore_keys=lowerCamelCase,  metric_key_prefix=lowerCamelCase,  )
								finally:
												lowercase__    =			compute_metrics
								lowercase__    =			self.args.eval_batch_size * self.args.world_size
								if F"""{metric_key_prefix}_jit_compilation_time""" in output.metrics:
												start_time += output.metrics[F"""{metric_key_prefix}_jit_compilation_time"""]
								output.metrics.update(
								    speed_metrics(
								        lowerCamelCase,  lowerCamelCase,  num_samples=output.num_samples,  num_steps=math.ceil(output.num_samples / total_batch_size  ),  )  )

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

								lowercase__    =			self.post_process_function(lowerCamelCase,  lowerCamelCase,  output.predictions,  '''predict'''  )
								lowercase__    =			self.compute_metrics(lowerCamelCase  )

								# Prefix all keys with metric_key_prefix + '_'
								for key in list(metrics.keys()  ):
												if not key.startswith(F"""{metric_key_prefix}_"""  ):
																lowercase__    =			metrics.pop(lowerCamelCase  )
								metrics.update(output.metrics  )
								return PredictionOutput(predictions=predictions.predictions,  label_ids=predictions.label_ids,  metrics=lowerCamelCase  )




