


'''simple docstring'''





from sklearn.metrics import fa_score, matthews_corrcoef

import datasets

from .record_evaluation import evaluate as evaluate_record


__lowerCamelCase												=						'''\
@article{wang2019superglue,
  title={SuperGLUE: A Stickier Benchmark for General-Purpose Language Understanding Systems},
  author={Wang, Alex and Pruksachatkun, Yada and Nangia, Nikita and Singh, Amanpreet and Michael, Julian and Hill, Felix and Levy, Omer and Bowman, Samuel R},
  journal={arXiv preprint arXiv:1905.00537},
  year={2019}
}
'''

__lowerCamelCase												=						'''\
SuperGLUE (https://super.gluebenchmark.com/) is a new benchmark styled after
GLUE with a new set of more difficult language understanding tasks, improved
resources, and a new public leaderboard.
'''

__lowerCamelCase												=						'''
Compute SuperGLUE evaluation metric associated to each SuperGLUE dataset.
Args:
    predictions: list of predictions to score. Depending on the SuperGlUE subset:
        - for \'record\': list of question-answer dictionaries with the following keys:
            - \'idx\': index of the question as specified by the dataset
            - \'prediction_text\': the predicted answer text
        - for \'multirc\': list of question-answer dictionaries with the following keys:
            - \'idx\': index of the question-answer pair as specified by the dataset
            - \'prediction\': the predicted answer label
        - otherwise: list of predicted labels
    references: list of reference labels. Depending on the SuperGLUE subset:
        - for \'record\': list of question-answers dictionaries with the following keys:
            - \'idx\': index of the question as specified by the dataset
            - \'answers\': list of possible answers
        - otherwise: list of reference labels
Returns: depending on the SuperGLUE subset:
    - for \'record\':
        - \'exact_match\': Exact match between answer and gold answer
        - \'f1\': F1 score
    - for \'multirc\':
        - \'exact_match\': Exact match between answer and gold answer
        - \'f1_m\': Per-question macro-F1 score
        - \'f1_a\': Average F1 score over all answers
    - for \'axb\':
        \'matthews_correlation\': Matthew Correlation
    - for \'cb\':
        - \'accuracy\': Accuracy
        - \'f1\': F1 score
    - for all others:
        - \'accuracy\': Accuracy
Examples:

    >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'copa\')  # any of [\"copa\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"boolq\", \"axg\"]
    >>> predictions = [0, 1]
    >>> references = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'accuracy\': 1.0}

    >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'cb\')
    >>> predictions = [0, 1]
    >>> references = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'accuracy\': 1.0, \'f1\': 1.0}

    >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'record\')
    >>> predictions = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'prediction_text\': \'answer\'}]
    >>> references = [{\'idx\': {\'passage\': 0, \'query\': 0}, \'answers\': [\'answer\', \'another_answer\']}]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'exact_match\': 1.0, \'f1\': 1.0}

    >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'multirc\')
    >>> predictions = [{\'idx\': {\'answer\': 0, \'paragraph\': 0, \'question\': 0}, \'prediction\': 0}, {\'idx\': {\'answer\': 1, \'paragraph\': 2, \'question\': 3}, \'prediction\': 1}]
    >>> references = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'exact_match\': 1.0, \'f1_m\': 1.0, \'f1_a\': 1.0}

    >>> super_glue_metric = datasets.load_metric(\'super_glue\', \'axb\')
    >>> references = [0, 1]
    >>> predictions = [0, 1]
    >>> results = super_glue_metric.compute(predictions=predictions, references=references)
    >>> print(results)
    {\'matthews_correlation\': 1.0}
'''


def        a__     (					UpperCamelCase_						:      Dict,   UpperCamelCase_						:      Dict						):
   return float((preds == labels).mean()						)


