


import unittest

import numpy as np

from transformers.testing_utils import is_flaky, require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
						import torch

if is_vision_available():
						from PIL import Image

						from transformers import DonutImageProcessor







class 				snake_case__(							unittest.TestCase							):







						'''simple docstring'''




						def __init__(				self   ,						__lowercase   ,						__lowercase=7   ,						__lowercase=3   ,						__lowercase=1_8   ,						__lowercase=3_0   ,						__lowercase=4_0_0   ,						__lowercase=True   ,						__lowercase=None   ,						__lowercase=True   ,						__lowercase=False   ,						__lowercase=True   ,						__lowercase=True   ,						__lowercase=[0.5, 0.5, 0.5]   ,						__lowercase=[0.5, 0.5, 0.5]   ,						)					->    List[str]:
										lowerCAmelCase_ :      Any    				=				parent
										lowerCAmelCase_ :      List[str]    				=				batch_size
										lowerCAmelCase_ :      Tuple    				=				num_channels
										lowerCAmelCase_ :      int    				=				image_size
										lowerCAmelCase_ :      Optional[Any]    				=				min_resolution
										lowerCAmelCase_ :      List[str]    				=				max_resolution
										lowerCAmelCase_ :      Any    				=				do_resize
										lowerCAmelCase_ :      str    				=				size if size is not None else {'''height''': 1_8, '''width''': 2_0}
										lowerCAmelCase_ :      List[str]    				=				do_thumbnail
										lowerCAmelCase_ :      str    				=				do_align_axis
										lowerCAmelCase_ :      Any    				=				do_pad
										lowerCAmelCase_ :      Any    				=				do_normalize
										lowerCAmelCase_ :      Optional[Any]    				=				image_mean
										lowerCAmelCase_ :      str    				=				image_std




						def 						lowercase_ (				self							)					->    int:
										return {
										    "do_resize": self.do_resize,
										    "size": self.size,
										    "do_thumbnail": self.do_thumbnail,
										    "do_align_long_axis": self.do_align_axis,
										    "do_pad": self.do_pad,
										    "do_normalize": self.do_normalize,
										    "image_mean": self.image_mean,
										    "image_std": self.image_std,
										}







