



"""simple docstring"""

import warnings

from transformers import AutoTokenizer
from transformers.utils import is_torch_available
from transformers.utils.generic import ExplicitEnum

from ...processing_utils import ProcessorMixin


if is_torch_available():
	import torch




class SCREAMING_SNAKE_CASE__					(		__snake_case      ):
						_lowerCAmelCase							=					"char"
						_lowerCAmelCase							=					"bpe"
						_lowerCAmelCase							=					"wp"







lowercase__			   =  (DecodeType.CHARACTER, DecodeType.BPE, DecodeType.WORDPIECE)




class SCREAMING_SNAKE_CASE__					(		__snake_case      ):
						_lowerCAmelCase							=					["image_processor", "char_tokenizer"]
						_lowerCAmelCase							=					"ViTImageProcessor"
						_lowerCAmelCase							=					"MgpstrTokenizer"



						def __init__(self						,     _lowercase=None						,     _lowercase=None						,     **_lowercase							):





													'''simple docstring'''

													__a		:		Tuple									=       None
													if "feature_extractor" in kwargs:
																				warnings.warn(
																				    """The `feature_extractor` argument is deprecated and will be removed in v5, use `image_processor`"""
																				    """ instead."""						,     _lowercase						,     )
																				__a		:		Union[str, Any]									=       kwargs.pop("""feature_extractor"""							)

													__a		:		Optional[int]									=       image_processor if image_processor is not None else feature_extractor
													if image_processor is None:
																				raise ValueError("""You need to specify an `image_processor`."""							)
													if tokenizer is None:
																				raise ValueError("""You need to specify a `tokenizer`."""							)

													__a		:		Optional[Any]									=       tokenizer
													__a		:		Dict									=       AutoTokenizer.from_pretrained("""gpt2"""							)
													__a		:		Any									=       AutoTokenizer.from_pretrained("""bert-base-uncased"""							)

													super().__init__(_lowercase						,     _lowercase							)



						def __call__(self						,     _lowercase=None						,     _lowercase=None						,     _lowercase=None						,     **_lowercase							):





													'''simple docstring'''

													if images is None and text is None:
																				raise ValueError("""You need to specify either an `images` or `text` input to process."""							)

													if images is not None:
																				__a		:		int									=       self.image_processor(_lowercase						,     return_tensors=_lowercase						,     **_lowercase							)
													if text is not None:
																				__a		:		Optional[Any]									=       self.char_tokenizer(_lowercase						,     return_tensors=_lowercase						,     **_lowercase							)

													if text is None:
																				return inputs
													elif images is None:
																				return encodings
													else:
																				__a		:		List[str]									=       encodings["""input_ids"""]
																				return inputs



						def     lowerCAmelCase__(self						,     _lowercase							):





													'''simple docstring'''

													__a							,     __a							,     __a		:		Optional[int]									=       sequences
													__a		:		Optional[int]									=       char_preds.size(0							)

													__a							,     __a		:		Optional[int]									=       self._decode_helper(_lowercase						,     """char"""							)
													__a							,     __a		:		Tuple									=       self._decode_helper(_lowercase						,     """bpe"""							)
													__a							,     __a		:		str									=       self._decode_helper(_lowercase						,     """wp"""							)

													__a		:		Optional[int]									=       []
													__a		:		List[str]									=       []
													for i in range(_lowercase							):
																				__a		:		Optional[int]									=       [char_scores[i], bpe_scores[i], wp_scores[i]]
																				__a		:		List[str]									=       [char_strs[i], bpe_strs[i], wp_strs[i]]
																				__a		:		Optional[Any]									=       scores.index(max(_lowercase							)							)
																				final_strs.append(strs[max_score_index]							)
																				final_scores.append(scores[max_score_index]							)

													__a		:		Any									=       {}
													__a		:		Any									=       final_strs
													__a		:		int									=       final_scores
													__a		:		Tuple									=       char_strs
													__a		:		Union[str, Any]									=       bpe_strs
													__a		:		Optional[Any]									=       wp_strs
													return out



						def     lowerCAmelCase__(self						,     _lowercase						,     _lowercase							):





													'''simple docstring'''

													if format == DecodeType.CHARACTER:
																				__a		:		List[Any]									=       self.char_decode
																				__a		:		Tuple									=       1
																				__a		:		List[Any]									=       """[s]"""
													elif format == DecodeType.BPE:
																				__a		:		Union[str, Any]									=       self.bpe_decode
																				__a		:		Optional[int]									=       2
																				__a		:		Optional[int]									=       """#"""
													elif format == DecodeType.WORDPIECE:
																				__a		:		Union[str, Any]									=       self.wp_decode
																				__a		:		int									=       102
																				__a		:		Dict									=       """[SEP]"""
													else:
																				raise ValueError(F'''Format {format} is not supported.'''							)

													__a							,     __a		:		Tuple									=       [], []
													__a		:		Tuple									=       pred_logits.size(0							)
													__a		:		Optional[int]									=       pred_logits.size(1							)
													__a							,     __a		:		List[Any]									=       pred_logits.topk(1						,     dim=-1						,     largest=_lowercase						,     sorted=_lowercase							)
													__a		:		Optional[Any]									=       preds_index.view(-1						,     _lowercase							)[:, 1:]
													__a		:		Any									=       decoder(_lowercase							)
													__a							,     __a		:		Dict									=       torch.nn.functional.softmax(_lowercase						,     dim=2							).max(dim=2							)
													__a		:		Any									=       preds_max_prob[:, 1:]

													for index in range(_lowercase							):
																				__a		:		int									=       preds_str[index].find(_lowercase							)
																				__a		:		Tuple									=       preds_str[index][:pred_eos]
																				__a		:		List[str]									=       preds_index[index].cpu().tolist()
																				__a		:		int									=       pred_index.index(_lowercase							) if eos_token in pred_index else -1
																				__a		:		Dict									=       preds_max_prob[index][: pred_eos_index + 1]
																				__a		:		Any									=       pred_max_prob.cumprod(dim=0							)[-1] if pred_max_prob.nelement() != 0 else 0.0
																				dec_strs.append(_lowercase							)
																				conf_scores.append(_lowercase							)

													return dec_strs, conf_scores



						def     lowerCAmelCase__(self						,     _lowercase							):





													'''simple docstring'''

													__a		:		int									=       [seq.replace(""" """						,     """"""							) for seq in self.char_tokenizer.batch_decode(_lowercase							)]
													return decode_strs



						def     lowerCAmelCase__(self						,     _lowercase							):





													'''simple docstring'''

													return self.bpe_tokenizer.batch_decode(_lowercase							)





						def     lowerCAmelCase__(self						,     _lowercase							):





													'''simple docstring'''

													__a		:		Tuple									=       [seq.replace(""" """						,     """"""							) for seq in self.wp_tokenizer.batch_decode(_lowercase							)]
													return decode_strs






