




import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


UpperCamelCase__					    =    logging.getLogger(__name__)
@dataclass(frozen=__lowerCAmelCase   )
class     a__					:
						_a							:					str
						_a							:					str
						_a							:					Optional[str]      =					None
						_a							:					Optional[str]      =					None
						_a							:					Optional[str]      =					None






@dataclass(frozen=__lowerCAmelCase   )
class     a__					:
						_a							:					List[int]
						_a							:					Optional[List[int]]      =					None
						_a							:					Optional[List[int]]      =					None
						_a							:					Optional[Union[int, float]]      =					None
						_a							:					Optional[int]      =					None


if is_torch_available():
					import torch
					from torch.utils.data import Dataset
					class     a__					(      __lowerCAmelCase   ):
											_a							:					List[InputFeatures]






											def __init__(     self	,    _A	,    _A	,    _A	,    _A = None	,    _A=False	,    _A = False	,    ):




																"""simple docstring"""



																__lowerCAmelCase            = hans_processors[task]()

																__lowerCAmelCase            = os.path.join(
																    lowerCAmelCase_	,    "cached_{}_{}_{}_{}".format(
																        "dev" if evaluate else "train"	,    tokenizer.__class__.__name__	,    str(lowerCAmelCase_    )	,    lowerCAmelCase_	,    )	,    )
																__lowerCAmelCase            = processor.get_labels()
																if tokenizer.__class__ in (
																    RobertaTokenizer,
																    RobertaTokenizerFast,
																    XLMRobertaTokenizer,
																    BartTokenizer,
																    BartTokenizerFast,
																):
																					# HACK(label indices are swapped in RoBERTa pretrained model)
																					__lowerCAmelCase     ,							__lowerCAmelCase            = label_list[2], label_list[1]
																__lowerCAmelCase            = label_list

																# Make sure only the first process in distributed training processes the dataset,
																# and the others will use the cache.
																__lowerCAmelCase            = cached_features_file + ".lock"
																with FileLock(lowerCAmelCase_    ):
																					if os.path.exists(lowerCAmelCase_    ) and not overwrite_cache:
																										logger.info(f"""Loading features from cached file {cached_features_file}"""    )
																										__lowerCAmelCase            = torch.load(lowerCAmelCase_    )
																					else:
																										logger.info(f"""Creating features from dataset file at {data_dir}"""    )

																										__lowerCAmelCase            = (
																										    processor.get_dev_examples(lowerCAmelCase_    ) if evaluate else processor.get_train_examples(lowerCAmelCase_    )
																										)

																										logger.info("Training examples: %s"	,    len(lowerCAmelCase_    )    )
																										__lowerCAmelCase            = hans_convert_examples_to_features(lowerCAmelCase_	,    lowerCAmelCase_	,    lowerCAmelCase_	,    lowerCAmelCase_    )
																										logger.info("Saving features into cached file %s"	,    lowerCAmelCase_    )
																										torch.save(self.features	,    lowerCAmelCase_    )






											def __len__(     self    ):




																"""simple docstring"""



																return len(self.features    )






											def __getitem__(     self	,    _A    ):




																"""simple docstring"""



																return self.features[i]






											def        __SCREAMING_SNAKE_CASE(     self    ):




																"""simple docstring"""



																return self.label_list


