




"""simple docstring"""





from __future__ import annotations

import unittest

from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available
from transformers.testing_utils import require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
					import tensorflow as tf

					from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel



@require_tf
class      a__				:
			lowercase_    =	BlenderbotConfig
			lowercase_    =	{}
			lowercase_    =	"gelu"


			def __init__(      self      :    Union[str, Any]      ,  UpperCamelCase_      :    Dict      ,  UpperCamelCase_      :    int=13      ,  UpperCamelCase_      :    int=7      ,  UpperCamelCase_      :    Any=True      ,  UpperCamelCase_      :    List[Any]=False      ,  UpperCamelCase_      :    Optional[Any]=99      ,  UpperCamelCase_      :    Union[str, Any]=32      ,  UpperCamelCase_      :    Optional[int]=2      ,  UpperCamelCase_      :    Dict=4      ,  UpperCamelCase_      :    Any=37      ,  UpperCamelCase_      :    str=0.1      ,  UpperCamelCase_      :    List[Any]=0.1      ,  UpperCamelCase_      :    Union[str, Any]=20      ,  UpperCamelCase_      :    str=2      ,  UpperCamelCase_      :    List[str]=1      ,  UpperCamelCase_      :    Dict=0      ,  ):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[int]		=					parent
						__UpperCAmelCase	: Optional[Any]		=					batch_size
						__UpperCAmelCase	: Tuple		=					seq_length
						__UpperCAmelCase	: Dict		=					is_training
						__UpperCAmelCase	: int		=					use_labels
						__UpperCAmelCase	: Optional[int]		=					vocab_size
						__UpperCAmelCase	: Optional[Any]		=					hidden_size
						__UpperCAmelCase	: Any		=					num_hidden_layers
						__UpperCAmelCase	: int		=					num_attention_heads
						__UpperCAmelCase	: Tuple		=					intermediate_size

						__UpperCAmelCase	: List[Any]		=					hidden_dropout_prob
						__UpperCAmelCase	: int		=					attention_probs_dropout_prob
						__UpperCAmelCase	: Union[str, Any]		=					max_position_embeddings
						__UpperCAmelCase	: Dict		=					eos_token_id
						__UpperCAmelCase	: Optional[Any]		=					pad_token_id
						__UpperCAmelCase	: List[str]		=					bos_token_id


			def       a_     (      self      :    Optional[Any]):


						"""simple docstring"""




						__UpperCAmelCase	: Union[str, Any]		=					ids_tensor([self.batch_size, self.seq_length - 1]      ,  self.vocab_size)
						__UpperCAmelCase	: str		=					tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size)      ,  1)
						__UpperCAmelCase	: Optional[Any]		=					tf.concat([input_ids, eos_tensor]      ,  axis=1)

						__UpperCAmelCase	: Tuple		=					ids_tensor([self.batch_size, self.seq_length]      ,  self.vocab_size)

						__UpperCAmelCase	: List[str]		=					self.config_cls(
						    vocab_size=self.vocab_size      ,  d_model=self.hidden_size      ,  encoder_layers=self.num_hidden_layers      ,  decoder_layers=self.num_hidden_layers      ,  encoder_attention_heads=self.num_attention_heads      ,  decoder_attention_heads=self.num_attention_heads      ,  encoder_ffn_dim=self.intermediate_size      ,  decoder_ffn_dim=self.intermediate_size      ,  dropout=self.hidden_dropout_prob      ,  attention_dropout=self.attention_probs_dropout_prob      ,  max_position_embeddings=self.max_position_embeddings      ,  eos_token_ids=[2]      ,  bos_token_id=self.bos_token_id      ,  pad_token_id=self.pad_token_id      ,  decoder_start_token_id=self.pad_token_id      ,  **self.config_updates      ,  )
						__UpperCAmelCase	: Optional[Any]		=					prepare_blenderbot_inputs_dict(UpperCamelCase_      ,  UpperCamelCase_      ,  UpperCamelCase_)
						return config, inputs_dict


			def       a_     (      self      :    Optional[int]      ,  UpperCamelCase_      :    Optional[int]      ,  UpperCamelCase_      :    Dict):


						"""simple docstring"""




						__UpperCAmelCase	: List[str]		=					TFBlenderbotModel(config=UpperCamelCase_).get_decoder()
						__UpperCAmelCase	: int		=					inputs_dict["input_ids"]

						__UpperCAmelCase	: Union[str, Any]		=					input_ids[:1, :]
						__UpperCAmelCase	: int		=					inputs_dict["attention_mask"][:1, :]
						__UpperCAmelCase	: Optional[int]		=					inputs_dict["head_mask"]
						__UpperCAmelCase	: Optional[int]		=					1

						# first forward pass
						__UpperCAmelCase	: Dict		=					model(UpperCamelCase_      ,  attention_mask=UpperCamelCase_      ,  head_mask=UpperCamelCase_      ,  use_cache=UpperCamelCase_)

						__UpperCAmelCase	,     __UpperCAmelCase	: str		=					outputs.to_tuple()

						# create hypothetical next token and extent to next_input_ids
						__UpperCAmelCase	: Tuple		=					ids_tensor((self.batch_size, 3)      ,  config.vocab_size)
						__UpperCAmelCase	: Optional[int]		=					tf.cast(ids_tensor((self.batch_size, 3)      ,  2)      ,  tf.inta)

						# append to next input_ids and
						__UpperCAmelCase	: List[str]		=					tf.concat([input_ids, next_tokens]      ,  axis=-1)
						__UpperCAmelCase	: Optional[int]		=					tf.concat([attention_mask, next_attn_mask]      ,  axis=-1)

						__UpperCAmelCase	: int		=					model(UpperCamelCase_      ,  attention_mask=UpperCamelCase_)[0]
						__UpperCAmelCase	: List[Any]		=					model(UpperCamelCase_      ,  attention_mask=UpperCamelCase_      ,  past_key_values=UpperCamelCase_)[0]

						self.parent.assertEqual(next_tokens.shape[1]      ,  output_from_past.shape[1])

						# select random slice
						__UpperCAmelCase	: Tuple		=					int(ids_tensor((1,)      ,  output_from_past.shape[-1]))
						__UpperCAmelCase	: int		=					output_from_no_past[:, -3:, random_slice_idx]
						__UpperCAmelCase	: Union[str, Any]		=					output_from_past[:, :, random_slice_idx]

						# test that outputs are equal for slice
						tf.debugging.assert_near(UpperCamelCase_      ,  UpperCamelCase_      ,  rtol=1e-3)






