
"""simple docstring"""







import io
import json

import fsspec
import pytest

from datasets import Dataset, DatasetDict, Features, NamedSplit, Value
from datasets.io.json import JsonDatasetReader, JsonDatasetWriter

from ..utils import assert_arrow_memory_doesnt_increase, assert_arrow_memory_increases
def     __lowerCAmelCase				(_UpperCamelCase       ,  _UpperCamelCase		):
	assert isinstance(lowercase__       ,  lowercase__		)
	assert dataset.num_rows == 4
	assert dataset.num_columns == 3
	assert dataset.column_names == ["col_1", "col_2", "col_3"]
	for feature, expected_dtype in expected_features.items():
		assert dataset.features[feature].dtype == expected_dtype
@pytest.mark.parametrize('keep_in_memory'       ,  [False, True]		)
def     __lowerCAmelCase				(_UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase		):
	__lowerCAmelCase			:						Dict									=      tmp_path / '''cache'''
	__lowerCAmelCase			:						Optional[Any]									=      {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
	with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
		__lowerCAmelCase			:						Union[str, Any]									=      JsonDatasetReader(lowercase__       ,  cache_dir=lowercase__       ,  keep_in_memory=lowercase__		).read()
	_check_json_dataset(lowercase__       ,  lowercase__		)
@pytest.mark.parametrize(
    'features'       ,  [
        None,
        {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'},
        {'col_1': 'string', 'col_2': 'string', 'col_3': 'string'},
        {'col_1': 'int32', 'col_2': 'int32', 'col_3': 'int32'},
        {'col_1': 'float32', 'col_2': 'float32', 'col_3': 'float32'},
    ]       ,  )
def     __lowerCAmelCase				(_UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase		):
	__lowerCAmelCase			:						List[Any]									=      tmp_path / '''cache'''
	__lowerCAmelCase			:						str									=      {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
	__lowerCAmelCase			:						Union[str, Any]									=      features.copy() if features else default_expected_features
	__lowerCAmelCase			:						Any									=      (
	    Features({feature: Value(lowercase__		) for feature, dtype in features.items()}		) if features is not None else None
	)
	__lowerCAmelCase			:						str									=      JsonDatasetReader(lowercase__       ,  features=lowercase__       ,  cache_dir=lowercase__		).read()
	_check_json_dataset(lowercase__       ,  lowercase__		)
@pytest.mark.parametrize(
    'features'       ,  [
        None,
        {'col_3': 'float64', 'col_1': 'string', 'col_2': 'int64'},
    ]       ,  )
def     __lowerCAmelCase				(_UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase		):
	__lowerCAmelCase			:						List[Any]									=      tmp_path / '''cache'''
	__lowerCAmelCase			:						Dict									=      {'''col_3''': '''float64''', '''col_1''': '''string''', '''col_2''': '''int64'''}
	__lowerCAmelCase			:						Tuple									=      features.copy() if features else default_expected_features
	__lowerCAmelCase			:						Dict									=      (
	    Features({feature: Value(lowercase__		) for feature, dtype in features.items()}		) if features is not None else None
	)
	__lowerCAmelCase			:						Optional[Any]									=      JsonDatasetReader(lowercase__       ,  features=lowercase__       ,  cache_dir=lowercase__		).read()
	assert isinstance(lowercase__       ,  lowercase__		)
	assert dataset.num_rows == 2
	assert dataset.num_columns == 3
	assert dataset.column_names == ["col_3", "col_1", "col_2"]
	for feature, expected_dtype in expected_features.items():
		assert dataset.features[feature].dtype == expected_dtype
def     __lowerCAmelCase				(_UpperCamelCase       ,  _UpperCamelCase		):
	# jsonl_312_path features are {"col_3": "float64", "col_1": "string", "col_2": "int64"}
	__lowerCAmelCase			:						Optional[Any]									=      {'''col_2''': '''int64''', '''col_3''': '''float64''', '''col_1''': '''string'''}
	__lowerCAmelCase			:						List[Any]									=      features.copy()
	__lowerCAmelCase			:						List[Any]									=      (
	    Features({feature: Value(lowercase__		) for feature, dtype in features.items()}		) if features is not None else None
	)
	__lowerCAmelCase			:						Dict									=      tmp_path / '''cache'''
	__lowerCAmelCase			:						Dict									=      JsonDatasetReader(lowercase__       ,  features=lowercase__       ,  cache_dir=lowercase__		).read()
	assert isinstance(lowercase__       ,  lowercase__		)
	assert dataset.num_rows == 2
	assert dataset.num_columns == 3
	assert dataset.column_names == ["col_2", "col_3", "col_1"]
	for feature, expected_dtype in expected_features.items():
		assert dataset.features[feature].dtype == expected_dtype
@pytest.mark.parametrize('split'       ,  [None, NamedSplit('train'		), 'train', 'test']		)
def     __lowerCAmelCase				(_UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase		):
	__lowerCAmelCase			:						Dict									=      tmp_path / '''cache'''
	__lowerCAmelCase			:						Dict									=      {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
	__lowerCAmelCase			:						List[str]									=      JsonDatasetReader(lowercase__       ,  cache_dir=lowercase__       ,  split=lowercase__		).read()
	_check_json_dataset(lowercase__       ,  lowercase__		)
	assert dataset.split == split if split else "train"
@pytest.mark.parametrize('path_type'       ,  [str, list]		)
def     __lowerCAmelCase				(_UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase		):
	if issubclass(lowercase__       ,  lowercase__		):
		__lowerCAmelCase			:						Dict									=      jsonl_path
	elif issubclass(lowercase__       ,  lowercase__		):
		__lowerCAmelCase			:						int									=      [jsonl_path]
	__lowerCAmelCase			:						int									=      tmp_path / '''cache'''
	__lowerCAmelCase			:						Any									=      {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
	__lowerCAmelCase			:						List[Any]									=      JsonDatasetReader(lowercase__       ,  cache_dir=lowercase__		).read()
	_check_json_dataset(lowercase__       ,  lowercase__		)
def     __lowerCAmelCase				(_UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase=("train",)		):
	assert isinstance(lowercase__       ,  lowercase__		)
	for split in splits:
		__lowerCAmelCase			:						Optional[int]									=      dataset_dict[split]
		assert dataset.num_rows == 4
		assert dataset.num_columns == 3
		assert dataset.column_names == ["col_1", "col_2", "col_3"]
		for feature, expected_dtype in expected_features.items():
			assert dataset.features[feature].dtype == expected_dtype
@pytest.mark.parametrize('keep_in_memory'       ,  [False, True]		)
def     __lowerCAmelCase				(_UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase		):
	__lowerCAmelCase			:						Dict									=      tmp_path / '''cache'''
	__lowerCAmelCase			:						Union[str, Any]									=      {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
	with assert_arrow_memory_increases() if keep_in_memory else assert_arrow_memory_doesnt_increase():
		__lowerCAmelCase			:						Dict									=      JsonDatasetReader({'train': jsonl_path}       ,  cache_dir=lowercase__       ,  keep_in_memory=lowercase__		).read()
	_check_json_datasetdict(lowercase__       ,  lowercase__		)
@pytest.mark.parametrize(
    'features'       ,  [
        None,
        {'col_1': 'string', 'col_2': 'int64', 'col_3': 'float64'},
        {'col_1': 'string', 'col_2': 'string', 'col_3': 'string'},
        {'col_1': 'int32', 'col_2': 'int32', 'col_3': 'int32'},
        {'col_1': 'float32', 'col_2': 'float32', 'col_3': 'float32'},
    ]       ,  )
def     __lowerCAmelCase				(_UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase		):
	__lowerCAmelCase			:						Union[str, Any]									=      tmp_path / '''cache'''
	__lowerCAmelCase			:						List[Any]									=      {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
	__lowerCAmelCase			:						Union[str, Any]									=      features.copy() if features else default_expected_features
	__lowerCAmelCase			:						Optional[Any]									=      (
	    Features({feature: Value(lowercase__		) for feature, dtype in features.items()}		) if features is not None else None
	)
	__lowerCAmelCase			:						List[Any]									=      JsonDatasetReader({'train': jsonl_path}       ,  features=lowercase__       ,  cache_dir=lowercase__		).read()
	_check_json_datasetdict(lowercase__       ,  lowercase__		)
@pytest.mark.parametrize('split'       ,  [None, NamedSplit('train'		), 'train', 'test']		)
def     __lowerCAmelCase				(_UpperCamelCase       ,  _UpperCamelCase       ,  _UpperCamelCase		):
	if split:
		__lowerCAmelCase			:						Union[str, Any]									=      {split: jsonl_path}
	else:
		__lowerCAmelCase			:						str									=      '''train'''
		__lowerCAmelCase			:						List[str]									=      {'''train''': jsonl_path, '''test''': jsonl_path}
	__lowerCAmelCase			:						Union[str, Any]									=      tmp_path / '''cache'''
	__lowerCAmelCase			:						Union[str, Any]									=      {'''col_1''': '''string''', '''col_2''': '''int64''', '''col_3''': '''float64'''}
	__lowerCAmelCase			:						Dict									=      JsonDatasetReader(lowercase__       ,  cache_dir=lowercase__		).read()
	_check_json_datasetdict(lowercase__       ,  lowercase__       ,  splits=list(path.keys()		)		)
	assert all(dataset[split].split == split for split in path.keys()		)
def     __lowerCAmelCase				(_UpperCamelCase		):
	return json.load(lowercase__		)
def     __lowerCAmelCase				(_UpperCamelCase		):
	return [json.loads(lowercase__		) for line in buffer]




class  A__       :

		@pytest.mark.parametrize('lines, load_json_function'       ,		[(True, load_json_lines), (False, load_json)] )
		def 							__lowerCamelCase							(      self       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE ):
			with io.BytesIO() as buffer:
				JsonDatasetWriter(lowerCAmelCase__       ,		lowerCAmelCase__       ,		lines=lowerCAmelCase__ ).write()
				buffer.seek(0 )
				__lowerCAmelCase			:						Tuple									=      load_json_function(lowerCAmelCase__ )
			assert isinstance(lowerCAmelCase__       ,		lowerCAmelCase__ )
			assert isinstance(exported_content[0]       ,		lowerCAmelCase__ )
			assert len(lowerCAmelCase__ ) == 10

		@pytest.mark.parametrize(
		    'orient, container, keys, len_at'       ,		[
		        ('records', list, {'tokens', 'labels', 'answers', 'id'}, None),
		        ('split', dict, {'columns', 'data'}, 'data'),
		        ('index', dict, set('0123456789' ), None),
		        ('columns', dict, {'tokens', 'labels', 'answers', 'id'}, 'tokens'),
		        ('values', list, None, None),
		        ('table', dict, {'schema', 'data'}, 'data'),
		    ]       ,		)
		def 							__lowerCamelCase							(      self       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE ):
			with io.BytesIO() as buffer:
				JsonDatasetWriter(lowerCAmelCase__       ,		lowerCAmelCase__       ,		lines=lowerCAmelCase__       ,		orient=lowerCAmelCase__ ).write()
				buffer.seek(0 )
				__lowerCAmelCase			:						List[Any]									=      load_json(lowerCAmelCase__ )
			assert isinstance(lowerCAmelCase__       ,		lowerCAmelCase__ )
			if keys:
				if container is dict:
					assert exported_content.keys() == keys
				else:
					assert exported_content[0].keys() == keys
			else:
				assert not hasattr(lowerCAmelCase__       ,		'keys' ) and not hasattr(exported_content[0]       ,		'keys' )
			if len_at:
				assert len(exported_content[len_at] ) == 10
			else:
				assert len(lowerCAmelCase__ ) == 10

		@pytest.mark.parametrize('lines, load_json_function'       ,		[(True, load_json_lines), (False, load_json)] )
		def 							__lowerCamelCase							(      self       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE ):
			with io.BytesIO() as buffer:
				JsonDatasetWriter(lowerCAmelCase__       ,		lowerCAmelCase__       ,		lines=lowerCAmelCase__       ,		num_proc=2 ).write()
				buffer.seek(0 )
				__lowerCAmelCase			:						Tuple									=      load_json_function(lowerCAmelCase__ )
			assert isinstance(lowerCAmelCase__       ,		lowerCAmelCase__ )
			assert isinstance(exported_content[0]       ,		lowerCAmelCase__ )
			assert len(lowerCAmelCase__ ) == 10

		@pytest.mark.parametrize(
		    'orient, container, keys, len_at'       ,		[
		        ('records', list, {'tokens', 'labels', 'answers', 'id'}, None),
		        ('split', dict, {'columns', 'data'}, 'data'),
		        ('index', dict, set('0123456789' ), None),
		        ('columns', dict, {'tokens', 'labels', 'answers', 'id'}, 'tokens'),
		        ('values', list, None, None),
		        ('table', dict, {'schema', 'data'}, 'data'),
		    ]       ,		)
		def 							__lowerCamelCase							(      self       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE ):
			with io.BytesIO() as buffer:
				JsonDatasetWriter(lowerCAmelCase__       ,		lowerCAmelCase__       ,		lines=lowerCAmelCase__       ,		orient=lowerCAmelCase__       ,		num_proc=2 ).write()
				buffer.seek(0 )
				__lowerCAmelCase			:						Tuple									=      load_json(lowerCAmelCase__ )
			assert isinstance(lowerCAmelCase__       ,		lowerCAmelCase__ )
			if keys:
				if container is dict:
					assert exported_content.keys() == keys
				else:
					assert exported_content[0].keys() == keys
			else:
				assert not hasattr(lowerCAmelCase__       ,		'keys' ) and not hasattr(exported_content[0]       ,		'keys' )
			if len_at:
				assert len(exported_content[len_at] ) == 10
			else:
				assert len(lowerCAmelCase__ ) == 10

		def 							__lowerCamelCase							(      self       ,		_SCREAMING_SNAKE_CASE ):
			with pytest.raises(lowerCAmelCase__ ):
				with io.BytesIO() as buffer:
					JsonDatasetWriter(lowerCAmelCase__       ,		lowerCAmelCase__       ,		num_proc=0 )







		@pytest.mark.parametrize('compression, extension'       ,		[('gzip', 'gz'), ('bz2', 'bz2'), ('xz', 'xz')] )
		def 							__lowerCamelCase							(      self       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE       ,		_SCREAMING_SNAKE_CASE ):
			__lowerCAmelCase			:						Tuple									=      tmp_path_factory.mktemp('data' ) / f"test.json.{extension}"
			__lowerCAmelCase			:						Dict									=      str(shared_datadir / f"test_file.json.{extension}" )
			JsonDatasetWriter(lowerCAmelCase__       ,		lowerCAmelCase__       ,		compression=lowerCAmelCase__ ).write()

			with fsspec.open(lowerCAmelCase__       ,		'rb'       ,		compression='infer' ) as f:
				__lowerCAmelCase			:						Dict									=      f.read()
			with fsspec.open(lowerCAmelCase__       ,		'rb'       ,		compression='infer' ) as f:
				__lowerCAmelCase			:						str									=      f.read()
			assert exported_content == original_content



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        _lowercase      :






				'''simple docstring'''



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


				def 		__magic_name__(       self			:Optional[Any]	)		->			List[Any]:
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		ids_tensor([self.batch_size, self.seq_length]			,			self.vocab_size	)

										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		None
										if self.use_token_type_ids:
																__SCREAMING_SNAKE_CASE						:     Union[str, Any]					=		ids_tensor([self.batch_size, self.seq_length]			,			self.type_vocab_size	)

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

										__SCREAMING_SNAKE_CASE						:     Optional[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			,			)

										__SCREAMING_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 		__magic_name__(       self			:Tuple			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:List[str]			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:Any			,			*lowerCAmelCase__			:Union[str, Any]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Any					=		OpenAIGPTModel(config=lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()

										__SCREAMING_SNAKE_CASE						:     Dict					=		model(lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__			,			head_mask=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Tuple					=		model(lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     str					=		model(lowerCAmelCase__	)

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


				def 		__magic_name__(       self			:Optional[Any]			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:Any			,			lowerCAmelCase__			:Dict			,			*lowerCAmelCase__			:List[Any]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		OpenAIGPTLMHeadModel(lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()

										__SCREAMING_SNAKE_CASE						:     Tuple					=		model(lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__			,			labels=lowerCAmelCase__	)
										self.parent.assertEqual(result.loss.shape			,			()	)
										self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.seq_length, self.vocab_size)	)


				def 		__magic_name__(       self			:Tuple			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:Optional[Any]			,			lowerCAmelCase__			:List[str]			,			*lowerCAmelCase__			:Optional[Any]	)		->			Any:
										__SCREAMING_SNAKE_CASE						:     Any					=		OpenAIGPTDoubleHeadsModel(lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()

										__SCREAMING_SNAKE_CASE						:     Any					=		model(lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__			,			labels=lowerCAmelCase__	)
										self.parent.assertEqual(result.loss.shape			,			()	)
										self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.seq_length, self.vocab_size)	)


				def 		__magic_name__(       self			:Dict			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:Union[str, Any]			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:str			,			*lowerCAmelCase__			:Optional[int]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		self.num_labels
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		OpenAIGPTForSequenceClassification(lowerCAmelCase__	)
										model.to(lowerCAmelCase__	)
										model.eval()

										__SCREAMING_SNAKE_CASE						:     List[str]					=		ids_tensor([self.batch_size]			,			self.type_sequence_label_size	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		model(lowerCAmelCase__			,			token_type_ids=lowerCAmelCase__			,			labels=lowerCAmelCase__	)
										self.parent.assertEqual(result.logits.shape			,			(self.batch_size, self.num_labels)	)



				def 		__magic_name__(       self			:Optional[Any]	)		->			str:
										__SCREAMING_SNAKE_CASE						:     str					=		self.prepare_config_and_inputs()
										(
										    (
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			(
										    __SCREAMING_SNAKE_CASE
										)			,			
										)						:     List[str]					=		config_and_inputs
										__SCREAMING_SNAKE_CASE						:     List[str]					=		{
										    '''input_ids''': input_ids,
										    '''token_type_ids''': token_type_ids,
										    '''head_mask''': head_mask,
										}

										return config, inputs_dict




@require_torch
class        _lowercase      (			A__						,			A__						,			A__						,			unittest.TestCase       ):






				'''simple docstring'''



				SCREAMING_SNAKE_CASE__				:			int						=		(
				    (OpenAIGPTModel, OpenAIGPTLMHeadModel, OpenAIGPTDoubleHeadsModel, OpenAIGPTForSequenceClassification)
				    if is_torch_available()
				    else ()
				)
				SCREAMING_SNAKE_CASE__				:			str						=		(
				    (OpenAIGPTLMHeadModel,) if is_torch_available() else ()
				)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
				SCREAMING_SNAKE_CASE__				:			str						=		(
				    {
				        '''feature-extraction''': OpenAIGPTModel,
				        '''text-classification''': OpenAIGPTForSequenceClassification,
				        '''text-generation''': OpenAIGPTLMHeadModel,
				        '''zero-shot''': OpenAIGPTForSequenceClassification,
				    }
				    if is_torch_available()
				    else {}
				)


				def 		__magic_name__(       self			:Optional[int]			,			lowerCAmelCase__			:str			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:List[Any]			,			lowerCAmelCase__			:Union[str, Any]	)		->			Tuple:
										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 		__magic_name__(       self			:List[str]			,			lowerCAmelCase__			:Dict			,			lowerCAmelCase__			:int			,			lowerCAmelCase__			:int=False	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		super()._prepare_for_class(lowerCAmelCase__			,			lowerCAmelCase__			,			return_labels=lowerCAmelCase__	)

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


				def 		__magic_name__(       self			:Optional[int]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     int					=		OpenAIGPTModelTester(self	)
										__SCREAMING_SNAKE_CASE						:     Optional[Any]					=		ConfigTester(self			,			config_class=lowerCAmelCase__			,			n_embd=37	)


				def 		__magic_name__(       self			:Any	)		->			Optional[Any]:
										self.config_tester.run_common_tests()


				def 		__magic_name__(       self			:List[str]	)		->			Union[str, Any]:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__	)


				def 		__magic_name__(       self			:int	)		->			int:
										__SCREAMING_SNAKE_CASE						:     Tuple					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__	)


				def 		__magic_name__(       self			:List[str]	)		->			Dict:
										__SCREAMING_SNAKE_CASE						:     List[str]					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__	)


				def 		__magic_name__(       self			:List[str]	)		->			str:
										__SCREAMING_SNAKE_CASE						:     List[Any]					=		self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__	)



				@slow
				def 		__magic_name__(       self			:Any	)		->			List[Any]:
										for model_name in OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																__SCREAMING_SNAKE_CASE						:     Dict					=		OpenAIGPTModel.from_pretrained(lowerCAmelCase__	)
																self.assertIsNotNone(lowerCAmelCase__	)




@require_torch
class        _lowercase      (			unittest.TestCase       ):






				'''simple docstring'''



				@slow
				def 		__magic_name__(       self			:Union[str, Any]	)		->			Optional[int]:
										__SCREAMING_SNAKE_CASE						:     List[str]					=		OpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''	)
										model.to(lowerCAmelCase__	)
										__SCREAMING_SNAKE_CASE						:     Optional[int]					=		torch.tensor([[481, 4_735, 544]]			,			dtype=torch.long			,			device=lowerCAmelCase__	)  # the president is
										__SCREAMING_SNAKE_CASE						:     Dict					=		[
										    481,
										    4_735,
										    544,
										    246,
										    963,
										    870,
										    762,
										    239,
										    244,
										    40_477,
										    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

										__SCREAMING_SNAKE_CASE						:     Dict					=		model.generate(lowerCAmelCase__			,			do_sample=lowerCAmelCase__	)
										self.assertListEqual(output_ids[0].tolist()			,			lowerCAmelCase__	)
