



'''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 				SCREAMING_SNAKE_CASE:







	"""simple docstring"""






	lowerCamelCase__			 =       BlenderbotConfig
	lowerCamelCase__			 =       {}
	lowerCamelCase__			 =       """gelu"""







	def __init__(						self			:     Any		,			__snake_case			:     str		,			__snake_case			:     int=13		,			__snake_case			:     Union[str, Any]=7		,			__snake_case			:     Optional[Any]=True		,			__snake_case			:     Optional[int]=False		,			__snake_case			:     Optional[int]=99		,			__snake_case			:     str=32		,			__snake_case			:     Dict=2		,			__snake_case			:     int=4		,			__snake_case			:     List[Any]=37		,			__snake_case			:     Union[str, Any]=0.1		,			__snake_case			:     Optional[Any]=0.1		,			__snake_case			:     Optional[int]=20		,			__snake_case			:     int=2		,			__snake_case			:     Optional[Any]=1		,			__snake_case			:     List[str]=0		,			)			->		Tuple:
								UpperCAmelCase :       int 							=		parent
								UpperCAmelCase :       int 							=		batch_size
								UpperCAmelCase :       Optional[Any] 							=		seq_length
								UpperCAmelCase :       Dict 							=		is_training
								UpperCAmelCase :       List[Any] 							=		use_labels
								UpperCAmelCase :       int 							=		vocab_size
								UpperCAmelCase :       str 							=		hidden_size
								UpperCAmelCase :       List[str] 							=		num_hidden_layers
								UpperCAmelCase :       Tuple 							=		num_attention_heads
								UpperCAmelCase :       str 							=		intermediate_size

								UpperCAmelCase :       str 							=		hidden_dropout_prob
								UpperCAmelCase :       str 							=		attention_probs_dropout_prob
								UpperCAmelCase :       Union[str, Any] 							=		max_position_embeddings
								UpperCAmelCase :       int 							=		eos_token_id
								UpperCAmelCase :       str 							=		pad_token_id
								UpperCAmelCase :       int 							=		bos_token_id







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

								UpperCAmelCase :       Any 							=		ids_tensor([self.batch_size, self.seq_length]		,			self.vocab_size		)

								UpperCAmelCase :       Any 							=		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 :       Dict 							=		prepare_blenderbot_inputs_dict(__snake_case		,			__snake_case		,			__snake_case		)
								return config, inputs_dict







	def      A	(						self			:     List[str]		,			__snake_case			:     int		,			__snake_case			:     Optional[int]		)			->		List[Any]:
								UpperCAmelCase :       Dict 							=		TFBlenderbotModel(config=__snake_case		).get_decoder()
								UpperCAmelCase :       Tuple 							=		inputs_dict['''input_ids''']

								UpperCAmelCase :       Union[str, Any] 							=		input_ids[:1, :]
								UpperCAmelCase :       Union[str, Any] 							=		inputs_dict['''attention_mask'''][:1, :]
								UpperCAmelCase :       Union[str, Any] 							=		inputs_dict['''head_mask''']
								UpperCAmelCase :       List[Any] 							=		1

								# first forward pass
								UpperCAmelCase :       List[Any] 							=		model(__snake_case		,			attention_mask=__snake_case		,			head_mask=__snake_case		,			use_cache=__snake_case		)

								UpperCAmelCase							,		UpperCAmelCase :       List[Any] 							=		outputs.to_tuple()

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

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

								UpperCAmelCase :       int 							=		model(__snake_case		,			attention_mask=__snake_case		)[0]
								UpperCAmelCase :       Optional[Any] 							=		model(__snake_case		,			attention_mask=__snake_case		,			past_key_values=__snake_case		)[0]

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

								# select random slice
								UpperCAmelCase :       Dict 							=		int(ids_tensor((1,)		,			output_from_past.shape[-1]		)		)
								UpperCAmelCase :       List[str] 							=		output_from_no_past[:, -3:, random_slice_idx]
								UpperCAmelCase :       List[Any] 							=		output_from_past[:, :, random_slice_idx]

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


def  snake_case_     (					_lowerCAmelCase  :						Optional[Any]   ,      _lowerCAmelCase  :						Optional[int]   ,      _lowerCAmelCase  :						List[Any]   ,      _lowerCAmelCase  :						str=None   ,      _lowerCAmelCase  :						List[str]=None   ,      _lowerCAmelCase  :						Optional[int]=None   ,      _lowerCAmelCase  :						List[str]=None   ,      _lowerCAmelCase  :						str=None   ,      )      ->   Any:
							if attention_mask is None:
														UpperCAmelCase :       str 							=		tf.cast(tf.math.not_equal(_lowerCAmelCase   ,      config.pad_token_id  )   ,      tf.inta  )
							if decoder_attention_mask is None:
														UpperCAmelCase :       List[Any] 							=		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 :       Union[str, Any] 							=		tf.ones((config.encoder_layers, config.encoder_attention_heads)  )
							if decoder_head_mask is None:
														UpperCAmelCase :       Optional[Any] 							=		tf.ones((config.decoder_layers, config.decoder_attention_heads)  )
							if cross_attn_head_mask is None:
														UpperCAmelCase :       str 							=		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 				SCREAMING_SNAKE_CASE(							A__							,		A__							,		unittest.TestCase			):







	"""simple docstring"""






	lowerCamelCase__			 =       (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
	lowerCamelCase__			 =       (TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
	lowerCamelCase__			 =       (
	    {
	        """conversational""": TFBlenderbotForConditionalGeneration,
	        """feature-extraction""": TFBlenderbotModel,
	        """summarization""": TFBlenderbotForConditionalGeneration,
	        """text2text-generation""": TFBlenderbotForConditionalGeneration,
	        """translation""": TFBlenderbotForConditionalGeneration,
	    }
	    if is_tf_available()
	    else {}
	)
	lowerCamelCase__			 =       True
	lowerCamelCase__			 =       False
	lowerCamelCase__			 =       False







	def      A	(						self			:     str		)			->		int:
								UpperCAmelCase :       str 							=		TFBlenderbotModelTester(self		)
								UpperCAmelCase :       Optional[Any] 							=		ConfigTester(self		,			config_class=__snake_case		)







	def      A	(						self			:     int		)			->		str:
								self.config_tester.run_common_tests()







	def      A	(						self			:     Any		)			->		Tuple:
								UpperCAmelCase :       Dict 							=		self.model_tester.prepare_config_and_inputs_for_common()
								self.model_tester.check_decoder_model_past_large_inputs(*__snake_case		)

@require_tokenizers
@require_tf
class 				SCREAMING_SNAKE_CASE(							unittest.TestCase			):







	"""simple docstring"""






	lowerCamelCase__			 =       ["""My friends are cool but they eat too many carbs."""]
	lowerCamelCase__			 =       """facebook/blenderbot-400M-distill"""







	@cached_property
	def      A	(						self			:     Union[str, Any]		)			->		Union[str, Any]:
								return BlenderbotTokenizer.from_pretrained(self.model_name		)







	@cached_property
	def      A	(						self			:     Union[str, Any]		)			->		Tuple:
								UpperCAmelCase :       Optional[Any] 							=		TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name		)
								return model







	@slow
	def      A	(						self			:     Dict		)			->		List[str]:
								UpperCAmelCase :       List[Any] 							=		self.tokenizer(self.src_text		,			return_tensors='''tf'''		)
								UpperCAmelCase :       Tuple 							=		self.model.generate(
								    model_inputs.input_ids		,			)
								UpperCAmelCase :       List[Any] 							=		self.tokenizer.batch_decode(generated_ids.numpy()		,			skip_special_tokens=__snake_case		)[0]
								assert (
								    generated_words
								    == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?"
								)










'''simple docstring'''






# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# 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.

# this script dumps information about the environment

import os
import sys

import transformers


UpperCamelCase__:   Optional[int]								=			"3"

print("Python version:", sys.version)
print("transformers version:", transformers.__version__)

try:
						import torch

						print("Torch version:", torch.__version__)
						print("Cuda available:", torch.cuda.is_available())
						print("Cuda version:", torch.version.cuda)
						print("CuDNN version:", torch.backends.cudnn.version())
						print("Number of GPUs available:", torch.cuda.device_count())
						print("NCCL version:", torch.cuda.nccl.version())
except ImportError:
						print("Torch version:", None)

try:
						import deepspeed

						print("DeepSpeed version:", deepspeed.__version__)
except ImportError:
						print("DeepSpeed version:", None)

try:
						import tensorflow as tf

						print("TensorFlow version:", tf.__version__)
						print("TF GPUs available:", bool(tf.config.list_physical_devices("GPU")))
						print("Number of TF GPUs available:", len(tf.config.list_physical_devices("GPU")))
except ImportError:
						print("TensorFlow version:", None)





