




import math
import time
from typing import Dict, List, Optional

from torch.utils.data import Dataset

from transformers import SeqaSeqTrainer, 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_    ( UpperCAmelCase__     ):

    '''simple docstring'''






    def __init__(			self      :	Optional[int]					, *UpperCAmelCase__      :	int					, UpperCAmelCase__      :	Optional[int]=None					, UpperCAmelCase__      :	List[Any]=None					, **UpperCAmelCase__      :	Optional[Any])						->Any:





           '''simple docstring'''

           super().__init__(*UpperCAmelCase__					, **UpperCAmelCase__)
           A__       =    eval_examples
           A__       =    post_process_function


    def        SCREAMING_SNAKE_CASE							(			self      :	Any					, UpperCAmelCase__      :	Optional[Dataset] = None					, UpperCAmelCase__      :	Dict=None					, UpperCAmelCase__      :	Optional[List[str]] = None					, UpperCAmelCase__      :	str = "eval"					, **UpperCAmelCase__      :	List[str]					, )						->Dict[str, float]:





           '''simple docstring'''

           A__       =    gen_kwargs.copy()
           A__       =    (
               gen_kwargs['''max_length'''] if gen_kwargs.get('''max_length''') is not None else self.args.generation_max_length
           )
           A__       =    (
               gen_kwargs['''num_beams'''] if gen_kwargs.get('''num_beams''') is not None else self.args.generation_num_beams
           )
           A__       =    gen_kwargs

           A__       =    self.eval_dataset if eval_dataset is None else eval_dataset
           A__       =    self.get_eval_dataloader(UpperCAmelCase__)
           A__       =    self.eval_examples if eval_examples is None else eval_examples

           # Temporarily disable metric computation, we will do it in the loop here.
           A__       =    self.compute_metrics
           A__       =    None
           A__       =    time.time()
           A__       =    self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
           try:
                  A__       =    eval_loop(
                      UpperCAmelCase__					, description='''Evaluation'''					, prediction_loss_only=True if compute_metrics is None else None					, ignore_keys=UpperCAmelCase__					, metric_key_prefix=UpperCAmelCase__					, )
           finally:
                  A__       =    compute_metrics
           A__       =    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(
                   UpperCAmelCase__					, UpperCAmelCase__					, 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
                  A__       =    self.post_process_function(UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__)
                  A__       =    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}_"""):
                                A__       =    metrics.pop(UpperCAmelCase__)

                  metrics.update(output.metrics)
           else:
                  A__       =    output.metrics

           if self.args.should_log:
                  # Only the main node log the results by default
                  self.log(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())

           A__       =    self.callback_handler.on_evaluate(self.args					, self.state					, self.control					, UpperCAmelCase__)
           return metrics





    def        SCREAMING_SNAKE_CASE							(			self      :	Optional[Any]					, UpperCAmelCase__      :	int					, UpperCAmelCase__      :	Dict					, UpperCAmelCase__      :	Dict=None					, UpperCAmelCase__      :	str = "test"					, **UpperCAmelCase__      :	int)						->int:





           '''simple docstring'''

           A__       =    gen_kwargs.copy()

           A__       =    self.get_test_dataloader(UpperCAmelCase__)

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

           A__       =    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(
                   UpperCAmelCase__					, UpperCAmelCase__					, 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

           A__       =    self.post_process_function(UpperCAmelCase__					, UpperCAmelCase__					, UpperCAmelCase__					, '''predict''')
           A__       =    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}_"""):
                         A__       =    metrics.pop(UpperCAmelCase__)
           metrics.update(output.metrics)
           return PredictionOutput(predictions=predictions.predictions					, label_ids=predictions.label_ids					, metrics=UpperCAmelCase__)








def SCREAMING_SNAKE_CASE				(       lowercase_ ,				lowercase_						)   ->       int:



       """simple docstring"""

       return int(input_a == input_a == 0						)







def SCREAMING_SNAKE_CASE				(       )   ->       None:



       """simple docstring"""

       print('''Truth Table of NOR Gate:'''						)
       print('''|   Input 1   |  Input 2  | Output |'''						)
       print(f"""|      0      |    0      |  {nor_gate(0 ,				0						)}     |"""						)
       print(f"""|      0      |    1      |  {nor_gate(0 ,				1						)}     |"""						)
       print(f"""|      1      |    0      |  {nor_gate(1 ,				0						)}     |"""						)
       print(f"""|      1      |    1      |  {nor_gate(1 ,				1						)}     |"""						)


if __name__ == "__main__":
  import doctest

  doctest.testmod()
  main()


