




import unittest

import numpy as np

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

from ...test_image_processing_common import ImageProcessingSavingTestMixin


if is_torch_available():
		import torch

if is_vision_available():
		from PIL import Image

		from transformers import TvltImageProcessor


def 						lowerCamelCase__   (		_A					,     _A=10					,     _A=10					,     _A=False					,     _A=False							):







	'''simple docstring'''





	snake_case_		   =					[]
	for i in range(image_processor_tester.num_frames							):
		video.append(np.random.randint(255					,     size=(image_processor_tester.num_channels, width, height)					,     dtype=np.uinta							)							)

	if not numpify and not torchify:
		# PIL expects the channel dimension as last dimension
		snake_case_		   =					[Image.fromarray(np.moveaxis(_A					,     0					,     -1							)							) for frame in video]

	if torchify:
		snake_case_		   =					[torch.from_numpy(_A							) for frame in video]

	return video


def 						lowerCamelCase__   (		_A					,     _A=False					,     _A=False					,     _A=False							):







	'''simple docstring'''





	assert not (numpify and torchify), "You cannot specify both numpy and PyTorch tensors at the same time"

	snake_case_		   =					[]
	for i in range(image_processor_tester.batch_size							):
		if equal_resolution:
			snake_case_		   =					snake_case_		   =					image_processor_tester.max_resolution
		else:
			snake_case_   ,				snake_case_		   =					np.random.choice(
			    np.arange(image_processor_tester.min_resolution					,     image_processor_tester.max_resolution							)					,     2							)
			snake_case_		   =					prepare_video(
			    image_processor_tester=_A					,     width=_A					,     height=_A					,     numpify=_A					,     torchify=_A					,     )
		video_inputs.append(_A							)

	return video_inputs