if is_tf_available():
					import tensorflow as tf
					class     a__					:
											_a							:					List[InputFeatures]






											def __init__(     self	,    _A	,    _A	,    _A	,    _A = 1_2_8	,    _A=False	,    _A = False	,    ):




																"""simple docstring"""



																__lowerCAmelCase            = hans_processors[task]()
																__lowerCAmelCase            = processor.get_labels()
																if tokenizer.__class__ in (
																    RobertaTokenizer,
																    RobertaTokenizerFast,
																    XLMRobertaTokenizer,
																    BartTokenizer,
																    BartTokenizerFast,
																):
																					# HACK(label indices are swapped in RoBERTa pretrained model)
																					__lowerCAmelCase     ,							__lowerCAmelCase            = label_list[2], label_list[1]
																__lowerCAmelCase            = label_list

																__lowerCAmelCase            = processor.get_dev_examples(lowerCAmelCase_    ) if evaluate else processor.get_train_examples(lowerCAmelCase_    )
																__lowerCAmelCase            = hans_convert_examples_to_features(lowerCAmelCase_	,    lowerCAmelCase_	,    lowerCAmelCase_	,    lowerCAmelCase_    )

																def gen():
																					for ex_index, ex in tqdm.tqdm(enumerate(self.features    )	,    desc="convert examples to features"    ):
																										if ex_index % 1_0_0_0_0 == 0:
																															logger.info("Writing example %d of %d" % (ex_index, len(lowerCAmelCase_    ))    )

																										yield (
																										    {
																										        "example_id": 0,
																										        "input_ids": ex.input_ids,
																										        "attention_mask": ex.attention_mask,
																										        "token_type_ids": ex.token_type_ids,
																										    },
																										    ex.label,
																										)

																__lowerCAmelCase            = tf.data.Dataset.from_generator(
																    lowerCAmelCase_	,    (
																        {
																            "example_id": tf.intaa,
																            "input_ids": tf.intaa,
																            "attention_mask": tf.intaa,
																            "token_type_ids": tf.intaa,
																        },
																        tf.intaa,
																    )	,    (
																        {
																            "example_id": tf.TensorShape([]    ),
																            "input_ids": tf.TensorShape([None, None]    ),
																            "attention_mask": tf.TensorShape([None, None]    ),
																            "token_type_ids": tf.TensorShape([None, None]    ),
																        },
																        tf.TensorShape([]    ),
																    )	,    )






											def        __SCREAMING_SNAKE_CASE(     self    ):




																"""simple docstring"""



																return self.dataset






											def __len__(     self    ):




																"""simple docstring"""



																return len(self.features    )






											def __getitem__(     self	,    _A    ):




																"""simple docstring"""



																return self.features[i]






											def        __SCREAMING_SNAKE_CASE(     self    ):




																"""simple docstring"""



																return self.label_list
class     a__					(      __lowerCAmelCase   ):






						def        __SCREAMING_SNAKE_CASE(     self	,    _A    ):




											"""simple docstring"""



											return self._create_examples(self._read_tsv(os.path.join(lowerCAmelCase_	,    "heuristics_train_set.txt"    )    )	,    "train"    )






						def        __SCREAMING_SNAKE_CASE(     self	,    _A    ):




											"""simple docstring"""



											return self._create_examples(self._read_tsv(os.path.join(lowerCAmelCase_	,    "heuristics_evaluation_set.txt"    )    )	,    "dev"    )






						def        __SCREAMING_SNAKE_CASE(     self    ):




											"""simple docstring"""



											return ["contradiction", "entailment", "neutral"]






						def        __SCREAMING_SNAKE_CASE(     self	,    _A	,    _A    ):




											"""simple docstring"""



											__lowerCAmelCase            = []
											for i, line in enumerate(lowerCAmelCase_    ):
																if i == 0:
																					continue
																__lowerCAmelCase            = "%s-%s" % (set_type, line[0])
																__lowerCAmelCase            = line[5]
																__lowerCAmelCase            = line[6]
																__lowerCAmelCase            = line[7][2:] if line[7].startswith("ex"    ) else line[7]
																__lowerCAmelCase            = line[0]
																examples.append(InputExample(guid=lowerCAmelCase_	,    text_a=lowerCAmelCase_	,    text_b=lowerCAmelCase_	,    label=lowerCAmelCase_	,    pairID=lowerCAmelCase_    )    )
											return examples







def 			_a   (		SCREAMING_SNAKE_CASE_							:       List[InputExample]   ,   SCREAMING_SNAKE_CASE_							:       List[str]   ,   SCREAMING_SNAKE_CASE_							:       int   ,   SCREAMING_SNAKE_CASE_							:       PreTrainedTokenizer   ,   ):
					__lowerCAmelCase            = {label: i for i, label in enumerate(snake_case__  )}

					__lowerCAmelCase            = []
					for ex_index, example in tqdm.tqdm(enumerate(snake_case__  )   ,   desc="convert examples to features"  ):
										if ex_index % 1_00_00 == 0:
															logger.info("Writing example %d" % (ex_index)  )

										__lowerCAmelCase            = tokenizer(
										    example.text_a   ,   example.text_b   ,   add_special_tokens=snake_case__   ,   max_length=snake_case__   ,   padding="max_length"   ,   truncation=snake_case__   ,   return_overflowing_tokens=snake_case__   ,   )

										__lowerCAmelCase            = label_map[example.label] if example.label in label_map else 0

										__lowerCAmelCase            = int(example.pairID  )

										features.append(InputFeatures(**snake_case__   ,   label=snake_case__   ,   pairID=snake_case__  )  )

					for i, example in enumerate(examples[:5]  ):
										logger.info("*** Example ***"  )
										logger.info(F"""guid: {example}"""  )
										logger.info(F"""features: {features[i]}"""  )

					return features


