
import os
import socket
from contextlib import contextmanager

import torch

from ..commands.config.default import write_basic_config  # noqa: F401
from ..state import PartialState
from .dataclasses import DistributedType
from .imports import is_deepspeed_available, is_tpu_available
from .transformer_engine import convert_model
from .versions import is_torch_version


if is_deepspeed_available():
					from deepspeed import DeepSpeedEngine

if is_tpu_available(check_device=False):
					import torch_xla.core.xla_model as xm


def       A       (					lowercase )   ->  Dict:
		'''simple docstring'''

		if is_torch_version('<'    ,       '2.0.0' ) or not hasattr(lowercase    ,       '_dynamo' ):
				return False
		return isinstance(lowercase    ,       torch._dynamo.eval_frame.OptimizedModule )


def       A       (					lowercase    ,       lowercase = True )   ->  List[Any]:
		'''simple docstring'''

		UpperCamelCase		=     (torch.nn.parallel.DistributedDataParallel, torch.nn.DataParallel)

		UpperCamelCase		=     is_compiled_module(lowercase )
		if is_compiled:
				UpperCamelCase		=     model
				UpperCamelCase		=     model._orig_mod

		if is_deepspeed_available():
				options += (DeepSpeedEngine,)

		while isinstance(lowercase    ,       lowercase ):
				UpperCamelCase		=     model.module

		if not keep_fpaa_wrapper:
				UpperCamelCase		=     getattr(lowercase    ,       'forward' )
				UpperCamelCase		=     model.__dict__.pop('_original_forward'    ,       lowercase )
				if original_forward is not None:
						while hasattr(lowercase    ,       '__wrapped__' ):
								UpperCamelCase		=     forward.__wrapped__
								if forward == original_forward:
										break
						UpperCamelCase		=     forward
				if getattr(lowercase    ,       '_converted_to_transformer_engine'    ,       lowercase ):
						convert_model(lowercase    ,       to_transformer_engine=lowercase )

		if is_compiled:
				UpperCamelCase		=     model
				UpperCamelCase		=     compiled_model

		return model


def       A       (					)   ->  Optional[int]:
		'''simple docstring'''

		PartialState().wait_for_everyone()


def       A       (					lowercase    ,       lowercase )   ->  Dict:
		'''simple docstring'''

		if PartialState().distributed_type == DistributedType.TPU:
				xm.save(lowercase    ,       lowercase )
		elif PartialState().local_process_index == 0:
				torch.save(lowercase    ,       lowercase )


@contextmanager
def       A       (					**lowercase )   ->  Optional[Any]:
		'''simple docstring'''

		for key, value in kwargs.items():
				UpperCamelCase		=     str(lowercase )

		yield

		for key in kwargs:
				if key.upper() in os.environ:
						del os.environ[key.upper()]


def       A       (					lowercase )   ->  str:
		'''simple docstring'''

		if not hasattr(lowercase    ,       '__qualname__' ) and not hasattr(lowercase    ,       '__name__' ):
				UpperCamelCase		=     getattr(lowercase    ,       '__class__'    ,       lowercase )
		if hasattr(lowercase    ,       '__qualname__' ):
				return obj.__qualname__
		if hasattr(lowercase    ,       '__name__' ):
				return obj.__name__
		return str(lowercase )


def       A       (					lowercase    ,       lowercase )   ->  Union[str, Any]:
		'''simple docstring'''

		for key, value in source.items():
				if isinstance(lowercase    ,       lowercase ):
						UpperCamelCase		=     destination.setdefault(lowercase    ,       {} )
						merge_dicts(lowercase    ,       lowercase )
				else:
						UpperCamelCase		=     value

		return destination


def       A       (					lowercase = None )   ->  bool:
		'''simple docstring'''

		if port is None:
				UpperCamelCase		=     29_500
		with socket.socket(socket.AF_INET    ,       socket.SOCK_STREAM ) as s:
				return s.connect_ex(('localhost', port) ) == 0


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 				lowercase					(		unittest.TestCase   ):
		__lowercase       :       Any	     =				MODEL_FOR_VIDEO_CLASSIFICATION_MAPPING



		def __UpperCamelCase ( self				,      A_				,      A_				,      A_       )  ->					List[Any]:
				"""simple docstring"""
				UpperCamelCase		=     hf_hub_download(
				    repo_id='nateraw/video-demo'				,      filename='archery.mp4'				,      repo_type='dataset'       )
				UpperCamelCase		=     VideoClassificationPipeline(model=A_				,      image_processor=A_				,      top_k=2       )
				UpperCamelCase		=     [
				    example_video_filepath,
				    'https://huggingface.co/datasets/nateraw/video-demo/resolve/main/archery.mp4',
				]
				return video_classifier, examples



		def __UpperCamelCase ( self				,      A_				,      A_       )  ->					Optional[int]:
				"""simple docstring"""
				for example in examples:
						UpperCamelCase		=     video_classifier(A_       )

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



		@require_torch
		def __UpperCamelCase ( self       )  ->					Optional[Any]:
				"""simple docstring"""
				UpperCamelCase		=     'hf-internal-testing/tiny-random-VideoMAEForVideoClassification'
				UpperCamelCase		=     VideoMAEFeatureExtractor(
				    size={'shortest_edge': 10}				,      crop_size={'height': 10, 'width': 10}       )
				UpperCamelCase		=     pipeline(
				    'video-classification'				,      model=A_				,      feature_extractor=A_				,      frame_sampling_rate=4       )

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

				UpperCamelCase		=     video_classifier(
				    [
				        video_file_path,
				        video_file_path,
				    ]				,      top_k=2				,      )
				self.assertEqual(
				    nested_simplify(A_				,      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 __UpperCamelCase ( self       )  ->					Tuple:
				"""simple docstring"""
				pass
