



'''simple docstring'''
import unittest
from queue import Empty
from threading import Thread

from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available
from transformers.testing_utils import CaptureStdout, require_torch, torch_device

from ..test_modeling_common import ids_tensor


if is_torch_available():
					import torch

					from transformers import AutoModelForCausalLM






@require_torch
class 							lowercase__	(							unittest.TestCase   ):





	def 	UpperCamelCase_  ( self					:				Dict ):







							'''simple docstring'''
							_UpperCamelCase					:		Any 				=   AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2' )
							_UpperCamelCase					:		Any 				=   AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ).to(lowerCamelCase__ )
							_UpperCamelCase					:		str 				=   -1

							_UpperCamelCase					:		Optional[Any] 				=   ids_tensor((1, 5) ,vocab_size=model.config.vocab_size ).to(lowerCamelCase__ )
							_UpperCamelCase					:		Union[str, Any] 				=   model.generate(lowerCamelCase__ ,max_new_tokens=10 ,do_sample=lowerCamelCase__ )
							_UpperCamelCase					:		Union[str, Any] 				=   tokenizer.decode(greedy_ids[0] )

							with CaptureStdout() as cs:
													_UpperCamelCase					:		Any 				=   TextStreamer(lowerCamelCase__ )
													model.generate(lowerCamelCase__ ,max_new_tokens=10 ,do_sample=lowerCamelCase__ ,streamer=lowerCamelCase__ )
							# The greedy text should be printed to stdout, except for the final "\n" in the streamer
							_UpperCamelCase					:		Optional[int] 				=   cs.out[:-1]

							self.assertEqual(lowerCamelCase__ ,lowerCamelCase__ )





	def 	UpperCamelCase_  ( self					:				str ):







							'''simple docstring'''
							_UpperCamelCase					:		Any 				=   AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2' )
							_UpperCamelCase					:		Union[str, Any] 				=   AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ).to(lowerCamelCase__ )
							_UpperCamelCase					:		Optional[int] 				=   -1

							_UpperCamelCase					:		Optional[int] 				=   ids_tensor((1, 5) ,vocab_size=model.config.vocab_size ).to(lowerCamelCase__ )
							_UpperCamelCase					:		List[Any] 				=   model.generate(lowerCamelCase__ ,max_new_tokens=10 ,do_sample=lowerCamelCase__ )
							_UpperCamelCase					:		int 				=   tokenizer.decode(greedy_ids[0] )

							_UpperCamelCase					:		List[str] 				=   TextIteratorStreamer(lowerCamelCase__ )
							_UpperCamelCase					:		Union[str, Any] 				=   {'input_ids': input_ids, 'max_new_tokens': 10, 'do_sample': False, 'streamer': streamer}
							_UpperCamelCase					:		Optional[Any] 				=   Thread(target=model.generate ,kwargs=lowerCamelCase__ )
							thread.start()
							_UpperCamelCase					:		Tuple 				=   ''
							for new_text in streamer:
													streamer_text += new_text

							self.assertEqual(lowerCamelCase__ ,lowerCamelCase__ )





	def 	UpperCamelCase_  ( self					:				Union[str, Any] ):







							'''simple docstring'''
							_UpperCamelCase					:		Any 				=   AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2' )
							_UpperCamelCase					:		int 				=   AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ).to(lowerCamelCase__ )
							_UpperCamelCase					:		List[Any] 				=   -1

							_UpperCamelCase					:		Dict 				=   ids_tensor((1, 5) ,vocab_size=model.config.vocab_size ).to(lowerCamelCase__ )
							_UpperCamelCase					:		str 				=   model.generate(lowerCamelCase__ ,max_new_tokens=10 ,do_sample=lowerCamelCase__ )
							_UpperCamelCase					:		Optional[Any] 				=   greedy_ids[:, input_ids.shape[1] :]
							_UpperCamelCase					:		str 				=   tokenizer.decode(new_greedy_ids[0] )

							with CaptureStdout() as cs:
													_UpperCamelCase					:		Optional[int] 				=   TextStreamer(lowerCamelCase__ ,skip_prompt=lowerCamelCase__ )
													model.generate(lowerCamelCase__ ,max_new_tokens=10 ,do_sample=lowerCamelCase__ ,streamer=lowerCamelCase__ )
							# The greedy text should be printed to stdout, except for the final "\n" in the streamer
							_UpperCamelCase					:		Optional[int] 				=   cs.out[:-1]

							self.assertEqual(lowerCamelCase__ ,lowerCamelCase__ )





	def 	UpperCamelCase_  ( self					:				Optional[Any] ):







							'''simple docstring'''
							# Tests that we can pass `decode_kwargs` to the streamer to control how the tokens are decoded. Must be tested
							# with actual models -- the dummy models' tokenizers are not aligned with their models, and
							# `skip_special_tokens=True` has no effect on them
							_UpperCamelCase					:		Dict 				=   AutoTokenizer.from_pretrained('distilgpt2' )
							_UpperCamelCase					:		int 				=   AutoModelForCausalLM.from_pretrained('distilgpt2' ).to(lowerCamelCase__ )
							_UpperCamelCase					:		Optional[Any] 				=   -1

							_UpperCamelCase					:		List[str] 				=   torch.ones((1, 5) ,device=lowerCamelCase__ ).long() * model.config.bos_token_id
							with CaptureStdout() as cs:
													_UpperCamelCase					:		Dict 				=   TextStreamer(lowerCamelCase__ ,skip_special_tokens=lowerCamelCase__ )
													model.generate(lowerCamelCase__ ,max_new_tokens=1 ,do_sample=lowerCamelCase__ ,streamer=lowerCamelCase__ )

							# The prompt contains a special token, so the streamer should not print it. As such, the output text, when
							# re-tokenized, must only contain one token
							_UpperCamelCase					:		Dict 				=   cs.out[:-1]  # Remove the final "\n"
							_UpperCamelCase					:		str 				=   tokenizer(lowerCamelCase__ ,return_tensors='pt' )
							self.assertEqual(streamer_text_tokenized.input_ids.shape ,(1, 1) )





	def 	UpperCamelCase_  ( self					:				Optional[Any] ):







							'''simple docstring'''
							_UpperCamelCase					:		Any 				=   AutoTokenizer.from_pretrained('hf-internal-testing/tiny-random-gpt2' )
							_UpperCamelCase					:		Optional[Any] 				=   AutoModelForCausalLM.from_pretrained('hf-internal-testing/tiny-random-gpt2' ).to(lowerCamelCase__ )
							_UpperCamelCase					:		Any 				=   -1

							_UpperCamelCase					:		Dict 				=   ids_tensor((1, 5) ,vocab_size=model.config.vocab_size ).to(lowerCamelCase__ )
							_UpperCamelCase					:		str 				=   TextIteratorStreamer(lowerCamelCase__ ,timeout=0.0_0_1 )
							_UpperCamelCase					:		Any 				=   {'input_ids': input_ids, 'max_new_tokens': 10, 'do_sample': False, 'streamer': streamer}
							_UpperCamelCase					:		Optional[int] 				=   Thread(target=model.generate ,kwargs=lowerCamelCase__ )
							thread.start()

							# The streamer will timeout after 0.001 seconds, so an exception will be raised
							with self.assertRaises(lowerCamelCase__ ):
													_UpperCamelCase					:		int 				=   ''
													for new_text in streamer:
																			streamer_text += new_text








