






import collections
import inspect
import unittest

from transformers import DonutSwinConfig
from transformers.testing_utils import require_torch, slow, torch_device
from transformers.utils import is_torch_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
							import torch
							from torch import nn

							from transformers import DonutSwinModel
							from transformers.models.donut.modeling_donut_swin import DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST




class 							__A	:





				"""simple docstring"""







				def __init__(					self				,    lowerCamelCase__				,    lowerCamelCase__=13				,    lowerCamelCase__=32				,    lowerCamelCase__=2				,    lowerCamelCase__=3				,    lowerCamelCase__=16				,    lowerCamelCase__=[1, 2, 1]				,    lowerCamelCase__=[2, 2, 4]				,    lowerCamelCase__=2				,    lowerCamelCase__=2.0				,    lowerCamelCase__=True				,    lowerCamelCase__=0.0				,    lowerCamelCase__=0.0				,    lowerCamelCase__=0.1				,    lowerCamelCase__="gelu"				,    lowerCamelCase__=False				,    lowerCamelCase__=True				,    lowerCamelCase__=0.02				,    lowerCamelCase__=1E-5				,    lowerCamelCase__=True				,    lowerCamelCase__=None				,    lowerCamelCase__=True				,    lowerCamelCase__=10				,    lowerCamelCase__=8				,    ):




									"""simple docstring"""

									__UpperCamelCase :   Tuple									=parent
									__UpperCamelCase :   Tuple									=batch_size
									__UpperCamelCase :   Dict									=image_size
									__UpperCamelCase :   Union[str, Any]									=patch_size
									__UpperCamelCase :   Any									=num_channels
									__UpperCamelCase :   Optional[Any]									=embed_dim
									__UpperCamelCase :   List[Any]									=depths
									__UpperCamelCase :   Union[str, Any]									=num_heads
									__UpperCamelCase :   Any									=window_size
									__UpperCamelCase :   str									=mlp_ratio
									__UpperCamelCase :   str									=qkv_bias
									__UpperCamelCase :   Any									=hidden_dropout_prob
									__UpperCamelCase :   Dict									=attention_probs_dropout_prob
									__UpperCamelCase :   Dict									=drop_path_rate
									__UpperCamelCase :   int									=hidden_act
									__UpperCamelCase :   str									=use_absolute_embeddings
									__UpperCamelCase :   List[str]									=patch_norm
									__UpperCamelCase :   Dict									=layer_norm_eps
									__UpperCamelCase :   int									=initializer_range
									__UpperCamelCase :   Tuple									=is_training
									__UpperCamelCase :   Dict									=scope
									__UpperCamelCase :   str									=use_labels
									__UpperCamelCase :   List[str]									=type_sequence_label_size
									__UpperCamelCase :   Union[str, Any]									=encoder_stride


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   int									=floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]       )

									__UpperCamelCase :   List[str]									=None
									if self.use_labels:
														__UpperCamelCase :   Optional[int]									=ids_tensor([self.batch_size]				,    self.type_sequence_label_size       )

									__UpperCamelCase :   Dict									=self.get_config()

									return config, pixel_values, labels


				def  __lowercase				(					self       ):




									"""simple docstring"""

									return DonutSwinConfig(
									    image_size=self.image_size				,    patch_size=self.patch_size				,    num_channels=self.num_channels				,    embed_dim=self.embed_dim				,    depths=self.depths				,    num_heads=self.num_heads				,    window_size=self.window_size				,    mlp_ratio=self.mlp_ratio				,    qkv_bias=self.qkv_bias				,    hidden_dropout_prob=self.hidden_dropout_prob				,    attention_probs_dropout_prob=self.attention_probs_dropout_prob				,    drop_path_rate=self.drop_path_rate				,    hidden_act=self.hidden_act				,    use_absolute_embeddings=self.use_absolute_embeddings				,    path_norm=self.patch_norm				,    layer_norm_eps=self.layer_norm_eps				,    initializer_range=self.initializer_range				,    encoder_stride=self.encoder_stride				,    )


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   Optional[Any]									=DonutSwinModel(config=a__       )
									model.to(a__       )
									model.eval()
									__UpperCamelCase :   Optional[int]									=model(a__       )

									__UpperCamelCase :   Dict									=((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths       ) - 1))
									__UpperCamelCase :   Optional[int]									=int(config.embed_dim * 2 ** (len(config.depths       ) - 1)       )

									self.parent.assertEqual(result.last_hidden_state.shape				,    (self.batch_size, expected_seq_len, expected_dim)       )







				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Dict									=self.prepare_config_and_inputs()
									(
									    (
									    __UpperCamelCase		
									)						,							(
									    __UpperCamelCase		
									)						,							(
									    __UpperCamelCase		
									)						,									
									) :   int									=config_and_inputs
									__UpperCamelCase :   str									={'pixel_values': pixel_values}
									return config, inputs_dict




