




from queue import Queue
from typing import TYPE_CHECKING, Optional


if TYPE_CHECKING:
		from ..models.auto import AutoTokenizer




class 			UpperCAmelCase_      :




							'''simple docstring'''



							def    _UpperCAmelCase     (					self       :	Any       ,     a       :	str			)			->					Tuple:
										raise NotImplementedError()







							def    _UpperCAmelCase     (					self       :	Optional[int]			)			->					str:
										raise NotImplementedError()





class 			UpperCAmelCase_      (   A       ):




							'''simple docstring'''



							def __init__(					self       :	Tuple       ,     a       :	"AutoTokenizer"       ,     a       :	bool = False       ,     **a       :	str			)			->					List[Any]:
										SCREAMING_SNAKE_CASE           =					tokenizer
										SCREAMING_SNAKE_CASE           =					skip_prompt
										SCREAMING_SNAKE_CASE           =					decode_kwargs

										# variables used in the streaming process
										SCREAMING_SNAKE_CASE           =					[]
										SCREAMING_SNAKE_CASE           =					0
										SCREAMING_SNAKE_CASE           =					True





							def    _UpperCAmelCase     (					self       :	List[Any]       ,     a       :	Optional[int]			)			->					Union[str, Any]:
										if len(value.shape			) > 1 and value.shape[0] > 1:
													raise ValueError("""TextStreamer only supports batch size 1"""			)
										elif len(value.shape			) > 1:
													SCREAMING_SNAKE_CASE           =					value[0]

										if self.skip_prompt and self.next_tokens_are_prompt:
													SCREAMING_SNAKE_CASE           =					False
													return

										# Add the new token to the cache and decodes the entire thing.
										self.token_cache.extend(value.tolist()			)
										SCREAMING_SNAKE_CASE           =					self.tokenizer.decode(self.token_cache       ,     **self.decode_kwargs			)

										# After the symbol for a new line, we flush the cache.
										if text.endswith("""\n"""			):
													SCREAMING_SNAKE_CASE           =					text[self.print_len :]
													SCREAMING_SNAKE_CASE           =					[]
													SCREAMING_SNAKE_CASE           =					0
										# If the last token is a CJK character, we print the characters.
										elif len(a			) > 0 and self._is_chinese_char(ord(text[-1]			)			):
													SCREAMING_SNAKE_CASE           =					text[self.print_len :]
													self.print_len += len(a			)
										# Otherwise, prints until the last space char (simple heuristic to avoid printing incomplete words,
										# which may change with the subsequent token -- there are probably smarter ways to do this!)
										else:
													SCREAMING_SNAKE_CASE           =					text[self.print_len : text.rfind(""" """			) + 1]
													self.print_len += len(a			)

										self.on_finalized_text(a			)





							def    _UpperCAmelCase     (					self       :	Any			)			->					int:
										# Flush the cache, if it exists
										if len(self.token_cache			) > 0:
													SCREAMING_SNAKE_CASE           =					self.tokenizer.decode(self.token_cache       ,     **self.decode_kwargs			)
													SCREAMING_SNAKE_CASE           =					text[self.print_len :]
													SCREAMING_SNAKE_CASE           =					[]
													SCREAMING_SNAKE_CASE           =					0
										else:
													SCREAMING_SNAKE_CASE           =					""""""

										SCREAMING_SNAKE_CASE           =					True
										self.on_finalized_text(a       ,     stream_end=a			)





							def    _UpperCAmelCase     (					self       :	Optional[Any]       ,     a       :	str       ,     a       :	bool = False			)			->					Union[str, Any]:
										print(a       ,     flush=a       ,     end="""""" if not stream_end else None			)







							def    _UpperCAmelCase     (					self       :	Optional[int]       ,     a       :	Optional[int]			)			->					Optional[Any]:
										# This defines a "chinese character" as anything in the CJK Unicode block:
										#   https://en.wikipedia.org/wiki/CJK_Unified_Ideographs_(Unicode_block)
										#
										# Note that the CJK Unicode block is NOT all Japanese and Korean characters,
										# despite its name. The modern Korean Hangul alphabet is a different block,
										# as is Japanese Hiragana and Katakana. Those alphabets are used to write
										# space-separated words, so they are not treated specially and handled
										# like the all of the other languages.
										if (
										    (cp >= 0x4_E00 and cp <= 0x9_FFF)
										    or (cp >= 0x3_400 and cp <= 0x4_DBF)  #
										    or (cp >= 0x20_000 and cp <= 0x2A_6DF)  #
										    or (cp >= 0x2A_700 and cp <= 0x2B_73F)  #
										    or (cp >= 0x2B_740 and cp <= 0x2B_81F)  #
										    or (cp >= 0x2B_820 and cp <= 0x2C_EAF)  #
										    or (cp >= 0xF_900 and cp <= 0xF_AFF)
										    or (cp >= 0x2F_800 and cp <= 0x2F_A1F)  #
										):  #
													return True

										return False





