





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






							def 					__a     (     self   )			->				int:
								a   :	Union[str, Any]			=       ids_tensor([self.batch_size, self.seq_length]   ,      self.vocab_size   )

								a   :	str			=       None
								if self.use_token_type_ids:
									a   :	List[str]			=       ids_tensor([self.batch_size, self.seq_length]   ,      self.type_vocab_size   )

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

								a   :	Any			=       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   ,      )

								a   :	Optional[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 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      *lowerCAmelCase__   )			->				Union[str, Any]:
								a   :	Tuple			=       OpenAIGPTModel(config=lowerCAmelCase__   )
								model.to(lowerCAmelCase__   )
								model.eval()

								a   :	List[str]			=       model(lowerCAmelCase__   ,      token_type_ids=lowerCAmelCase__   ,      head_mask=lowerCAmelCase__   )
								a   :	Optional[int]			=       model(lowerCAmelCase__   ,      token_type_ids=lowerCAmelCase__   )
								a   :	Optional[int]			=       model(lowerCAmelCase__   )

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






							def 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      *lowerCAmelCase__   )			->				Tuple:
								a   :	Any			=       OpenAIGPTLMHeadModel(lowerCAmelCase__   )
								model.to(lowerCAmelCase__   )
								model.eval()

								a   :	List[str]			=       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 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      *lowerCAmelCase__   )			->				Tuple:
								a   :	Tuple			=       OpenAIGPTDoubleHeadsModel(lowerCAmelCase__   )
								model.to(lowerCAmelCase__   )
								model.eval()

								a   :	Dict			=       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 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      *lowerCAmelCase__   )			->				Dict:
								a   :	Optional[Any]			=       self.num_labels
								a   :	Dict			=       OpenAIGPTForSequenceClassification(lowerCAmelCase__   )
								model.to(lowerCAmelCase__   )
								model.eval()

								a   :	str			=       ids_tensor([self.batch_size]   ,      self.type_sequence_label_size   )
								a   :	Tuple			=       model(lowerCAmelCase__   ,      token_type_ids=lowerCAmelCase__   ,      labels=lowerCAmelCase__   )
								self.parent.assertEqual(result.logits.shape   ,      (self.batch_size, self.num_labels)   )






							def 					__a     (     self   )			->				Optional[int]:
								a   :	Optional[int]			=       self.prepare_config_and_inputs()
								(
								    (
								    a
								), (
								    a
								), (
								    a
								), (
								    a
								), (
								    a
								), (
								    a
								), (
								    a
								), 
								)   :	Tuple			=       config_and_inputs
								a   :	List[str]			=       {
								    "input_ids": input_ids,
								    "token_type_ids": token_type_ids,
								    "head_mask": head_mask,
								}

								return config, inputs_dict


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






							def 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__   )			->				int:
								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 					__a     (     self   ,      lowerCAmelCase__   ,      lowerCAmelCase__   ,      lowerCAmelCase__=False   )			->				Tuple:
								a   :	Dict			=       super()._prepare_for_class(lowerCAmelCase__   ,      lowerCAmelCase__   ,      return_labels=lowerCAmelCase__   )

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






							def 					__a     (     self   )			->				Dict:
								a   :	List[str]			=       OpenAIGPTModelTester(self   )
								a   :	Any			=       ConfigTester(self   ,      config_class=lowerCAmelCase__   ,      n_embd=37   )






							def 					__a     (     self   )			->				Dict:
								self.config_tester.run_common_tests()






							def 					__a     (     self   )			->				str:
								a   :	Union[str, Any]			=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__   )






							def 					__a     (     self   )			->				List[str]:
								a   :	Dict			=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase__   )






							def 					__a     (     self   )			->				Any:
								a   :	List[Any]			=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_double_lm_head_model(*lowerCAmelCase__   )






							def 					__a     (     self   )			->				Optional[Any]:
								a   :	int			=       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__   )






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