"""simple docstring"""

from manim import *




class SCREAMING_SNAKE_CASE__					(		__snake_case      ):



						def     lowerCAmelCase__(self							):





													'''simple docstring'''

													__a		:		List[str]									=       Rectangle(height=0.5						,     width=0.5							)
													__a		:		Union[str, Any]									=       Rectangle(height=0.25						,     width=0.25							)
													__a		:		Dict									=       Rectangle(height=0.46						,     width=0.46							).set_stroke(width=0							)

													__a		:		Dict									=       [mem.copy() for i in range(6							)]
													__a		:		str									=       [mem.copy() for i in range(6							)]
													__a		:		Tuple									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)
													__a		:		List[Any]									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)
													__a		:		List[Any]									=       VGroup(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0							)
													__a		:		Union[str, Any]									=       Text("""CPU"""						,     font_size=24							)
													__a		:		Dict									=       Group(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0.5						,     aligned_edge=_lowercase							)
													cpu.move_to([-2.5, -0.5, 0]							)
													self.add(_lowercase							)

													__a		:		Optional[Any]									=       [mem.copy() for i in range(4							)]
													__a		:		Dict									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)
													__a		:		List[str]									=       Text("""GPU"""						,     font_size=24							)
													__a		:		Any									=       Group(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0.5						,     aligned_edge=_lowercase							)
													gpu.move_to([-1, -1, 0]							)
													self.add(_lowercase							)

													__a		:		List[Any]									=       [mem.copy() for i in range(6							)]
													__a		:		Any									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)

													__a		:		Optional[Any]									=       Text("""Model"""						,     font_size=24							)
													__a		:		Any									=       Group(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0.5						,     aligned_edge=_lowercase							)
													model.move_to([3, -1.0, 0]							)
													self.add(_lowercase							)

													__a		:		Tuple									=       []
													__a		:		Tuple									=       []
													__a		:		Optional[int]									=       []

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

																				__a		:		str									=       Rectangle(height=0.46 / 4						,     width=0.46 / 3							).set_stroke(width=0.0							).set_fill(_lowercase						,     opacity=0.7							)

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

													self.add(*_lowercase						,     *_lowercase						,     *_lowercase							)

													__a		:		Optional[Any]									=       [mem.copy() for i in range(6							)]
													__a		:		Union[str, Any]									=       VGroup(*_lowercase							).arrange(_lowercase						,     buff=0							)

													__a		:		Any									=       Text("""Loaded Checkpoint"""						,     font_size=24							)
													__a		:		str									=       Group(_lowercase						,     _lowercase							).arrange(_lowercase						,     buff=0.5						,     aligned_edge=_lowercase							)
													checkpoint.move_to([3, 0.5, 0]							)

													self.add(_lowercase							)

													__a		:		Dict									=       []
													__a		:		int									=       []

													for i, rect in enumerate(_lowercase							):
																				__a		:		List[str]									=       fill.copy().set_fill(_lowercase						,     opacity=0.7							)
																				target.move_to(_lowercase							)
																				ckpt_arr.append(_lowercase							)

																				__a		:		Union[str, Any]									=       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(_lowercase							)
													self.add(*_lowercase						,     *_lowercase							)

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

													__a		:		List[Any]									=       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(_lowercase						,     _lowercase							)

													__a		:		str									=       MarkupText(
													    F'''<span fgcolor=\'{BLUE}\'>●</span> Checkpoint'''						,     font_size=18						,     )

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

													__a		:		Optional[int]									=       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]							)

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

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

													self.play(FadeOut(_lowercase							)							)

													__a		:		List[str]									=       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(_lowercase						,     run_time=3							)							)

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

													self.wait()