@require_torch
@require_vision
class 				snake_case__(							UpperCAmelCase__,						unittest.TestCase							):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__      :  Dict					=							DonutImageProcessor if is_vision_available() else None




						def 						lowercase_ (				self							)					->    int:
										lowerCAmelCase_ :      Union[str, Any]    				=				DonutImageProcessingTester(self							)




						@property
						def 						lowercase_ (				self							)					->    Any:
										return self.image_processor_tester.prepare_image_processor_dict()




						def 						lowercase_ (				self							)					->    Tuple:
										lowerCAmelCase_ :      Tuple    				=				self.image_processing_class(**self.image_processor_dict							)
										self.assertTrue(hasattr(__lowercase   ,						'''do_resize'''							)							)
										self.assertTrue(hasattr(__lowercase   ,						'''size'''							)							)
										self.assertTrue(hasattr(__lowercase   ,						'''do_thumbnail'''							)							)
										self.assertTrue(hasattr(__lowercase   ,						'''do_align_long_axis'''							)							)
										self.assertTrue(hasattr(__lowercase   ,						'''do_pad'''							)							)
										self.assertTrue(hasattr(__lowercase   ,						'''do_normalize'''							)							)
										self.assertTrue(hasattr(__lowercase   ,						'''image_mean'''							)							)
										self.assertTrue(hasattr(__lowercase   ,						'''image_std'''							)							)




						def 						lowercase_ (				self							)					->    List[Any]:
										lowerCAmelCase_ :      Dict    				=				self.image_processing_class.from_dict(self.image_processor_dict							)
										self.assertEqual(image_processor.size   ,						{'''height''': 1_8, '''width''': 2_0}							)

										lowerCAmelCase_ :      Optional[int]    				=				self.image_processing_class.from_dict(self.image_processor_dict   ,						size=4_2							)
										self.assertEqual(image_processor.size   ,						{'''height''': 4_2, '''width''': 4_2}							)

										# Previous config had dimensions in (width, height) order
										lowerCAmelCase_ :      int    				=				self.image_processing_class.from_dict(self.image_processor_dict   ,						size=(4_2, 8_4)							)
										self.assertEqual(image_processor.size   ,						{'''height''': 8_4, '''width''': 4_2}							)




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




						@is_flaky()
						def 						lowercase_ (				self							)					->    Optional[Any]:
										# Initialize image_processing
										lowerCAmelCase_ :      int    				=				self.image_processing_class(**self.image_processor_dict							)
										# create random PIL images
										lowerCAmelCase_ :      int    				=				prepare_image_inputs(self.image_processor_tester   ,						equal_resolution=__lowercase							)
										for image in image_inputs:
														self.assertIsInstance(__lowercase   ,						Image.Image							)

										# Test not batched input
										lowerCAmelCase_ :      List[str]    				=				image_processing(image_inputs[0]   ,						return_tensors='''pt'''							).pixel_values
										self.assertEqual(
										    encoded_images.shape   ,						(
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['''height'''],
										        self.image_processor_tester.size['''width'''],
										    )   ,						)

										# Test batched
										lowerCAmelCase_ :      Optional[int]    				=				image_processing(__lowercase   ,						return_tensors='''pt'''							).pixel_values
										self.assertEqual(
										    encoded_images.shape   ,						(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['''height'''],
										        self.image_processor_tester.size['''width'''],
										    )   ,						)




						@is_flaky()
						def 						lowercase_ (				self							)					->    List[Any]:
										# Initialize image_processing
										lowerCAmelCase_ :      Optional[int]    				=				self.image_processing_class(**self.image_processor_dict							)
										# create random numpy tensors
										lowerCAmelCase_ :      Optional[Any]    				=				prepare_image_inputs(self.image_processor_tester   ,						equal_resolution=__lowercase   ,						numpify=__lowercase							)
										for image in image_inputs:
														self.assertIsInstance(__lowercase   ,						np.ndarray							)

										# Test not batched input
										lowerCAmelCase_ :      Optional[int]    				=				image_processing(image_inputs[0]   ,						return_tensors='''pt'''							).pixel_values
										self.assertEqual(
										    encoded_images.shape   ,						(
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['''height'''],
										        self.image_processor_tester.size['''width'''],
										    )   ,						)

										# Test batched
										lowerCAmelCase_ :      List[str]    				=				image_processing(__lowercase   ,						return_tensors='''pt'''							).pixel_values
										self.assertEqual(
										    encoded_images.shape   ,						(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['''height'''],
										        self.image_processor_tester.size['''width'''],
										    )   ,						)




						@is_flaky()
						def 						lowercase_ (				self							)					->    Any:
										# Initialize image_processing
										lowerCAmelCase_ :      Optional[int]    				=				self.image_processing_class(**self.image_processor_dict							)
										# create random PyTorch tensors
										lowerCAmelCase_ :      Optional[int]    				=				prepare_image_inputs(self.image_processor_tester   ,						equal_resolution=__lowercase   ,						torchify=__lowercase							)
										for image in image_inputs:
														self.assertIsInstance(__lowercase   ,						torch.Tensor							)

										# Test not batched input
										lowerCAmelCase_ :      List[Any]    				=				image_processing(image_inputs[0]   ,						return_tensors='''pt'''							).pixel_values
										self.assertEqual(
										    encoded_images.shape   ,						(
										        1,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['''height'''],
										        self.image_processor_tester.size['''width'''],
										    )   ,						)

										# Test batched
										lowerCAmelCase_ :      Tuple    				=				image_processing(__lowercase   ,						return_tensors='''pt'''							).pixel_values
										self.assertEqual(
										    encoded_images.shape   ,						(
										        self.image_processor_tester.batch_size,
										        self.image_processor_tester.num_channels,
										        self.image_processor_tester.size['''height'''],
										        self.image_processor_tester.size['''width'''],
										    )   ,						)



from manim import *