@require_torch
class  __UpperCamelCase       (		unittest.TestCase		):






							@slow
							def 					__a     (     self   )			->				Any:
								a   :	str			=       OpenAIGPTLMHeadModel.from_pretrained("openai-gpt"   )
								model.to(lowerCAmelCase__   )
								a   :	Union[str, Any]			=       torch.tensor([[481, 4735, 544]]   ,      dtype=torch.long   ,      device=lowerCAmelCase__   )  # the president is
								a   :	str			=       [
								    481,
								    4735,
								    544,
								    246,
								    963,
								    870,
								    762,
								    239,
								    244,
								    4_0477,
								    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

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









"""simple docstring"""




# Copyright 2022 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import argparse
import os
import subprocess

from packaging.version import Version, parse

from accelerate.commands.config.config_args import default_config_file, load_config_from_file


a		:				Optional[int]  			=    '''Run commands across TPU VMs for initial setup before running `accelerate launch`.'''






def    _SCREAMING_SNAKE_CASE	(			_lowercase		:					Any=None      )				->Optional[Any]:






	'''simple docstring'''






	if subparsers is not None:
		a   :	int			=       subparsers.add_parser("tpu-config"					, description=_description      )
	else:
		a   :	List[Any]			=       argparse.ArgumentParser("Accelerate tpu-config command"					, description=_description      )
	# Core arguments
	a   :	Dict			=       parser.add_argument_group(
	    "Config Arguments"					, "Arguments that can be configured through `accelerate config`."      )
	config_args.add_argument(
	    "--config_file"					, type=_lowercase					, default=_lowercase					, help="Path to the config file to use for accelerate."					, )
	config_args.add_argument(
	    "--tpu_name"					, default=_lowercase					, help="The name of the TPU to use. If not specified, will use the TPU specified in the config file."					, )
	config_args.add_argument(
	    "--tpu_zone"					, default=_lowercase					, help="The zone of the TPU to use. If not specified, will use the zone specified in the config file."					, )
	a   :	Any			=       parser.add_argument_group("TPU Arguments"					, "Arguments for options ran inside the TPU."      )
	pod_args.add_argument(
	    "--use_alpha"					, action="store_true"					, help="Whether to use `gcloud alpha` when running the TPU training script instead of `gcloud`."					, )
	pod_args.add_argument(
	    "--command_file"					, default=_lowercase					, help="The path to the file containing the commands to run on the pod on startup."					, )
	pod_args.add_argument(
	    "--command"					, action="append"					, nargs="+"					, help="A command to run on the pod. Can be passed multiple times."					, )
	pod_args.add_argument(
	    "--install_accelerate"					, action="store_true"					, help="Whether to install accelerate on the pod. Defaults to False."					, )
	pod_args.add_argument(
	    "--accelerate_version"					, default="latest"					, help="The version of accelerate to install on the pod. If not specified, will use the latest pypi version. Specify 'dev' to install from GitHub."					, )
	pod_args.add_argument(
	    "--debug"					, action="store_true"					, help="If set, will print the command that would be run instead of running it."      )

	if subparsers is not None:
		parser.set_defaults(func=_lowercase      )
	return parser






def    _SCREAMING_SNAKE_CASE	(			_lowercase		:					Any      )				->Tuple:






	'''simple docstring'''






	a   :	Union[str, Any]			=       None

	# Get the default from the config file if it exists.
	if args.config_file is not None or os.path.isfile(_lowercase      ):
		a   :	Optional[Any]			=       load_config_from_file(args.config_file      )
		if not args.command_file and defaults.command_file is not None and not args.command:
			a   :	int			=       defaults.command_file
		if not args.command and defaults.commands is not None:
			a   :	Union[str, Any]			=       defaults.commands
		if not args.tpu_name:
			a   :	int			=       defaults.tpu_name
		if not args.tpu_zone:
			a   :	Union[str, Any]			=       defaults.tpu_zone
	if args.accelerate_version == "dev":
		a   :	int			=       "git+https://github.com/huggingface/accelerate.git"
	elif args.accelerate_version == "latest":
		a   :	Optional[Any]			=       "accelerate -U"
	elif isinstance(parse(args.accelerate_version      )					, _lowercase      ):
		a   :	Optional[Any]			=       F"""accelerate=={args.accelerate_version}"""

	if not args.command_file and not args.command:
		raise ValueError("You must specify either a command file or a command to run on the pod."      )

	if args.command_file:
		with open(args.command_file					, "r"      ) as f:
			a   :	int			=       [f.read().splitlines()]

    # To turn list of lists into list of strings
	if isinstance(args.command[0]					, _lowercase      ):
		a   :	Union[str, Any]			=       [line for cmd in args.command for line in cmd]
	# Default to the shared folder and install accelerate
	a   :	Tuple			=       ["cd /usr/share"]
	if args.install_accelerate:
		new_cmd += [F"""pip install {args.accelerate_version}"""]
	new_cmd += args.command
	a   :	List[Any]			=       "; ".join(_lowercase      )

	# Then send it to gcloud
	# Eventually try to use google-api-core to do this instead of subprocess
	a   :	str			=       ["gcloud"]
	if args.use_alpha:
		cmd += ["alpha"]
	cmd += [
	    "compute",
	    "tpus",
	    "tpu-vm",
	    "ssh",
	    args.tpu_name,
	    "--zone",
	    args.tpu_zone,
	    "--command",
	    args.command,
	    "--worker",
	    "all",
	]
	if args.debug:
		print(F"""Running {' '.join(_lowercase      )}"""      )
		return
	subprocess.run(_lowercase      )
	print("Successfully setup pod."      )







def    _SCREAMING_SNAKE_CASE	(			)				->Tuple:






	'''simple docstring'''






	a   :	List[Any]			=       tpu_command_parser()
	a   :	Optional[int]			=       parser.parse_args()

	tpu_command_launcher(_lowercase      )