@require_torch
class 							__A	(     _UpperCamelCase ,	_UpperCamelCase ,	unittest.TestCase					):





				"""simple docstring"""







				UpperCamelCase__					: Optional[Any]     					=(DonutSwinModel,) if is_torch_available() else ()
				UpperCamelCase__					: List[Any]     					={"""feature-extraction""": DonutSwinModel} if is_torch_available() else {}
				UpperCamelCase__					: Tuple     					=True

				UpperCamelCase__					: List[str]     					=False
				UpperCamelCase__					: Dict     					=False
				UpperCamelCase__					: Optional[Any]     					=False


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Dict									=DonutSwinModelTester(self       )
									__UpperCamelCase :   Union[str, Any]									=ConfigTester(self				,    config_class=a__				,    embed_dim=37       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									self.create_and_test_config_common_properties()
									self.config_tester.create_and_test_config_to_json_string()
									self.config_tester.create_and_test_config_to_json_file()
									self.config_tester.create_and_test_config_from_and_save_pretrained()
									self.config_tester.create_and_test_config_with_num_labels()
									self.config_tester.check_config_can_be_init_without_params()
									self.config_tester.check_config_arguments_init()


				def  __lowercase				(					self       ):




									"""simple docstring"""

									return


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase :   Dict									=self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*a__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									pass


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase						,							__UpperCamelCase :   Union[str, Any]									=self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
														__UpperCamelCase :   List[str]									=model_class(a__       )
														self.assertIsInstance(model.get_input_embeddings()				,    (nn.Module)       )
														__UpperCamelCase :   Optional[Any]									=model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(a__				,    nn.Linear       )       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase						,							__UpperCamelCase :   str									=self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
														__UpperCamelCase :   List[str]									=model_class(a__       )
														__UpperCamelCase :   Any									=inspect.signature(model.forward       )
														# signature.parameters is an OrderedDict => so arg_names order is deterministic
														__UpperCamelCase :   Optional[int]									=[*signature.parameters.keys()]

														__UpperCamelCase :   Dict									=['pixel_values']
														self.assertListEqual(arg_names[:1]				,    a__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase						,							__UpperCamelCase :   Optional[int]									=self.model_tester.prepare_config_and_inputs_for_common()
									__UpperCamelCase :   Union[str, Any]									=True

									for model_class in self.all_model_classes:
														__UpperCamelCase :   Tuple									=True
														__UpperCamelCase :   int									=False
														__UpperCamelCase :   Optional[Any]									=True
														__UpperCamelCase :   Any									=model_class(a__       )
														model.to(a__       )
														model.eval()
														with torch.no_grad():
																			__UpperCamelCase :   Tuple									=model(**self._prepare_for_class(a__				,    a__       )       )
														__UpperCamelCase :   Tuple									=outputs.attentions
														__UpperCamelCase :   Optional[Any]									=len(self.model_tester.depths       )
														self.assertEqual(len(a__       )				,    a__       )

														# check that output_attentions also work using config
														del inputs_dict["output_attentions"]
														__UpperCamelCase :   Optional[int]									=True
														__UpperCamelCase :   str									=config.window_size**2
														__UpperCamelCase :   Union[str, Any]									=model_class(a__       )
														model.to(a__       )
														model.eval()
														with torch.no_grad():
																			__UpperCamelCase :   Optional[int]									=model(**self._prepare_for_class(a__				,    a__       )       )
														__UpperCamelCase :   Dict									=outputs.attentions
														self.assertEqual(len(a__       )				,    a__       )

														self.assertListEqual(
														    list(attentions[0].shape[-3:]       )				,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]				,    )
														__UpperCamelCase :   Optional[Any]									=len(a__       )

														# Check attention is always last and order is fine
														__UpperCamelCase :   Tuple									=True
														__UpperCamelCase :   Any									=True
														__UpperCamelCase :   Any									=model_class(a__       )
														model.to(a__       )
														model.eval()
														with torch.no_grad():
																			__UpperCamelCase :   Dict									=model(**self._prepare_for_class(a__				,    a__       )       )

														if hasattr(self.model_tester				,    'num_hidden_states_types'       ):
																			__UpperCamelCase :   Optional[Any]									=self.model_tester.num_hidden_states_types
														else:
																			# also another +1 for reshaped_hidden_states
																			__UpperCamelCase :   int									=2
														self.assertEqual(out_len + added_hidden_states				,    len(a__       )       )

														__UpperCamelCase :   int									=outputs.attentions

														self.assertEqual(len(a__       )				,    a__       )

														self.assertListEqual(
														    list(self_attentions[0].shape[-3:]       )				,    [self.model_tester.num_heads[0], window_size_squared, window_size_squared]				,    )


				def  __lowercase				(					self				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__				,    lowerCamelCase__       ):




									"""simple docstring"""

									__UpperCamelCase :   List[str]									=model_class(a__       )
									model.to(a__       )
									model.eval()

									with torch.no_grad():
														__UpperCamelCase :   Optional[int]									=model(**self._prepare_for_class(a__				,    a__       )       )

									__UpperCamelCase :   Optional[int]									=outputs.hidden_states

									__UpperCamelCase :   Dict									=getattr(
									    self.model_tester				,    'expected_num_hidden_layers'				,    len(self.model_tester.depths       ) + 1       )
									self.assertEqual(len(a__       )				,    a__       )

									# DonutSwin has a different seq_length
									__UpperCamelCase :   List[Any]									=(
									    config.patch_size
									    if isinstance(config.patch_size				,    collections.abc.Iterable       )
									    else (config.patch_size, config.patch_size)
									)

									__UpperCamelCase :   str									=(image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

									self.assertListEqual(
									    list(hidden_states[0].shape[-2:]       )				,    [num_patches, self.model_tester.embed_dim]				,    )

									__UpperCamelCase :   Optional[Any]									=outputs.reshaped_hidden_states
									self.assertEqual(len(a__       )				,    a__       )

									__UpperCamelCase						,							__UpperCamelCase						,							__UpperCamelCase						,							__UpperCamelCase :   Optional[Any]									=reshaped_hidden_states[0].shape
									__UpperCamelCase :   Union[str, Any]									=(
									    reshaped_hidden_states[0].view(a__				,    a__				,    height * width       ).permute(0				,    2				,    1       )
									)
									self.assertListEqual(
									    list(reshaped_hidden_states.shape[-2:]       )				,    [num_patches, self.model_tester.embed_dim]				,    )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase						,							__UpperCamelCase :   Any									=self.model_tester.prepare_config_and_inputs_for_common()

									__UpperCamelCase :   int									=(
									    self.model_tester.image_size
									    if isinstance(self.model_tester.image_size				,    collections.abc.Iterable       )
									    else (self.model_tester.image_size, self.model_tester.image_size)
									)

									for model_class in self.all_model_classes:
														__UpperCamelCase :   List[str]									=True
														self.check_hidden_states_output(a__				,    a__				,    a__				,    a__       )

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														__UpperCamelCase :   Optional[int]									=True

														self.check_hidden_states_output(a__				,    a__				,    a__				,    a__       )


				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase						,							__UpperCamelCase :   Dict									=self.model_tester.prepare_config_and_inputs_for_common()
									__UpperCamelCase :   int									=3

									__UpperCamelCase :   Optional[Any]									=(
									    self.model_tester.image_size
									    if isinstance(self.model_tester.image_size				,    collections.abc.Iterable       )
									    else (self.model_tester.image_size, self.model_tester.image_size)
									)
									__UpperCamelCase :   Dict									=(
									    config.patch_size
									    if isinstance(config.patch_size				,    collections.abc.Iterable       )
									    else (config.patch_size, config.patch_size)
									)

									__UpperCamelCase :   Optional[Any]									=image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
									__UpperCamelCase :   Any									=image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

									for model_class in self.all_model_classes:
														__UpperCamelCase :   Dict									=True
														self.check_hidden_states_output(a__				,    a__				,    a__				,    (padded_height, padded_width)       )

														# check that output_hidden_states also work using config
														del inputs_dict["output_hidden_states"]
														__UpperCamelCase :   List[str]									=True
														self.check_hidden_states_output(a__				,    a__				,    a__				,    (padded_height, padded_width)       )


				@slow
				def  __lowercase				(					self       ):




									"""simple docstring"""

									for model_name in DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														__UpperCamelCase :   Union[str, Any]									=DonutSwinModel.from_pretrained(a__       )
														self.assertIsNotNone(a__       )







				def  __lowercase				(					self       ):




									"""simple docstring"""

									__UpperCamelCase						,							__UpperCamelCase :   Optional[Any]									=self.model_tester.prepare_config_and_inputs_for_common()

									__UpperCamelCase :   int									=_config_zero_init(a__       )
									for model_class in self.all_model_classes:
														__UpperCamelCase :   Optional[int]									=model_class(config=a__       )
														for name, param in model.named_parameters():
																			if "embeddings" not in name and param.requires_grad:
																								self.assertIn(
																								    ((param.data.mean() * 1E9).round() / 1E9).item()				,    [0.0, 1.0]				,    msg=f'Parameter {name} of model {model_class} seems not properly initialized'				,    )





"""simple docstring"""


import os

from apache_beam.io.filesystems import FileSystems
from apache_beam.pipeline import Pipeline

from .logging import get_logger


a_											=     2 << 20  # 2mb
a_											=     get_logger(__name__)
class snake_case   (	_UpperCamelCase):







		def 			a_			(       self   :							Tuple		)							->    Optional[int]:





						'''simple docstring'''







						_A       	=      self._options.get_all_options().get("runner"		)
						return runner in [None, "DirectRunner", "PortableRunner"]






def   a__			(					__lowercase  ,      __lowercase  ,      __lowercase=False )						->						Any:
				_A       	=      FileSystems
				if fs.exists(__lowercase ):
								if force_upload:
												logger.info(f"""Remote path already exist: {remote_file_path}. Overwriting it as force_upload=True.""" )
								else:
												logger.info(f"""Remote path already exist: {remote_file_path}. Skipping it as force_upload=False.""" )
												return
				with fs.create(__lowercase ) as remote_file:
								with open(__lowercase  ,      "rb" ) as local_file:
												_A       	=      local_file.read(__lowercase )
												while chunk:
																remote_file.write(__lowercase )
																_A       	=      local_file.read(__lowercase )






def   a__			(					__lowercase  ,      __lowercase  ,      __lowercase=False )						->						Union[str, Any]:
				_A       	=      FileSystems
				if os.path.exists(__lowercase ):
								if force_download:
												logger.info(f"""Local path already exist: {remote_file_path}. Overwriting it as force_upload=True.""" )
								else:
												logger.info(f"""Local path already exist: {remote_file_path}. Skipping it as force_upload=False.""" )
												return
				with fs.open(__lowercase ) as remote_file:
								with open(__lowercase  ,      "wb" ) as local_file:
												_A       	=      remote_file.read(__lowercase )
												while chunk:
																local_file.write(__lowercase )
																_A       	=      remote_file.read(__lowercase )