
import unittest

from transformers import (
    MODEL_FOR_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
    TextGenerationPipeline,
    logging,
    pipeline,
)
from transformers.testing_utils import (
    CaptureLogger,
    is_pipeline_test,
    require_accelerate,
    require_tf,
    require_torch,
    require_torch_gpu,
    require_torch_or_tf,
)

from .test_pipelines_common import ANY







@is_pipeline_test
@require_torch_or_tf
class        lowercase	(      unittest.TestCase	):
							lowercase_ : int       =MODEL_FOR_CAUSAL_LM_MAPPING
							lowercase_ : Any       =TF_MODEL_FOR_CAUSAL_LM_MAPPING




							@require_torch
							def 				A__   (       self):
												lowercase					=		pipeline(task='''text-generation'''     ,model='''sshleifer/tiny-ctrl'''     ,framework='''pt''')
												# Using `do_sample=False` to force deterministic output
												lowercase					=		text_generator('''This is a test'''     ,do_sample=A__)
												self.assertEqual(
												    A__     ,[
												        {
												            '''generated_text''': (
												                '''This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.'''
												                ''' oscope. FiliFili@@'''
												            )
												        }
												    ]     ,)

												lowercase					=		text_generator(['''This is a test''', '''This is a second test'''])
												self.assertEqual(
												    A__     ,[
												        [
												            {
												                '''generated_text''': (
												                    '''This is a test ☃ ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy oscope.'''
												                    ''' oscope. FiliFili@@'''
												                )
												            }
												        ],
												        [
												            {
												                '''generated_text''': (
												                    '''This is a second test ☃ segmental segmental segmental 议议eski eski flutter flutter Lacy'''
												                    ''' oscope. oscope. FiliFili@@'''
												                )
												            }
												        ],
												    ]     ,)

												lowercase					=		text_generator('''This is a test'''     ,do_sample=A__     ,num_return_sequences=2     ,return_tensors=A__)
												self.assertEqual(
												    A__     ,[
												        {'''generated_token_ids''': ANY(A__)},
												        {'''generated_token_ids''': ANY(A__)},
												    ]     ,)
												lowercase					=		text_generator.model.config.eos_token_id
												lowercase					=		'''<pad>'''
												lowercase					=		text_generator(
												    ['''This is a test''', '''This is a second test''']     ,do_sample=A__     ,num_return_sequences=2     ,batch_size=2     ,return_tensors=A__     ,)
												self.assertEqual(
												    A__     ,[
												        [
												            {'''generated_token_ids''': ANY(A__)},
												            {'''generated_token_ids''': ANY(A__)},
												        ],
												        [
												            {'''generated_token_ids''': ANY(A__)},
												            {'''generated_token_ids''': ANY(A__)},
												        ],
												    ]     ,)




							@require_tf
							def 				A__   (       self):
												lowercase					=		pipeline(task='''text-generation'''     ,model='''sshleifer/tiny-ctrl'''     ,framework='''tf''')

												# Using `do_sample=False` to force deterministic output
												lowercase					=		text_generator('''This is a test'''     ,do_sample=A__)
												self.assertEqual(
												    A__     ,[
												        {
												            '''generated_text''': (
												                '''This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵'''
												                ''' please,'''
												            )
												        }
												    ]     ,)

												lowercase					=		text_generator(['''This is a test''', '''This is a second test''']     ,do_sample=A__)
												self.assertEqual(
												    A__     ,[
												        [
												            {
												                '''generated_text''': (
												                    '''This is a test FeyFeyFey(Croatis.), s.), Cannes Cannes Cannes 閲閲Cannes Cannes Cannes 攵'''
												                    ''' please,'''
												                )
												            }
												        ],
												        [
												            {
												                '''generated_text''': (
												                    '''This is a second test Chieftain Chieftain prefecture prefecture prefecture Cannes Cannes'''
												                    ''' Cannes 閲閲Cannes Cannes Cannes 攵 please,'''
												                )
												            }
												        ],
												    ]     ,)




							def 				A__   (       self     ,A__     ,A__     ,A__):
												lowercase					=		TextGenerationPipeline(model=A__     ,tokenizer=A__)
												return text_generator, ["This is a test", "Another test"]




							def 				A__   (       self):
												lowercase					=		'''Hello I believe in'''
												lowercase					=		pipeline('''text-generation'''     ,model='''hf-internal-testing/tiny-random-gpt2''')
												lowercase					=		text_generator(A__)
												self.assertEqual(
												    A__     ,[{'''generated_text''': '''Hello I believe in fe fe fe fe fe fe fe fe fe fe fe fe'''}]     ,)

												lowercase					=		text_generator(A__     ,stop_sequence=''' fe''')
												self.assertEqual(A__     ,[{'''generated_text''': '''Hello I believe in fe'''}])




							def 				A__   (       self     ,A__     ,A__):
												lowercase					=		text_generator.model
												lowercase					=		text_generator.tokenizer

												lowercase					=		text_generator('''This is a test''')
												self.assertEqual(A__     ,[{'''generated_text''': ANY(A__)}])
												self.assertTrue(outputs[0]['''generated_text'''].startswith('''This is a test'''))

												lowercase					=		text_generator('''This is a test'''     ,return_full_text=A__)
												self.assertEqual(A__     ,[{'''generated_text''': ANY(A__)}])
												self.assertNotIn('''This is a test'''     ,outputs[0]['''generated_text'''])

												lowercase					=		pipeline(task='''text-generation'''     ,model=A__     ,tokenizer=A__     ,return_full_text=A__)
												lowercase					=		text_generator('''This is a test''')
												self.assertEqual(A__     ,[{'''generated_text''': ANY(A__)}])
												self.assertNotIn('''This is a test'''     ,outputs[0]['''generated_text'''])

												lowercase					=		text_generator('''This is a test'''     ,return_full_text=A__)
												self.assertEqual(A__     ,[{'''generated_text''': ANY(A__)}])
												self.assertTrue(outputs[0]['''generated_text'''].startswith('''This is a test'''))

												lowercase					=		text_generator(['''This is great !''', '''Something else''']     ,num_return_sequences=2     ,do_sample=A__)
												self.assertEqual(
												    A__     ,[
												        [{'''generated_text''': ANY(A__)}, {'''generated_text''': ANY(A__)}],
												        [{'''generated_text''': ANY(A__)}, {'''generated_text''': ANY(A__)}],
												    ]     ,)

												if text_generator.tokenizer.pad_token is not None:
																	lowercase					=		text_generator(
																	    ['''This is great !''', '''Something else''']     ,num_return_sequences=2     ,batch_size=2     ,do_sample=A__)
																	self.assertEqual(
																	    A__     ,[
																	        [{'''generated_text''': ANY(A__)}, {'''generated_text''': ANY(A__)}],
																	        [{'''generated_text''': ANY(A__)}, {'''generated_text''': ANY(A__)}],
																	    ]     ,)

												with self.assertRaises(A__):
																	lowercase					=		text_generator('''test'''     ,return_full_text=A__     ,return_text=A__)
												with self.assertRaises(A__):
																	lowercase					=		text_generator('''test'''     ,return_full_text=A__     ,return_tensors=A__)
												with self.assertRaises(A__):
																	lowercase					=		text_generator('''test'''     ,return_text=A__     ,return_tensors=A__)

												# Empty prompt is slighly special
												# it requires BOS token to exist.
												# Special case for Pegasus which will always append EOS so will
												# work even without BOS.
												if (
												    text_generator.tokenizer.bos_token_id is not None
												    or "Pegasus" in tokenizer.__class__.__name__
												    or "Git" in model.__class__.__name__
												):
																	lowercase					=		text_generator('''''')
																	self.assertEqual(A__     ,[{'''generated_text''': ANY(A__)}])
												else:
																	with self.assertRaises((ValueError, AssertionError)):
																						lowercase					=		text_generator('''''')

												if text_generator.framework == "tf":
																	# TF generation does not support max_new_tokens, and it's impossible
																	# to control long generation with only max_length without
																	# fancy calculation, dismissing tests for now.
																	return
												# We don't care about infinite range models.
												# They already work.
												# Skip this test for XGLM, since it uses sinusoidal positional embeddings which are resized on-the-fly.
												lowercase					=		['''RwkvForCausalLM''', '''XGLMForCausalLM''', '''GPTNeoXForCausalLM''']
												if (
												    tokenizer.model_max_length < 1_0_0_0_0
												    and text_generator.model.__class__.__name__ not in EXTRA_MODELS_CAN_HANDLE_LONG_INPUTS
												):
																	# Handling of large generations
																	with self.assertRaises((RuntimeError, IndexError, ValueError, AssertionError)):
																						text_generator('''This is a test''' * 5_0_0     ,max_new_tokens=2_0)

																	lowercase					=		text_generator('''This is a test''' * 5_0_0     ,handle_long_generation='''hole'''     ,max_new_tokens=2_0)
																	# Hole strategy cannot work
																	with self.assertRaises(A__):
																						text_generator(
																						    '''This is a test''' * 5_0_0     ,handle_long_generation='''hole'''     ,max_new_tokens=tokenizer.model_max_length + 1_0     ,)




							@require_torch
							@require_accelerate
							@require_torch_gpu
							def 				A__   (       self):
												import torch

												# Classic `model_kwargs`
												lowercase					=		pipeline(
												    model='''hf-internal-testing/tiny-random-bloom'''     ,model_kwargs={'''device_map''': '''auto''', '''torch_dtype''': torch.bfloataa}     ,)
												self.assertEqual(pipe.model.device     ,torch.device(0))
												self.assertEqual(pipe.model.lm_head.weight.dtype     ,torch.bfloataa)
												lowercase					=		pipe('''This is a test''')
												self.assertEqual(
												    A__     ,[
												        {
												            '''generated_text''': (
												                '''This is a test test test test test test test test test test test test test test test test'''
												                ''' test'''
												            )
												        }
												    ]     ,)

												# Upgraded those two to real pipeline arguments (they just get sent for the model as they're unlikely to mean anything else.)
												lowercase					=		pipeline(model='''hf-internal-testing/tiny-random-bloom'''     ,device_map='''auto'''     ,torch_dtype=torch.bfloataa)
												self.assertEqual(pipe.model.device     ,torch.device(0))
												self.assertEqual(pipe.model.lm_head.weight.dtype     ,torch.bfloataa)
												lowercase					=		pipe('''This is a test''')
												self.assertEqual(
												    A__     ,[
												        {
												            '''generated_text''': (
												                '''This is a test test test test test test test test test test test test test test test test'''
												                ''' test'''
												            )
												        }
												    ]     ,)

												# torch_dtype will be automatically set to float32 if not provided - check: https://github.com/huggingface/transformers/pull/20602
												lowercase					=		pipeline(model='''hf-internal-testing/tiny-random-bloom'''     ,device_map='''auto''')
												self.assertEqual(pipe.model.device     ,torch.device(0))
												self.assertEqual(pipe.model.lm_head.weight.dtype     ,torch.floataa)
												lowercase					=		pipe('''This is a test''')
												self.assertEqual(
												    A__     ,[
												        {
												            '''generated_text''': (
												                '''This is a test test test test test test test test test test test test test test test test'''
												                ''' test'''
												            )
												        }
												    ]     ,)




							@require_torch
							@require_torch_gpu
							def 				A__   (       self):
												import torch

												lowercase					=		pipeline(model='''hf-internal-testing/tiny-random-bloom'''     ,device=0     ,torch_dtype=torch.floataa)
												pipe('''This is a test''')




							@require_torch
							@require_accelerate
							@require_torch_gpu
							def 				A__   (       self):
												import torch

												lowercase					=		pipeline(model='''hf-internal-testing/tiny-random-bloom'''     ,device_map='''auto'''     ,torch_dtype=torch.floataa)
												pipe('''This is a test'''     ,do_sample=A__     ,top_p=0.5)





							def 				A__   (       self):
												lowercase					=		'''Hello world'''
												lowercase					=		pipeline('''text-generation'''     ,model='''hf-internal-testing/tiny-random-gpt2''')
												if text_generator.model.framework == "tf":
																	lowercase					=		logging.get_logger('''transformers.generation.tf_utils''')
												else:
																	lowercase					=		logging.get_logger('''transformers.generation.utils''')
												lowercase					=		'''Both `max_new_tokens`'''  # The beggining of the message to be checked in this test

												# Both are set by the user -> log warning
												with CaptureLogger(A__) as cl:
																	lowercase					=		text_generator(A__     ,max_length=1_0     ,max_new_tokens=1)
												self.assertIn(A__     ,cl.out)

												# The user only sets one -> no warning
												with CaptureLogger(A__) as cl:
																	lowercase					=		text_generator(A__     ,max_new_tokens=1)
												self.assertNotIn(A__     ,cl.out)

												with CaptureLogger(A__) as cl:
																	lowercase					=		text_generator(A__     ,max_length=1_0)
												self.assertNotIn(A__     ,cl.out)






