



"""simple docstring"""
import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
					import torch

if is_vision_available():
					from PIL import Image

					from transformers import BeitImageProcessor




class 			snake_case_						(						unittest.TestCase				):






							def __init__(   self			,     a_			,     a_=7			,     a_=3			,     a_=1_8			,     a_=3_0			,     a_=4_0_0			,     a_=True			,     a_=None			,     a_=True			,     a_=None			,     a_=True			,     a_=[0.5, 0.5, 0.5]			,     a_=[0.5, 0.5, 0.5]			,     a_=False			,     ):
									a_     :      Union[str, Any] 						=     size if size is not None else {"height": 2_0, "width": 2_0}
									a_     :      Any 						=     crop_size if crop_size is not None else {"height": 1_8, "width": 1_8}
									a_     :      Any 						=     parent
									a_     :      Dict 						=     batch_size
									a_     :      int 						=     num_channels
									a_     :      int 						=     image_size
									a_     :      Optional[int] 						=     min_resolution
									a_     :      Optional[int] 						=     max_resolution
									a_     :      List[Any] 						=     do_resize
									a_     :      Any 						=     size
									a_     :      Optional[Any] 						=     do_center_crop
									a_     :      Any 						=     crop_size
									a_     :      List[Any] 						=     do_normalize
									a_     :      Union[str, Any] 						=     image_mean
									a_     :      Optional[Any] 						=     image_std
									a_     :      Dict 						=     do_reduce_labels






							def snake_case_  (   self      ):
									return {
									    "do_resize": self.do_resize,
									    "size": self.size,
									    "do_center_crop": self.do_center_crop,
									    "crop_size": self.crop_size,
									    "do_normalize": self.do_normalize,
									    "image_mean": self.image_mean,
									    "image_std": self.image_std,
									    "do_reduce_labels": self.do_reduce_labels,
									}



def        lowerCAmelCase_						(							)			->      Dict:
		a_     :      List[Any] 						=     load_dataset("hf-internal-testing/fixtures_ade20k", split="test"	)

		a_     :      Optional[int] 						=     Image.open(dataset[0]["file"]	)
		a_     :      Optional[Any] 						=     Image.open(dataset[1]["file"]	)

		return image, map



def        lowerCAmelCase_						(							)			->      str:
		a_     :      Dict 						=     load_dataset("hf-internal-testing/fixtures_ade20k", split="test"	)

		a_     :      List[str] 						=     Image.open(ds[0]["file"]	)
		a_     :      Optional[int] 						=     Image.open(ds[1]["file"]	)
		a_     :      Dict 						=     Image.open(ds[2]["file"]	)
		a_     :      Tuple 						=     Image.open(ds[3]["file"]	)

		return [imagea, imagea], [mapa, mapa]