def        a__     (					UpperCamelCase_						:      Union[str, Any],   UpperCamelCase_						:      Any,   UpperCamelCase_						:      str="binary"						):
   UpperCAmelCase__	:Tuple      =      simple_accuracy(SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_						)
   UpperCAmelCase__	:Tuple      =      float(fa_score(y_true=SCREAMING_SNAKE_CASE_,   y_pred=SCREAMING_SNAKE_CASE_,   average=SCREAMING_SNAKE_CASE_						)						)
   return {
       "accuracy": acc,
       "f1": fa,
   }


def        a__     (					UpperCamelCase_						:      Any,   UpperCamelCase_						:      str						):
   UpperCAmelCase__	:str      =      {}
   for id_pred, label in zip(SCREAMING_SNAKE_CASE_,   SCREAMING_SNAKE_CASE_						):
      UpperCAmelCase__	:Union[str, Any]      =      F'''{id_pred["idx"]["paragraph"]}-{id_pred["idx"]["question"]}'''
      UpperCAmelCase__	:int      =      id_pred['''prediction''']
      if question_id in question_map:
         question_map[question_id].append((pred, label)						)
      else:
         UpperCAmelCase__	:Any      =      [(pred, label)]
   UpperCAmelCase__   ,						UpperCAmelCase__	:str      =      [], []
   for question, preds_labels in question_map.items():
      UpperCAmelCase__   ,						UpperCAmelCase__	:str      =      zip(*SCREAMING_SNAKE_CASE_						)
      UpperCAmelCase__	:List[str]      =      fa_score(y_true=SCREAMING_SNAKE_CASE_,   y_pred=SCREAMING_SNAKE_CASE_,   average='''macro'''						)
      fas.append(SCREAMING_SNAKE_CASE_						)
      UpperCAmelCase__	:Optional[Any]      =      int(sum(pred == label for pred, label in preds_labels						) == len(SCREAMING_SNAKE_CASE_						)						)
      ems.append(SCREAMING_SNAKE_CASE_						)
   UpperCAmelCase__	:Tuple      =      float(sum(SCREAMING_SNAKE_CASE_						) / len(SCREAMING_SNAKE_CASE_						)						)
   UpperCAmelCase__	:int      =      sum(SCREAMING_SNAKE_CASE_						) / len(SCREAMING_SNAKE_CASE_						)
   UpperCAmelCase__	:Any      =      float(fa_score(y_true=SCREAMING_SNAKE_CASE_,   y_pred=[id_pred['''prediction'''] for id_pred in ids_preds]						)						)
   return {"exact_match": em, "f1_m": fa_m, "f1_a": fa_a}