import random
import unittest

import numpy as np

import transformers
from transformers import is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax


if is_flax_available():
							import os

							import jax.numpy as jnp
							from jax import jit

							from transformers import AutoTokenizer, FlaxAutoModelForCausalLM
							from transformers.modeling_flax_pytorch_utils import load_flax_weights_in_pytorch_model

							lowercase__	:Optional[Any]										=					"0.12"  # assumed parallelism: 8


if is_torch_available():
							import torch



def  UpperCamelCase					(							lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__=None	):
					'''simple docstring'''
					if rng is None:
										lowercase					=		random.Random()

					lowercase					=		1
					for dim in shape:
										total_dims *= dim

					lowercase					=		[]
					for _ in range(lowerCAmelCase__	):
										values.append(rng.randint(0				,    vocab_size - 1	)	)

					lowercase					=		np.array(lowerCAmelCase__				,    dtype=jnp.intaa	).reshape(lowerCAmelCase__	)

					return output



def  UpperCamelCase					(							lowerCAmelCase__				,    lowerCAmelCase__=None	):
					'''simple docstring'''
					lowercase					=		ids_tensor(lowerCAmelCase__				,    vocab_size=2				,    rng=lowerCAmelCase__	)
					# make sure that at least one token is attended to for each batch
					lowercase					=		1
					return attn_mask