from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig


__lowerCAmelCase		:				int       			=		{
    'albert-base-v1': 'https://huggingface.co/albert-base-v1/resolve/main/config.json',
    'albert-large-v1': 'https://huggingface.co/albert-large-v1/resolve/main/config.json',
    'albert-xlarge-v1': 'https://huggingface.co/albert-xlarge-v1/resolve/main/config.json',
    'albert-xxlarge-v1': 'https://huggingface.co/albert-xxlarge-v1/resolve/main/config.json',
    'albert-base-v2': 'https://huggingface.co/albert-base-v2/resolve/main/config.json',
    'albert-large-v2': 'https://huggingface.co/albert-large-v2/resolve/main/config.json',
    'albert-xlarge-v2': 'https://huggingface.co/albert-xlarge-v2/resolve/main/config.json',
    'albert-xxlarge-v2': 'https://huggingface.co/albert-xxlarge-v2/resolve/main/config.json',
}




class     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_       :							Optional[int]							 =					"""albert"""





				def __init__(      self				:     List[str]					,      __lowerCamelCase				:     Union[str, Any]=3_00_00					,      __lowerCamelCase				:     Union[str, Any]=1_28					,      __lowerCamelCase				:     Any=40_96					,      __lowerCamelCase				:     str=12					,      __lowerCamelCase				:     Union[str, Any]=1					,      __lowerCamelCase				:     Union[str, Any]=64					,      __lowerCamelCase				:     Dict=1_63_84					,      __lowerCamelCase				:     List[Any]=1					,      __lowerCamelCase				:     Optional[Any]="gelu_new"					,      __lowerCamelCase				:     List[str]=0					,      __lowerCamelCase				:     List[str]=0					,      __lowerCamelCase				:     Tuple=5_12					,      __lowerCamelCase				:     Optional[Any]=2					,      __lowerCamelCase				:     Optional[Any]=0.02					,      __lowerCamelCase				:     Optional[Any]=1e-12					,      __lowerCamelCase				:     Union[str, Any]=0.1					,      __lowerCamelCase				:     Any="absolute"					,      __lowerCamelCase				:     List[str]=0					,      __lowerCamelCase				:     List[str]=2					,      __lowerCamelCase				:     Any=3					,      **__lowerCamelCase				:     int					,      )					->     int:
							super().__init__(pad_token_id=__lowerCamelCase					,      bos_token_id=__lowerCamelCase					,      eos_token_id=__lowerCamelCase					,      **__lowerCamelCase							)

							a    =			vocab_size
							a    =			embedding_size
							a    =			hidden_size
							a    =			num_hidden_layers
							a    =			num_hidden_groups
							a    =			num_attention_heads
							a    =			inner_group_num
							a    =			hidden_act
							a    =			intermediate_size
							a    =			hidden_dropout_prob
							a    =			attention_probs_dropout_prob
							a    =			max_position_embeddings
							a    =			type_vocab_size
							a    =			initializer_range
							a    =			layer_norm_eps
							a    =			classifier_dropout_prob
							a    =			position_embedding_type




class     snake_case__							(_UpperCamelCase   ):
				"""simple docstring"""



				@property
				def 							__UpperCAmelCase							(      self				:     List[Any]							)					->     Mapping[str, Mapping[int, str]]:
							if self.task == "multiple-choice":
										a    =			{0: "batch", 1: "choice", 2: "sequence"}
							else:
										a    =			{0: "batch", 1: "sequence"}
							return OrderedDict(
							    [
							        ("input_ids", dynamic_axis),
							        ("attention_mask", dynamic_axis),
							        ("token_type_ids", dynamic_axis),
							    ]							)






