




import inspect
import os
import unittest

import torch

import accelerate
from accelerate import Accelerator
from accelerate.test_utils import execute_subprocess_async, require_multi_gpu
from accelerate.utils import patch_environment


class 							__UpperCamelCase			( unittest.TestCase  ):





				'''simple docstring'''



				def       __snake_case		( self						):
											lowerCAmelCase   =		inspect.getfile(accelerate.test_utils						)
											lowerCAmelCase   =		os.path.sep.join(mod_file.split(os.path.sep						)[:-1] + ['''scripts''', '''test_script.py''']						)
											lowerCAmelCase   =		os.path.sep.join(
											    mod_file.split(os.path.sep						)[:-1] + ['''scripts''', '''test_distributed_data_loop.py''']						)
											lowerCAmelCase   =		os.path.sep.join(mod_file.split(os.path.sep						)[:-1] + ['''scripts''', '''test_ops.py''']						)



				@require_multi_gpu
				def       __snake_case		( self						):
											print(F"""Found {torch.cuda.device_count()} devices."""						)
											lowerCAmelCase   =		['''torchrun''', F"""--nproc_per_node={torch.cuda.device_count()}""", self.test_file_path]
											with patch_environment(omp_num_threads=1						):
																		execute_subprocess_async(UpperCAmelCase_       , env=os.environ.copy()						)



				@require_multi_gpu
				def       __snake_case		( self						):
											print(F"""Found {torch.cuda.device_count()} devices."""						)
											lowerCAmelCase   =		['''torchrun''', F"""--nproc_per_node={torch.cuda.device_count()}""", self.operation_file_path]
											print(F"""Command: {cmd}"""						)
											with patch_environment(omp_num_threads=1						):
																		execute_subprocess_async(UpperCAmelCase_       , env=os.environ.copy()						)



				@require_multi_gpu
				def       __snake_case		( self						):
											lowerCAmelCase   =		['''torchrun''', F"""--nproc_per_node={torch.cuda.device_count()}""", inspect.getfile(self.__class__						)]
											with patch_environment(omp_num_threads=1						):
																		execute_subprocess_async(UpperCAmelCase_       , env=os.environ.copy()						)






				@require_multi_gpu
				def       __snake_case		( self						):
											print(F"""Found {torch.cuda.device_count()} devices, using 2 devices only"""						)
											lowerCAmelCase   =		['''torchrun''', F"""--nproc_per_node={torch.cuda.device_count()}""", self.data_loop_file_path]
											with patch_environment(omp_num_threads=1       , cuda_visible_devices='''0,1'''						):
																		execute_subprocess_async(UpperCAmelCase_       , env=os.environ.copy()						)


if __name__ == "__main__":
		UpperCAmelCase_						=Accelerator()
		UpperCAmelCase_						=(accelerator.state.process_index + 2, 10)
		UpperCAmelCase_						=torch.randint(0, 10, shape).to(accelerator.device)

		UpperCAmelCase_						=""""""

		UpperCAmelCase_						=accelerator.pad_across_processes(tensor)
		if tensora.shape[0] != accelerator.state.num_processes + 1:
				error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0."
		if not torch.equal(tensora[: accelerator.state.process_index + 2], tensor):
				error_msg += "Tensors have different values."
		if not torch.all(tensora[accelerator.state.process_index + 2 :] == 0):
				error_msg += "Padding was not done with the right value (0)."

		UpperCAmelCase_						=accelerator.pad_across_processes(tensor, pad_first=True)
		if tensora.shape[0] != accelerator.state.num_processes + 1:
				error_msg += F"Found shape {tensora.shape} but should have {accelerator.state.num_processes + 1} at dim 0."
		UpperCAmelCase_						=accelerator.state.num_processes - accelerator.state.process_index - 1
		if not torch.equal(tensora[index:], tensor):
				error_msg += "Tensors have different values."
		if not torch.all(tensora[:index] == 0):
				error_msg += "Padding was not done with the right value (0)."

		# Raise error at the end to make sure we don't stop at the first failure.
		if len(error_msg) > 0:
				raise ValueError(error_msg)









import unittest

from huggingface_hub import hf_hub_download

from transformers import MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING, VideoMAEFeatureExtractor
from transformers.pipelines import VideoClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_decord,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
)

from .test_pipelines_common import ANY


@is_pipeline_test
@require_torch_or_tf
@require_vision
@require_decord
class 							__UpperCamelCase			( unittest.TestCase  ):





				'''simple docstring'''



				__a							: str										=MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING



				def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_       , UpperCAmelCase_						):
											lowerCAmelCase   =		hf_hub_download(
											    repo_id='''nateraw/video-demo'''       , filename='''archery.mp4'''       , repo_type='''dataset'''						)
											lowerCAmelCase   =		VideoClassificationPipeline(model=UpperCAmelCase_       , image_processor=UpperCAmelCase_       , top_k=2						)
											lowerCAmelCase   =		[
											    example_video_filepath,
											    '''https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4''',
											]
											return video_classifier, examples



				def       __snake_case		( self       , UpperCAmelCase_       , UpperCAmelCase_						):
											for example in examples:
																		lowerCAmelCase   =		video_classifier(UpperCAmelCase_						)

																		self.assertEqual(
																		    UpperCAmelCase_       , [
																		        {'''score''': ANY(UpperCAmelCase_						), '''label''': ANY(UpperCAmelCase_						)},
																		        {'''score''': ANY(UpperCAmelCase_						), '''label''': ANY(UpperCAmelCase_						)},
																		    ]       , )



				@require_torch
				def       __snake_case		( self						):
											lowerCAmelCase   =		'''hf-internal-testing/tiny-random-VideoMAEForVideoClassification'''
											lowerCAmelCase   =		VideoMAEFeatureExtractor(
											    size={'''shortest_edge''': 10}       , crop_size={'''height''': 10, '''width''': 10}						)
											lowerCAmelCase   =		pipeline(
											    '''video-classification'''       , model=UpperCAmelCase_       , feature_extractor=UpperCAmelCase_       , frame_sampling_rate=4						)

											lowerCAmelCase   =		hf_hub_download(repo_id='''nateraw/video-demo'''       , filename='''archery.mp4'''       , repo_type='''dataset'''						)
											lowerCAmelCase   =		video_classifier(UpperCAmelCase_       , top_k=2						)
											self.assertEqual(
											    nested_simplify(UpperCAmelCase_       , decimals=4						)       , [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}]       , )

											lowerCAmelCase   =		video_classifier(
											    [
											        video_file_path,
											        video_file_path,
											    ]       , top_k=2       , )
											self.assertEqual(
											    nested_simplify(UpperCAmelCase_       , decimals=4						)       , [
											        [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}],
											        [{'''score''': 0.5199, '''label''': '''LABEL_0'''}, {'''score''': 0.4801, '''label''': '''LABEL_1'''}],
											    ]       , )






				@require_tf
				def       __snake_case		( self						):
											pass