@require_flax
class        lowercase	:
							lowercase_ : Any       =None
							lowercase_ : List[str]       =()




							def 				A__   (       self):
												lowercase			,		lowercase					=		self.model_tester.prepare_config_and_inputs_for_common()

												# cut to half length & take max batch_size 3
												lowercase					=		2
												lowercase					=		inputs['''input_ids'''].shape[-1] // 2
												lowercase					=		inputs['''input_ids'''][:max_batch_size, :sequence_length]

												lowercase					=		jnp.ones_like(A__)
												lowercase					=		attention_mask[:max_batch_size, :sequence_length]

												# generate max 5 tokens
												lowercase					=		input_ids.shape[-1] + 5
												if config.eos_token_id is not None and config.pad_token_id is None:
																	# hack to allow generate for models such as GPT2 as is done in `generate()`
																	lowercase					=		config.eos_token_id
												return config, input_ids, attention_mask, max_length




							@is_pt_flax_cross_test
							def 				A__   (       self):
												lowercase			,		lowercase			,		lowercase			,		lowercase					=		self._get_input_ids_and_config()
												lowercase					=		False
												lowercase					=		max_length
												lowercase					=		0

												for model_class in self.all_generative_model_classes:
																	lowercase					=		model_class(A__)

																	lowercase					=		model_class.__name__[4:]  # Skip the "Flax" at the beginning
																	lowercase					=		getattr(A__     ,A__)
																	lowercase					=		pt_model_class(A__).eval()
																	lowercase					=		load_flax_weights_in_pytorch_model(A__     ,flax_model.params)

																	lowercase					=		flax_model.generate(A__).sequences
																	lowercase					=		pt_model.generate(torch.tensor(A__     ,dtype=torch.long))

																	if flax_generation_outputs.shape[-1] > pt_generation_outputs.shape[-1]:
																						lowercase					=		flax_generation_outputs[:, : pt_generation_outputs.shape[-1]]

																	self.assertListEqual(pt_generation_outputs.numpy().tolist()     ,flax_generation_outputs.tolist())




							def 				A__   (       self):
												lowercase			,		lowercase			,		lowercase			,		lowercase					=		self._get_input_ids_and_config()
												lowercase					=		False
												lowercase					=		max_length

												for model_class in self.all_generative_model_classes:
																	lowercase					=		model_class(A__)

																	lowercase					=		model.generate(A__).sequences
																	self.assertEqual(generation_outputs.shape[-1]     ,A__)

																	lowercase					=		jit(model.generate)
																	lowercase					=		jit_generate(A__).sequences

																	self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())




							def 				A__   (       self):
												lowercase			,		lowercase			,		lowercase			,		lowercase					=		self._get_input_ids_and_config()
												lowercase					=		True
												lowercase					=		max_length

												for model_class in self.all_generative_model_classes:
																	lowercase					=		model_class(A__)

																	lowercase					=		model.generate(A__).sequences
																	self.assertEqual(generation_outputs.shape[-1]     ,A__)

																	lowercase					=		jit(model.generate)
																	lowercase					=		jit_generate(A__).sequences

																	self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())




							def 				A__   (       self):
												lowercase			,		lowercase			,		lowercase			,		lowercase					=		self._get_input_ids_and_config()
												lowercase					=		False
												lowercase					=		max_length
												lowercase					=		2

												for model_class in self.all_generative_model_classes:
																	lowercase					=		model_class(A__)

																	lowercase					=		model.generate(A__).sequences
																	self.assertEqual(generation_outputs.shape[-1]     ,A__)

																	lowercase					=		jit(model.generate)
																	lowercase					=		jit_generate(A__).sequences

																	self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())




							def 				A__   (       self):
												lowercase			,		lowercase			,		lowercase			,		lowercase					=		self._get_input_ids_and_config()
												lowercase					=		False
												lowercase					=		max_length
												lowercase					=		2
												lowercase					=		2

												for model_class in self.all_generative_model_classes:
																	lowercase					=		model_class(A__)

																	lowercase					=		model.generate(A__).sequences
																	self.assertEqual(generation_outputs.shape[0]     ,input_ids.shape[0] * config.num_return_sequences)




							def 				A__   (       self):
												lowercase			,		lowercase			,		lowercase			,		lowercase					=		self._get_input_ids_and_config()
												lowercase					=		True
												lowercase					=		max_length
												lowercase					=		0.8
												lowercase					=		1_0
												lowercase					=		0.3
												lowercase					=		1
												lowercase					=		8
												lowercase					=		9

												for model_class in self.all_generative_model_classes:
																	lowercase					=		model_class(A__)

																	lowercase					=		model.generate(A__).sequences
																	self.assertEqual(generation_outputs.shape[-1]     ,A__)

																	lowercase					=		jit(model.generate)
																	lowercase					=		jit_generate(A__).sequences

																	self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())




							def 				A__   (       self):
												lowercase			,		lowercase			,		lowercase			,		lowercase					=		self._get_input_ids_and_config()
												lowercase					=		max_length
												lowercase					=		1
												lowercase					=		8
												lowercase					=		9

												for model_class in self.all_generative_model_classes:
																	lowercase					=		model_class(A__)

																	lowercase					=		model.generate(A__).sequences
																	self.assertEqual(generation_outputs.shape[-1]     ,A__)

																	lowercase					=		jit(model.generate)
																	lowercase					=		jit_generate(A__).sequences

																	self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())




							def 				A__   (       self):
												lowercase			,		lowercase			,		lowercase			,		lowercase					=		self._get_input_ids_and_config()
												lowercase					=		max_length
												lowercase					=		2
												lowercase					=		1
												lowercase					=		8
												lowercase					=		9

												for model_class in self.all_generative_model_classes:
																	lowercase					=		model_class(A__)

																	lowercase					=		model.generate(A__).sequences
																	self.assertEqual(generation_outputs.shape[-1]     ,A__)

																	lowercase					=		jit(model.generate)
																	lowercase					=		jit_generate(A__).sequences

																	self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())




							def 				A__   (       self):
												lowercase			,		lowercase			,		lowercase			,		lowercase					=		self._get_input_ids_and_config()

												# pad attention mask on the left
												lowercase					=		attention_mask.at[(0, 0)].set(0)

												lowercase					=		False
												lowercase					=		max_length

												for model_class in self.all_generative_model_classes:
																	lowercase					=		model_class(A__)

																	lowercase					=		model.generate(A__     ,attention_mask=A__).sequences
																	self.assertEqual(generation_outputs.shape[-1]     ,A__)

																	lowercase					=		jit(model.generate)
																	lowercase					=		jit_generate(A__     ,attention_mask=A__).sequences

																	self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())




							def 				A__   (       self):
												lowercase			,		lowercase			,		lowercase			,		lowercase					=		self._get_input_ids_and_config()

												# pad attention mask on the left
												lowercase					=		attention_mask.at[(0, 0)].set(0)

												lowercase					=		True
												lowercase					=		max_length

												for model_class in self.all_generative_model_classes:
																	lowercase					=		model_class(A__)

																	lowercase					=		model.generate(A__     ,attention_mask=A__).sequences
																	self.assertEqual(generation_outputs.shape[-1]     ,A__)

																	lowercase					=		jit(model.generate)
																	lowercase					=		jit_generate(A__     ,attention_mask=A__).sequences

																	self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())





							def 				A__   (       self):
												lowercase			,		lowercase			,		lowercase			,		lowercase					=		self._get_input_ids_and_config()

												# pad attention mask on the left
												lowercase					=		attention_mask.at[(0, 0)].set(0)

												lowercase					=		2
												lowercase					=		max_length

												for model_class in self.all_generative_model_classes:
																	lowercase					=		model_class(A__)

																	lowercase					=		model.generate(A__     ,attention_mask=A__).sequences
																	self.assertEqual(generation_outputs.shape[-1]     ,A__)

																	lowercase					=		jit(model.generate)
																	lowercase					=		jit_generate(A__     ,attention_mask=A__).sequences

																	self.assertListEqual(generation_outputs.tolist()     ,jit_generation_outputs.tolist())







@require_flax
class        lowercase	(      unittest.TestCase	):




							def 				A__   (       self):
												lowercase					=		AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-bert''')
												lowercase					=		FlaxAutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-bert-flax-only''')

												lowercase					=		'''Hello world'''
												lowercase					=		tokenizer(A__     ,return_tensors='''np''').input_ids

												# typos are quickly detected (the correct argument is `do_sample`)
												with self.assertRaisesRegex(A__     ,'''do_samples'''):
																	model.generate(A__     ,do_samples=A__)

												# arbitrary arguments that will not be used anywhere are also not accepted
												with self.assertRaisesRegex(A__     ,'''foo'''):
																	lowercase					=		{'''foo''': '''bar'''}
																	model.generate(A__     ,**A__)