class 		UpperCAmelCase       (    unittest.TestCase      ):







						'''simple docstring'''





						def __init__(						self							:					Any	,     __lowercase							:					Dict	,     __lowercase							:					Optional[Any]=7	,     __lowercase							:					Tuple=3	,     __lowercase							:					Any=4	,     __lowercase							:					str=18	,     __lowercase							:					int=30	,     __lowercase							:					Dict=4_00	,     __lowercase							:					int=True	,     __lowercase							:					Dict=None	,     __lowercase							:					Optional[int]=True	,     __lowercase							:					List[str]=[0.5, 0.5, 0.5]	,     __lowercase							:					Optional[int]=[0.5, 0.5, 0.5]	,     __lowercase							:					Union[str, Any]=True	,     __lowercase							:					Tuple=None	,     ):






							"""simple docstring"""






							snake_case_		   =					size if size is not None else {"shortest_edge": 18}
							snake_case_		   =					crop_size if crop_size is not None else {"height": 18, "width": 18}

							snake_case_		   =					parent
							snake_case_		   =					batch_size
							snake_case_		   =					num_channels
							snake_case_		   =					num_frames
							snake_case_		   =					image_size
							snake_case_		   =					min_resolution
							snake_case_		   =					max_resolution
							snake_case_		   =					do_resize
							snake_case_		   =					size
							snake_case_		   =					do_normalize
							snake_case_		   =					image_mean
							snake_case_		   =					image_std
							snake_case_		   =					do_center_crop
							snake_case_		   =					crop_size






						def 	snake_case__						(						self							:					List[str]						):






							"""simple docstring"""






							return {
							    "image_mean": self.image_mean,
							    "image_std": self.image_std,
							    "do_normalize": self.do_normalize,
							    "do_resize": self.do_resize,
							    "size": self.size,
							    "do_center_crop": self.do_center_crop,
							    "crop_size": self.crop_size,
							}







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







						'''simple docstring'''





						lowerCAmelCase_							=       TvltImageProcessor if is_vision_available() else None






						def 	snake_case__						(						self							:					List[str]						):






							"""simple docstring"""






							snake_case_		   =					TvltImageProcessorTester(self						)






						@property
						def 	snake_case__						(						self							:					Any						):






							"""simple docstring"""






							return self.image_processor_tester.prepare_image_processor_dict()






						def 	snake_case__						(						self							:					str						):






							"""simple docstring"""






							snake_case_		   =					self.image_processing_class(**self.image_processor_dict						)
							self.assertTrue(hasattr(__lowercase	,     "image_mean"						)						)
							self.assertTrue(hasattr(__lowercase	,     "image_std"						)						)
							self.assertTrue(hasattr(__lowercase	,     "do_normalize"						)						)
							self.assertTrue(hasattr(__lowercase	,     "do_resize"						)						)
							self.assertTrue(hasattr(__lowercase	,     "do_center_crop"						)						)
							self.assertTrue(hasattr(__lowercase	,     "size"						)						)






						def 	snake_case__						(						self							:					Any						):






							"""simple docstring"""






							snake_case_		   =					self.image_processing_class(**self.image_processor_dict						)
							# create random PIL videos
							snake_case_		   =					prepare_video_inputs(self.image_processor_tester	,     equal_resolution=__lowercase						)
							for video in video_inputs:
								self.assertIsInstance(__lowercase	,     __lowercase						)
								self.assertIsInstance(video[0]	,     Image.Image						)

							# Test not batched input
							snake_case_		   =					image_processor(video_inputs[0]	,     return_tensors="pt"						).pixel_values
							self.assertEqual(
							    encoded_videos.shape	,     (
							        1,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )	,     )

							# Test batched
							snake_case_		   =					image_processor(__lowercase	,     return_tensors="pt"						).pixel_values
							self.assertEqual(
							    encoded_videos.shape	,     (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )	,     )






						def 	snake_case__						(						self							:					List[str]						):






							"""simple docstring"""






							snake_case_		   =					self.image_processing_class(**self.image_processor_dict						)
							# create random numpy tensors
							snake_case_		   =					prepare_video_inputs(self.image_processor_tester	,     equal_resolution=__lowercase	,     numpify=__lowercase						)
							for video in video_inputs:
								self.assertIsInstance(__lowercase	,     __lowercase						)
								self.assertIsInstance(video[0]	,     np.ndarray						)

							# Test not batched input
							snake_case_		   =					image_processor(video_inputs[0]	,     return_tensors="pt"						).pixel_values
							self.assertEqual(
							    encoded_videos.shape	,     (
							        1,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )	,     )

							# Test batched
							snake_case_		   =					image_processor(__lowercase	,     return_tensors="pt"						).pixel_values
							self.assertEqual(
							    encoded_videos.shape	,     (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )	,     )






						def 	snake_case__						(						self							:					int						):






							"""simple docstring"""






							snake_case_		   =					self.image_processing_class(**self.image_processor_dict						)
							# create random PyTorch tensors
							snake_case_		   =					prepare_video_inputs(self.image_processor_tester	,     equal_resolution=__lowercase	,     torchify=__lowercase						)
							for video in video_inputs:
								self.assertIsInstance(__lowercase	,     __lowercase						)
								self.assertIsInstance(video[0]	,     torch.Tensor						)

							# Test not batched input
							snake_case_		   =					image_processor(video_inputs[0]	,     return_tensors="pt"						).pixel_values
							self.assertEqual(
							    encoded_videos.shape	,     (
							        1,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )	,     )

							# Test batched
							snake_case_		   =					image_processor(__lowercase	,     return_tensors="pt"						).pixel_values
							self.assertEqual(
							    encoded_videos.shape	,     (
							        self.image_processor_tester.batch_size,
							        self.image_processor_tester.num_frames,
							        self.image_processor_tester.num_channels,
							        self.image_processor_tester.crop_size["height"],
							        self.image_processor_tester.crop_size["width"],
							    )	,     )






import unittest
from functools import partial

import torch

from accelerate import Accelerator, debug_launcher
from accelerate.state import AcceleratorState, GradientState
from accelerate.test_utils import require_cpu, require_huggingface_suite
from accelerate.utils import GradientAccumulationPlugin


def 						lowerCamelCase__   (		_A=2					,     _A=True					,     _A=False							):







	'''simple docstring'''





	snake_case_		   =					Accelerator(step_scheduler_with_optimizer=_A					,     split_batches=_A							)
	snake_case_		   =					torch.nn.Linear(2					,     4							)
	snake_case_		   =					torch.optim.AdamW(model.parameters()					,     lr=1.0							)
	snake_case_		   =					torch.optim.lr_scheduler.OneCycleLR(_A					,     max_lr=0.01					,     steps_per_epoch=2					,     epochs=1							)
	snake_case_   ,				snake_case_   ,				snake_case_		   =					accelerator.prepare(_A					,     _A					,     _A							)

	# Optimizer has stepped
	scheduler.step()
	if step_scheduler_with_optimizer or (num_processes == 1):
		assert (
		    scheduler.scheduler.last_epoch == num_processes
		), f"Last Epoch ({scheduler.scheduler.last_epoch}) != Num Processes ({num_processes})"
	else:
		assert (
		    scheduler.scheduler.last_epoch != num_processes
		), f"Last Epoch ({scheduler.scheduler.last_epoch}) == Num Processes ({num_processes})"


