






"""simple docstring"""







import json
from typing import List, Optional, Tuple

from tokenizers import normalizers

from ...tokenization_utils_base import BatchEncoding
from ...tokenization_utils_fast import PreTrainedTokenizerFast
from ...utils import PaddingStrategy, logging
from .tokenization_realm import RealmTokenizer


_lowerCAmelCase            =	logging.get_logger(__name__)

_lowerCAmelCase            =	{'vocab_file': 'vocab.txt', 'tokenizer_file': 'tokenizer.json'}

_lowerCAmelCase            =	{
    'vocab_file': {
        'google/realm-cc-news-pretrained-embedder': (
            'https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/vocab.txt'
        ),
        'google/realm-cc-news-pretrained-encoder': (
            'https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/vocab.txt'
        ),
        'google/realm-cc-news-pretrained-scorer': (
            'https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/vocab.txt'
        ),
        'google/realm-cc-news-pretrained-openqa': (
            'https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/vocab.txt'
        ),
        'google/realm-orqa-nq-openqa': 'https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/vocab.txt',
        'google/realm-orqa-nq-reader': 'https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/vocab.txt',
        'google/realm-orqa-wq-openqa': 'https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/vocab.txt',
        'google/realm-orqa-wq-reader': 'https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/vocab.txt',
    },
    'tokenizer_file': {
        'google/realm-cc-news-pretrained-embedder': (
            'https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/tokenizer.jsont'
        ),
        'google/realm-cc-news-pretrained-encoder': (
            'https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/tokenizer.json'
        ),
        'google/realm-cc-news-pretrained-scorer': (
            'https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/tokenizer.json'
        ),
        'google/realm-cc-news-pretrained-openqa': (
            'https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/tokenizer.json'
        ),
        'google/realm-orqa-nq-openqa': (
            'https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/tokenizer.json'
        ),
        'google/realm-orqa-nq-reader': (
            'https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/tokenizer.json'
        ),
        'google/realm-orqa-wq-openqa': (
            'https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/tokenizer.json'
        ),
        'google/realm-orqa-wq-reader': (
            'https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/tokenizer.json'
        ),
    },
}

_lowerCAmelCase            =	{
    'google/realm-cc-news-pretrained-embedder': 5_12,
    'google/realm-cc-news-pretrained-encoder': 5_12,
    'google/realm-cc-news-pretrained-scorer': 5_12,
    'google/realm-cc-news-pretrained-openqa': 5_12,
    'google/realm-orqa-nq-openqa': 5_12,
    'google/realm-orqa-nq-reader': 5_12,
    'google/realm-orqa-wq-openqa': 5_12,
    'google/realm-orqa-wq-reader': 5_12,
}

_lowerCAmelCase            =	{
    'google/realm-cc-news-pretrained-embedder': {'do_lower_case': True},
    'google/realm-cc-news-pretrained-encoder': {'do_lower_case': True},
    'google/realm-cc-news-pretrained-scorer': {'do_lower_case': True},
    'google/realm-cc-news-pretrained-openqa': {'do_lower_case': True},
    'google/realm-orqa-nq-openqa': {'do_lower_case': True},
    'google/realm-orqa-nq-reader': {'do_lower_case': True},
    'google/realm-orqa-wq-openqa': {'do_lower_case': True},
    'google/realm-orqa-wq-reader': {'do_lower_case': True},
}