@datasets.utils.file_utils.add_start_docstrings(_DESCRIPTION						, _KWARGS_DESCRIPTION						)
class 				UpperCAmelCase			(			datasets.Metric						):


 def        __SCREAMING_SNAKE_CASE					(      self							:	List[Any] ):


    if self.config_name not in [
        "boolq",
        "cb",
        "copa",
        "multirc",
        "record",
        "rte",
        "wic",
        "wsc",
        "wsc.fixed",
        "axb",
        "axg",
    ]:
       raise KeyError(
           '''You should supply a configuration name selected in '''
           '''[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]''' )
    return datasets.MetricInfo(
        description=_DESCRIPTION			,       citation=_CITATION			,       inputs_description=_KWARGS_DESCRIPTION			,       features=datasets.Features(self._get_feature_types() )			,       codebase_urls=[]			,       reference_urls=[]			,       format='''numpy''' if not self.config_name == '''record''' and not self.config_name == '''multirc''' else None			,       )


 def        __SCREAMING_SNAKE_CASE					(      self							:	str ):


    if self.config_name == "record":
       return {
           "predictions": {
               "idx": {
                   "passage": datasets.Value('''int64''' ),
                   "query": datasets.Value('''int64''' ),
               },
               "prediction_text": datasets.Value('''string''' ),
           },
           "references": {
               "idx": {
                   "passage": datasets.Value('''int64''' ),
                   "query": datasets.Value('''int64''' ),
               },
               "answers": datasets.Sequence(datasets.Value('''string''' ) ),
           },
       }
    elif self.config_name == "multirc":
       return {
           "predictions": {
               "idx": {
                   "answer": datasets.Value('''int64''' ),
                   "paragraph": datasets.Value('''int64''' ),
                   "question": datasets.Value('''int64''' ),
               },
               "prediction": datasets.Value('''int64''' ),
           },
           "references": datasets.Value('''int64''' ),
       }
    else:
       return {
           "predictions": datasets.Value('''int64''' ),
           "references": datasets.Value('''int64''' ),
       }





 def        __SCREAMING_SNAKE_CASE					(      self							:	Union[str, Any]			,       __lowerCamelCase							:	Tuple			,       __lowerCamelCase							:	Dict ):


    if self.config_name == "axb":
       return {"matthews_correlation": matthews_corrcoef(lowercase_			,       lowercase_ )}
    elif self.config_name == "cb":
       return acc_and_fa(lowercase_			,       lowercase_			,       fa_avg='''macro''' )
    elif self.config_name == "record":
       UpperCAmelCase__	:Optional[Any]      =      [
           {
               '''qas''': [
                   {'''id''': ref['''idx''']['''query'''], '''answers''': [{'''text''': ans} for ans in ref['''answers''']]}
                   for ref in references
               ]
           }
       ]
       UpperCAmelCase__	:str      =      {pred['''idx''']['''query''']: pred['''prediction_text'''] for pred in predictions}
       return evaluate_record(lowercase_			,       lowercase_ )[0]
    elif self.config_name == "multirc":
       return evaluate_multirc(lowercase_			,       lowercase_ )
    elif self.config_name in ["copa", "rte", "wic", "wsc", "wsc.fixed", "boolq", "axg"]:
       return {"accuracy": simple_accuracy(lowercase_			,       lowercase_ )}
    else:
       raise KeyError(
           '''You should supply a configuration name selected in '''
           '''[\"boolq\", \"cb\", \"copa\", \"multirc\", \"record\", \"rte\", \"wic\", \"wsc\", \"wsc.fixed\", \"axb\", \"axg\",]''' )




'''simple docstring'''


from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging
from ..auto import CONFIG_MAPPING


__snake_case						  =		logging.get_logger(__name__)

__snake_case						  =		{
    """microsoft/table-transformer-detection""": (
        """https://huggingface.co/microsoft/table-transformer-detection/resolve/main/config.json"""
    ),
}





