
import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
						import os

						import jax.numpy as jnp
						from jax import jit

						from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
						from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

						__a  			=					'0.12'  # assumed parallelism: 8


if is_torch_available():
						import torch




def 				a						(							snake_case__:					Dict						,   snake_case__:					List[str]						,   snake_case__:					str=None					):


				'''simple docstring'''
				if rng is None:
								lowercase_       =					random.Random()

				lowercase_       =					1
				for dim in shape:
								total_dims *= dim

				lowercase_       =					[]
				for _ in range(snake_case_					):
								values.append(rng.randint(0						,   vocab_size - 1					)					)

				lowercase_       =					np.array(snake_case_						,   dtype=jnp.intaa					).reshape(snake_case_					)

				return output




def 				a						(							snake_case__:					int						,   snake_case__:					List[str]=None					):


				'''simple docstring'''
				lowercase_       =					ids_tensor(snake_case_						,   vocab_size=2						,   rng=snake_case_					)
				# make sure that at least one token is attended to for each batch
				lowercase_       =					1
				return attn_mask
@require_flax
class  lowercase__:



	"""simple docstring"""




	a			:Optional[int]         =	None
	a			:Union[str, Any]         =	()
	def 						_lowercase  (       self  :			int       )       ->     Dict:
					lowercase_  ,	lowercase_       =					self.model_tester.prepare_config_and_inputs_for_common()

					# cut to half length & take max batch_size 3
					lowercase_       =					2
					lowercase_       =					inputs['''input_ids'''].shape[-1] // 2
					lowercase_       =					inputs['''input_ids'''][:max_batch_size, :sequence_length]

					lowercase_       =					jnp.ones_like(a__       )
					lowercase_       =					attention_mask[:max_batch_size, :sequence_length]

					# generate max 5 tokens
					lowercase_       =					input_ids.shape[-1] + 5
					if config.eos_token_id is not None and config.pad_token_id is None:
									# hack to allow generate for models such as GPT2 as is done in `generate()`
									lowercase_       =					config.eos_token_id
					return config, input_ids, attention_mask, max_length
	@is_pt_flax_cross_test
	def 						_lowercase  (       self  :			Optional[int]       )       ->     Any:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					self._get_input_ids_and_config()
					lowercase_       =					False
					lowercase_       =					max_length
					lowercase_       =					0

					for model_class in self.all_generative_model_classes:
									lowercase_       =					model_class(a__       )

									lowercase_       =					model_class.__name__[4:]  # Skip the "Flax" at the beginning
									lowercase_       =					getattr(a__       ,							a__       )
									lowercase_       =					pt_model_class(a__       ).eval()
									lowercase_       =					load_flax_weights_in_pytorch_model(a__       ,							flax_model.params       )

									lowercase_       =					flax_model.generate(a__       ).sequences
									lowercase_       =					pt_model.generate(torch.tensor(a__       ,							dtype=torch.long       )       )

									if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
													lowercase_       =					flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

									self.assertListEqual(pt_generation_outputs.numpy().tolist()       ,							flax_generation_outputs.tolist()       )
	def 						_lowercase  (       self  :			Optional[int]       )       ->     str:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					self._get_input_ids_and_config()
					lowercase_       =					False
					lowercase_       =					max_length

					for model_class in self.all_generative_model_classes:
									lowercase_       =					model_class(a__       )

									lowercase_       =					model.generate(a__       ).sequences
									self.assertEqual(generation_outputs.shape[-1]       ,							a__       )

									lowercase_       =					jit(model.generate       )
									lowercase_       =					jit_generate(a__       ).sequences

									self.assertListEqual(generation_outputs.tolist()       ,							jit_generation_outputs.tolist()       )
	def 						_lowercase  (       self  :			List[Any]       )       ->     List[str]:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					self._get_input_ids_and_config()
					lowercase_       =					True
					lowercase_       =					max_length

					for model_class in self.all_generative_model_classes:
									lowercase_       =					model_class(a__       )

									lowercase_       =					model.generate(a__       ).sequences
									self.assertEqual(generation_outputs.shape[-1]       ,							a__       )

									lowercase_       =					jit(model.generate       )
									lowercase_       =					jit_generate(a__       ).sequences

									self.assertListEqual(generation_outputs.tolist()       ,							jit_generation_outputs.tolist()       )
	def 						_lowercase  (       self  :			int       )       ->     Dict:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					self._get_input_ids_and_config()
					lowercase_       =					False
					lowercase_       =					max_length
					lowercase_       =					2

					for model_class in self.all_generative_model_classes:
									lowercase_       =					model_class(a__       )

									lowercase_       =					model.generate(a__       ).sequences
									self.assertEqual(generation_outputs.shape[-1]       ,							a__       )

									lowercase_       =					jit(model.generate       )
									lowercase_       =					jit_generate(a__       ).sequences

									self.assertListEqual(generation_outputs.tolist()       ,							jit_generation_outputs.tolist()       )
	def 						_lowercase  (       self  :			Optional[int]       )       ->     Optional[int]:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					self._get_input_ids_and_config()
					lowercase_       =					False
					lowercase_       =					max_length
					lowercase_       =					2
					lowercase_       =					2

					for model_class in self.all_generative_model_classes:
									lowercase_       =					model_class(a__       )

									lowercase_       =					model.generate(a__       ).sequences
									self.assertEqual(generation_outputs.shape[0]       ,							input_ids.shape[0] * config.num_return_sequences       )
	def 						_lowercase  (       self  :			List[str]       )       ->     Any:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					self._get_input_ids_and_config()
					lowercase_       =					True
					lowercase_       =					max_length
					lowercase_       =					0.8
					lowercase_       =					1_0
					lowercase_       =					0.3
					lowercase_       =					1
					lowercase_       =					8
					lowercase_       =					9

					for model_class in self.all_generative_model_classes:
									lowercase_       =					model_class(a__       )

									lowercase_       =					model.generate(a__       ).sequences
									self.assertEqual(generation_outputs.shape[-1]       ,							a__       )

									lowercase_       =					jit(model.generate       )
									lowercase_       =					jit_generate(a__       ).sequences

									self.assertListEqual(generation_outputs.tolist()       ,							jit_generation_outputs.tolist()       )
	def 						_lowercase  (       self  :			str       )       ->     Optional[Any]:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					self._get_input_ids_and_config()
					lowercase_       =					max_length
					lowercase_       =					1
					lowercase_       =					8
					lowercase_       =					9

					for model_class in self.all_generative_model_classes:
									lowercase_       =					model_class(a__       )

									lowercase_       =					model.generate(a__       ).sequences
									self.assertEqual(generation_outputs.shape[-1]       ,							a__       )

									lowercase_       =					jit(model.generate       )
									lowercase_       =					jit_generate(a__       ).sequences

									self.assertListEqual(generation_outputs.tolist()       ,							jit_generation_outputs.tolist()       )
	def 						_lowercase  (       self  :			Dict       )       ->     Tuple:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					self._get_input_ids_and_config()
					lowercase_       =					max_length
					lowercase_       =					2
					lowercase_       =					1
					lowercase_       =					8
					lowercase_       =					9

					for model_class in self.all_generative_model_classes:
									lowercase_       =					model_class(a__       )

									lowercase_       =					model.generate(a__       ).sequences
									self.assertEqual(generation_outputs.shape[-1]       ,							a__       )

									lowercase_       =					jit(model.generate       )
									lowercase_       =					jit_generate(a__       ).sequences

									self.assertListEqual(generation_outputs.tolist()       ,							jit_generation_outputs.tolist()       )
	def 						_lowercase  (       self  :			Optional[Any]       )       ->     Dict:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					self._get_input_ids_and_config()

					# pad attention mask on the left
					lowercase_       =					attention_mask.at[(0, 0)].set(0       )

					lowercase_       =					False
					lowercase_       =					max_length

					for model_class in self.all_generative_model_classes:
									lowercase_       =					model_class(a__       )

									lowercase_       =					model.generate(a__       ,							attention_mask=a__       ).sequences
									self.assertEqual(generation_outputs.shape[-1]       ,							a__       )

									lowercase_       =					jit(model.generate       )
									lowercase_       =					jit_generate(a__       ,							attention_mask=a__       ).sequences

									self.assertListEqual(generation_outputs.tolist()       ,							jit_generation_outputs.tolist()       )
	def 						_lowercase  (       self  :			Any       )       ->     List[Any]:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					self._get_input_ids_and_config()

					# pad attention mask on the left
					lowercase_       =					attention_mask.at[(0, 0)].set(0       )

					lowercase_       =					True
					lowercase_       =					max_length

					for model_class in self.all_generative_model_classes:
									lowercase_       =					model_class(a__       )

									lowercase_       =					model.generate(a__       ,							attention_mask=a__       ).sequences
									self.assertEqual(generation_outputs.shape[-1]       ,							a__       )

									lowercase_       =					jit(model.generate       )
									lowercase_       =					jit_generate(a__       ,							attention_mask=a__       ).sequences

									self.assertListEqual(generation_outputs.tolist()       ,							jit_generation_outputs.tolist()       )




	def 						_lowercase  (       self  :			str       )       ->     List[Any]:
					lowercase_  ,	lowercase_  ,	lowercase_  ,	lowercase_       =					self._get_input_ids_and_config()

					# pad attention mask on the left
					lowercase_       =					attention_mask.at[(0, 0)].set(0       )

					lowercase_       =					2
					lowercase_       =					max_length

					for model_class in self.all_generative_model_classes:
									lowercase_       =					model_class(a__       )

									lowercase_       =					model.generate(a__       ,							attention_mask=a__       ).sequences
									self.assertEqual(generation_outputs.shape[-1]       ,							a__       )

									lowercase_       =					jit(model.generate       )
									lowercase_       =					jit_generate(a__       ,							attention_mask=a__       ).sequences

									self.assertListEqual(generation_outputs.tolist()       ,							jit_generation_outputs.tolist()       )

