





from queue import Queue
from typing import TYPE_CHECKING, Optional


if TYPE_CHECKING:
  from ..models.auto import AutoTokenizer






class 					lowercase__ :

   '''simple docstring'''


   def    UpperCamelCase__						(					self,	__magic_name__							)				->   int:






       """simple docstring"""
       raise NotImplementedError()




   def    UpperCamelCase__						(					self							)				->   Dict:






       """simple docstring"""
       raise NotImplementedError()






class 					lowercase__ (			__lowerCamelCase       ):

   '''simple docstring'''


   def __init__(					self,	__magic_name__,	__magic_name__ = False,	**__magic_name__							)				->   str:






       """simple docstring"""
       UpperCamelCase__							:       Dict      =   tokenizer
       UpperCamelCase__							:       Tuple      =   skip_prompt
       UpperCamelCase__							:       Any      =   decode_kwargs

       # variables used in the streaming process
       UpperCamelCase__							:       List[str]      =   []
       UpperCamelCase__							:       Tuple      =   0
       UpperCamelCase__							:       Optional[int]      =   True




   def    UpperCamelCase__						(					self,	__magic_name__							)				->   Tuple:






       """simple docstring"""
       if len(value.shape							) > 1 and value.shape[0] > 1:
           raise ValueError('''TextStreamer only supports batch size 1'''							)
       elif len(value.shape							) > 1:
           UpperCamelCase__							:       str      =   value[0]

       if self.skip_prompt and self.next_tokens_are_prompt:
           UpperCamelCase__							:       Optional[Any]      =   False
           return

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

       # After the symbol for a new line, we flush the cache.
       if text.endswith('''\n'''							):
           UpperCamelCase__							:       Optional[int]      =   text[self.print_len :]
           UpperCamelCase__							:       Any      =   []
           UpperCamelCase__							:       str      =   0
       # If the last token is a CJK character, we print the characters.
       elif len(__magic_name__							) > 0 and self._is_chinese_char(ord(text[-1]							)							):
           UpperCamelCase__							:       Optional[int]      =   text[self.print_len :]
           self.print_len += len(__magic_name__							)
       # 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:
           UpperCamelCase__							:       Optional[int]      =   text[self.print_len : text.rfind(''' '''							) + 1]
           self.print_len += len(__magic_name__							)

       self.on_finalized_text(__magic_name__							)




   def    UpperCamelCase__						(					self							)				->   Optional[Any]:






       """simple docstring"""
       # Flush the cache, if it exists
       if len(self.token_cache							) > 0:
           UpperCamelCase__							:       Optional[Any]      =   self.tokenizer.decode(self.token_cache,	**self.decode_kwargs							)
           UpperCamelCase__							:       List[Any]      =   text[self.print_len :]
           UpperCamelCase__							:       Optional[int]      =   []
           UpperCamelCase__							:       int      =   0
       else:
           UpperCamelCase__							:       List[str]      =   ''''''

       UpperCamelCase__							:       Any      =   True
       self.on_finalized_text(__magic_name__,	stream_end=__magic_name__							)




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__ = False							)				->   Dict:






       """simple docstring"""
       print(__magic_name__,	flush=__magic_name__,	end='''''' if not stream_end else None							)




   def    UpperCamelCase__						(					self,	__magic_name__							)				->   Any:






       """simple docstring"""
       # 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_E_0_0 and cp <= 0x9_F_F_F)
           or (cp >= 0x3_4_0_0 and cp <= 0x4_D_B_F)  #
           or (cp >= 0x2_0_0_0_0 and cp <= 0x2_A_6_D_F)  #
           or (cp >= 0x2_A_7_0_0 and cp <= 0x2_B_7_3_F)  #
           or (cp >= 0x2_B_7_4_0 and cp <= 0x2_B_8_1_F)  #
           or (cp >= 0x2_B_8_2_0 and cp <= 0x2_C_E_A_F)  #
           or (cp >= 0xF_9_0_0 and cp <= 0xF_A_F_F)
           or (cp >= 0x2_F_8_0_0 and cp <= 0x2_F_A_1_F)  #
       ):  #
           return True

       return False