class      UpperCamelCase		(__snake_case     ):
						_SCREAMING_SNAKE_CASE  : int												=		VOCAB_FILES_NAMES
						_SCREAMING_SNAKE_CASE  : int												=		PRETRAINED_VOCAB_FILES_MAP
						_SCREAMING_SNAKE_CASE  : int												=		PRETRAINED_INIT_CONFIGURATION
						_SCREAMING_SNAKE_CASE  : int												=		PRETRAINED_POSITIONAL_EMBEDDINGS_SIZES
						_SCREAMING_SNAKE_CASE  : Union[str, Any]												=		RealmTokenizer






						def __init__(		self  :List[Any]			, __magic_name__  :List[Any]=None			, __magic_name__  :int=None			, __magic_name__  :int=True			, __magic_name__  :Union[str, Any]="[UNK]"			, __magic_name__  :str="[SEP]"			, __magic_name__  :Any="[PAD]"			, __magic_name__  :Optional[Any]="[CLS]"			, __magic_name__  :List[Any]="[MASK]"			, __magic_name__  :Any=True			, __magic_name__  :Optional[Any]=None			, **__magic_name__  :Optional[int]			, )      ->Optional[Any]:
										super().__init__(
										    __magic_name__			, tokenizer_file=__magic_name__			, do_lower_case=__magic_name__			, unk_token=__magic_name__			, sep_token=__magic_name__			, pad_token=__magic_name__			, cls_token=__magic_name__			, mask_token=__magic_name__			, tokenize_chinese_chars=__magic_name__			, strip_accents=__magic_name__			, **__magic_name__			, )

										lowercase    :      Union[str, Any]       = json.loads(self.backend_tokenizer.normalizer.__getstate__()				)
										if (
										    normalizer_state.get("""lowercase"""			, __magic_name__				) != do_lower_case
										    or normalizer_state.get("""strip_accents"""			, __magic_name__				) != strip_accents
										    or normalizer_state.get("""handle_chinese_chars"""			, __magic_name__				) != tokenize_chinese_chars
										):
														lowercase    :      Optional[Any]       = getattr(__magic_name__			, normalizer_state.pop("""type"""				)				)
														lowercase    :      int       = do_lower_case
														lowercase    :      Optional[Any]       = strip_accents
														lowercase    :      Optional[int]       = tokenize_chinese_chars
														lowercase    :      Union[str, Any]       = normalizer_class(**__magic_name__				)

										lowercase    :      Optional[Any]       = do_lower_case






						def      __snake_case      (		self  :Optional[int]			, __magic_name__  :int			, **__magic_name__  :Any				)      ->Optional[Any]:
										lowercase    :      Dict       = PaddingStrategy.MAX_LENGTH

										lowercase    :      int       = text
										lowercase    :      Any       = kwargs.pop("""text_pair"""			, __magic_name__				)
										lowercase    :      str       = kwargs.pop("""return_tensors"""			, __magic_name__				)

										lowercase    :      str       = {
										    """input_ids""": [],
										    """attention_mask""": [],
										    """token_type_ids""": [],
										}

										for idx, candidate_text in enumerate(__magic_name__				):
														if batch_text_pair is not None:
																		lowercase    :      Any       = batch_text_pair[idx]
														else:
																		lowercase    :      Dict       = None

														lowercase    :      int       = super().__call__(__magic_name__			, __magic_name__			, return_tensors=__magic_name__			, **__magic_name__				)

														lowercase    :      List[str]       = encoded_candidates.get("""input_ids"""				)
														lowercase    :      List[str]       = encoded_candidates.get("""attention_mask"""				)
														lowercase    :      List[str]       = encoded_candidates.get("""token_type_ids"""				)

														if encoded_input_ids is not None:
																		output_data["input_ids"].append(__magic_name__				)
														if encoded_attention_mask is not None:
																		output_data["attention_mask"].append(__magic_name__				)
														if encoded_token_type_ids is not None:
																		output_data["token_type_ids"].append(__magic_name__				)

										lowercase    :      List[str]       = {key: item for key, item in output_data.items() if len(__magic_name__				) != 0}

										return BatchEncoding(__magic_name__			, tensor_type=__magic_name__				)






						def      __snake_case      (		self  :Union[str, Any]			, __magic_name__  :str			, __magic_name__  :Optional[int]=None				)      ->Union[str, Any]:
										lowercase    :      Dict       = [self.cls_token_id] + token_ids_a + [self.sep_token_id]

										if token_ids_a:
														output += token_ids_a + [self.sep_token_id]

										return output






						def      __snake_case      (		self  :List[Any]			, __magic_name__  :List[int]			, __magic_name__  :Optional[List[int]] = None				)      ->List[int]:
										lowercase    :      List[Any]       = [self.sep_token_id]
										lowercase    :      List[str]       = [self.cls_token_id]
										if token_ids_a is None:
														return len(cls + token_ids_a + sep				) * [0]
										return len(cls + token_ids_a + sep				) * [0] + len(token_ids_a + sep				) * [1]






						def      __snake_case      (		self  :Union[str, Any]			, __magic_name__  :str			, __magic_name__  :Optional[str] = None				)      ->Tuple[str]:
										lowercase    :      Dict       = self._tokenizer.model.save(__magic_name__			, name=__magic_name__				)
										return tuple(__magic_name__				)