@require_flax
class  lowercase__(				unittest.TestCase							):



	"""simple docstring"""




	def 						_lowercase  (       self  :			Any       )       ->     str:
					lowercase_       =					AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-bert'''       )
					lowercase_       =					FlaxAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-bert-flax-only'''       )

					lowercase_       =					'''Hello world'''
					lowercase_       =					tokenizer(a__       ,							return_tensors='''np'''       ).input_ids

					# typos are quickly detected (the correct argument is `do_sample`)
					with self.assertRaisesRegex(a__       ,							'''do_samples'''       ):
									model.generate(a__       ,							do_samples=a__       )

					# arbitrary arguments that will not be used anywhere are also not accepted
					with self.assertRaisesRegex(a__       ,							'''foo'''       ):
									lowercase_       =					{'''foo''': '''bar'''}
									model.generate(a__       ,							**a__       )











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,
)


snake_case_				=			logging.getLogger(__name__)

@dataclass(frozen=_UpperCAmelCase  )
class 		SCREAMING_SNAKE_CASE__				:
      A_						:				str
      A_						:				str
      A_						:				Optional[str]							=    None
      A_						:				Optional[str]							=    None
      A_						:				Optional[str]							=    None






@dataclass(frozen=_UpperCAmelCase  )
class 		SCREAMING_SNAKE_CASE__				:
      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 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase  ):
          A_						:				List[InputFeatures]


          def __init__(self       :      int				,			a__       :      str				,			a__       :      PreTrainedTokenizer				,			a__       :      str				,			a__       :      Optional[int] = None				,			a__       :      List[Any]=False				,			a__       :      bool = False				,			):





             """simple docstring"""



             __snake_case							       =   hans_processors[task]()

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

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

                   __snake_case							       =   (
                       processor.get_dev_examples(a__      ) if evaluate else processor.get_train_examples(a__      )
                   )

                   logger.info('''Training examples: %s'''				,			len(a__      )      )
                   __snake_case							       =   hans_convert_examples_to_features(a__				,			a__				,			a__				,			a__      )
                   logger.info('''Saving features into cached file %s'''				,			a__      )
                   torch.save(self.features				,			a__      )


          def __len__(self       :      int      ):





             """simple docstring"""



             return len(self.features      )


          def __getitem__(self       :      Dict				,			a__       :      List[Any]      ):





             """simple docstring"""



             return self.features[i]


          def    a (self       :      List[Any]      ):





             """simple docstring"""



             return self.label_list


