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     lowerCAmelCase_     (       unittest.TestCase ):
							UpperCAmelCase				   =     MODEL_FOR_CAUSAL_LM_MAPPING
							UpperCAmelCase				   =     TF_MODEL_FOR_CAUSAL_LM_MAPPING







							@require_torch
							def  UpperCamelCase_						(   self      :     str		):
												_UpperCamelCase			     =     pipeline(task='''text-generation''' ,			model='''sshleifer/tiny-ctrl''' ,			framework='''pt'''		)
												# Using `do_sample=False` to force deterministic output
												_UpperCamelCase			     =     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@@'''
												            )
												        }
												    ] ,			)

												_UpperCamelCase			     =     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@@'''
												                )
												            }
												        ],
												    ] ,			)

												_UpperCamelCase			     =     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		)},
												    ] ,			)
												_UpperCamelCase			     =     text_generator.model.config.eos_token_id
												_UpperCamelCase			     =     '''<pad>'''
												_UpperCamelCase			     =     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  UpperCamelCase_						(   self      :     Dict		):
												_UpperCamelCase			     =     pipeline(task='''text-generation''' ,			model='''sshleifer/tiny-ctrl''' ,			framework='''tf'''		)

												# Using `do_sample=False` to force deterministic output
												_UpperCamelCase			     =     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,'''
												            )
												        }
												    ] ,			)

												_UpperCamelCase			     =     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  UpperCamelCase_						(   self      :     int ,			_A      :     str ,			_A      :     Union[str, Any] ,			_A      :     Any		):
												_UpperCamelCase			     =     TextGenerationPipeline(model=_A ,			tokenizer=_A		)
												return text_generator, ["This is a test", "Another test"]







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

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







							def  UpperCamelCase_						(   self      :     Any ,			_A      :     List[Any] ,			_A      :     Union[str, Any]		):
												_UpperCamelCase			     =     text_generator.model
												_UpperCamelCase			     =     text_generator.tokenizer

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

												_UpperCamelCase			     =     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''']		)

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

												_UpperCamelCase			     =     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'''		)		)

												_UpperCamelCase			     =     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:
																	_UpperCamelCase			     =     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		):
																	_UpperCamelCase			     =     text_generator('''test''' ,			return_full_text=_A ,			return_text=_A		)
												with self.assertRaises(_A		):
																	_UpperCamelCase			     =     text_generator('''test''' ,			return_full_text=_A ,			return_tensors=_A		)
												with self.assertRaises(_A		):
																	_UpperCamelCase			     =     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__
												):
																	_UpperCamelCase			     =     text_generator(''''''		)
																	self.assertEqual(_A ,			[{'''generated_text''': ANY(_A		)}]		)
												else:
																	with self.assertRaises((ValueError, AssertionError)		):
																						_UpperCamelCase			     =     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.
												_UpperCamelCase			     =     ['''RwkvForCausalLM''', '''XGLMForCausalLM''', '''GPTNeoXForCausalLM''']
												if (
												    tokenizer.model_max_length < 1_0000
												    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''' * 500 ,			max_new_tokens=20		)

																	_UpperCamelCase			     =     text_generator('''This is a test''' * 500 ,			handle_long_generation='''hole''' ,			max_new_tokens=20		)
																	# Hole strategy cannot work
																	with self.assertRaises(_A		):
																						text_generator(
																						    '''This is a test''' * 500 ,			handle_long_generation='''hole''' ,			max_new_tokens=tokenizer.model_max_length + 10 ,			)







							@require_torch
							@require_accelerate
							@require_torch_gpu
							def  UpperCamelCase_						(   self      :     Optional[int]		):
												import torch

												# Classic `model_kwargs`
												_UpperCamelCase			     =     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		)
												_UpperCamelCase			     =     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.)
												_UpperCamelCase			     =     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		)
												_UpperCamelCase			     =     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
												_UpperCamelCase			     =     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		)
												_UpperCamelCase			     =     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  UpperCamelCase_						(   self      :     Union[str, Any]		):
												import torch

												_UpperCamelCase			     =     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  UpperCamelCase_						(   self      :     Optional[int]		):
												import torch

												_UpperCamelCase			     =     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  UpperCamelCase_						(   self      :     Tuple		):
												_UpperCamelCase			     =     '''Hello world'''
												_UpperCamelCase			     =     pipeline('''text-generation''' ,			model='''hf-internal-testing/tiny-random-gpt2'''		)
												if text_generator.model.framework == "tf":
																	_UpperCamelCase			     =     logging.get_logger('''transformers.generation.tf_utils'''		)
												else:
																	_UpperCamelCase			     =     logging.get_logger('''transformers.generation.utils'''		)
												_UpperCamelCase			     =     '''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:
																	_UpperCamelCase			     =     text_generator(_A ,			max_length=10 ,			max_new_tokens=1		)
												self.assertIn(_A ,			cl.out		)

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

												with CaptureLogger(_A		) as cl:
																	_UpperCamelCase			     =     text_generator(_A ,			max_length=10		)
												self.assertNotIn(_A ,			cl.out		)


"""simple docstring"""





import functools
import operator

from ...configuration_utils import PretrainedConfig
from ...utils import logging


_a								=						logging.get_logger(__name__)

_a								=						{
    """microsoft/wavlm-base""": """https://huggingface.co/microsoft/wavlm-base/resolve/main/config.json""",
    # See all WavLM models at https://huggingface.co/models?filter=wavlm
}






class        _UpperCAmelCase(       lowerCamelCase      ):
		lowercase__         = 'wavlm'



		def __init__(   self							,						__a=32							,						__a=7_68							,						__a=12							,						__a=12							,						__a=30_72							,						__a="gelu"							,						__a=0.1							,						__a=0.1							,						__a=0.1							,						__a=0.0							,						__a=0.1							,						__a=0.1							,						__a=0.02							,						__a=1e-5							,						__a="group"							,						__a="gelu"							,						__a=(5_12, 5_12, 5_12, 5_12, 5_12, 5_12, 5_12)							,						__a=(5, 2, 2, 2, 2, 2, 2)							,						__a=(10, 3, 3, 3, 3, 2, 2)							,						__a=False							,						__a=1_28							,						__a=16							,						__a=3_20							,						__a=8_00							,						__a=False							,						__a=True							,						__a=0.05							,						__a=10							,						__a=2							,						__a=0.0							,						__a=10							,						__a=3_20							,						__a=2							,						__a=0.1							,						__a=1_00							,						__a=2_56							,						__a=2_56							,						__a=0.1							,						__a="mean"							,						__a=False							,						__a=False							,						__a=2_56							,						__a=(5_12, 5_12, 5_12, 5_12, 15_00)							,						__a=(5, 3, 3, 1, 1)							,						__a=(1, 2, 3, 1, 1)							,						__a=5_12							,						__a=80							,						__a=0							,						__a=1							,						__a=2							,						__a=False							,						__a=3							,						__a=2							,						__a=3							,						__a=None							,						**__a							,						)			->			Union[str, Any]:






							'''simple docstring'''
							super().__init__(**__a							,						pad_token_id=__a							,						bos_token_id=__a							,						eos_token_id=__a)
							_UpperCamelCase									=    hidden_size
							_UpperCamelCase									=    feat_extract_norm
							_UpperCamelCase									=    feat_extract_activation
							_UpperCamelCase									=    list(__a)
							_UpperCamelCase									=    list(__a)
							_UpperCamelCase									=    list(__a)
							_UpperCamelCase									=    conv_bias
							_UpperCamelCase									=    num_buckets
							_UpperCamelCase									=    max_bucket_distance
							_UpperCamelCase									=    num_conv_pos_embeddings
							_UpperCamelCase									=    num_conv_pos_embedding_groups
							_UpperCamelCase									=    len(self.conv_dim)
							_UpperCamelCase									=    num_hidden_layers
							_UpperCamelCase									=    intermediate_size
							_UpperCamelCase									=    hidden_act
							_UpperCamelCase									=    num_attention_heads
							_UpperCamelCase									=    hidden_dropout
							_UpperCamelCase									=    attention_dropout
							_UpperCamelCase									=    activation_dropout
							_UpperCamelCase									=    feat_proj_dropout
							_UpperCamelCase									=    final_dropout
							_UpperCamelCase									=    layerdrop
							_UpperCamelCase									=    layer_norm_eps
							_UpperCamelCase									=    initializer_range
							_UpperCamelCase									=    num_ctc_classes
							_UpperCamelCase									=    vocab_size
							_UpperCamelCase									=    do_stable_layer_norm
							_UpperCamelCase									=    use_weighted_layer_sum
							_UpperCamelCase									=    classifier_proj_size

							if (
							    (len(self.conv_stride) != self.num_feat_extract_layers)
							    or (len(self.conv_kernel) != self.num_feat_extract_layers)
							    or (len(self.conv_dim) != self.num_feat_extract_layers)
							):
												raise ValueError(
												    '''Configuration for convolutional layers is incorrect. It is required that `len(config.conv_dim)` =='''
												    ''' `len(config.conv_stride)` == `len(config.conv_kernel)`, but is `len(config.conv_dim) ='''
												    F''' {len(self.conv_dim)}`, `len(config.conv_stride) = {len(self.conv_stride)}`,'''
												    F''' `len(config.conv_kernel) = {len(self.conv_kernel)}`.''')

							# fine-tuning config parameters for SpecAugment: https://arxiv.org/abs/1904.08779
							_UpperCamelCase									=    apply_spec_augment
							_UpperCamelCase									=    mask_time_prob
							_UpperCamelCase									=    mask_time_length
							_UpperCamelCase									=    mask_time_min_masks
							_UpperCamelCase									=    mask_feature_prob
							_UpperCamelCase									=    mask_feature_length

							# parameters for pretraining with codevector quantized representations
							_UpperCamelCase									=    num_codevectors_per_group
							_UpperCamelCase									=    num_codevector_groups
							_UpperCamelCase									=    contrastive_logits_temperature
							_UpperCamelCase									=    num_negatives
							_UpperCamelCase									=    codevector_dim
							_UpperCamelCase									=    proj_codevector_dim
							_UpperCamelCase									=    diversity_loss_weight

							# ctc loss
							_UpperCamelCase									=    ctc_loss_reduction
							_UpperCamelCase									=    ctc_zero_infinity

							# adapter
							_UpperCamelCase									=    add_adapter
							_UpperCamelCase									=    adapter_kernel_size
							_UpperCamelCase									=    adapter_stride
							_UpperCamelCase									=    num_adapter_layers
							_UpperCamelCase									=    output_hidden_size or hidden_size

							# SequenceClassification-specific parameter. Feel free to ignore for other classes.
							_UpperCamelCase									=    classifier_proj_size

							# XVector-specific parameters. Feel free to ignore for other classes.
							_UpperCamelCase									=    list(__a)
							_UpperCamelCase									=    list(__a)
							_UpperCamelCase									=    list(__a)
							_UpperCamelCase									=    xvector_output_dim




		@property
		def 		UpperCAmelCase				(   self)			->			Optional[Any]:






							'''simple docstring'''
							return functools.reduce(operator.mul							,						self.conv_stride							,						1)






