



"""simple docstring"""

import collections
import json
import os
import re
from typing import TYPE_CHECKING, List, Optional, Tuple

import numpy as np

from ...tokenization_utils_fast import PreTrainedTokenizer
from ...utils import logging


if TYPE_CHECKING:
     from transformers.pipelines.conversational import Conversation


lowercase__    :  int							    =	logging.get_logger(__name__)

lowercase__    :  Union[str, Any]							    =	{'''vocab_file''': '''vocab.txt''', '''emoji_file''': '''emoji.json'''}

lowercase__    :  List[Any]							    =	{
    '''vocab_file''': {
        '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/vocab.txt''',
    },
    '''emoji_file''': {
        '''abeja/gpt-neox-japanese-2.7b''': '''https://huggingface.co/abeja/gpt-neox-japanese-2.7b/resolve/main/emoji.json''',
    },
}

lowercase__    :  str							    =	{
    '''abeja/gpt-neox-japanese-2.7b''': 20_48,
}




def __lowercase			(							_a		,			_a    ):
    with open(_a		,			'''r'''		,			encoding='''utf-8'''    ) as f:
        snake_case_ :     str				=	json.loads(f.read()    )

    snake_case_ :     Any				=	collections.OrderedDict()
    snake_case_ :     Optional[Any]				=	collections.OrderedDict()
    snake_case_ :     Dict				=	collections.OrderedDict()
    with open(_a		,			'''r'''		,			encoding='''utf-8'''    ) as f:
        snake_case_ :     Dict				=	f.readlines()
    snake_case_ :     Optional[Any]				=	[[t.rstrip('''\n'''    )] if (t == ''',''' or ''',''' not in t) else t.rstrip('''\n'''    ).split(''','''    ) for t in token]
    for idx, b in enumerate(_a    ):
        snake_case_ :     Optional[Any]				=	b
        snake_case_ :     Tuple				=	idx
        for wd in b:
            snake_case_ :     int				=	idx

    return vocab, raw_vocab, ids_to_tokens, emoji