class 			UpperCAmelCase_      (   A       ):




							'''simple docstring'''



							def __init__(					self       :	int       ,     a       :	"AutoTokenizer"       ,     a       :	bool = False       ,     a       :	Optional[float] = None       ,     **a       :	Optional[int]			)			->					Dict:
										super().__init__(a       ,     a       ,     **a			)
										SCREAMING_SNAKE_CASE           =					Queue()
										SCREAMING_SNAKE_CASE           =					None
										SCREAMING_SNAKE_CASE           =					timeout





							def    _UpperCAmelCase     (					self       :	Any       ,     a       :	str       ,     a       :	bool = False			)			->					str:
										self.text_queue.put(a       ,     timeout=self.timeout			)
										if stream_end:
													self.text_queue.put(self.stop_signal       ,     timeout=self.timeout			)





							def __iter__(					self       :	Optional[int]			)			->					Optional[Any]:
										return self







							def    _UpperCAmelCase     (					self       :	List[str]			)			->					Union[str, Any]:
										SCREAMING_SNAKE_CASE           =					self.text_queue.get(timeout=self.timeout			)
										if value == self.stop_signal:
													raise StopIteration()
										else:
													return value












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


__A						:							Any							=  logging.get_logger(__name__)

__A						:							int							=  {
    # See all MEGATRON_BERT models at https://huggingface.co/models?filter=bert
}




class 			UpperCAmelCase_      (   A       ):




							'''simple docstring'''



							a__						  =     '''megatron-bert'''





							def __init__(					self       :	str       ,     a       :	Union[str, Any]=29_056       ,     a       :	Optional[int]=1_024       ,     a       :	str=24       ,     a       :	str=16       ,     a       :	str=4_096       ,     a       :	Any="gelu"       ,     a       :	Any=0.1       ,     a       :	Optional[int]=0.1       ,     a       :	Optional[int]=512       ,     a       :	Optional[Any]=2       ,     a       :	List[str]=0.02       ,     a       :	List[Any]=1E-1_2       ,     a       :	Any=0       ,     a       :	List[Any]="absolute"       ,     a       :	List[str]=True       ,     **a       :	Optional[Any]       ,     )			->					List[str]:
										super().__init__(pad_token_id=a       ,     **a			)

										SCREAMING_SNAKE_CASE           =					vocab_size
										SCREAMING_SNAKE_CASE           =					hidden_size
										SCREAMING_SNAKE_CASE           =					num_hidden_layers
										SCREAMING_SNAKE_CASE           =					num_attention_heads
										SCREAMING_SNAKE_CASE           =					hidden_act
										SCREAMING_SNAKE_CASE           =					intermediate_size
										SCREAMING_SNAKE_CASE           =					hidden_dropout_prob
										SCREAMING_SNAKE_CASE           =					attention_probs_dropout_prob
										SCREAMING_SNAKE_CASE           =					max_position_embeddings
										SCREAMING_SNAKE_CASE           =					type_vocab_size
										SCREAMING_SNAKE_CASE           =					initializer_range
										SCREAMING_SNAKE_CASE           =					layer_norm_eps
										SCREAMING_SNAKE_CASE           =					position_embedding_type
										SCREAMING_SNAKE_CASE           =					use_cache