@require_torch
@require_vision
class 			snake_case_						(						a_     ,unittest.TestCase				):
							__lowerCAmelCase    					=					BeitImageProcessor if is_vision_available() else None






							def snake_case_  (   self      ):
									a_     :      Any 						=     BeitImageProcessingTester(self      )






							@property
							def snake_case_  (   self      ):
									return self.image_processor_tester.prepare_image_processor_dict()






							def snake_case_  (   self      ):
									a_     :      Optional[Any] 						=     self.image_processing_class(**self.image_processor_dict      )
									self.assertTrue(hasattr(a_			,     "do_resize"      )      )
									self.assertTrue(hasattr(a_			,     "size"      )      )
									self.assertTrue(hasattr(a_			,     "do_center_crop"      )      )
									self.assertTrue(hasattr(a_			,     "center_crop"      )      )
									self.assertTrue(hasattr(a_			,     "do_normalize"      )      )
									self.assertTrue(hasattr(a_			,     "image_mean"      )      )
									self.assertTrue(hasattr(a_			,     "image_std"      )      )






							def snake_case_  (   self      ):
									a_     :      Union[str, Any] 						=     self.image_processing_class.from_dict(self.image_processor_dict      )
									self.assertEqual(image_processor.size			,     {"height": 2_0, "width": 2_0}      )
									self.assertEqual(image_processor.crop_size			,     {"height": 1_8, "width": 1_8}      )
									self.assertEqual(image_processor.do_reduce_labels			,     a_      )

									a_     :      List[str] 						=     self.image_processing_class.from_dict(
									    self.image_processor_dict			,     size=4_2			,     crop_size=8_4			,     reduce_labels=a_      )
									self.assertEqual(image_processor.size			,     {"height": 4_2, "width": 4_2}      )
									self.assertEqual(image_processor.crop_size			,     {"height": 8_4, "width": 8_4}      )
									self.assertEqual(image_processor.do_reduce_labels			,     a_      )






							def snake_case_  (   self      ):
									pass






							def snake_case_  (   self      ):
									# Initialize image_processing
									a_     :      Any 						=     self.image_processing_class(**self.image_processor_dict      )
									# create random PIL images
									a_     :      Dict 						=     prepare_image_inputs(self.image_processor_tester			,     equal_resolution=a_      )
									for image in image_inputs:
											self.assertIsInstance(a_			,     Image.Image      )

									# Test not batched input
									a_     :      int 						=     image_processing(image_inputs[0]			,     return_tensors="pt"      ).pixel_values
									self.assertEqual(
									    encoded_images.shape			,     (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )

									# Test batched
									a_     :      str 						=     image_processing(a_			,     return_tensors="pt"      ).pixel_values
									self.assertEqual(
									    encoded_images.shape			,     (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )






							def snake_case_  (   self      ):
									# Initialize image_processing
									a_     :      List[Any] 						=     self.image_processing_class(**self.image_processor_dict      )
									# create random numpy tensors
									a_     :      int 						=     prepare_image_inputs(self.image_processor_tester			,     equal_resolution=a_			,     numpify=a_      )
									for image in image_inputs:
											self.assertIsInstance(a_			,     np.ndarray      )

									# Test not batched input
									a_     :      Tuple 						=     image_processing(image_inputs[0]			,     return_tensors="pt"      ).pixel_values
									self.assertEqual(
									    encoded_images.shape			,     (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )

									# Test batched
									a_     :      Union[str, Any] 						=     image_processing(a_			,     return_tensors="pt"      ).pixel_values
									self.assertEqual(
									    encoded_images.shape			,     (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )






							def snake_case_  (   self      ):
									# Initialize image_processing
									a_     :      Dict 						=     self.image_processing_class(**self.image_processor_dict      )
									# create random PyTorch tensors
									a_     :      Dict 						=     prepare_image_inputs(self.image_processor_tester			,     equal_resolution=a_			,     torchify=a_      )
									for image in image_inputs:
											self.assertIsInstance(a_			,     torch.Tensor      )

									# Test not batched input
									a_     :      int 						=     image_processing(image_inputs[0]			,     return_tensors="pt"      ).pixel_values
									self.assertEqual(
									    encoded_images.shape			,     (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )

									# Test batched
									a_     :      Tuple 						=     image_processing(a_			,     return_tensors="pt"      ).pixel_values
									self.assertEqual(
									    encoded_images.shape			,     (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )






							def snake_case_  (   self      ):
									# Initialize image_processing
									a_     :      Dict 						=     self.image_processing_class(**self.image_processor_dict      )
									# create random PyTorch tensors
									a_     :      Optional[Any] 						=     prepare_image_inputs(self.image_processor_tester			,     equal_resolution=a_			,     torchify=a_      )
									a_     :      List[Any] 						=     []
									for image in image_inputs:
											self.assertIsInstance(a_			,     torch.Tensor      )
											maps.append(torch.zeros(image.shape[-2:]      ).long()      )

									# Test not batched input
									a_     :      int 						=     image_processing(image_inputs[0]			,     maps[0]			,     return_tensors="pt"      )
									self.assertEqual(
									    encoding["pixel_values"].shape			,     (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )
									self.assertEqual(
									    encoding["labels"].shape			,     (
									        1,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )
									self.assertEqual(encoding["labels"].dtype			,     torch.long      )
									self.assertTrue(encoding["labels"].min().item() >= 0      )
									self.assertTrue(encoding["labels"].max().item() <= 2_5_5      )

									# Test batched
									a_     :      Dict 						=     image_processing(a_			,     a_			,     return_tensors="pt"      )
									self.assertEqual(
									    encoding["pixel_values"].shape			,     (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )
									self.assertEqual(
									    encoding["labels"].shape			,     (
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )
									self.assertEqual(encoding["labels"].dtype			,     torch.long      )
									self.assertTrue(encoding["labels"].min().item() >= 0      )
									self.assertTrue(encoding["labels"].max().item() <= 2_5_5      )

									# Test not batched input (PIL images)
									a_						,				a_     :      Optional[Any] 						=     prepare_semantic_single_inputs()

									a_     :      Tuple 						=     image_processing(a_			,     a_			,     return_tensors="pt"      )
									self.assertEqual(
									    encoding["pixel_values"].shape			,     (
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )
									self.assertEqual(
									    encoding["labels"].shape			,     (
									        1,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )
									self.assertEqual(encoding["labels"].dtype			,     torch.long      )
									self.assertTrue(encoding["labels"].min().item() >= 0      )
									self.assertTrue(encoding["labels"].max().item() <= 2_5_5      )

									# Test batched input (PIL images)
									a_						,				a_     :      Tuple 						=     prepare_semantic_batch_inputs()

									a_     :      Union[str, Any] 						=     image_processing(a_			,     a_			,     return_tensors="pt"      )
									self.assertEqual(
									    encoding["pixel_values"].shape			,     (
									        2,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )
									self.assertEqual(
									    encoding["labels"].shape			,     (
									        2,
									        self.image_processor_tester.crop_size["height"],
									        self.image_processor_tester.crop_size["width"],
									    )			,     )
									self.assertEqual(encoding["labels"].dtype			,     torch.long      )
									self.assertTrue(encoding["labels"].min().item() >= 0      )
									self.assertTrue(encoding["labels"].max().item() <= 2_5_5      )






							def snake_case_  (   self      ):
									# Initialize image_processing
									a_     :      int 						=     self.image_processing_class(**self.image_processor_dict      )

									# ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
									a_						,				a_     :      List[Any] 						=     prepare_semantic_single_inputs()
									a_     :      Optional[int] 						=     image_processing(a_			,     a_			,     return_tensors="pt"      )
									self.assertTrue(encoding["labels"].min().item() >= 0      )
									self.assertTrue(encoding["labels"].max().item() <= 1_5_0      )

									a_     :      Optional[int] 						=     True
									a_     :      Optional[Any] 						=     image_processing(a_			,     a_			,     return_tensors="pt"      )
									self.assertTrue(encoding["labels"].min().item() >= 0      )
									self.assertTrue(encoding["labels"].max().item() <= 2_5_5      )




"""simple docstring"""
from ...configuration_utils import PretrainedConfig
from ...utils import logging


SCREAMING_SNAKE_CASE_				= logging.get_logger(__name__)

SCREAMING_SNAKE_CASE_				= {
    """google/realm-cc-news-pretrained-embedder""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-embedder/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-encoder""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-encoder/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-scorer""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-scorer/resolve/main/config.json"""
    ),
    """google/realm-cc-news-pretrained-openqa""": (
        """https://huggingface.co/google/realm-cc-news-pretrained-openqa/aresolve/main/config.json"""
    ),
    """google/realm-orqa-nq-openqa""": """https://huggingface.co/google/realm-orqa-nq-openqa/resolve/main/config.json""",
    """google/realm-orqa-nq-reader""": """https://huggingface.co/google/realm-orqa-nq-reader/resolve/main/config.json""",
    """google/realm-orqa-wq-openqa""": """https://huggingface.co/google/realm-orqa-wq-openqa/resolve/main/config.json""",
    """google/realm-orqa-wq-reader""": """https://huggingface.co/google/realm-orqa-wq-reader/resolve/main/config.json""",
    # See all REALM models at https://huggingface.co/models?filter=realm
}




class 			snake_case_						(						a_				):
							__lowerCAmelCase    					=					"realm"






							def __init__(   self			,     a_=3_0_5_2_2			,     a_=7_6_8			,     a_=1_2_8			,     a_=1_2			,     a_=1_2			,     a_=8			,     a_=3_0_7_2			,     a_="gelu_new"			,     a_=0.1			,     a_=0.1			,     a_=5_1_2			,     a_=2			,     a_=0.02			,     a_=1e-12			,     a_=2_5_6			,     a_=1_0			,     a_=1e-3			,     a_=5			,     a_=3_2_0			,     a_=1_3_3_5_3_7_1_8			,     a_=5_0_0_0			,     a_=1			,     a_=0			,     a_=2			,     **a_			,     ):
									super().__init__(pad_token_id=a_			,     bos_token_id=a_			,     eos_token_id=a_			,     **a_      )

									# Common config
									a_     :      Optional[int] 						=     vocab_size
									a_     :      List[Any] 						=     max_position_embeddings
									a_     :      Optional[Any] 						=     hidden_size
									a_     :      Optional[Any] 						=     retriever_proj_size
									a_     :      List[str] 						=     num_hidden_layers
									a_     :      List[Any] 						=     num_attention_heads
									a_     :      Tuple 						=     num_candidates
									a_     :      str 						=     intermediate_size
									a_     :      Optional[int] 						=     hidden_act
									a_     :      List[str] 						=     hidden_dropout_prob
									a_     :      List[str] 						=     attention_probs_dropout_prob
									a_     :      Tuple 						=     initializer_range
									a_     :      Tuple 						=     type_vocab_size
									a_     :      str 						=     layer_norm_eps

									# Reader config
									a_     :      str 						=     span_hidden_size
									a_     :      Union[str, Any] 						=     max_span_width
									a_     :      Tuple 						=     reader_layer_norm_eps
									a_     :      List[Any] 						=     reader_beam_size
									a_     :      str 						=     reader_seq_len

									# Retrieval config
									a_     :      str 						=     num_block_records
									a_     :      int 						=     searcher_beam_size