class        _UpperCAmelCase       (			lowerCAmelCase__):
 _lowerCAmelCase   :			List[Any]							=		VOCAB_FILES_NAMES
 _lowerCAmelCase   :			Dict							=		PRETRAINED_VOCAB_FILES_MAP
 _lowerCAmelCase   :			List[str]							=		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
 _lowerCAmelCase   :			Optional[int]							=		["""input_ids""", """attention_mask"""]

 def __init__(     self		:       int  ,     lowercase_		:       Optional[Any]  ,     lowercase_		:       List[str]  ,     lowercase_		:       str="<|endoftext|>"  ,     lowercase_		:       List[Any]="<|endoftext|>"  ,     lowercase_		:       str="<|startoftext|>"  ,     lowercase_		:       List[Any]="<|endoftext|>"  ,     lowercase_		:       str=False  ,     **lowercase_		:       int  ,     ):
     super().__init__(
         unk_token=lowercase_  ,     pad_token=lowercase_  ,     bos_token=lowercase_  ,     eos_token=lowercase_  ,     do_clean_text=lowercase_  ,     **lowercase_  ,     )
     if not os.path.isfile(lowercase_				):
         raise ValueError(
             f"Can't find a vocabulary file at path '{vocab_file}'. To load the vocabulary from a Google pretrained"
             ''' model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`'''				)
     if not os.path.isfile(lowercase_				):
         raise ValueError(
             f"Can't find a emoji file at path '{emoji_file}'. To load the emoji information from a Google"
             ''' pretrained model use `tokenizer = GPTNeoXJapaneseokenizer.from_pretrained(PRETRAINED_MODEL_NAME)`'''				)
     snake_case_ :     Union[str, Any]				=	do_clean_text
     snake_case_,	snake_case_,	snake_case_,	snake_case_ :     Optional[int]				=	load_vocab_and_emoji(lowercase_  ,     lowercase_				)
     snake_case_ :     Optional[int]				=	SubWordJapaneseTokenizer(
         vocab=self.vocab  ,     ids_to_tokens=self.ids_to_tokens  ,     emoji=self.emoji				)

 @property
 def 						_snake_case					(     self		:       Union[str, Any]				):
     # self.vocab contains support for character fluctuation unique to Japanese, and has a large number of vocab
     return len(self.raw_vocab				)

 def 						_snake_case					(     self		:       List[Any]				):
     return dict(self.raw_vocab  ,     **self.added_tokens_encoder				)

 def 						_snake_case					(     self		:       Union[str, Any]  ,     lowercase_		:       List[Any]				):
     return self.subword_tokenizer.tokenize(lowercase_  ,     clean=self.do_clean_text				)

 def 						_snake_case					(     self		:       str  ,     lowercase_		:       Dict				):
     return self.vocab.get(lowercase_  ,     self.vocab.get(self.unk_token				)				)

 def 						_snake_case					(     self		:       int  ,     lowercase_		:       Tuple				):
     return self.subword_tokenizer.convert_id_to_token(lowercase_				)

 def 						_snake_case					(     self		:       Union[str, Any]  ,     lowercase_		:       str				):
     snake_case_ :     Tuple				=	''''''.join(lowercase_				).strip()
     return out_string

 def 						_snake_case					(     self		:       Tuple  ,     lowercase_		:       "Conversation"				):
     snake_case_ :     Optional[int]				=	[]
     for is_user, text in conversation.iter_texts():
         input_ids.extend(self.encode(lowercase_  ,     add_special_tokens=lowercase_				) + [self.eos_token_id]				)

     if len(lowercase_				) > self.model_max_length:
         snake_case_ :     Optional[int]				=	input_ids[-self.model_max_length :]
     return input_ids






 def 						_snake_case					(     self		:       Optional[Any]  ,     lowercase_		:       str  ,     lowercase_		:       Optional[str] = None				):
     snake_case_ :     int				=	0
     if os.path.isdir(lowercase_				):
         snake_case_ :     Any				=	os.path.join(
             lowercase_  ,     (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''vocab_file''']				)
         snake_case_ :     List[Any]				=	os.path.join(
             lowercase_  ,     (filename_prefix + '''-''' if filename_prefix else '''''') + VOCAB_FILES_NAMES['''emoji_file''']				)
     else:
         snake_case_ :     Optional[int]				=	(
             (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory + VOCAB_FILES_NAMES['''vocab_file''']
         )
         snake_case_ :     List[Any]				=	(
             (filename_prefix + '''-''' if filename_prefix else '''''') + save_directory + VOCAB_FILES_NAMES['''emoji_file''']
         )
     with open(lowercase_  ,     '''w'''  ,     encoding='''utf-8'''				) as writer:
         for token_index, token in self.ids_to_tokens.items():
             if index != token_index:
                 logger.warning(
                     f"Saving vocabulary to {vocab_file}: vocabulary indices are not consecutive."
                     ''' Please check that the vocabulary is not corrupted!'''				)
                 snake_case_ :     List[Any]				=	token_index
             writer.write(''','''.join(lowercase_				) + '''\n'''				)
             index += 1
     with open(lowercase_  ,     '''w'''  ,     encoding='''utf-8'''				) as writer:
         json.dump(self.emoji  ,     lowercase_				)
     return vocab_file, emoji_file





class        _UpperCAmelCase       (			lowerCAmelCase__):

 def __init__(     self		:       int  ,     lowercase_		:       int  ,     lowercase_		:       List[str]  ,     lowercase_		:       int				):
     snake_case_ :     List[str]				=	vocab  # same as swe
     snake_case_ :     Dict				=	ids_to_tokens  # same as bpe
     snake_case_ :     Dict				=	emoji
     snake_case_ :     int				=	np.max([len(lowercase_				) for w in self.vocab.keys()]				)
     snake_case_ :     str				=	re.compile(r'''(https?|ftp)(:\/\/[-_\.!~*\'()a-zA-Z0-9;\/?:\@&=\+$,%#]+)'''				)
     snake_case_ :     Tuple				=	re.compile(r'''[A-Za-z0-9\._+]*@[\-_0-9A-Za-z]+(\.[A-Za-z]+)*'''				)
     snake_case_ :     List[str]				=	re.compile(r'''[\(]{0,1}[0-9]{2,4}[\)\-\(]{0,1}[0-9]{2,4}[\)\-]{0,1}[0-9]{3,4}'''				)
     snake_case_ :     Union[str, Any]				=	re.compile(
         r'''([12]\d{3}[/\-年])*(0?[1-9]|1[0-2])[/\-月]((0?[1-9]|[12][0-9]|3[01])日?)*(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*'''				)
     snake_case_ :     Any				=	re.compile(
         r'''(明治|大正|昭和|平成|令和|㍾|㍽|㍼|㍻|\u32ff)\d{1,2}年(0?[1-9]|1[0-2])月(0?[1-9]|[12][0-9]|3[01])日(\d{1,2}|:|\d{1,2}時|\d{1,2}分|\(日\)|\(月\)|\(火\)|\(水\)|\(木\)|\(金\)|\(土\)|㈰|㈪|㈫|㈬|㈭|㈮|㈯)*'''				)
     snake_case_ :     Tuple				=	re.compile(
         r'''((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*億)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*万)*((0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*千)*(0|[1-9]\d*|[1-9]\d{0,2}(,\d{3})+)*(千円|万円|千万円|円|千ドル|万ドル|千万ドル|ドル|千ユーロ|万ユーロ|千万ユーロ|ユーロ)+(\(税込\)|\(税抜\)|\+tax)*'''				)
     snake_case_ :     Dict				=	'''─━│┃┄┅┆┇┈┉┊┋┌┍┎┏┐┑┒┓└┕┖┗┘┙┚┛├┝┞┟┠┡┢┣┤┥┦┧┨┩┪┫┬┭┮┯┰┱┲┳┴┵┶┷┸┹┺┻┼┽┾┿╀╁╂╃╄╅╆╇╈╉╊╋╌╍╎╏═║╒╓╔╕╖╗╘╙╚╛╜╝╞╟╠╡╢╣╤╥╦╧╨╩╪╫╬╭╮╯╰╱╲╳╴╵╶╷╸╹╺╻╼╽╾╿'''
     snake_case_ :     Optional[int]				=	'''▀▁▂▃▄▅▆▇█▉▊▋▌▍▎▏▐░▒▓▔▕▖▗▘▙▚▛▜▝▞▟'''
     snake_case_ :     List[Any]				=	str.maketrans({k: '''<BLOCK>''' for k in keisen + blocks}				)

 def __len__(     self		:       Optional[Any]				):
     return len(self.ids_to_tokens				)

 def 						_snake_case					(     self		:       Union[str, Any]  ,     lowercase_		:       Optional[int]				):
     snake_case_ :     int				=	self.content_repattera.sub('''<URL>'''  ,     lowercase_				)
     snake_case_ :     int				=	self.content_repattera.sub('''<EMAIL>'''  ,     lowercase_				)
     snake_case_ :     Tuple				=	self.content_repattera.sub('''<TEL>'''  ,     lowercase_				)
     snake_case_ :     Tuple				=	self.content_repattera.sub('''<DATE>'''  ,     lowercase_				)
     snake_case_ :     Union[str, Any]				=	self.content_repattera.sub('''<DATE>'''  ,     lowercase_				)
     snake_case_ :     Any				=	self.content_repattera.sub('''<PRICE>'''  ,     lowercase_				)
     snake_case_ :     List[Any]				=	content.translate(self.content_transa				)
     while "<BLOCK><BLOCK>" in content:
         snake_case_ :     Optional[Any]				=	content.replace('''<BLOCK><BLOCK>'''  ,     '''<BLOCK>'''				)
     return content

 def 						_snake_case					(     self		:       str  ,     lowercase_		:       List[str]  ,     lowercase_		:       Union[str, Any]=False				):
     snake_case_ :     Any				=	text.replace(''' '''  ,     '''<SP>'''				)
     snake_case_ :     Any				=	text.replace('''　'''  ,     '''<SP>'''				)
     snake_case_ :     Optional[int]				=	text.replace('''\r\n'''  ,     '''<BR>'''				)
     snake_case_ :     Optional[Any]				=	text.replace('''\n'''  ,     '''<BR>'''				)
     snake_case_ :     Optional[Any]				=	text.replace('''\r'''  ,     '''<BR>'''				)
     snake_case_ :     List[Any]				=	text.replace('''\t'''  ,     '''<TAB>'''				)
     snake_case_ :     Tuple				=	text.replace('''—'''  ,     '''ー'''				)
     snake_case_ :     Any				=	text.replace('''−'''  ,     '''ー'''				)
     for k, v in self.emoji["emoji"].items():
         if k in text:
             snake_case_ :     Optional[Any]				=	text.replace(lowercase_  ,     lowercase_				)
     if clean:
         snake_case_ :     Optional[int]				=	self.clean_text(lowercase_				)

     def check_simbol(lowercase_		:       int				):
         snake_case_ :     Optional[Any]				=	x.encode()
         if len(lowercase_				) == 1 and len(lowercase_				) == 2:
             snake_case_ :     Any				=	(int(e[0]				) << 8) + int(e[1]				)
             if (
                 (c >= 0xC2_A1 and c <= 0xC2_BF)
                 or (c >= 0xC7_80 and c <= 0xC7_83)
                 or (c >= 0xCA_B9 and c <= 0xCB_BF)
                 or (c >= 0xCC_80 and c <= 0xCD_A2)
             ):
                 return True
         return False

     def checkuae(lowercase_		:       Any				):
         snake_case_ :     Any				=	x.encode()
         if len(lowercase_				) == 1 and len(lowercase_				) == 3:
             snake_case_ :     Dict				=	(int(e[0]				) << 16) + (int(e[1]				) << 8) + int(e[2]				)
             if c >= 0xE2_80_80 and c <= 0xE2_B0_7F:
                 return True
         return False

     snake_case_ :     Union[str, Any]				=	0
     snake_case_ :     int				=	[]
     while pos < len(lowercase_				):
         snake_case_ :     Tuple				=	min(len(lowercase_				)  ,     pos + self.maxlen + 1				) if text[pos] == '''<''' else pos + 3
         snake_case_ :     Tuple				=	[]  # (token_id, token, pos)
         for e in range(lowercase_  ,     lowercase_  ,     -1				):
             snake_case_ :     Optional[int]				=	text[pos:e]
             if wd in self.vocab:
                 if wd[0] == "<" and len(lowercase_				) > 2:
                     snake_case_ :     int				=	[(self.vocab[wd], wd, e)]
                     break
                 else:
                     candidates.append((self.vocab[wd], wd, e)				)
         if len(lowercase_				) > 0:
             # the smallest token_id is adopted
             snake_case_,	snake_case_,	snake_case_ :     Tuple				=	sorted(lowercase_  ,     key=lambda lowercase_				: x[0]				)[0]
             result.append(lowercase_				)
             snake_case_ :     List[str]				=	e
         else:
             snake_case_ :     Dict				=	pos + 1
             snake_case_ :     Tuple				=	text[pos:end]
             if check_simbol(lowercase_				):
                 result.append('''<KIGOU>'''				)
             elif checkuae(lowercase_				):
                 result.append('''<U2000U2BFF>'''				)
             else:
                 for i in wd.encode('''utf-8'''				):
                     result.append('''<|byte%d|>''' % i				)
             snake_case_ :     Tuple				=	end
     return result






 def 						_snake_case					(     self		:       Optional[int]  ,     lowercase_		:       Dict  ,     lowercase_		:       List[Any]="\n"				):
     snake_case_ :     Tuple				=	[]
     snake_case_ :     List[str]				=	[]
     snake_case_ :     Any				=	self.ids_to_tokens[index][0]
     if word[:6] == "<|byte" and word[-2:] == "|>":
         byte_tokens.append(int(word[6:-2]				)				)
     else:
         if len(lowercase_				) > 0:
             words.append(bytearray(lowercase_				).decode('''utf-8'''  ,     errors='''replace'''				)				)
             snake_case_ :     List[Any]				=	[]
         if word[:7] == "<|emoji" and word[-2:] == "|>":
             words.append(self.emoji['''emoji_inv'''][word]				)
         elif word == "<SP>":
             words.append(''' '''				)
         elif word == "<BR>":
             words.append(lowercase_				)
         elif word == "<TAB>":
             words.append('''\t'''				)
         elif word == "<BLOCK>":
             words.append('''▀'''				)
         elif word == "<KIGOU>":
             words.append('''ǀ'''				)
         elif word == "<U2000U2BFF>":
             words.append('''‖'''				)
         else:
             words.append(lowercase_				)
     if len(lowercase_				) > 0:
         words.append(bytearray(lowercase_				).decode('''utf-8'''  ,     errors='''replace'''				)				)
     snake_case_ :     Dict				=	''''''.join(lowercase_				)
     return text










"""simple docstring"""

import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch

     from transformers import (
         OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
         OpenAIGPTConfig,
         OpenAIGPTDoubleHeadsModel,
         OpenAIGPTForSequenceClassification,
         OpenAIGPTLMHeadModel,
         OpenAIGPTModel,
     )




class        _UpperCAmelCase       :

 def __init__(     self		:       Union[str, Any]  ,     lowercase_		:       List[Any]  ,     lowercase_		:       int=13  ,     lowercase_		:       Optional[int]=7  ,     lowercase_		:       Any=True  ,     lowercase_		:       Dict=True  ,     lowercase_		:       Dict=True  ,     lowercase_		:       Optional[Any]=99  ,     lowercase_		:       Union[str, Any]=32  ,     lowercase_		:       str=5  ,     lowercase_		:       Union[str, Any]=4  ,     lowercase_		:       Any=37  ,     lowercase_		:       Tuple="gelu"  ,     lowercase_		:       Dict=0.1  ,     lowercase_		:       Tuple=0.1  ,     lowercase_		:       Optional[int]=512  ,     lowercase_		:       Optional[Any]=16  ,     lowercase_		:       Optional[Any]=2  ,     lowercase_		:       Optional[Any]=0.02  ,     lowercase_		:       List[Any]=3  ,     lowercase_		:       Union[str, Any]=4  ,     lowercase_		:       List[Any]=None  ,     ):
     snake_case_ :     Any				=	parent
     snake_case_ :     List[str]				=	batch_size
     snake_case_ :     List[Any]				=	seq_length
     snake_case_ :     Optional[int]				=	is_training
     snake_case_ :     Union[str, Any]				=	use_token_type_ids
     snake_case_ :     Optional[Any]				=	use_labels
     snake_case_ :     Union[str, Any]				=	vocab_size
     snake_case_ :     Any				=	hidden_size
     snake_case_ :     List[Any]				=	num_hidden_layers
     snake_case_ :     Any				=	num_attention_heads
     snake_case_ :     Dict				=	intermediate_size
     snake_case_ :     Union[str, Any]				=	hidden_act
     snake_case_ :     Optional[int]				=	hidden_dropout_prob
     snake_case_ :     Optional[Any]				=	attention_probs_dropout_prob
     snake_case_ :     Tuple				=	max_position_embeddings
     snake_case_ :     int				=	type_vocab_size
     snake_case_ :     Tuple				=	type_sequence_label_size
     snake_case_ :     str				=	initializer_range
     snake_case_ :     Tuple				=	num_labels
     snake_case_ :     str				=	num_choices
     snake_case_ :     Any				=	scope
     snake_case_ :     Dict				=	self.vocab_size - 1

 def 						_snake_case					(     self		:       int				):
     snake_case_ :     Optional[Any]				=	ids_tensor([self.batch_size, self.seq_length]  ,     self.vocab_size				)

     snake_case_ :     Optional[Any]				=	None
     if self.use_token_type_ids:
         snake_case_ :     Tuple				=	ids_tensor([self.batch_size, self.seq_length]  ,     self.type_vocab_size				)

     snake_case_ :     str				=	None
     snake_case_ :     Dict				=	None
     snake_case_ :     str				=	None
     if self.use_labels:
         snake_case_ :     Optional[int]				=	ids_tensor([self.batch_size]  ,     self.type_sequence_label_size				)
         snake_case_ :     List[Any]				=	ids_tensor([self.batch_size, self.seq_length]  ,     self.num_labels				)
         snake_case_ :     Tuple				=	ids_tensor([self.batch_size]  ,     self.num_choices				)

     snake_case_ :     int				=	OpenAIGPTConfig(
         vocab_size=self.vocab_size  ,     n_embd=self.hidden_size  ,     n_layer=self.num_hidden_layers  ,     n_head=self.num_attention_heads  ,     n_positions=self.max_position_embeddings  ,     pad_token_id=self.pad_token_id  ,     )

     snake_case_ :     Any				=	ids_tensor([self.num_hidden_layers, self.num_attention_heads]  ,     2				)

     return (
         config,
         input_ids,
         head_mask,
         token_type_ids,
         sequence_labels,
         token_labels,
         choice_labels,
     )

 def 						_snake_case					(     self		:       Tuple  ,     lowercase_		:       Any  ,     lowercase_		:       Union[str, Any]  ,     lowercase_		:       str  ,     lowercase_		:       Dict  ,     *lowercase_		:       Dict				):
     snake_case_ :     List[Any]				=	OpenAIGPTModel(config=lowercase_				)
     model.to(lowercase_				)
     model.eval()

     snake_case_ :     Any				=	model(lowercase_  ,     token_type_ids=lowercase_  ,     head_mask=lowercase_				)
     snake_case_ :     Optional[Any]				=	model(lowercase_  ,     token_type_ids=lowercase_				)
     snake_case_ :     Optional[Any]				=	model(lowercase_				)

     self.parent.assertEqual(result.last_hidden_state.shape  ,     (self.batch_size, self.seq_length, self.hidden_size)				)

 def 						_snake_case					(     self		:       Tuple  ,     lowercase_		:       Dict  ,     lowercase_		:       str  ,     lowercase_		:       Optional[Any]  ,     lowercase_		:       List[Any]  ,     *lowercase_		:       Optional[Any]				):
     snake_case_ :     Union[str, Any]				=	OpenAIGPTLMHeadModel(lowercase_				)
     model.to(lowercase_				)
     model.eval()

     snake_case_ :     Union[str, Any]				=	model(lowercase_  ,     token_type_ids=lowercase_  ,     labels=lowercase_				)
     self.parent.assertEqual(result.loss.shape  ,     ()				)
     self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)				)

 def 						_snake_case					(     self		:       List[str]  ,     lowercase_		:       Dict  ,     lowercase_		:       List[str]  ,     lowercase_		:       Any  ,     lowercase_		:       Dict  ,     *lowercase_		:       Union[str, Any]				):
     snake_case_ :     Tuple				=	OpenAIGPTDoubleHeadsModel(lowercase_				)
     model.to(lowercase_				)
     model.eval()

     snake_case_ :     Dict				=	model(lowercase_  ,     token_type_ids=lowercase_  ,     labels=lowercase_				)
     self.parent.assertEqual(result.loss.shape  ,     ()				)
     self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.seq_length, self.vocab_size)				)

 def 						_snake_case					(     self		:       Any  ,     lowercase_		:       str  ,     lowercase_		:       List[str]  ,     lowercase_		:       Optional[Any]  ,     lowercase_		:       Optional[Any]  ,     *lowercase_		:       Any				):
     snake_case_ :     int				=	self.num_labels
     snake_case_ :     Any				=	OpenAIGPTForSequenceClassification(lowercase_				)
     model.to(lowercase_				)
     model.eval()

     snake_case_ :     Optional[int]				=	ids_tensor([self.batch_size]  ,     self.type_sequence_label_size				)
     snake_case_ :     Optional[Any]				=	model(lowercase_  ,     token_type_ids=lowercase_  ,     labels=lowercase_				)
     self.parent.assertEqual(result.logits.shape  ,     (self.batch_size, self.num_labels)				)






 def 						_snake_case					(     self		:       int				):
     snake_case_ :     Dict				=	self.prepare_config_and_inputs()
     (
         (
         snake_case_
     ),	(
         snake_case_
     ),	(
         snake_case_
     ),	(
         snake_case_
     ),	(
         snake_case_
     ),	(
         snake_case_
     ),	(
         snake_case_
     ),	
     ) :     str				=	config_and_inputs
     snake_case_ :     str				=	{
         '''input_ids''': input_ids,
         '''token_type_ids''': token_type_ids,
         '''head_mask''': head_mask,
     }

     return config, inputs_dict





