
"""simple docstring"""




import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


A__ :       List[Any]				= logging.get_logger(__name__)

A__ :       Optional[int]				= {
    'google/pix2struct-textcaps-base': (
        'https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json'
    ),
}


class       __magic_name__							(							snake_case_			):
       UpperCamelCase_ =		'pix2struct_text_model'
       UpperCamelCase_ =		['past_key_values']
       UpperCamelCase_ =		{
           'hidden_size': 'hidden_size',
           'num_attention_heads': 'num_heads',
           'num_hidden_layers': 'num_layers',
       }
       def __init__(  self			,					A_=5_0244			,					A_=768			,					A_=64			,					A_=2048			,					A_=12			,					A_=12			,					A_=32			,					A_=128			,					A_=0.1			,					A_=1E-6			,					A_=1.0			,					A_="gelu_new"			,					A_=0			,					A_=False			,					A_=0			,					A_=1			,					A_=False			,					A_=True			,					**A_			,					)  ->						List[str]:





         """simple docstring"""




         _lowercase:      Tuple   			=  vocab_size
         _lowercase:      Tuple   			=  hidden_size
         _lowercase:      int   			=  d_kv
         _lowercase:      Optional[Any]   			=  d_ff
         _lowercase:      Optional[int]   			=  num_layers
         _lowercase:      int   			=  num_heads
         _lowercase:      List[str]   			=  relative_attention_num_buckets
         _lowercase:      Dict   			=  relative_attention_max_distance
         _lowercase:      List[Any]   			=  dropout_rate
         _lowercase:      List[str]   			=  layer_norm_epsilon
         _lowercase:      str   			=  initializer_factor
         _lowercase:      Tuple   			=  use_cache

         _lowercase:      List[Any]   			=  eos_token_id
         _lowercase:      List[str]   			=  decoder_start_token_id

         # for backwards compatibility
         _lowercase:      List[str]   			=  dense_act_fn

         super().__init__(
             pad_token_id=lowerCAmelCase__			,					eos_token_id=lowerCAmelCase__			,					decoder_start_token_id=lowerCAmelCase__			,					tie_word_embeddings=lowerCAmelCase__			,					is_decoder=lowerCAmelCase__			,					**lowerCAmelCase__			,					)



       @classmethod
       def 				lowercase_				(  cls			,					A_			,					**A_							)  ->						Dict:





         """simple docstring"""




         cls._set_token_in_kwargs(lowerCAmelCase__							)

         _lowercase:      List[Any]   			=  cls.get_config_dict(lowerCAmelCase__			,					**lowerCAmelCase__							)

         # get the text config dict if we are loading from Pix2StructConfig
         if config_dict.get('''model_type'''							) == "pix2struct":
           _lowercase:      Dict   			=  config_dict['''text_config''']

         if "model_type" in config_dict and hasattr(cls			,					'''model_type'''							) and config_dict["model_type"] != cls.model_type:
           logger.warning(
               f'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
               f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

         return cls.from_dict(lowerCAmelCase__			,					**lowerCAmelCase__							)


class       __magic_name__							(							snake_case_			):
       UpperCamelCase_ =		'pix2struct_vision_model'
       def __init__(  self			,					A_=768			,					A_=768			,					A_=2048			,					A_=64			,					A_=12			,					A_=12			,					A_="gelu_new"			,					A_=1E-6			,					A_=0.0			,					A_=0.0			,					A_=1E-10			,					A_=1.0			,					A_=4096			,					A_=32			,					A_=128			,					**A_			,					)  ->						List[Any]:





         """simple docstring"""




         super().__init__(**lowerCAmelCase__							)

         _lowercase:      int   			=  hidden_size
         _lowercase:      Optional[Any]   			=  patch_embed_hidden_size
         _lowercase:      Optional[int]   			=  d_ff
         _lowercase:      Optional[Any]   			=  dropout_rate
         _lowercase:      int   			=  num_hidden_layers
         _lowercase:      Optional[int]   			=  num_attention_heads
         _lowercase:      Dict   			=  initializer_range
         _lowercase:      Optional[int]   			=  initializer_factor
         _lowercase:      str   			=  attention_dropout
         _lowercase:      int   			=  layer_norm_eps
         _lowercase:      Dict   			=  dense_act_fn
         _lowercase:      List[str]   			=  seq_len
         _lowercase:      Optional[int]   			=  relative_attention_num_buckets
         _lowercase:      List[Any]   			=  relative_attention_max_distance
         _lowercase:      List[str]   			=  d_kv



       @classmethod
       def 				lowercase_				(  cls			,					A_			,					**A_							)  ->						Optional[int]:





         """simple docstring"""




         cls._set_token_in_kwargs(lowerCAmelCase__							)

         _lowercase:      Optional[Any]   			=  cls.get_config_dict(lowerCAmelCase__			,					**lowerCAmelCase__							)

         # get the vision config dict if we are loading from Pix2StructConfig
         if config_dict.get('''model_type'''							) == "pix2struct":
           _lowercase:      Any   			=  config_dict['''vision_config''']

         if "model_type" in config_dict and hasattr(cls			,					'''model_type'''							) and config_dict["model_type"] != cls.model_type:
           logger.warning(
               f'''You are using a model of type {config_dict["model_type"]} to instantiate a model of type '''
               f'''{cls.model_type}. This is not supported for all configurations of models and can yield errors.'''							)

         return cls.from_dict(lowerCAmelCase__			,					**lowerCAmelCase__							)


class       __magic_name__							(							snake_case_			):
       UpperCamelCase_ =		'pix2struct'
       UpperCamelCase_ =		True
       def __init__(  self			,					A_=None			,					A_=None			,					A_=1.0			,					A_=0.02			,					A_=False			,					A_=False			,					A_=True			,					**A_			,					)  ->						int:





         """simple docstring"""




         super().__init__(tie_word_embeddings=lowerCAmelCase__			,					is_encoder_decoder=lowerCAmelCase__			,					**lowerCAmelCase__							)

         if text_config is None:
           _lowercase:      Tuple   			=  {}
           logger.info('''text_config is None. Initializing the Pix2StructTextConfig with default values.'''							)

         if vision_config is None:
           _lowercase:      int   			=  {}
           logger.info('''vision_config is None. Initializing the Pix2StructVisionConfig with default values.'''							)

         _lowercase:      str   			=  PixaStructTextConfig(**lowerCAmelCase__							)
         _lowercase:      str   			=  PixaStructVisionConfig(**lowerCAmelCase__							)

         _lowercase:      Tuple   			=  self.text_config.decoder_start_token_id
         _lowercase:      int   			=  self.text_config.pad_token_id
         _lowercase:      Optional[Any]   			=  self.text_config.eos_token_id

         _lowercase:      str   			=  initializer_factor
         _lowercase:      Optional[Any]   			=  initializer_range

         _lowercase:      Dict   			=  self.initializer_range
         _lowercase:      Optional[Any]   			=  self.initializer_range

         _lowercase:      List[str]   			=  is_vqa
       @classmethod
       def 				lowercase_				(  cls			,					A_			,					A_			,					**A_							)  ->						List[Any]:





         """simple docstring"""




         return cls(text_config=text_config.to_dict()			,					vision_config=vision_config.to_dict()			,					**lowerCAmelCase__							)



       def 				lowercase_				(  self							)  ->						Any:





         """simple docstring"""




         _lowercase:      Tuple   			=  copy.deepcopy(self.__dict__							)
         _lowercase:      Tuple   			=  self.text_config.to_dict()
         _lowercase:      List[Any]   			=  self.vision_config.to_dict()
         _lowercase:      Optional[Any]   			=  self.__class__.model_type
         return output





'''simple docstring'''



import json
import os
from pathlib import Path

import pytest

from datasets.download.download_config import DownloadConfig
from datasets.download.download_manager import DownloadManager
from datasets.utils.file_utils import hash_url_to_filename


lowerCAmelCase_  :							Any             =    'http://www.mocksite.com/file1.txt'
lowerCAmelCase_  :							List[str]             =    '"text": ["foo", "foo"]'
lowerCAmelCase_  :							Any             =    '6d8ce9aa78a471c7477201efbeabd3bb01ac2e7d100a6dc024ba1608361f90a8'
class 						lowerCamelCase_				:
						_lowerCAmelCase  : Dict						  =					2_0_0
						_lowerCAmelCase  : int						  =					{'Content-Length': '100'}
						_lowerCAmelCase  : Optional[Any]						  =					{}







						def        __lowercase     (	self :   str					,     **lowerCAmelCase__ :   Any					):

													"""simple docstring"""
													return [bytes(lowerCAmelCase__					,     '''utf-8'''					)]


def        UpperCAmelCase (    *A	:					int       ,		**A	:					Optional[Any]					):
							return MockResponse()
@pytest.mark.parametrize('''urls_type'''       ,		[str, list, dict]					)
def        UpperCAmelCase (    A	:					int       ,		A	:					Union[str, Any]       ,		A	:					Union[str, Any]					):
							import requests

							monkeypatch.setattr(A       ,		'''request'''       ,		A					)

							SCREAMING_SNAKE_CASE       :   Optional[Any]         =      URL
							if issubclass(A       ,		A					):
														SCREAMING_SNAKE_CASE       :   List[Any]         =      url
							elif issubclass(A       ,		A					):
														SCREAMING_SNAKE_CASE       :   List[Any]         =      [url]
							elif issubclass(A       ,		A					):
														SCREAMING_SNAKE_CASE       :   str         =      {'''train''': url}
							SCREAMING_SNAKE_CASE       :   Any         =      '''dummy'''
							SCREAMING_SNAKE_CASE       :   Optional[Any]         =      '''downloads'''
							SCREAMING_SNAKE_CASE       :   Optional[Any]         =      tmp_path
							SCREAMING_SNAKE_CASE       :   List[str]         =      DownloadConfig(
							    cache_dir=os.path.join(A       ,		A					)       ,		use_etag=A       ,		)
							SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      DownloadManager(dataset_name=A       ,		download_config=A					)
							SCREAMING_SNAKE_CASE       :   List[str]         =      dl_manager.download(A					)
							SCREAMING_SNAKE_CASE       :   Dict         =      urls
							for downloaded_paths in [downloaded_paths]:
														if isinstance(A       ,		A					):
																					SCREAMING_SNAKE_CASE       :   List[str]         =      [downloaded_paths]
																					SCREAMING_SNAKE_CASE       :   int         =      [urls]
														elif isinstance(A       ,		A					):
																					assert "train" in downloaded_paths.keys()
																					SCREAMING_SNAKE_CASE       :   Optional[int]         =      downloaded_paths.values()
																					SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      urls.values()
														assert downloaded_paths
														for downloaded_path, input_url in zip(A       ,		A					):
																					assert downloaded_path == dl_manager.downloaded_paths[input_url]
																					SCREAMING_SNAKE_CASE       :   Dict         =      Path(A					)
																					SCREAMING_SNAKE_CASE       :   Optional[Any]         =      downloaded_path.parts
																					assert parts[-1] == HASH
																					assert parts[-2] == cache_subdir
																					assert downloaded_path.exists()
																					SCREAMING_SNAKE_CASE       :   str         =      downloaded_path.read_text()
																					assert content == CONTENT
																					SCREAMING_SNAKE_CASE       :   str         =      downloaded_path.with_suffix('''.json'''					)
																					assert metadata_downloaded_path.exists()
																					SCREAMING_SNAKE_CASE       :   Optional[int]         =      json.loads(metadata_downloaded_path.read_text()					)
																					assert metadata_content == {"url": URL, "etag": None}
@pytest.mark.parametrize('''paths_type'''       ,		[str, list, dict]					)
def        UpperCAmelCase (    A	:					Tuple       ,		A	:					Union[str, Any]       ,		A	:					Dict					):
							SCREAMING_SNAKE_CASE       :   List[str]         =      str(A					)
							if issubclass(A       ,		A					):
														SCREAMING_SNAKE_CASE       :   Optional[int]         =      filename
							elif issubclass(A       ,		A					):
														SCREAMING_SNAKE_CASE       :   Optional[int]         =      [filename]
							elif issubclass(A       ,		A					):
														SCREAMING_SNAKE_CASE       :   List[Any]         =      {'''train''': filename}
							SCREAMING_SNAKE_CASE       :   Dict         =      '''dummy'''
							SCREAMING_SNAKE_CASE       :   Optional[Any]         =      xz_file.parent
							SCREAMING_SNAKE_CASE       :   List[Any]         =      '''extracted'''
							SCREAMING_SNAKE_CASE       :   Optional[Any]         =      DownloadConfig(
							    cache_dir=A       ,		use_etag=A       ,		)
							SCREAMING_SNAKE_CASE       :   Optional[int]         =      DownloadManager(dataset_name=A       ,		download_config=A					)
							SCREAMING_SNAKE_CASE       :   Optional[int]         =      dl_manager.extract(A					)
							SCREAMING_SNAKE_CASE       :   Tuple         =      paths
							for extracted_paths in [extracted_paths]:
														if isinstance(A       ,		A					):
																					SCREAMING_SNAKE_CASE       :   List[Any]         =      [extracted_paths]
																					SCREAMING_SNAKE_CASE       :   str         =      [paths]
														elif isinstance(A       ,		A					):
																					assert "train" in extracted_paths.keys()
																					SCREAMING_SNAKE_CASE       :   Optional[int]         =      extracted_paths.values()
																					SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      paths.values()
														assert extracted_paths
														for extracted_path, input_path in zip(A       ,		A					):
																					assert extracted_path == dl_manager.extracted_paths[input_path]
																					SCREAMING_SNAKE_CASE       :   Any         =      Path(A					)
																					SCREAMING_SNAKE_CASE       :   Optional[Any]         =      extracted_path.parts
																					assert parts[-1] == hash_url_to_filename(A       ,		etag=A					)
																					assert parts[-2] == extracted_subdir
																					assert extracted_path.exists()
																					SCREAMING_SNAKE_CASE       :   int         =      extracted_path.read_text()
																					SCREAMING_SNAKE_CASE       :   Optional[int]         =      text_file.read_text()
																					assert extracted_file_content == expected_file_content
def        UpperCAmelCase (    A	:					Optional[int]       ,		A	:					int					):
							assert path.endswith('''.jsonl'''					)
							for num_items, line in enumerate(A       ,		start=1					):
														SCREAMING_SNAKE_CASE       :   List[Any]         =      json.loads(line.decode('''utf-8'''					)					)
														assert item.keys() == {"col_1", "col_2", "col_3"}
							assert num_items == 4
@pytest.mark.parametrize('''archive_jsonl'''       ,		['''tar_jsonl_path''', '''zip_jsonl_path''']					)
def        UpperCAmelCase (    A	:					Any       ,		A	:					str					):
							SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      request.getfixturevalue(A					)
							SCREAMING_SNAKE_CASE       :   List[Any]         =      DownloadManager()
							for num_jsonl, (path, file) in enumerate(dl_manager.iter_archive(A					)       ,		start=1					):
														_test_jsonl(A       ,		A					)
							assert num_jsonl == 2
@pytest.mark.parametrize('''archive_nested_jsonl'''       ,		['''tar_nested_jsonl_path''', '''zip_nested_jsonl_path''']					)
def        UpperCAmelCase (    A	:					Dict       ,		A	:					Optional[Any]					):
							SCREAMING_SNAKE_CASE       :   Union[str, Any]         =      request.getfixturevalue(A					)
							SCREAMING_SNAKE_CASE       :   Optional[int]         =      DownloadManager()
							for num_tar, (path, file) in enumerate(dl_manager.iter_archive(A					)       ,		start=1					):
														for num_jsonl, (subpath, subfile) in enumerate(dl_manager.iter_archive(A					)       ,		start=1					):
																					_test_jsonl(A       ,		A					)
							assert num_tar == 1
							assert num_jsonl == 2






def        UpperCAmelCase (    A	:					List[Any]					):
							SCREAMING_SNAKE_CASE       :   Optional[Any]         =      DownloadManager()
							for num_file, file in enumerate(dl_manager.iter_files(A					)       ,		start=1					):
														assert os.path.basename(A					) == ("test.txt" if num_file == 1 else "train.txt")
							assert num_file == 2




