






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, prepare_image_inputs


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import EfficientNetImageProcessor




class 			UpperCamelCase_				(      unittest.TestCase    ):



	'''simple docstring'''




	def __init__(	self	,      a	,      a=13	,      a=3	,      a=18	,      a=30	,      a=4_00	,      a=True	,      a=None	,      a=True	,      a=[0.5, 0.5, 0.5]	,      a=[0.5, 0.5, 0.5]	,      )	->   Optional[Any]:
								snake_case_           =							size if size is not None else {'height': 18, 'width': 18}
								snake_case_           =							parent
								snake_case_           =							batch_size
								snake_case_           =							num_channels
								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






	def 							_UpperCamelCase      (	self )	->   List[str]:
								return {
								    "image_mean": self.image_mean,
								    "image_std": self.image_std,
								    "do_normalize": self.do_normalize,
								    "do_resize": self.do_resize,
								    "size": self.size,
								}






@require_torch
@require_vision
class 			UpperCamelCase_				(      SCREAMING_SNAKE_CASE_	, unittest.TestCase    ):



	'''simple docstring'''




	lowerCAmelCase													=			EfficientNetImageProcessor if is_vision_available() else None




	def 							_UpperCamelCase      (	self )	->   List[Any]:
								snake_case_           =							EfficientNetImageProcessorTester(self )




	@property
	def 							_UpperCamelCase      (	self )	->   str:
								return self.image_processor_tester.prepare_image_processor_dict()




	def 							_UpperCamelCase      (	self )	->   Optional[Any]:
								snake_case_           =							self.image_processing_class(**self.image_processor_dict )
								self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_	,      'image_mean' ) )
								self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_	,      'image_std' ) )
								self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_	,      'do_normalize' ) )
								self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_	,      'do_resize' ) )
								self.assertTrue(hasattr(SCREAMING_SNAKE_CASE_	,      'size' ) )




	def 							_UpperCamelCase      (	self )	->   List[str]:
								snake_case_           =							self.image_processing_class.from_dict(self.image_processor_dict )
								self.assertEqual(image_processor.size	,      {'height': 18, 'width': 18} )

								snake_case_           =							self.image_processing_class.from_dict(self.image_processor_dict	,      size=42 )
								self.assertEqual(image_processor.size	,      {'height': 42, 'width': 42} )




	def 							_UpperCamelCase      (	self )	->   Union[str, Any]:
								snake_case_           =							self.image_processing_class(**self.image_processor_dict )
								# create random PIL images
								snake_case_           =							prepare_image_inputs(self.image_processor_tester	,      equal_resolution=SCREAMING_SNAKE_CASE_ )
								for image in image_inputs:
															self.assertIsInstance(SCREAMING_SNAKE_CASE_	,      Image.Image )

								# Test not batched input
								snake_case_           =							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
								snake_case_           =							image_processing(SCREAMING_SNAKE_CASE_	,      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'],
								    )	,      )




	def 							_UpperCamelCase      (	self )	->   List[Any]:
								snake_case_           =							self.image_processing_class(**self.image_processor_dict )
								# create random numpy tensors
								snake_case_           =							prepare_image_inputs(self.image_processor_tester	,      equal_resolution=SCREAMING_SNAKE_CASE_	,      numpify=SCREAMING_SNAKE_CASE_ )
								for image in image_inputs:
															self.assertIsInstance(SCREAMING_SNAKE_CASE_	,      np.ndarray )

								# Test not batched input
								snake_case_           =							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
								snake_case_           =							image_processing(SCREAMING_SNAKE_CASE_	,      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'],
								    )	,      )






	def 							_UpperCamelCase      (	self )	->   Optional[Any]:
								snake_case_           =							self.image_processing_class(**self.image_processor_dict )
								# create random PyTorch tensors
								snake_case_           =							prepare_image_inputs(self.image_processor_tester	,      equal_resolution=SCREAMING_SNAKE_CASE_	,      torchify=SCREAMING_SNAKE_CASE_ )
								for image in image_inputs:
															self.assertIsInstance(SCREAMING_SNAKE_CASE_	,      torch.Tensor )

								# Test not batched input
								snake_case_           =							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
								snake_case_           =							image_processing(SCREAMING_SNAKE_CASE_	,      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'],
								    )	,      )













import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
	import transformer_engine.pytorch as te





def 						__UpperCAmelCase      (					a_							,					a_=True							,					a_=True							,					a_=True):
							if not is_fpa_available():
														raise ImportError('Using `convert_model` requires transformer_engine to be installed.')
							for name, module in model.named_children():
														if isinstance(a_							,					nn.Linear) and to_transformer_engine and _convert_linear:
																					# Return early if the linear layer weights are not multiples of 16
																					if any(p % 16 != 0 for p in module.weight.shape):
																												return
																					snake_case_           =							module.bias is not None
																					snake_case_           =							te.Linear(
																					    module.in_features							,					module.out_features							,					bias=a_							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					if has_bias:
																												snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														elif isinstance(a_							,					nn.LayerNorm) and to_transformer_engine and _convert_ln:
																					snake_case_           =							te.LayerNorm(module.normalized_shape[0]							,					eps=module.eps							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														elif isinstance(a_							,					te.Linear) and not to_transformer_engine and _convert_linear:
																					snake_case_           =							module.bias is not None
																					snake_case_           =							nn.Linear(
																					    module.in_features							,					module.out_features							,					bias=a_							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					if has_bias:
																												snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														elif isinstance(a_							,					te.LayerNorm) and not to_transformer_engine and _convert_ln:
																					snake_case_           =							nn.LayerNorm(module.normalized_shape[0]							,					eps=module.eps							,					params_dtype=module.weight.dtype)
																					snake_case_           =							module.weight.data.clone()
																					snake_case_           =							module.bias.data.clone()

																					setattr(a_							,					a_							,					a_)
														else:
																					convert_model(
																					    a_							,					to_transformer_engine=a_							,					_convert_linear=_convert_linear							,					_convert_ln=_convert_ln							,					)





def 						__UpperCAmelCase      (					a_):
							if not is_fpa_available():
														raise ImportError('Using `has_transformer_engine_layers` requires transformer_engine to be installed.')
							for m in model.modules():
														if isinstance(a_							,					(te.LayerNorm, te.Linear)):
																					return True
							return False