class       _a    (		__a      ):


  """simple docstring"""

  A_    							=						'''table-transformer'''
  A_    							=						['''past_key_values''']
  A_    							=						{
      '''hidden_size''': '''d_model''',
      '''num_attention_heads''': '''encoder_attention_heads''',
  }




  def __init__(		self      :       str   ,				lowercase_      :       str=True   ,				lowercase_      :       List[str]=None   ,				lowercase_      :       Dict=3   ,				lowercase_      :       Optional[Any]=100   ,				lowercase_      :       Optional[int]=6   ,				lowercase_      :       Optional[Any]=2_048   ,				lowercase_      :       List[Any]=8   ,				lowercase_      :       Optional[Any]=6   ,				lowercase_      :       int=2_048   ,				lowercase_      :       Any=8   ,				lowercase_      :       Optional[Any]=0.0   ,				lowercase_      :       int=0.0   ,				lowercase_      :       Dict=True   ,				lowercase_      :       int="relu"   ,				lowercase_      :       Tuple=256   ,				lowercase_      :       Dict=0.1   ,				lowercase_      :       List[str]=0.0   ,				lowercase_      :       List[str]=0.0   ,				lowercase_      :       int=0.0_2   ,				lowercase_      :       List[str]=1.0   ,				lowercase_      :       Optional[int]=False   ,				lowercase_      :       List[Any]="sine"   ,				lowercase_      :       Optional[int]="resnet50"   ,				lowercase_      :       Union[str, Any]=True   ,				lowercase_      :       Union[str, Any]=False   ,				lowercase_      :       List[str]=1   ,				lowercase_      :       Any=5   ,				lowercase_      :       Optional[int]=2   ,				lowercase_      :       Dict=1   ,				lowercase_      :       Optional[int]=1   ,				lowercase_      :       Dict=5   ,				lowercase_      :       Optional[int]=2   ,				lowercase_      :       Optional[Any]=0.1   ,				**lowercase_      :       List[str]   ,				):







    '''simple docstring'''


    if backbone_config is not None and use_timm_backbone:
      raise ValueError("""You can't specify both `backbone_config` and `use_timm_backbone`."""      )

    if not use_timm_backbone:
      if backbone_config is None:
        logger.info("""`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."""      )
        lowercase_     =       CONFIG_MAPPING["""resnet"""](out_features=["""stage4"""]      )
      elif isinstance(lowercase_   ,				lowercase_      ):
        lowercase_     =       backbone_config.get("""model_type"""      )
        lowercase_     =       CONFIG_MAPPING[backbone_model_type]
        lowercase_     =       config_class.from_dict(lowercase_      )
      # set timm attributes to None
      lowercase_     ,	lowercase_     ,	lowercase_     =       None, None, None

    lowercase_     =       use_timm_backbone
    lowercase_     =       backbone_config
    lowercase_     =       num_channels
    lowercase_     =       num_queries
    lowercase_     =       d_model
    lowercase_     =       encoder_ffn_dim
    lowercase_     =       encoder_layers
    lowercase_     =       encoder_attention_heads
    lowercase_     =       decoder_ffn_dim
    lowercase_     =       decoder_layers
    lowercase_     =       decoder_attention_heads
    lowercase_     =       dropout
    lowercase_     =       attention_dropout
    lowercase_     =       activation_dropout
    lowercase_     =       activation_function
    lowercase_     =       init_std
    lowercase_     =       init_xavier_std
    lowercase_     =       encoder_layerdrop
    lowercase_     =       decoder_layerdrop
    lowercase_     =       encoder_layers
    lowercase_     =       auxiliary_loss
    lowercase_     =       position_embedding_type
    lowercase_     =       backbone
    lowercase_     =       use_pretrained_backbone
    lowercase_     =       dilation
    # Hungarian matcher
    lowercase_     =       class_cost
    lowercase_     =       bbox_cost
    lowercase_     =       giou_cost
    # Loss coefficients
    lowercase_     =       mask_loss_coefficient
    lowercase_     =       dice_loss_coefficient
    lowercase_     =       bbox_loss_coefficient
    lowercase_     =       giou_loss_coefficient
    lowercase_     =       eos_coefficient
    super().__init__(is_encoder_decoder=lowercase_   ,				**lowercase_      )




  @property
  def 		lowerCamelCase__  (		self      :       Tuple      ):







    '''simple docstring'''


    return self.encoder_attention_heads





  @property
  def 		lowerCamelCase__  (		self      :       Dict      ):







    '''simple docstring'''


    return self.d_model






class       _a    (		__a      ):


  """simple docstring"""

  A_    							=						version.parse('''1.11'''      )




  @property
  def 		lowerCamelCase__  (		self      :       int      ):







    '''simple docstring'''


    return OrderedDict(
        [
            ("""pixel_values""", {0: """batch""", 1: """num_channels""", 2: """height""", 3: """width"""}),
            ("""pixel_mask""", {0: """batch"""}),
        ]      )




  @property
  def 		lowerCamelCase__  (		self      :       List[Any]      ):







    '''simple docstring'''


    return 1e-5





  @property
  def 		lowerCamelCase__  (		self      :       Union[str, Any]      ):







    '''simple docstring'''


    return 12

