



"""simple docstring"""
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,
)


_lowerCAmelCase    :Optional[Any]     =					logging.getLogger(__name__)
@dataclass(frozen=a      )
class 							_UpperCAmelCase							:





     '''simple docstring'''



     a__									=42
     a__									=42
     a__									=None
     a__									=None
     a__									=None



@dataclass(frozen=a      )
class 							_UpperCAmelCase							:





     '''simple docstring'''



     a__									=42
     a__									=None
     a__									=None
     a__									=None
     a__									=None


if is_torch_available():
  import torch
  from torch.utils.data import Dataset
  class 							_UpperCAmelCase							( a      ):





       '''simple docstring'''



       a__									=42





       def __init__(					self       ,	A       ,	A       ,	A       ,	A = None       ,	A=False       ,	A = False       ,	)							->					str:
           _UpperCAmelCase							:  Union[str, Any]											=    hans_processors[task]()

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

           # Make sure only the first process in distributed training processes the dataset,
           # and the others will use the cache.
           _UpperCAmelCase							:  str											=    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}'		)
                   _UpperCAmelCase							:  str											=    torch.load(A		)
               else:
                   logger.info(f'Creating features from dataset file at {data_dir}'		)

                   _UpperCAmelCase							:  Any											=    (
                       processor.get_dev_examples(A		) if evaluate else processor.get_train_examples(A		)
                   )

                   logger.info('''Training examples: %s'''       ,	len(A		)		)
                   _UpperCAmelCase							:  Optional[Any]											=    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		)							->					str:
           return len(self.features		)





       def __getitem__(					self       ,	A		)							->					InputFeatures:
           return self.features[i]





       def  __lowerCAmelCase	(					self		)							->					Union[str, Any]:
           return self.label_list


if is_tf_available():
  import tensorflow as tf
  class 							_UpperCAmelCase							:





       '''simple docstring'''



       a__									=42





       def __init__(					self       ,	A       ,	A       ,	A       ,	A = 1_2_8       ,	A=False       ,	A = False       ,	)							->					List[Any]:
           _UpperCAmelCase							:  List[str]											=    hans_processors[task]()
           _UpperCAmelCase							:  Tuple											=    processor.get_labels()
           if tokenizer.__class__ in (
               RobertaTokenizer,
               RobertaTokenizerFast,
               XLMRobertaTokenizer,
               BartTokenizer,
               BartTokenizerFast,
           ):
               # HACK(label indices are swapped in RoBERTa pretrained model)
               _UpperCAmelCase							,				_UpperCAmelCase							:  Optional[Any]											=    label_list[2], label_list[1]
           _UpperCAmelCase							:  List[str]											=    label_list

           _UpperCAmelCase							:  Any											=    processor.get_dev_examples(A		) if evaluate else processor.get_train_examples(A		)
           _UpperCAmelCase							:  Optional[int]											=    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_0_0_0_0 == 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,
                   )

           _UpperCAmelCase							:  Optional[Any]											=    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  __lowerCAmelCase	(					self		)							->					List[str]:
           return self.dataset





       def __len__(					self		)							->					List[Any]:
           return len(self.features		)





       def __getitem__(					self       ,	A		)							->					InputFeatures:
           return self.features[i]





       def  __lowerCAmelCase	(					self		)							->					List[str]:
           return self.label_list
class 							_UpperCAmelCase							( a      ):





     '''simple docstring'''



     def  __lowerCAmelCase	(					self       ,	A		)							->					Optional[int]:
         return self._create_examples(self._read_tsv(os.path.join(A       ,	'''heuristics_train_set.txt'''		)		)       ,	'''train'''		)





     def  __lowerCAmelCase	(					self       ,	A		)							->					Optional[Any]:
         return self._create_examples(self._read_tsv(os.path.join(A       ,	'''heuristics_evaluation_set.txt'''		)		)       ,	'''dev'''		)





     def  __lowerCAmelCase	(					self		)							->					Dict:
         return ["contradiction", "entailment", "neutral"]





     def  __lowerCAmelCase	(					self       ,	A       ,	A		)							->					Union[str, Any]:
         _UpperCAmelCase							:  List[str]											=    []
         for i, line in enumerate(A		):
             if i == 0:
                 continue
             _UpperCAmelCase							:  Any											=    '''%s-%s''' % (set_type, line[0])
             _UpperCAmelCase							:  List[str]											=    line[5]
             _UpperCAmelCase							:  int											=    line[6]
             _UpperCAmelCase							:  Union[str, Any]											=    line[7][2:] if line[7].startswith('''ex'''		) else line[7]
             _UpperCAmelCase							:  Optional[Any]											=    line[0]
             examples.append(InputExample(guid=A       ,	text_a=A       ,	text_b=A       ,	label=A       ,	pairID=A		)		)
         return examples





def  lowerCamelCase_						(UpperCamelCase__		:		List[InputExample]     ,       UpperCamelCase__		:		List[str]     ,       UpperCamelCase__		:		int     ,       UpperCamelCase__		:		PreTrainedTokenizer     ,       ):
    _UpperCAmelCase							:  Optional[int]											=    {label: i for i, label in enumerate(UpperCamelCase__							)}

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

        _UpperCAmelCase							:  List[str]											=    tokenizer(
            example.text_a     ,       example.text_b     ,       add_special_tokens=UpperCamelCase__     ,       max_length=UpperCamelCase__     ,       padding='''max_length'''     ,       truncation=UpperCamelCase__     ,       return_overflowing_tokens=UpperCamelCase__     ,       )

        _UpperCAmelCase							:  Any											=    label_map[example.label] if example.label in label_map else 0

        _UpperCAmelCase							:  Tuple											=    int(example.pairID							)

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

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

    return features


_lowerCAmelCase    :str     =					{
    'hans': 3,
}


_lowerCAmelCase    :Any     =					{
    'hans': HansProcessor,
}







"""simple docstring"""
import argparse
import os

import torch

from transformers.utils import WEIGHTS_NAME


_lowerCAmelCase    :int     =					['small', 'medium', 'large']

_lowerCAmelCase    :int     =					'lm_head.decoder.weight'
_lowerCAmelCase    :Dict     =					'lm_head.weight'





def  lowerCamelCase_						(UpperCamelCase__		:		str     ,       UpperCamelCase__		:		str							):
    _UpperCAmelCase							:  List[Any]											=    torch.load(UpperCamelCase__							)
    _UpperCAmelCase							:  List[str]											=    d.pop(UpperCamelCase__							)
    os.makedirs(UpperCamelCase__     ,       exist_ok=UpperCamelCase__							)
    torch.save(UpperCamelCase__     ,       os.path.join(UpperCamelCase__     ,       UpperCamelCase__							)							)


if __name__ == "__main__":
  _lowerCAmelCase    :Dict     =					argparse.ArgumentParser()
  parser.add_argument('--dialogpt_path', default='.', type=str)
  _lowerCAmelCase    :str     =					parser.parse_args()
  for MODEL in DIALOGPT_MODELS:
    _lowerCAmelCase    :Tuple     =					os.path.join(args.dialogpt_path, f"{MODEL}_ft.pkl")
    _lowerCAmelCase    :int     =					f"./DialoGPT-{MODEL}"
    convert_dialogpt_checkpoint(
        checkpoint_path,
        pytorch_dump_folder_path,
    )