def 						lowerCamelCase__   (		_A=2					,     _A=True					,     _A=False							):







	'''simple docstring'''





	snake_case_		   =					Accelerator(step_scheduler_with_optimizer=_A					,     split_batches=_A							)
	snake_case_		   =					torch.nn.Linear(2					,     4							)
	snake_case_		   =					torch.optim.AdamW(model.parameters()					,     lr=1.0							)
	snake_case_		   =					torch.optim.lr_scheduler.LambdaLR(_A					,     lr_lambda=lambda _A							: 1 - n / 10							)
	snake_case_   ,				snake_case_   ,				snake_case_		   =					accelerator.prepare(_A					,     _A					,     _A							)

	# Optimizer has stepped
	snake_case_		   =					False
	scheduler.step()
	snake_case_		   =					1 - (num_processes if (step_scheduler_with_optimizer and not split_batches) else 1) / 10
	assert (
	    scheduler.get_last_lr()[0] == expected_lr
	), f"Wrong lr found at first step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}"

	# Optimizer has not stepped
	snake_case_		   =					True
	scheduler.step()
	if not step_scheduler_with_optimizer:
		snake_case_		   =					1 - 2 / 10
	assert (
	    scheduler.get_last_lr()[0] == expected_lr
	), f"Wrong lr found at second step, expected {expected_lr}, got {scheduler.get_last_lr()[0]}"


def 						lowerCamelCase__   (		_A = 2							):







	'''simple docstring'''





	from transformers import get_linear_schedule_with_warmup

	snake_case_		   =					[1, 2, 4]
	for num_steps in steps:
		snake_case_		   =					GradientAccumulationPlugin(num_steps=_A					,     adjust_scheduler=num_steps > 1							)
		snake_case_		   =					Accelerator(gradient_accumulation_plugin=_A							)
		snake_case_		   =					torch.nn.Linear(2					,     4							)
		snake_case_		   =					torch.optim.AdamW(model.parameters()					,     lr=10.0							)
		snake_case_		   =					get_linear_schedule_with_warmup(optimizer=_A					,     num_warmup_steps=0					,     num_training_steps=20							)

		snake_case_   ,				snake_case_   ,				snake_case_		   =					accelerator.prepare(_A					,     _A					,     _A							)

		for i in range(10 * num_steps							):
			with accelerator.accumulate(_A							):
				optimizer.step()
				scheduler.step()

			if i == (10 * num_steps - 2):
				assert (
				    scheduler.get_last_lr()[0] != 0
				), f"Wrong lr found at second-to-last step, expected non-zero, got {scheduler.get_last_lr()[0]}. num_steps: {num_steps}"
		assert (
		    scheduler.get_last_lr()[0] == 0
		), f"Wrong lr found at last step, expected 0, got {scheduler.get_last_lr()[0]}"
		GradientState._reset_state()






@require_cpu
class 		UpperCAmelCase       (    unittest.TestCase      ):







						'''simple docstring'''





						def 	snake_case__						(						self							:					Tuple						):






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1						)	,     num_processes=1						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					int						):






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1						)	,     num_processes=1						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					Dict						):






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1	,     step_scheduler_with_optimizer=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					Union[str, Any]						):






							"""simple docstring"""






							debug_launcher(partial(__lowercase	,     num_processes=1	,     step_scheduler_with_optimizer=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					List[Any]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(__lowercase						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					Dict						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(__lowercase						)
							debug_launcher(partial(__lowercase	,     num_processes=1	,     split_batches=__lowercase						)	,     num_processes=1						)






						def 	snake_case__						(						self							:					List[str]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(partial(__lowercase	,     step_scheduler_with_optimizer=__lowercase						)						)






						def 	snake_case__						(						self							:					Optional[Any]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(partial(__lowercase	,     step_scheduler_with_optimizer=__lowercase						)						)






						@require_huggingface_suite
						def 	snake_case__						(						self							:					Optional[Any]						):






							"""simple docstring"""






							AcceleratorState._reset_state(__lowercase						)
							debug_launcher(partial(__lowercase	,     num_processes=1						)						)
							debug_launcher(__lowercase						)
