





import json
import os
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from requests.exceptions import HTTPError

from transformers.utils import (
    CONFIG_NAME,
    FLAX_WEIGHTS_NAME,
    TF2_WEIGHTS_NAME,
    TRANSFORMERS_CACHE,
    WEIGHTS_NAME,
    cached_file,
    get_file_from_repo,
    has_file,
)


UpperCamelCase_												=	'hf-internal-testing/tiny-random-bert'
UpperCamelCase_												=	os.path.join(TRANSFORMERS_CACHE, 'models--hf-internal-testing--tiny-random-bert')
UpperCamelCase_												=	'9b8c223d42b2188cb49d29af482996f9d0f3e5a6'

class     snake_case_		(   unittest.TestCase							):






 '''simple docstring'''





 def  __UpperCAmelCase						(  self		)	->       Dict:
    UpperCAmelCase__						=cached_file(A_,		A_		)
    # Should have downloaded the file in here
    self.assertTrue(os.path.isdir(A_		)		)
    # Cache should contain at least those three subfolders:
    for subfolder in ["blobs", "refs", "snapshots"]:
       self.assertTrue(os.path.isdir(os.path.join(A_,		A_		)		)		)
    with open(os.path.join(A_,		"refs",		"main"		)		) as f:
       UpperCAmelCase__						=f.read()
    self.assertEqual(A_,		os.path.join(A_,		"snapshots",		A_,		A_		)		)
    self.assertTrue(os.path.isfile(A_		)		)

    # File is cached at the same place the second time.
    UpperCAmelCase__						=cached_file(A_,		A_		)
    self.assertEqual(A_,		A_		)

    # Using a specific revision to test the full commit hash.
    UpperCAmelCase__						=cached_file(A_,		A_,		revision="9b8c223"		)
    self.assertEqual(A_,		os.path.join(A_,		"snapshots",		A_,		A_		)		)

 def  __UpperCAmelCase						(  self		)	->       List[str]:
    with self.assertRaisesRegex(A_,		"is not a valid model identifier"		):
       UpperCAmelCase__						=cached_file("tiny-random-bert",		A_		)

    with self.assertRaisesRegex(A_,		"is not a valid git identifier"		):
       UpperCAmelCase__						=cached_file(A_,		A_,		revision="aaaa"		)

    with self.assertRaisesRegex(A_,		"does not appear to have a file named"		):
       UpperCAmelCase__						=cached_file(A_,		"conf"		)

 def  __UpperCAmelCase						(  self		)	->       Any:
    with self.assertRaisesRegex(A_,		"does not appear to have a file named"		):
       UpperCAmelCase__						=cached_file(A_,		"conf"		)

    with open(os.path.join(A_,		"refs",		"main"		)		) as f:
       UpperCAmelCase__						=f.read()
    self.assertTrue(os.path.isfile(os.path.join(A_,		".no_exist",		A_,		"conf"		)		)		)

    UpperCAmelCase__						=cached_file(A_,		"conf",		_raise_exceptions_for_missing_entries=A_		)
    self.assertIsNone(A_		)

    UpperCAmelCase__						=cached_file(A_,		"conf",		local_files_only=A_,		_raise_exceptions_for_missing_entries=A_		)
    self.assertIsNone(A_		)

    UpperCAmelCase__						=mock.Mock()
    UpperCAmelCase__						=500
    UpperCAmelCase__						={}
    UpperCAmelCase__						=HTTPError
    UpperCAmelCase__						={}

    # Under the mock environment we get a 500 error when trying to reach the tokenizer.
    with mock.patch("requests.Session.request",		return_value=A_		) as mock_head:
       UpperCAmelCase__						=cached_file(A_,		"conf",		_raise_exceptions_for_connection_errors=A_		)
       self.assertIsNone(A_		)
       # This check we did call the fake head request
       mock_head.assert_called()

 def  __UpperCAmelCase						(  self		)	->       List[str]:
    self.assertTrue(has_file("hf-internal-testing/tiny-bert-pt-only",		A_		)		)
    self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only",		A_		)		)
    self.assertFalse(has_file("hf-internal-testing/tiny-bert-pt-only",		A_		)		)

 def  __UpperCAmelCase						(  self		)	->       int:
    # `get_file_from_repo` returns None if the file does not exist
    self.assertIsNone(get_file_from_repo("bert-base-cased",		"ahah.txt"		)		)

    # The function raises if the repository does not exist.
    with self.assertRaisesRegex(A_,		"is not a valid model identifier"		):
       get_file_from_repo("bert-base-case",		A_		)

    # The function raises if the revision does not exist.
    with self.assertRaisesRegex(A_,		"is not a valid git identifier"		):
       get_file_from_repo("bert-base-cased",		A_,		revision="ahaha"		)

    UpperCAmelCase__						=get_file_from_repo("bert-base-cased",		A_		)
    # The name is the cached name which is not very easy to test, so instead we load the content.
    UpperCAmelCase__						=json.loads(open(A_,		"r"		).read()		)
    self.assertEqual(config["hidden_size"],		768		)






 def  __UpperCAmelCase						(  self		)	->       str:
    with tempfile.TemporaryDirectory() as tmp_dir:
       UpperCAmelCase__						=Path(A_		) / "a.txt"
       filename.touch()
       self.assertEqual(get_file_from_repo(A_,		"a.txt"		),		str(A_		)		)

       self.assertIsNone(get_file_from_repo(A_,		"b.txt"		)		)










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


UpperCamelCase_												=	logging.get_logger(__name__)

UpperCamelCase_												=	{
    'naver-clova-ix/donut-base': 'https://huggingface.co/naver-clova-ix/donut-base/resolve/main/config.json',
    # See all Donut models at https://huggingface.co/models?filter=donut-swin
}

class     snake_case_		(   a							):






 '''simple docstring'''





 __UpperCamelCase       =      'donut-swin'

 __UpperCamelCase       =      {
     'num_attention_heads': 'num_heads',
     'num_hidden_layers': 'num_layers',
 }

 def __init__(  self,		A_=224,		A_=4,		A_=3,		A_=96,		A_=[2, 2, 6, 2],		A_=[3, 6, 12, 24],		A_=7,		A_=4.0,		A_=True,		A_=0.0,		A_=0.0,		A_=0.1,		A_="gelu",		A_=False,		A_=0.02,		A_=1E-5,		**A_,		)	->       List[str]:
    super().__init__(**A_		)

    UpperCAmelCase__						=image_size
    UpperCAmelCase__						=patch_size
    UpperCAmelCase__						=num_channels
    UpperCAmelCase__						=embed_dim
    UpperCAmelCase__						=depths
    UpperCAmelCase__						=len(A_		)
    UpperCAmelCase__						=num_heads
    UpperCAmelCase__						=window_size
    UpperCAmelCase__						=mlp_ratio
    UpperCAmelCase__						=qkv_bias
    UpperCAmelCase__						=hidden_dropout_prob
    UpperCAmelCase__						=attention_probs_dropout_prob
    UpperCAmelCase__						=drop_path_rate
    UpperCAmelCase__						=hidden_act
    UpperCAmelCase__						=use_absolute_embeddings
    UpperCAmelCase__						=layer_norm_eps
    UpperCAmelCase__						=initializer_range
    # we set the hidden_size attribute in order to make Swin work with VisionEncoderDecoderModel
    # this indicates the channel dimension after the last stage of the model
    UpperCAmelCase__						=int(embed_dim * 2 ** (len(A_		) - 1)		)



