
"""simple docstring"""




from typing import Dict, List, Optional, Union

import numpy as np

from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy


A__      :	List[Any]          =    logging.get_logger(__name__)







class   _lowercase						(       lowerCAmelCase_				):


					'''simple docstring'''





					def __init__(      self ,			__UpperCamelCase ,			__UpperCamelCase ,			__UpperCamelCase ,			**__UpperCamelCase  )->					Union[str, Any]:
										UpperCAmelCase__							: str 							=						feature_size
										UpperCAmelCase__							: List[str] 							=						sampling_rate
										UpperCAmelCase__							: List[str] 							=						padding_value

										UpperCAmelCase__							: Dict 							=						kwargs.pop("padding_side" ,			"right"  )
										UpperCAmelCase__							: Optional[int] 							=						kwargs.pop("return_attention_mask" ,			__UpperCamelCase  )

										super().__init__(**__UpperCamelCase  )



					def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase = True ,			__UpperCamelCase = None ,			__UpperCamelCase = False ,			__UpperCamelCase = None ,			__UpperCamelCase = None ,			__UpperCamelCase = None ,			)->					BatchFeature:
										# If we have a list of dicts, let's convert it in a dict of lists
										# We do this to allow using this method as a collate_fn function in PyTorch Dataloader
										if isinstance(__UpperCamelCase ,			(list, tuple)  ) and isinstance(processed_features[0] ,			(dict, BatchFeature)  ):
															UpperCAmelCase__							: Any 							=						{
															    key: [example[key] for example in processed_features] for key in processed_features[0].keys()
															}

										# The model's main input name, usually `input_values`, has be passed for padding
										if self.model_input_names[0] not in processed_features:
															raise ValueError(
															    "You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`"
															    F" to this method that includes {self.model_input_names[0]}, but you provided"
															    F" {list(processed_features.keys()  )}"  )

										UpperCAmelCase__							: Optional[Any] 							=						processed_features[self.model_input_names[0]]
										UpperCAmelCase__							: List[str] 							=						(
										    return_attention_mask if return_attention_mask is not None else self.return_attention_mask
										)

										if len(__UpperCamelCase  ) == 0:
															if return_attention_mask:
																				UpperCAmelCase__							: Optional[int] 							=						[]
															return processed_features

										# If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays
										# and rebuild them afterwards if no return_tensors is specified
										# Note that we lose the specific device the tensor may be on for PyTorch

										UpperCAmelCase__							: Union[str, Any] 							=						required_input[0]
										if isinstance(__UpperCamelCase ,			(list, tuple)  ):
															# first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
															UpperCAmelCase__							: Tuple 							=						0
															while len(required_input[index]  ) == 0:
																				index += 1
															if index < len(__UpperCamelCase  ):
																				UpperCAmelCase__							: Optional[Any] 							=						required_input[index][0]

										if return_tensors is None:
															if is_tf_tensor(__UpperCamelCase  ):
																				UpperCAmelCase__							: int 							=						"tf"
															elif is_torch_tensor(__UpperCamelCase  ):
																				UpperCAmelCase__							: Optional[Any] 							=						"pt"
															elif isinstance(__UpperCamelCase ,			(int, float, list, tuple, np.ndarray)  ):
																				UpperCAmelCase__							: Optional[int] 							=						"np"
															else:
																				raise ValueError(
																				    F"type of {first_element} unknown: {type(__UpperCamelCase  )}. "
																				    "Should be one of a python, numpy, pytorch or tensorflow object."  )

										for key, value in processed_features.items():
															if isinstance(value[0] ,			(int, float)  ):
																				UpperCAmelCase__							: List[Any] 							=						to_numpy(__UpperCamelCase  )
															else:
																				UpperCAmelCase__							: Tuple 							=						[to_numpy(__UpperCamelCase  ) for v in value]

        # Convert padding_strategy in PaddingStrategy
										UpperCAmelCase__							: str 							=						self._get_padding_strategies(padding=__UpperCamelCase ,			max_length=__UpperCamelCase  )

										UpperCAmelCase__							: Tuple 							=						processed_features[self.model_input_names[0]]

										UpperCAmelCase__							: Optional[int] 							=						len(__UpperCamelCase  )
										if not all(len(__UpperCamelCase  ) == batch_size for v in processed_features.values()  ):
															raise ValueError("Some items in the output dictionary have a different batch size than others."  )

										UpperCAmelCase__							: str 							=						[]
										for i in range(__UpperCamelCase  ):
															UpperCAmelCase__							: List[str] 							=						{k: v[i] for k, v in processed_features.items()}
															# truncation
															UpperCAmelCase__							: Tuple 							=						self._truncate(
															    __UpperCamelCase ,			max_length=__UpperCamelCase ,			pad_to_multiple_of=__UpperCamelCase ,			truncation=__UpperCamelCase ,			)
															truncated_inputs.append(__UpperCamelCase  )

										if padding_strategy == PaddingStrategy.LONGEST:
															# make sure that `max_length` cannot be longer than the longest truncated length
															UpperCAmelCase__							: List[Any] 							=						max(len(input_slice[self.model_input_names[0]]  ) for input_slice in truncated_inputs  )
															UpperCAmelCase__							: Tuple 							=						PaddingStrategy.MAX_LENGTH

										UpperCAmelCase__							: List[str] 							=						{}
										for i in range(__UpperCamelCase  ):
															# padding
															UpperCAmelCase__							: int 							=						self._pad(
															    truncated_inputs[i] ,			max_length=__UpperCamelCase ,			padding_strategy=__UpperCamelCase ,			pad_to_multiple_of=__UpperCamelCase ,			return_attention_mask=__UpperCamelCase ,			)

															for key, value in outputs.items():
																				if key not in batch_outputs:
																									UpperCAmelCase__							: List[str] 							=						[]
																				if value.dtype is np.dtype(np.floataa  ):
																									UpperCAmelCase__							: List[str] 							=						value.astype(np.floataa  )
																				batch_outputs[key].append(__UpperCamelCase  )

										return BatchFeature(__UpperCamelCase ,			tensor_type=__UpperCamelCase  )



					def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase = None ,			__UpperCamelCase = PaddingStrategy.DO_NOT_PAD ,			__UpperCamelCase = None ,			__UpperCamelCase = None ,			)->					dict:
										UpperCAmelCase__							: str 							=						processed_features[self.model_input_names[0]]

										if padding_strategy == PaddingStrategy.LONGEST:
															UpperCAmelCase__							: Dict 							=						len(__UpperCamelCase  )

										if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
															UpperCAmelCase__							: Tuple 							=						((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

										UpperCAmelCase__							: List[str] 							=						padding_strategy != PaddingStrategy.DO_NOT_PAD and len(__UpperCamelCase  ) < max_length

										if return_attention_mask and "attention_mask" not in processed_features:
															UpperCAmelCase__							: List[str] 							=						np.ones(len(__UpperCamelCase  ) ,			dtype=np.intaa  )

										if needs_to_be_padded:
															UpperCAmelCase__							: List[Any] 							=						max_length - len(__UpperCamelCase  )
															if self.padding_side == "right":
																				if return_attention_mask:
																									UpperCAmelCase__							: Optional[Any] 							=						np.pad(
																									    processed_features["attention_mask"] ,			(0, difference)  )
																				UpperCAmelCase__							: Any 							=						((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
																				UpperCAmelCase__							: List[Any] 							=						np.pad(
																				    __UpperCamelCase ,			__UpperCamelCase ,			"constant" ,			constant_values=self.padding_value  )
															elif self.padding_side == "left":
																				if return_attention_mask:
																									UpperCAmelCase__							: List[str] 							=						np.pad(
																									    processed_features["attention_mask"] ,			(difference, 0)  )
																				UpperCAmelCase__							: Dict 							=						((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
																				UpperCAmelCase__							: List[Any] 							=						np.pad(
																				    __UpperCamelCase ,			__UpperCamelCase ,			"constant" ,			constant_values=self.padding_value  )
															else:
																				raise ValueError("Invalid padding strategy:" + str(self.padding_side  )  )

										return processed_features



					def  lowerCAmelCase__       (      self ,			__UpperCamelCase ,			__UpperCamelCase = None ,			__UpperCamelCase = None ,			__UpperCamelCase = None ,			)->					Optional[Any]:
										if not truncation:
															return processed_features
										elif truncation and max_length is None:
															raise ValueError("When setting ``truncation=True``, make sure that ``max_length`` is defined."  )

										UpperCAmelCase__							: Optional[int] 							=						processed_features[self.model_input_names[0]]

										# find `max_length` that fits `pad_to_multiple_of`
										if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
															UpperCAmelCase__							: Tuple 							=						((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

										UpperCAmelCase__							: Optional[Any] 							=						len(__UpperCamelCase  ) > max_length

										if needs_to_be_truncated:
															UpperCAmelCase__							: Dict 							=						processed_features[self.model_input_names[0]][:max_length]
															if "attention_mask" in processed_features:
																				UpperCAmelCase__							: int 							=						processed_features["attention_mask"][:max_length]

										return processed_features



					def  lowerCAmelCase__       (      self ,			__UpperCamelCase=False ,			__UpperCamelCase=None  )->					Union[str, Any]:

										# Get padding strategy
										if padding is not False:
															if padding is True:
																				UpperCAmelCase__							: Dict 							=						PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
															elif not isinstance(__UpperCamelCase ,			__UpperCamelCase  ):
																				UpperCAmelCase__							: Union[str, Any] 							=						PaddingStrategy(__UpperCamelCase  )
															elif isinstance(__UpperCamelCase ,			__UpperCamelCase  ):
																				UpperCAmelCase__							: Tuple 							=						padding
										else:
															UpperCAmelCase__							: Union[str, Any] 							=						PaddingStrategy.DO_NOT_PAD

										# Set max length if needed
										if max_length is None:
															if padding_strategy == PaddingStrategy.MAX_LENGTH:
																				raise ValueError(
																				    F"When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined"  )

        # Test if we have a padding value
										if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
															raise ValueError(
															    "Asking to pad but the feature_extractor does not have a padding value. Please select a value to use"
															    " as `padding_value`. For example: `feature_extractor.padding_value = 0.0`."  )

										return padding_strategy





"""simple docstring"""




from manim import *







class   _lowercase						(       lowerCAmelCase_				):


     '''simple docstring'''





     def  lowerCAmelCase__       (      self  )->					Tuple:
          UpperCAmelCase__							: str 							=						Rectangle(height=0.5 ,			width=0.5  )
          UpperCAmelCase__							: List[str] 							=						Rectangle(height=0.46 ,			width=0.46  ).set_stroke(width=0  )

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

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

          UpperCAmelCase__							: Optional[int] 							=						[mem.copy() for i in range(6  )]
          UpperCAmelCase__							: List[str] 							=						VGroup(*__UpperCamelCase  ).arrange(__UpperCamelCase ,			buff=0  )

          UpperCAmelCase__							: Tuple 							=						Text("Model" ,			font_size=24  )
          UpperCAmelCase__							: Dict 							=						Group(__UpperCamelCase ,			__UpperCamelCase  ).arrange(__UpperCamelCase ,			buff=0.5 ,			aligned_edge=__UpperCamelCase  )
          model.move_to([3, -1.0, 0]  )
          self.add(__UpperCamelCase  )

          UpperCAmelCase__							: List[str] 							=						[]
          for i, rect in enumerate(__UpperCamelCase  ):
               rect.set_stroke(__UpperCamelCase  )
               # target = fill.copy().set_fill(YELLOW, opacity=0.7)
               # target.move_to(rect)
               # self.add(target)

               UpperCAmelCase__							: int 							=						Rectangle(height=0.46 / 4 ,			width=0.46 / 3  ).set_stroke(width=0.0  ).set_fill(__UpperCamelCase ,			opacity=0.7  )

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

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

          UpperCAmelCase__							: Tuple 							=						Text("Loaded Checkpoint" ,			font_size=24  )
          UpperCAmelCase__							: Any 							=						Group(__UpperCamelCase ,			__UpperCamelCase  ).arrange(__UpperCamelCase ,			aligned_edge=__UpperCamelCase ,			buff=0.4  )
          checkpoint.move_to([3, 0.5, 0]  )

          UpperCAmelCase__							: Optional[Any] 							=						Square(side_length=2.2  )
          key.move_to([-5, 2, 0]  )

          UpperCAmelCase__							: 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(__UpperCamelCase ,			__UpperCamelCase  )

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

          blue_text.next_to(__UpperCamelCase ,			DOWN * 2.4 ,			aligned_edge=key_text.get_left()  )

          UpperCAmelCase__							: Optional[Any] 							=						MarkupText(
              F"Next, a <i><span fgcolor=\"{BLUE}\">second</span></i> model is loaded into memory,\nwith the weights of a <span fgcolor=\"{BLUE}\">single shard</span>." ,			font_size=24 ,			)
          step_a.move_to([2, 2, 0]  )
          self.play(Write(__UpperCamelCase  ) ,			Write(__UpperCamelCase  )  )

          self.play(Write(__UpperCamelCase ,			run_time=1  ) ,			Create(__UpperCamelCase ,			run_time=1  )  )

          UpperCAmelCase__							: Union[str, Any] 							=						[]
          UpperCAmelCase__							: List[str] 							=						[]
          for i, rect in enumerate(__UpperCamelCase  ):
               UpperCAmelCase__							: Optional[Any] 							=						fill.copy().set_fill(__UpperCamelCase ,			opacity=0.7  )
               target.move_to(__UpperCamelCase  )
               first_animations.append(GrowFromCenter(__UpperCamelCase ,			run_time=1  )  )

               UpperCAmelCase__							: List[str] 							=						target.copy()
               cpu_target.generate_target()
               if i < 5:
                    cpu_target.target.move_to(cpu_left_col_base[i + 1]  )
               else:
                    cpu_target.target.move_to(cpu_right_col_base[i - 5]  )
               second_animations.append(MoveToTarget(__UpperCamelCase ,			run_time=1.5  )  )

          self.play(*__UpperCamelCase  )
          self.play(*__UpperCamelCase  )
          self.wait()