if is_tf_available():
    import tensorflow as tf

    class 		SCREAMING_SNAKE_CASE__				:
          A_						:				List[InputFeatures]


          def __init__(self       :      Tuple				,			a__       :      str				,			a__       :      PreTrainedTokenizer				,			a__       :      str				,			a__       :      Optional[int] = 128				,			a__       :      Any=False				,			a__       :      bool = False				,			):





             """simple docstring"""



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

             __snake_case							       =   processor.get_dev_examples(a__      ) if evaluate else processor.get_train_examples(a__      )
             __snake_case							       =   hans_convert_examples_to_features(a__				,			a__				,			a__				,			a__      )

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

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

             __snake_case							       =   tf.data.Dataset.from_generator(
                 a__				,			(
                     {
                         '''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    a (self       :      Union[str, Any]      ):





             """simple docstring"""



             return self.dataset


          def __len__(self       :      Dict      ):





             """simple docstring"""



             return len(self.features      )


          def __getitem__(self       :      Any				,			a__       :      Dict      ):





             """simple docstring"""



             return self.features[i]


          def    a (self       :      str      ):





             """simple docstring"""



             return self.label_list

class 		SCREAMING_SNAKE_CASE__				(       _UpperCAmelCase  ):


      def    a (self       :      Dict				,			a__       :      Dict      ):





         """simple docstring"""



         return self._create_examples(self._read_tsv(os.path.join(a__				,			'''heuristics_train_set.txt'''      )      )				,			'''train'''      )


      def    a (self       :      Optional[int]				,			a__       :      Tuple      ):





         """simple docstring"""



         return self._create_examples(self._read_tsv(os.path.join(a__				,			'''heuristics_evaluation_set.txt'''      )      )				,			'''dev'''      )


      def    a (self       :      int      ):





         """simple docstring"""



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


      def    a (self       :      Any				,			a__       :      Optional[int]				,			a__       :      List[Any]      ):





         """simple docstring"""



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






def lowerCamelCase__    (							snake_case_	:  List[InputExample]       ,      snake_case_	:  List[str]       ,      snake_case_	:  int       ,      snake_case_	:  PreTrainedTokenizer       ,      )      ->				List[str]:
   __snake_case							       =   {label: i for i, label in enumerate(snake_case_       )}

   __snake_case							       =   []
   for ex_index, example in tqdm.tqdm(enumerate(snake_case_       )       ,      desc='''convert examples to features'''       ):
      if ex_index % 1_0000 == 0:
         logger.info('''Writing example %d''' % (ex_index)       )

      __snake_case							       =   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_       ,      )

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

      __snake_case							       =   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


snake_case_				=			{
    'hans': 3,
}







snake_case_				=			{
    'hans': HansProcessor,
}