class 					lowercase__ (			__lowerCamelCase       ):

   '''simple docstring'''


   def __init__(					self,	__magic_name__,	__magic_name__ = False,	__magic_name__ = None,	**__magic_name__							)				->   Dict:






       """simple docstring"""
       super().__init__(__magic_name__,	__magic_name__,	**__magic_name__							)
       UpperCamelCase__							:       Any      =   Queue()
       UpperCamelCase__							:       Union[str, Any]      =   None
       UpperCamelCase__							:       str      =   timeout




   def    UpperCamelCase__						(					self,	__magic_name__,	__magic_name__ = False							)				->   Dict:






       """simple docstring"""
       self.text_queue.put(__magic_name__,	timeout=self.timeout							)
       if stream_end:
           self.text_queue.put(self.stop_signal,	timeout=self.timeout							)




   def __iter__(					self							)				->   Union[str, Any]:






       """simple docstring"""
       return self




   def    UpperCamelCase__						(					self							)				->   Any:






       """simple docstring"""
       UpperCamelCase__							:       str      =   self.text_queue.get(timeout=self.timeout							)
       if value == self.stop_signal:
           raise StopIteration()
       else:
           return value








from manim import *






class 					lowercase__ (			__lowerCamelCase       ):

   '''simple docstring'''


   def    UpperCamelCase__						(					self							)				->   List[str]:






       """simple docstring"""
       UpperCamelCase__							:       Any      =   Rectangle(height=0.5,	width=0.5							)
       UpperCamelCase__							:       Any      =   Rectangle(height=0.25,	width=0.25							)
       UpperCamelCase__							:       Dict      =   Rectangle(height=0.46,	width=0.46							).set_stroke(width=0							)

       UpperCamelCase__							:       List[Any]      =   [mem.copy() for i in range(6							)]
       UpperCamelCase__							:       Optional[int]      =   [mem.copy() for i in range(6							)]
       UpperCamelCase__							:       str      =   VGroup(*__magic_name__							).arrange(__magic_name__,	buff=0							)
       UpperCamelCase__							:       List[str]      =   VGroup(*__magic_name__							).arrange(__magic_name__,	buff=0							)
       UpperCamelCase__							:       Tuple      =   VGroup(__magic_name__,	__magic_name__							).arrange(__magic_name__,	buff=0							)
       UpperCamelCase__							:       List[str]      =   Text('''CPU''',	font_size=24							)
       UpperCamelCase__							:       Any      =   Group(__magic_name__,	__magic_name__							).arrange(__magic_name__,	buff=0.5,	aligned_edge=__magic_name__							)
       cpu.move_to([-2.5, -0.5, 0]							)
       self.add(__magic_name__							)

       UpperCamelCase__							:       int      =   [mem.copy() for i in range(4							)]
       UpperCamelCase__							:       str      =   VGroup(*__magic_name__							).arrange(__magic_name__,	buff=0							)
       UpperCamelCase__							:       Tuple      =   Text('''GPU''',	font_size=24							)
       UpperCamelCase__							:       str      =   Group(__magic_name__,	__magic_name__							).arrange(__magic_name__,	buff=0.5,	aligned_edge=__magic_name__							)
       gpu.move_to([-1, -1, 0]							)
       self.add(__magic_name__							)

       UpperCamelCase__							:       str      =   [mem.copy() for i in range(6							)]
       UpperCamelCase__							:       Tuple      =   VGroup(*__magic_name__							).arrange(__magic_name__,	buff=0							)

       UpperCamelCase__							:       Tuple      =   Text('''Model''',	font_size=24							)
       UpperCamelCase__							:       Tuple      =   Group(__magic_name__,	__magic_name__							).arrange(__magic_name__,	buff=0.5,	aligned_edge=__magic_name__							)
       model.move_to([3, -1.0, 0]							)
       self.add(__magic_name__							)

       UpperCamelCase__							:       Union[str, Any]      =   []
       UpperCamelCase__							:       Any      =   []
       UpperCamelCase__							:       List[Any]      =   []

       for i, rect in enumerate(__magic_name__							):
           rect.set_stroke(__magic_name__							)

           UpperCamelCase__							:       Tuple      =   Rectangle(height=0.46 / 4,	width=0.46 / 3							).set_stroke(width=0.0							).set_fill(__magic_name__,	opacity=0.7							)

           if i == 0:
               cpu_target.next_to(cpu_left_col_base[0].get_corner(DOWN + LEFT							),	buff=0.02,	direction=__magic_name__							)
               cpu_target.set_x(cpu_target.get_x() + 0.1							)
           elif i == 3:
               cpu_target.next_to(model_cpu_arr[0],	direction=__magic_name__,	buff=0.0							)
           else:
               cpu_target.next_to(model_cpu_arr[i - 1],	direction=__magic_name__,	buff=0.0							)
           self.add(__magic_name__							)
           model_cpu_arr.append(__magic_name__							)

       self.add(*__magic_name__,	*__magic_name__,	*__magic_name__							)

       UpperCamelCase__							:       Tuple      =   [mem.copy() for i in range(6							)]
       UpperCamelCase__							:       Dict      =   VGroup(*__magic_name__							).arrange(__magic_name__,	buff=0							)

       UpperCamelCase__							:       List[str]      =   Text('''Loaded Checkpoint''',	font_size=24							)
       UpperCamelCase__							:       Optional[Any]      =   Group(__magic_name__,	__magic_name__							).arrange(__magic_name__,	buff=0.5,	aligned_edge=__magic_name__							)
       checkpoint.move_to([3, 0.5, 0]							)

       self.add(__magic_name__							)

       UpperCamelCase__							:       Tuple      =   []
       UpperCamelCase__							:       List[str]      =   []

       for i, rect in enumerate(__magic_name__							):
           UpperCamelCase__							:       Optional[int]      =   fill.copy().set_fill(__magic_name__,	opacity=0.7							)
           target.move_to(__magic_name__							)
           ckpt_arr.append(__magic_name__							)

           UpperCamelCase__							:       int      =   target.copy()
           if i < 5:
               cpu_target.move_to(cpu_left_col_base[i + 1]							)
           else:
               cpu_target.move_to(cpu_right_col_base[i - 5]							)
           ckpt_cpu_arr.append(__magic_name__							)
       self.add(*__magic_name__,	*__magic_name__							)

       UpperCamelCase__							:       List[str]      =   Square(side_length=2.2							)
       key.move_to([-5, 2, 0]							)

       UpperCamelCase__							:       List[str]      =   MarkupText(
           f"<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model",	font_size=18,	)

       key_text.move_to([-5, 2.4, 0]							)

       self.add(__magic_name__,	__magic_name__							)

       UpperCamelCase__							:       Any      =   MarkupText(
           f"<span fgcolor='{BLUE}'>●</span> Checkpoint",	font_size=18,	)

       blue_text.next_to(__magic_name__,	DOWN * 2.4,	aligned_edge=key_text.get_left()							)
       self.add(__magic_name__							)

       UpperCamelCase__							:       Dict      =   MarkupText(
           f"Based on the passed in configuration, weights are stored in\na variety of np.memmaps on disk or to a particular device.",	font_size=24,	)
       step_a.move_to([2, 2, 0]							)

       UpperCamelCase__							:       Any      =   [meta_mem.copy() for i in range(6							)]
       UpperCamelCase__							:       int      =   [meta_mem.copy() for i in range(6							)]
       UpperCamelCase__							:       Optional[int]      =   VGroup(*__magic_name__							).arrange(__magic_name__,	buff=0							)
       UpperCamelCase__							:       Union[str, Any]      =   VGroup(*__magic_name__							).arrange(__magic_name__,	buff=0							)
       UpperCamelCase__							:       Union[str, Any]      =   VGroup(__magic_name__,	__magic_name__							).arrange(__magic_name__,	buff=0							)
       UpperCamelCase__							:       Any      =   Text('''Disk''',	font_size=24							)
       UpperCamelCase__							:       List[Any]      =   Group(__magic_name__,	__magic_name__							).arrange(__magic_name__,	buff=0.5,	aligned_edge=__magic_name__							)
       disk.move_to([-4.0, -1.25, 0]							)
       self.play(Write(__magic_name__,	run_time=3							),	Write(__magic_name__,	run_time=1							),	Create(__magic_name__,	run_time=1							)							)

       UpperCamelCase__							:       Union[str, Any]      =   []
       for i, rect in enumerate(__magic_name__							):
           UpperCamelCase__							:       Union[str, Any]      =   rect.copy()
           target.generate_target()
           target.target.move_to(disk_left_col_base[i]							).scale(0.5							)
           animations.append(MoveToTarget(__magic_name__,	run_time=1.5							)							)
       self.play(*__magic_name__							)

       self.play(FadeOut(__magic_name__							)							)

       UpperCamelCase__							:       Optional[int]      =   MarkupText(f"Then, the checkpoint is removed from memory\nthrough garbage collection.",	font_size=24							)
       step_a.move_to([2, 2, 0]							)

       self.play(Write(__magic_name__,	run_time=3							)							)

       self.play(
           FadeOut(__magic_name__,	__magic_name__,	*__magic_name__,	*__magic_name__							),	)

       self.wait()

