





"""simple docstring"""



import collections
import inspect
import unittest

from transformers import DinatConfig
from transformers.testing_utils import require_natten, require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
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 DinatBackbone, DinatForImageClassification, DinatModel
       from transformers.models.dinat.modeling_dinat import DINAT_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
       from PIL import Image

       from transformers import AutoImageProcessor






class 	__snake_case      :





 """simple docstring"""


 def __init__(					self      ,		__lowerCamelCase      ,		__lowerCamelCase=13      ,		__lowerCamelCase=64      ,		__lowerCamelCase=4      ,		__lowerCamelCase=3      ,		__lowerCamelCase=16      ,		__lowerCamelCase=[1, 2, 1]      ,		__lowerCamelCase=[2, 4, 8]      ,		__lowerCamelCase=3      ,		__lowerCamelCase=[[3], [1, 2], [1]]      ,		__lowerCamelCase=2.0      ,		__lowerCamelCase=True      ,		__lowerCamelCase=0.0      ,		__lowerCamelCase=0.0      ,		__lowerCamelCase=0.1      ,		__lowerCamelCase="gelu"      ,		__lowerCamelCase=True      ,		__lowerCamelCase=0.0_2      ,		__lowerCamelCase=1e-5      ,		__lowerCamelCase=True      ,		__lowerCamelCase=None      ,		__lowerCamelCase=True      ,		__lowerCamelCase=10      ,		__lowerCamelCase=["stage1", "stage2"]      ,		__lowerCamelCase=[1, 2]      ,		):



     '''simple docstring'''




     __A					:		List[str]						=  parent
     __A					:		int						=  batch_size
     __A					:		Dict						=  image_size
     __A					:		Union[str, Any]						=  patch_size
     __A					:		Optional[int]						=  num_channels
     __A					:		Optional[Any]						=  embed_dim
     __A					:		int						=  depths
     __A					:		Tuple						=  num_heads
     __A					:		Optional[Any]						=  kernel_size
     __A					:		Union[str, Any]						=  dilations
     __A					:		List[Any]						=  mlp_ratio
     __A					:		Union[str, Any]						=  qkv_bias
     __A					:		List[Any]						=  hidden_dropout_prob
     __A					:		int						=  attention_probs_dropout_prob
     __A					:		Any						=  drop_path_rate
     __A					:		Optional[int]						=  hidden_act
     __A					:		List[str]						=  patch_norm
     __A					:		List[str]						=  layer_norm_eps
     __A					:		int						=  initializer_range
     __A					:		Optional[Any]						=  is_training
     __A					:		Dict						=  scope
     __A					:		List[str]						=  use_labels
     __A					:		Tuple						=  num_labels
     __A					:		str						=  out_features
     __A					:		List[str]						=  out_indices

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Optional[Any]						=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]						)

     __A					:		Tuple						=  None
     if self.use_labels:
         __A					:		Optional[int]						=  ids_tensor([self.batch_size]      ,		self.num_labels						)

     __A					:		List[Any]						=  self.get_config()

     return config, pixel_values, labels

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     return DinatConfig(
         num_labels=self.num_labels      ,		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      ,		kernel_size=self.kernel_size      ,		dilations=self.dilations      ,		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      ,		patch_norm=self.patch_norm      ,		layer_norm_eps=self.layer_norm_eps      ,		initializer_range=self.initializer_range      ,		out_features=self.out_features      ,		out_indices=self.out_indices      ,		)

 def       UpperCamelCase__(					self      ,		__lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase						):



     '''simple docstring'''




     __A					:		str						=  DinatModel(config=__lowerCamelCase						)
     model.to(__lowerCamelCase						)
     model.eval()
     __A					:		Dict						=  model(__lowerCamelCase						)

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

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

 def       UpperCamelCase__(					self      ,		__lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase						):



     '''simple docstring'''




     __A					:		Dict						=  DinatForImageClassification(__lowerCamelCase						)
     model.to(__lowerCamelCase						)
     model.eval()
     __A					:		Tuple						=  model(__lowerCamelCase      ,		labels=__lowerCamelCase						)
     self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.num_labels)						)

     # test greyscale images
     __A					:		Tuple						=  1
     __A					:		Dict						=  DinatForImageClassification(__lowerCamelCase						)
     model.to(__lowerCamelCase						)
     model.eval()

     __A					:		str						=  floats_tensor([self.batch_size, 1, self.image_size, self.image_size]						)
     __A					:		Optional[int]						=  model(__lowerCamelCase						)
     self.parent.assertEqual(result.logits.shape      ,		(self.batch_size, self.num_labels)						)

 def       UpperCamelCase__(					self      ,		__lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase						):



     '''simple docstring'''




     __A					:		Dict						=  DinatBackbone(config=__lowerCamelCase						)
     model.to(__lowerCamelCase						)
     model.eval()
     __A					:		Optional[int]						=  model(__lowerCamelCase						)

     # verify hidden states
     self.parent.assertEqual(len(result.feature_maps						)      ,		len(config.out_features						)						)
     self.parent.assertListEqual(list(result.feature_maps[0].shape						)      ,		[self.batch_size, model.channels[0], 16, 16]						)

     # verify channels
     self.parent.assertEqual(len(model.channels						)      ,		len(config.out_features						)						)

     # verify backbone works with out_features=None
     __A					:		str						=  None
     __A					:		List[Any]						=  DinatBackbone(config=__lowerCamelCase						)
     model.to(__lowerCamelCase						)
     model.eval()
     __A					:		Any						=  model(__lowerCamelCase						)

     # verify feature maps
     self.parent.assertEqual(len(result.feature_maps						)      ,		1						)
     self.parent.assertListEqual(list(result.feature_maps[0].shape						)      ,		[self.batch_size, model.channels[-1], 4, 4]						)

     # verify channels
     self.parent.assertEqual(len(model.channels						)      ,		1						)







 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		List[Any]						=  self.prepare_config_and_inputs()
     __A				, __A				, __A					:		List[Any]						=  config_and_inputs
     __A					:		Dict						=  {'''pixel_values''': pixel_values}
     return config, inputs_dict