@require_torch
class        _UpperCAmelCase       (			lowerCAmelCase__    ,     lowerCAmelCase__    ,     lowerCAmelCase__    ,     unittest.TestCase):
 _lowerCAmelCase   :			Dict							=		(
     (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
     if is_torch_available()
     else ()
 )
 _lowerCAmelCase   :			int							=		(
     (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
 )  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
 _lowerCAmelCase   :			Union[str, Any]							=		(
     {
         """feature-extraction""": OpenAIGPTModel,
         """text-classification""": OpenAIGPTForSequenceClassification,
         """text-generation""": OpenAIGPTLMHeadModel,
         """zero-shot""": OpenAIGPTForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )

 def 						_snake_case					(     self		:       Tuple  ,     lowercase_		:       Optional[int]  ,     lowercase_		:       int  ,     lowercase_		:       List[Any]  ,     lowercase_		:       List[Any]  ,     lowercase_		:       Union[str, Any]				):
     if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
         # Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
         # `OpenAIGPTConfig` was never used in pipeline tests, either because of a missing checkpoint or because a
         # tiny config could not be created.
         return True

     return False

 def 						_snake_case					(     self		:       Optional[int]  ,     lowercase_		:       List[Any]  ,     lowercase_		:       Optional[int]  ,     lowercase_		:       List[str]=False				):
     snake_case_ :     Dict				=	super()._prepare_for_class(lowercase_  ,     lowercase_  ,     return_labels=lowercase_				)

     if return_labels:
         if model_class.__name__ == "OpenAIGPTDoubleHeadsModel":
             snake_case_ :     List[str]				=	torch.zeros(
                 (self.model_tester.batch_size, self.model_tester.num_choices, self.model_tester.seq_length)  ,     dtype=torch.long  ,     device=lowercase_  ,     )
             snake_case_ :     int				=	inputs_dict['''labels''']
             snake_case_ :     Optional[Any]				=	inputs_dict['''labels''']
             snake_case_ :     int				=	torch.zeros(
                 (self.model_tester.batch_size, self.model_tester.num_choices)  ,     dtype=torch.long  ,     device=lowercase_  ,     )
             snake_case_ :     Tuple				=	torch.zeros(
                 self.model_tester.batch_size  ,     dtype=torch.long  ,     device=lowercase_				)
     return inputs_dict

 def 						_snake_case					(     self		:       Any				):
     snake_case_ :     List[str]				=	OpenAIGPTModelTester(self				)
     snake_case_ :     Dict				=	ConfigTester(self  ,     config_class=lowercase_  ,     n_embd=37				)

 def 						_snake_case					(     self		:       List[str]				):
     self.config_tester.run_common_tests()

 def 						_snake_case					(     self		:       Optional[Any]				):
     snake_case_ :     Tuple				=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_model(*lowercase_				)

 def 						_snake_case					(     self		:       List[str]				):
     snake_case_ :     List[str]				=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_lm_head_model(*lowercase_				)

 def 						_snake_case					(     self		:       int				):
     snake_case_ :     Any				=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_double_lm_head_model(*lowercase_				)

 def 						_snake_case					(     self		:       List[str]				):
     snake_case_ :     Optional[int]				=	self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowercase_				)






 @slow
 def 						_snake_case					(     self		:       Dict				):
     for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         snake_case_ :     Optional[Any]				=	OpenAIGPTModel.from_pretrained(lowercase_				)
         self.assertIsNotNone(lowercase_				)





@require_torch
class        _UpperCAmelCase       (			unittest.TestCase):

 @slow
 def 						_snake_case					(     self		:       Optional[int]				):
     snake_case_ :     Optional[Any]				=	OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''				)
     model.to(lowercase_				)
     snake_case_ :     List[str]				=	torch.tensor([[481, 4735, 544]]  ,     dtype=torch.long  ,     device=lowercase_				)  # the president is
     snake_case_ :     List[Any]				=	[
         481,
         4735,
         544,
         246,
         963,
         870,
         762,
         239,
         244,
         40477,
         244,
         249,
         719,
         881,
         487,
         544,
         240,
         244,
         603,
         481,
     ]  # the president is a very good man. " \n " i\'m sure he is, " said the

     snake_case_ :     Optional[Any]				=	model.generate(lowercase_  ,     do_sample=lowercase_				)
     self.assertListEqual(output_ids[0].tolist()  ,     lowercase_				)