def 					_UpperCamelCase    (			UpperCamelCase				,       UpperCamelCase				,       UpperCamelCase				,       UpperCamelCase=None				,       UpperCamelCase=None				,       UpperCamelCase=None				,       UpperCamelCase=None				,       UpperCamelCase=None				,       )			->			Dict:


			"""simple docstring"""
			if attention_mask is None:
						__UpperCAmelCase	: Optional[int]		=					tf.cast(tf.math.not_equal(UpperCamelCase				,       config.pad_token_id							)				,       tf.inta							)
			if decoder_attention_mask is None:
						__UpperCAmelCase	: Dict		=					tf.concat(
						    [
						        tf.ones(decoder_input_ids[:, :1].shape				,       dtype=tf.inta							),
						        tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]				,       config.pad_token_id							)				,       tf.inta							),
						    ]				,       axis=-1				,       )
			if head_mask is None:
						__UpperCAmelCase	: Any		=					tf.ones((config.encoder_layers, config.encoder_attention_heads)							)
			if decoder_head_mask is None:
						__UpperCAmelCase	: Optional[int]		=					tf.ones((config.decoder_layers, config.decoder_attention_heads)							)
			if cross_attn_head_mask is None:
						__UpperCAmelCase	: List[Any]		=					tf.ones((config.decoder_layers, config.decoder_attention_heads)							)
			return {
			    "input_ids": input_ids,
			    "decoder_input_ids": decoder_input_ids,
			    "attention_mask": attention_mask,
			    "decoder_attention_mask": decoder_attention_mask,
			    "head_mask": head_mask,
			    "decoder_head_mask": decoder_head_mask,
			    "cross_attn_head_mask": cross_attn_head_mask,
			}