@require_natten
@require_torch
class 	__snake_case      (	SCREAMING_SNAKE_CASE__							,							SCREAMING_SNAKE_CASE__							,							unittest.TestCase  ):





 """simple docstring"""


 _lowerCamelCase				 =  (
     (
         DinatModel,
         DinatForImageClassification,
         DinatBackbone,
     )
     if is_torch_available()
     else ()
 )
 _lowerCamelCase				 =  (
     {"""feature-extraction""": DinatModel, """image-classification""": DinatForImageClassification}
     if is_torch_available()
     else {}
 )
 _lowerCamelCase				 =  False

 _lowerCamelCase				 =  False
 _lowerCamelCase				 =  False
 _lowerCamelCase				 =  False
 _lowerCamelCase				 =  False

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Optional[Any]						=  DinatModelTester(self						)
     __A					:		List[str]						=  ConfigTester(self      ,		config_class=__lowerCamelCase      ,		embed_dim=37						)

 def       UpperCamelCase__(					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       UpperCamelCase__(					self						):



     '''simple docstring'''




     return

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Union[str, Any]						=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_model(*__lowerCamelCase						)

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Optional[int]						=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_for_image_classification(*__lowerCamelCase						)

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Optional[Any]						=  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_backbone(*__lowerCamelCase						)

 @unittest.skip(reason='''Dinat does not use inputs_embeds'''						)
 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     pass

 @unittest.skip(reason='''Dinat does not use feedforward chunking'''						)
 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     pass

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




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

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

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A				, __A					:		List[Any]						=  self.model_tester.prepare_config_and_inputs_for_common()

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

         __A					:		str						=  ['''pixel_values''']
         self.assertListEqual(arg_names[:1]      ,		__lowerCamelCase						)

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     self.skipTest('''Dinat\'s attention operation is handled entirely by NATTEN.'''						)

 def       UpperCamelCase__(					self      ,		__lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase						):



     '''simple docstring'''




     __A					:		int						=  model_class(__lowerCamelCase						)
     model.to(__lowerCamelCase						)
     model.eval()

     with torch.no_grad():
         __A					:		Optional[Any]						=  model(**self._prepare_for_class(__lowerCamelCase      ,		__lowerCamelCase						)						)

     __A					:		Tuple						=  outputs.hidden_states

     __A					:		List[str]						=  getattr(
         self.model_tester      ,		'''expected_num_hidden_layers'''      ,		len(self.model_tester.depths						) + 1						)
     self.assertEqual(len(__lowerCamelCase						)      ,		__lowerCamelCase						)

     # Dinat has a different seq_length
     __A					:		Union[str, Any]						=  (
         config.patch_size
         if isinstance(config.patch_size      ,		collections.abc.Iterable						)
         else (config.patch_size, config.patch_size)
     )

     __A					:		Union[str, Any]						=  image_size[0] // patch_size[0]
     __A					:		Dict						=  image_size[1] // patch_size[1]

     self.assertListEqual(
         list(hidden_states[0].shape[-3:]						)      ,		[height, width, self.model_tester.embed_dim]      ,		)

     if model_class.__name__ != "DinatBackbone":
         __A					:		List[str]						=  outputs.reshaped_hidden_states
         self.assertEqual(len(__lowerCamelCase						)      ,		__lowerCamelCase						)

         __A				, __A				, __A				, __A					:		List[Any]						=  reshaped_hidden_states[0].shape
         __A					:		Any						=  (
             reshaped_hidden_states[0].view(__lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase						).permute(0      ,		2      ,		3      ,		1						)
         )
         self.assertListEqual(
             list(reshaped_hidden_states.shape[-3:]						)      ,		[height, width, self.model_tester.embed_dim]      ,		)

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




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

     __A					:		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:
         __A					:		Dict						=  True
         self.check_hidden_states_output(__lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase						)

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         __A					:		Dict						=  True

         self.check_hidden_states_output(__lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase      ,		__lowerCamelCase						)

 @slow
 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     for model_name in DINAT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         __A					:		Union[str, Any]						=  DinatModel.from_pretrained(__lowerCamelCase						)
         self.assertIsNotNone(__lowerCamelCase						)







 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A				, __A					:		List[str]						=  self.model_tester.prepare_config_and_inputs_for_common()

     __A					:		Tuple						=  _config_zero_init(__lowerCamelCase						)
     for model_class in self.all_model_classes:
         __A					:		Tuple						=  model_class(config=__lowerCamelCase						)
         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"""      ,		)






@require_natten
@require_vision
@require_torch
class 	__snake_case      (	unittest.TestCase  ):





 """simple docstring"""


 @cached_property
 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     return AutoImageProcessor.from_pretrained('''shi-labs/dinat-mini-in1k-224'''						) if is_vision_available() else None







 @slow
 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		Any						=  DinatForImageClassification.from_pretrained('''shi-labs/dinat-mini-in1k-224'''						).to(__lowerCamelCase						)
     __A					:		Union[str, Any]						=  self.default_image_processor

     __A					:		Tuple						=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''						)
     __A					:		Optional[int]						=  image_processor(images=__lowerCamelCase      ,		return_tensors='''pt'''						).to(__lowerCamelCase						)

     # forward pass
     with torch.no_grad():
         __A					:		Optional[int]						=  model(**__lowerCamelCase						)

     # verify the logits
     __A					:		Tuple						=  torch.Size((1, 1000)						)
     self.assertEqual(outputs.logits.shape      ,		__lowerCamelCase						)
     __A					:		Any						=  torch.tensor([-0.1_5_4_5, -0.7_6_6_7, 0.4_6_4_2]						).to(__lowerCamelCase						)
     self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,		__lowerCamelCase      ,		atol=1e-4						)						)






@require_torch
@require_natten
class 	__snake_case      (	unittest.TestCase							,							SCREAMING_SNAKE_CASE__  ):





 """simple docstring"""


 _lowerCamelCase				 =  (DinatBackbone,) if is_torch_available() else ()
 _lowerCamelCase				 =  DinatConfig

 def       UpperCamelCase__(					self						):



     '''simple docstring'''




     __A					:		int						=  DinatModelTester(self						)











"""simple docstring"""



import hashlib
import importlib.util
import json
import os
import pathlib
from types import ModuleType

import pytest
import requests

a_				=    pathlib.Path.cwd().joinpath("""project_euler""")
a_				=    pathlib.Path.cwd().joinpath(
    """scripts""", """project_euler_answers.json"""
)

with open(PROJECT_EULER_ANSWERS_PATH) as file_handle:
       a_				=    json.load(file_handle)







def 	__lowercase  (     snake_case_						:						pathlib.Path		)     ->ModuleType:



    '''simple docstring'''







    __A					:		str						=  importlib.util.spec_from_file_location(file_path.name       ,str(snake_case_		)		)
    __A					:		Optional[int]						=  importlib.util.module_from_spec(snake_case_		)  # type: ignore
    spec.loader.exec_module(snake_case_		)  # type: ignore
    return module







def 	__lowercase  (     )     ->list[pathlib.Path]:



    '''simple docstring'''







    __A					:		Optional[int]						=  []
    for problem_dir_path in PROJECT_EULER_DIR_PATH.iterdir():
        if problem_dir_path.is_file() or problem_dir_path.name.startswith('''_'''		):
            continue
        for file_path in problem_dir_path.iterdir():
            if file_path.suffix != ".py" or file_path.name.startswith(('''_''', '''test''')		):
                continue
            solution_file_paths.append(snake_case_		)
    return solution_file_paths







def 	__lowercase  (     )     ->str:



    '''simple docstring'''







    with open(os.environ['''GITHUB_EVENT_PATH''']		) as file:
        __A					:		Any						=  json.load(snake_case_		)
    return event["pull_request"]["url"] + "/files"







def 	__lowercase  (     )     ->list[pathlib.Path]:



    '''simple docstring'''







    __A					:		int						=  []
    __A					:		Optional[int]						=  {
        '''Accept''': '''application/vnd.github.v3+json''',
        '''Authorization''': '''token ''' + os.environ['''GITHUB_TOKEN'''],
    }
    __A					:		Optional[Any]						=  requests.get(get_files_url()       ,headers=snake_case_		).json()
    for file in files:
        __A					:		Union[str, Any]						=  pathlib.Path.cwd().joinpath(file['''filename''']		)
        if (
            filepath.suffix != ".py"
            or filepath.name.startswith(('''_''', '''test''')		)
            or not filepath.name.startswith('''sol'''		)
        ):
            continue
        solution_file_paths.append(snake_case_		)
    return solution_file_paths







def 	__lowercase  (     )     ->list[pathlib.Path]:



    '''simple docstring'''







    if os.environ.get('''CI'''		) and os.environ.get('''GITHUB_EVENT_NAME'''		) == "pull_request":
        # Return only if there are any, otherwise default to all solutions
        if filepaths := added_solution_file_path():
            return filepaths
    return all_solution_file_paths()







@pytest.mark.parametrize(
    '''solution_path'''       ,collect_solution_file_paths()       ,ids=lambda snake_case_		: F"""{path.parent.name}/{path.name}"""       ,)
def 	__lowercase  (     snake_case_						:						pathlib.Path		)     ->None:



    '''simple docstring'''







    __A					:		str						=  solution_path.parent.name[8:].zfill(3		)
    __A					:		str						=  PROBLEM_ANSWERS[problem_number]
    __A					:		Union[str, Any]						=  convert_path_to_module(snake_case_		)
    __A					:		List[str]						=  str(solution_module.solution()		)  # type: ignore
    __A					:		List[str]						=  hashlib.shaaaa(answer.encode()		).hexdigest()
    assert (
        answer == expected
    ), F"""Expected solution to {problem_number} to have hash {expected}, got {answer}"""