UpperCamelCase__					    =    {
    """hans""": 3,
}



UpperCamelCase__					    =    {
    """hans""": HansProcessor,
}










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


UpperCamelCase__					    =    logging.get_logger(__name__)

UpperCamelCase__					    =    {
    """facebook/nllb-moe-54B""": """https://huggingface.co/facebook/nllb-moe-54b/resolve/main/config.json""",
}
class     a__					(      snake_case__   ):
						_a							:					Tuple      =					"""nllb-moe"""
						_a							:					Dict      =					["""past_key_values"""]
						_a							:					Optional[int]      =					{"""num_attention_heads""": """encoder_attention_heads""", """hidden_size""": """d_model"""}






						def __init__(     self	,    _A=1_2_8_1_1_2	,    _A=1_0_2_4	,    _A=1_2	,    _A=4_0_9_6	,    _A=1_6	,    _A=1_2	,    _A=4_0_9_6	,    _A=1_6	,    _A=0.05	,    _A=0.05	,    _A=True	,    _A=True	,    _A="relu"	,    _A=1_0_2_4	,    _A=0.1	,    _A=0.1	,    _A=0.0	,    _A=0.02	,    _A=2	,    _A=True	,    _A=False	,    _A="float32"	,    _A=False	,    _A=1_2_8	,    _A=6_4	,    _A=4	,    _A=4	,    _A=0.0_01	,    _A=0.0_01	,    _A="all"	,    _A=False	,    _A=False	,    _A=1.0	,    _A=0.2	,    _A=1	,    _A=0	,    _A=2	,    _A=False	,    **_A	,    ):




											"""simple docstring"""



											__lowerCAmelCase            = vocab_size
											__lowerCAmelCase            = max_position_embeddings
											__lowerCAmelCase            = d_model
											__lowerCAmelCase            = encoder_ffn_dim
											__lowerCAmelCase            = encoder_layers
											__lowerCAmelCase            = encoder_attention_heads
											__lowerCAmelCase            = decoder_ffn_dim
											__lowerCAmelCase            = decoder_layers
											__lowerCAmelCase            = decoder_attention_heads
											__lowerCAmelCase            = dropout
											__lowerCAmelCase            = attention_dropout
											__lowerCAmelCase            = activation_dropout
											__lowerCAmelCase            = activation_function
											__lowerCAmelCase            = init_std
											__lowerCAmelCase            = encoder_layerdrop
											__lowerCAmelCase            = decoder_layerdrop
											__lowerCAmelCase            = use_cache
											__lowerCAmelCase            = encoder_layers
											__lowerCAmelCase            = scale_embedding  # scale factor will be sqrt(d_model) if True
											__lowerCAmelCase            = router_z_loss_coef
											__lowerCAmelCase            = router_aux_loss_coef
											__lowerCAmelCase            = decoder_sparse_step
											__lowerCAmelCase            = encoder_sparse_step
											__lowerCAmelCase            = num_experts
											__lowerCAmelCase            = expert_capacity
											__lowerCAmelCase            = router_bias
											if router_dtype not in ["float32", "float16", "bfloat16"]:
																raise ValueError(f"""`router_dtype` must be one of 'float32', 'float16' or 'bfloat16', got {router_dtype}"""    )
											__lowerCAmelCase            = router_dtype

											__lowerCAmelCase            = router_ignore_padding_tokens
											__lowerCAmelCase            = batch_prioritized_routing
											__lowerCAmelCase            = second_expert_policy
											__lowerCAmelCase            = normalize_router_prob_before_dropping
											__lowerCAmelCase            = moe_eval_capacity_token_fraction
											__lowerCAmelCase            = moe_token_dropout
											__lowerCAmelCase            = output_router_logits
											super().__init__(
											    pad_token_id=_A	,    bos_token_id=_A	,    eos_token_id=_A	,    is_encoder_decoder=_A	,    decoder_start_token_id=_A	,    **_A	,    )