class 				snake_case__(							UpperCAmelCase__							):







						'''simple docstring'''




						def 						lowercase_ (				self							)					->    Tuple:
										lowerCAmelCase_ :      Dict    				=				Rectangle(height=0.5   ,						width=0.5							)
										lowerCAmelCase_ :      Tuple    				=				Rectangle(height=0.25   ,						width=0.25							)
										lowerCAmelCase_ :      Tuple    				=				Rectangle(height=0.46   ,						width=0.46							).set_stroke(width=0							)

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

										lowerCAmelCase_ :      List[str]    				=				[mem.copy() for i in range(4							)]
										lowerCAmelCase_ :      Any    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)
										lowerCAmelCase_ :      List[Any]    				=				Text('''GPU'''   ,						font_size=2_4							)
										lowerCAmelCase_ :      int    				=				Group(__lowercase   ,						__lowercase							).arrange(__lowercase   ,						buff=0.5   ,						aligned_edge=__lowercase							)
										gpu.move_to([-1, -1, 0]							)
										self.add(__lowercase							)

										lowerCAmelCase_ :      str    				=				[mem.copy() for i in range(6							)]
										lowerCAmelCase_ :      Dict    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)

										lowerCAmelCase_ :      Dict    				=				Text('''Model'''   ,						font_size=2_4							)
										lowerCAmelCase_ :      str    				=				Group(__lowercase   ,						__lowercase							).arrange(__lowercase   ,						buff=0.5   ,						aligned_edge=__lowercase							)
										model.move_to([3, -1.0, 0]							)
										self.add(__lowercase							)

										lowerCAmelCase_ :      int    				=				[]
										lowerCAmelCase_ :      int    				=				[]
										lowerCAmelCase_ :      Dict    				=				[]

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

														lowerCAmelCase_ :      Any    				=				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							)

										lowerCAmelCase_ :      Union[str, Any]    				=				[mem.copy() for i in range(6							)]
										lowerCAmelCase_ :      List[str]    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)

										lowerCAmelCase_ :      Union[str, Any]    				=				Text('''Loaded Checkpoint'''   ,						font_size=2_4							)
										lowerCAmelCase_ :      int    				=				Group(__lowercase   ,						__lowercase							).arrange(__lowercase   ,						buff=0.5   ,						aligned_edge=__lowercase							)
										checkpoint.move_to([3, 0.5, 0]							)

										self.add(__lowercase							)

										lowerCAmelCase_ :      Optional[Any]    				=				[]
										lowerCAmelCase_ :      Dict    				=				[]

										for i, rect in enumerate(__lowercase							):
														lowerCAmelCase_ :      str    				=				fill.copy().set_fill(__lowercase   ,						opacity=0.7							)
														target.move_to(__lowercase							)
														ckpt_arr.append(__lowercase							)

														lowerCAmelCase_ :      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							)

										lowerCAmelCase_ :      Union[str, Any]    				=				Square(side_length=2.2							)
										key.move_to([-5, 2, 0]							)

										lowerCAmelCase_ :      str    				=				MarkupText(
										    f"""<b>Key:</b>\n\n<span fgcolor='{YELLOW}'>●</span> Empty Model"""   ,						font_size=1_8   ,						)

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

										self.add(__lowercase   ,						__lowercase							)

										lowerCAmelCase_ :      str    				=				MarkupText(
										    f"""<span fgcolor='{BLUE}'>●</span> Checkpoint"""   ,						font_size=1_8   ,						)

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

										lowerCAmelCase_ :      str    				=				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=2_4   ,						)
										step_a.move_to([2, 2, 0]							)

										lowerCAmelCase_ :      List[Any]    				=				[meta_mem.copy() for i in range(6							)]
										lowerCAmelCase_ :      Any    				=				[meta_mem.copy() for i in range(6							)]
										lowerCAmelCase_ :      Any    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)
										lowerCAmelCase_ :      Union[str, Any]    				=				VGroup(*__lowercase							).arrange(__lowercase   ,						buff=0							)
										lowerCAmelCase_ :      int    				=				VGroup(__lowercase   ,						__lowercase							).arrange(__lowercase   ,						buff=0							)
										lowerCAmelCase_ :      List[str]    				=				Text('''Disk'''   ,						font_size=2_4							)
										lowerCAmelCase_ :      Optional[int]    				=				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							)							)

										lowerCAmelCase_ :      int    				=				[]
										for i, rect in enumerate(__lowercase							):
														lowerCAmelCase_ :      int    				=				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							)							)

										lowerCAmelCase_ :      Union[str, Any]    				=				MarkupText(f"""Then, the checkpoint is removed from memory\nthrough garbage collection."""   ,						font_size=2_4							)
										step_a.move_to([2, 2, 0]							)

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

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

										self.wait()