"""simple docstring"""







import copy
import inspect
import unittest

from transformers import PretrainedConfig, SwiftFormerConfig
from transformers.testing_utils import (
    require_torch,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import cached_property, is_torch_available, is_vision_available

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


if is_torch_available():
				import torch
				from torch import nn

				from transformers import SwiftFormerForImageClassification, SwiftFormerModel
				from transformers.models.swiftformer.modeling_swiftformer import SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
				from PIL import Image

				from transformers import ViTImageProcessor


class      UpperCamelCase		:






						def __init__(		self  :Union[str, Any]			, __magic_name__  :Any			, __magic_name__  :List[str]=13			, __magic_name__  :Any=3			, __magic_name__  :str=True			, __magic_name__  :int=True			, __magic_name__  :Dict=0.1			, __magic_name__  :Optional[int]=0.1			, __magic_name__  :List[str]=224			, __magic_name__  :Any=1_000			, __magic_name__  :int=[3, 3, 6, 4]			, __magic_name__  :int=[48, 56, 112, 220]			, )      ->int:
										lowercase    :      Dict       = parent
										lowercase    :      List[str]       = batch_size
										lowercase    :      Union[str, Any]       = num_channels
										lowercase    :      Dict       = is_training
										lowercase    :      Any       = use_labels
										lowercase    :      Dict       = hidden_dropout_prob
										lowercase    :      Union[str, Any]       = attention_probs_dropout_prob
										lowercase    :      Optional[Any]       = num_labels
										lowercase    :      Optional[Any]       = image_size
										lowercase    :      Optional[int]       = layer_depths
										lowercase    :      Optional[int]       = embed_dims






						def      __snake_case      (		self  :Tuple				)      ->str:
										lowercase    :      Dict       = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)

										lowercase    :      Optional[Any]       = None
										if self.use_labels:
														lowercase    :      str       = ids_tensor([self.batch_size]			, self.num_labels				)

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

										return config, pixel_values, labels






						def      __snake_case      (		self  :Dict				)      ->Union[str, Any]:
										return SwiftFormerConfig(
										    depths=self.layer_depths			, embed_dims=self.embed_dims			, mlp_ratio=4			, downsamples=[True, True, True, True]			, hidden_act="""gelu"""			, num_labels=self.num_labels			, down_patch_size=3			, down_stride=2			, down_pad=1			, drop_rate=0.0			, drop_path_rate=0.0			, use_layer_scale=__magic_name__			, layer_scale_init_value=1E-5			, )






						def      __snake_case      (		self  :List[str]			, __magic_name__  :Union[str, Any]			, __magic_name__  :Dict			, __magic_name__  :Dict				)      ->Optional[Any]:
										lowercase    :      Optional[int]       = SwiftFormerModel(config=__magic_name__				)
										model.to(__magic_name__				)
										model.eval()
										lowercase    :      Union[str, Any]       = model(__magic_name__				)
										self.parent.assertEqual(result.last_hidden_state.shape			, (self.batch_size, self.embed_dims[-1], 7, 7)				)






						def      __snake_case      (		self  :Any			, __magic_name__  :Any			, __magic_name__  :Tuple			, __magic_name__  :Any				)      ->Union[str, Any]:
										lowercase    :      Optional[int]       = self.num_labels
										lowercase    :      List[str]       = SwiftFormerForImageClassification(__magic_name__				)
										model.to(__magic_name__				)
										model.eval()
										lowercase    :      List[Any]       = model(__magic_name__			, labels=__magic_name__				)
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.num_labels)				)

										lowercase    :      Union[str, Any]       = SwiftFormerForImageClassification(__magic_name__				)
										model.to(__magic_name__				)
										model.eval()

										lowercase    :      Tuple       = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]				)
										lowercase    :      Union[str, Any]       = model(__magic_name__				)
										self.parent.assertEqual(result.logits.shape			, (self.batch_size, self.num_labels)				)






						def      __snake_case      (		self  :Union[str, Any]				)      ->Optional[int]:
										((lowercase)      ,      (lowercase)      ,      (lowercase))    :      Optional[Any]       = self.prepare_config_and_inputs()
										lowercase    :      Any       = {"""pixel_values""": pixel_values}
										return config, inputs_dict