@require_tf
class      a__				(	__magic_name__							,    __magic_name__							,    unittest.TestCase   ):
			lowercase_    =	(TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
			lowercase_    =	(TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
			lowercase_    =	(
			    {
			        "conversational": TFBlenderbotForConditionalGeneration,
			        "feature-extraction": TFBlenderbotModel,
			        "summarization": TFBlenderbotForConditionalGeneration,
			        "text2text-generation": TFBlenderbotForConditionalGeneration,
			        "translation": TFBlenderbotForConditionalGeneration,
			    }
			    if is_tf_available()
			    else {}
			)
			lowercase_    =	True
			lowercase_    =	False
			lowercase_    =	False


			def       a_     (      self      :    Any):


						"""simple docstring"""




						__UpperCAmelCase	: int		=					TFBlenderbotModelTester(self)
						__UpperCAmelCase	: Optional[int]		=					ConfigTester(self      ,  config_class=UpperCamelCase_)


			def       a_     (      self      :    Tuple):


						"""simple docstring"""




						self.config_tester.run_common_tests()


			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: Tuple		=					self.model_tester.prepare_config_and_inputs_for_common()
						self.model_tester.check_decoder_model_past_large_inputs(*UpperCamelCase_)






@require_tokenizers
@require_tf
class      a__				(	unittest.TestCase   ):
			lowercase_    =	["My friends are cool but they eat too many carbs."]
			lowercase_    =	"facebook/blenderbot-400M-distill"


			@cached_property
			def       a_     (      self      :    int):


						"""simple docstring"""




						return BlenderbotTokenizer.from_pretrained(self.model_name)


			@cached_property
			def       a_     (      self      :    Optional[int]):


						"""simple docstring"""




						__UpperCAmelCase	: List[Any]		=					TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name)
						return model


			@slow
			def       a_     (      self      :    str):


						"""simple docstring"""




						__UpperCAmelCase	: Optional[Any]		=					self.tokenizer(self.src_text      ,  return_tensors="tf")
						__UpperCAmelCase	: Any		=					self.model.generate(
						    model_inputs.input_ids      ,  )
						__UpperCAmelCase	: List[str]		=					self.tokenizer.batch_decode(generated_ids.numpy()      ,  skip_special_tokens=UpperCamelCase_)[0]
						assert (
						    generated_words
						    == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?"
						)








"""simple docstring"""





import json
import logging
import os
import socket

import git
import numpy as np
import torch


logging.basicConfig(
    format="""%(asctime)s - %(levelname)s - %(name)s - PID: %(process)d -  %(message)s""",
    datefmt="""%m/%d/%Y %H:%M:%S""",
    level=logging.INFO,
)
A   =       logging.getLogger(__name__)

def 					_UpperCamelCase    (			UpperCamelCase							)			->			Any:


			"""simple docstring"""
			__UpperCAmelCase	: Optional[Any]		=					git.Repo(search_parent_directories=UpperCamelCase							)
			__UpperCAmelCase	: Any		=					{
			    "repo_id": str(UpperCamelCase							),
			    "repo_sha": str(repo.head.object.hexsha							),
			    "repo_branch": str(repo.active_branch							),
			}

			with open(os.path.join(UpperCamelCase				,       "git_log.json"							)				,       "w"							) as f:
						json.dump(UpperCamelCase				,       UpperCamelCase				,       indent=4							)

def 					_UpperCamelCase    (			UpperCamelCase							)			->			str:


			"""simple docstring"""
			if params.n_gpu <= 0:
						__UpperCAmelCase	: str		=					0
						__UpperCAmelCase	: Dict		=					-1
						__UpperCAmelCase	: Tuple		=					True
						__UpperCAmelCase	: List[str]		=					False
						return

			assert torch.cuda.is_available()

			logger.info("Initializing GPUs"							)
			if params.n_gpu > 1:
						assert params.local_rank != -1

						__UpperCAmelCase	: Optional[int]		=					int(os.environ["WORLD_SIZE"]							)
						__UpperCAmelCase	: Union[str, Any]		=					int(os.environ["N_GPU_NODE"]							)
						__UpperCAmelCase	: Optional[int]		=					int(os.environ["RANK"]							)

						# number of nodes / node ID
						__UpperCAmelCase	: int		=					params.world_size // params.n_gpu_per_node
						__UpperCAmelCase	: Optional[int]		=					params.global_rank // params.n_gpu_per_node
						__UpperCAmelCase	: Tuple		=					True

						assert params.n_nodes == int(os.environ["N_NODES"]							)
						assert params.node_id == int(os.environ["NODE_RANK"]							)

			# local job (single GPU)
			else:
						assert params.local_rank == -1

						__UpperCAmelCase	: Union[str, Any]		=					1
						__UpperCAmelCase	: Dict		=					0
						__UpperCAmelCase	: List[Any]		=					0
						__UpperCAmelCase	: Tuple		=					0
						__UpperCAmelCase	: Any		=					1
						__UpperCAmelCase	: str		=					1
						__UpperCAmelCase	: Dict		=					False

			# sanity checks
			assert params.n_nodes >= 1
			assert 0 <= params.node_id < params.n_nodes
			assert 0 <= params.local_rank <= params.global_rank < params.world_size
			assert params.world_size == params.n_nodes * params.n_gpu_per_node

			# define whether this is the master process / if we are in multi-node distributed mode
			__UpperCAmelCase	: Union[str, Any]		=					params.node_id == 0 and params.local_rank == 0
			__UpperCAmelCase	: Dict		=					params.n_nodes > 1

			# summary
			__UpperCAmelCase	: Any		=					f"--- Global rank: {params.global_rank} - "
			logger.info(PREFIX + "Number of nodes: %i" % params.n_nodes							)
			logger.info(PREFIX + "Node ID        : %i" % params.node_id							)
			logger.info(PREFIX + "Local rank     : %i" % params.local_rank							)
			logger.info(PREFIX + "World size     : %i" % params.world_size							)
			logger.info(PREFIX + "GPUs per node  : %i" % params.n_gpu_per_node							)
			logger.info(PREFIX + "Master         : %s" % str(params.is_master							)							)
			logger.info(PREFIX + "Multi-node     : %s" % str(params.multi_node							)							)
			logger.info(PREFIX + "Multi-GPU      : %s" % str(params.multi_gpu							)							)
			logger.info(PREFIX + "Hostname       : %s" % socket.gethostname()							)

			# set GPU device
			torch.cuda.set_device(params.local_rank							)

			# initialize multi-GPU
			if params.multi_gpu:
						logger.info("Initializing PyTorch distributed"							)
						torch.distributed.init_process_group(
						    init_method="env://"				,       backend="nccl"				,       )





def 					_UpperCamelCase    (			UpperCamelCase							)			->			Tuple:


			"""simple docstring"""
			np.random.seed(args.seed							)
			torch.manual_seed(args.seed							)
			if args.n_gpu > 0:
						torch.cuda.manual_seed_all(args.seed							)