@require_torch
class      UpperCamelCase		(__snake_case						,						__snake_case						,						unittest.TestCase     ):
						_SCREAMING_SNAKE_CASE  : List[str]												=		(SwiftFormerModel, SwiftFormerForImageClassification) if is_torch_available() else ()
						_SCREAMING_SNAKE_CASE  : Any												=		(
						    {"""feature-extraction""": SwiftFormerModel, """image-classification""": SwiftFormerForImageClassification}
						    if is_torch_available()
						    else {}
						)

						_SCREAMING_SNAKE_CASE  : Optional[Any]												=		False
						_SCREAMING_SNAKE_CASE  : Optional[Any]												=		False
						_SCREAMING_SNAKE_CASE  : List[str]												=		False
						_SCREAMING_SNAKE_CASE  : Tuple												=		False
						_SCREAMING_SNAKE_CASE  : List[Any]												=		False






						def      __snake_case      (		self  :Union[str, Any]				)      ->Optional[int]:
										lowercase    :      Optional[Any]       = SwiftFormerModelTester(self				)
										lowercase    :      List[str]       = ConfigTester(
										    self			, config_class=__magic_name__			, has_text_modality=__magic_name__			, hidden_size=37			, num_attention_heads=12			, num_hidden_layers=12			, )






						def      __snake_case      (		self  :Union[str, Any]				)      ->Union[str, Any]:
										self.config_tester.run_common_tests()






						@unittest.skip(reason="""SwiftFormer does not use inputs_embeds"""				)
						def      __snake_case      (		self  :Any				)      ->Any:
										pass






						def      __snake_case      (		self  :Optional[int]				)      ->List[Any]:
										lowercase      ,      lowercase    :      Optional[Any]       = self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
														lowercase    :      Dict       = model_class(__magic_name__				)
														lowercase    :      int       = model.get_output_embeddings()
														self.assertTrue(x is None or isinstance(__magic_name__			, nn.Linear				)				)






						def      __snake_case      (		self  :Any				)      ->List[str]:
										lowercase      ,      lowercase    :      int       = self.model_tester.prepare_config_and_inputs_for_common()

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

														lowercase    :      Any       = ["""pixel_values"""]
														self.assertListEqual(arg_names[:1]			, __magic_name__				)






						def      __snake_case      (		self  :Union[str, Any]				)      ->Optional[int]:
										lowercase    :      Union[str, Any]       = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(*__magic_name__				)






						def      __snake_case      (		self  :List[Any]				)      ->List[str]:
										lowercase    :      Any       = self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_image_classification(*__magic_name__				)






						@slow
						def      __snake_case      (		self  :Any				)      ->Dict:
										for model_name in SWIFTFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowercase    :      List[Any]       = SwiftFormerModel.from_pretrained(__magic_name__				)
														self.assertIsNotNone(__magic_name__				)






						@unittest.skip(reason="""SwiftFormer does not output attentions"""				)
						def      __snake_case      (		self  :List[str]				)      ->Optional[Any]:
										pass






						def      __snake_case      (		self  :List[str]				)      ->List[str]:
										def check_hidden_states_output(__magic_name__  :str			, __magic_name__  :Optional[Any]			, __magic_name__  :Tuple				):
														lowercase    :      Optional[Any]       = model_class(__magic_name__				)
														model.to(__magic_name__				)
														model.eval()

														with torch.no_grad():
																		lowercase    :      Dict       = model(**self._prepare_for_class(__magic_name__			, __magic_name__				)				)

														lowercase    :      Tuple       = outputs.hidden_states

														lowercase    :      List[str]       = 8
														self.assertEqual(len(__magic_name__				)			, __magic_name__				)  # TODO

														# SwiftFormer's feature maps are of shape (batch_size, embed_dims, height, width)
														# with the width and height being successively divided by 2, after every 2 blocks
														for i in range(len(__magic_name__				)				):
																		self.assertEqual(
																		    hidden_states[i].shape			, torch.Size(
																		        [
																		            self.model_tester.batch_size,
																		            self.model_tester.embed_dims[i // 2],
																		            (self.model_tester.image_size // 4) // 2 ** (i // 2),
																		            (self.model_tester.image_size // 4) // 2 ** (i // 2),
																		        ]				)			, )

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

										for model_class in self.all_model_classes:
														lowercase    :      Optional[int]       = True
														check_hidden_states_output(__magic_name__			, __magic_name__			, __magic_name__				)

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

														check_hidden_states_output(__magic_name__			, __magic_name__			, __magic_name__				)






						def      __snake_case      (		self  :Any				)      ->Optional[int]:
										def _config_zero_init(__magic_name__  :List[Any]				):
														lowercase    :      Tuple       = copy.deepcopy(__magic_name__				)
														for key in configs_no_init.__dict__.keys():
																		if "_range" in key or "_std" in key or "initializer_factor" in key or "layer_scale" in key:
																						setattr(__magic_name__			, __magic_name__			, 1E-10				)
																		if isinstance(getattr(__magic_name__			, __magic_name__			, __magic_name__				)			, __magic_name__				):
																						lowercase    :      Any       = _config_zero_init(getattr(__magic_name__			, __magic_name__				)				)
																						setattr(__magic_name__			, __magic_name__			, __magic_name__				)
														return configs_no_init

										lowercase      ,      lowercase    :      Dict       = self.model_tester.prepare_config_and_inputs_for_common()

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






						@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""				)
						def      __snake_case      (		self  :Any				)      ->Any:
										pass


def      UpperCamelCase       ( )     ->       Tuple:
				lowercase    :      int       = Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""" )
				return image


@require_torch
@require_vision
class      UpperCamelCase		(unittest.TestCase     ):






						@cached_property
						def      __snake_case      (		self  :Optional[Any]				)      ->List[str]:
										return ViTImageProcessor.from_pretrained("""MBZUAI/swiftformer-xs"""				) if is_vision_available() else None






						@slow
						def      __snake_case      (		self  :Optional[int]				)      ->int:
										lowercase    :      Union[str, Any]       = SwiftFormerForImageClassification.from_pretrained("""MBZUAI/swiftformer-xs"""				).to(__magic_name__				)

										lowercase    :      Any       = self.default_image_processor
										lowercase    :      str       = prepare_img()
										lowercase    :      Tuple       = image_processor(images=__magic_name__			, return_tensors="""pt"""				).to(__magic_name__				)

										# forward pass
										with torch.no_grad():
														lowercase    :      Union[str, Any]       = model(**__magic_name__				)

										# verify the logits
										lowercase    :      Optional[int]       = torch.Size((1, 1_000)				)
										self.assertEqual(outputs.logits.shape			, __magic_name__				)

										lowercase    :      Optional[Any]       = torch.tensor([[-2.17_03E00, 2.11_07E00, -2.08_11E00]]				).to(__magic_name__				)
										self.assertTrue(torch.allclose(outputs.logits[0, :3]			, __magic_name__			, atol=1E-4				)				)
