




'''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 SegformerImageProcessor






class     UpperCamelCase_     (unittest.TestCase     ):







						"""simple docstring"""


						def __init__(     self      :       Dict							, _lowerCamelCase      :       List[str]							, _lowerCamelCase      :       int=7							, _lowerCamelCase      :       Tuple=3							, _lowerCamelCase      :       int=30							, _lowerCamelCase      :       int=400							, _lowerCamelCase      :       Optional[Any]=True							, _lowerCamelCase      :       Tuple=None							, _lowerCamelCase      :       Optional[Any]=True							, _lowerCamelCase      :       Optional[Any]=[0.5, 0.5, 0.5]							, _lowerCamelCase      :       int=[0.5, 0.5, 0.5]							, _lowerCamelCase      :       List[Any]=False							, ):





											"""simple docstring"""







											A_ :     Optional[Any] =							size if size is not None else {"height": 30, "width": 30}
											A_ :     str =							parent
											A_ :     Tuple =							batch_size
											A_ :     int =							num_channels
											A_ :     List[str] =							min_resolution
											A_ :     int =							max_resolution
											A_ :     str =							do_resize
											A_ :     Optional[Any] =							size
											A_ :     Dict =							do_normalize
											A_ :     Union[str, Any] =							image_mean
											A_ :     List[Any] =							image_std
											A_ :     int =							do_reduce_labels







						def       _a    (     self      :       List[str]    ):





											"""simple docstring"""







											return {
											    "do_resize": self.do_resize,
											    "size": self.size,
											    "do_normalize": self.do_normalize,
											    "image_mean": self.image_mean,
											    "image_std": self.image_std,
											    "do_reduce_labels": self.do_reduce_labels,
											}






def 						snake_case__    ( )   ->   Optional[Any]:
					A_ :     Optional[int] =							load_dataset('''hf-internal-testing/fixtures_ade20k'''			,		split='''test'''			)

					A_ :     List[str] =							Image.open(dataset[0]['''file''']			)
					A_ :     List[Any] =							Image.open(dataset[1]['''file''']			)

					return image, map




def 						snake_case__    ( )   ->   Tuple:
					A_ :     List[Any] =							load_dataset('''hf-internal-testing/fixtures_ade20k'''			,		split='''test'''			)

					A_ :     str =							Image.open(dataset[0]['''file''']			)
					A_ :     List[str] =							Image.open(dataset[1]['''file''']			)
					A_ :     int =							Image.open(dataset[2]['''file''']			)
					A_ :     str =							Image.open(dataset[3]['''file''']			)

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






@require_torch
@require_vision
class     UpperCamelCase_     (_a,      unittest.TestCase     ):







						"""simple docstring"""


						_lowerCAmelCase    						=					SegformerImageProcessor if is_vision_available() else None







						def       _a    (     self      :       Optional[int]    ):





											"""simple docstring"""







											A_ :     List[Any] =							SegformerImageProcessingTester(self    )







						@property
						def       _a    (     self      :       List[str]    ):





											"""simple docstring"""







											return self.image_processor_tester.prepare_image_processor_dict()







						def       _a    (     self      :       List[Any]    ):





											"""simple docstring"""







											A_ :     str =							self.image_processing_class(**self.image_processor_dict    )
											self.assertTrue(hasattr(lowerCamelCase__							, '''do_resize'''    )    )
											self.assertTrue(hasattr(lowerCamelCase__							, '''size'''    )    )
											self.assertTrue(hasattr(lowerCamelCase__							, '''do_normalize'''    )    )
											self.assertTrue(hasattr(lowerCamelCase__							, '''image_mean'''    )    )
											self.assertTrue(hasattr(lowerCamelCase__							, '''image_std'''    )    )
											self.assertTrue(hasattr(lowerCamelCase__							, '''do_reduce_labels'''    )    )







						def       _a    (     self      :       Dict    ):





											"""simple docstring"""







											A_ :     int =							self.image_processing_class.from_dict(self.image_processor_dict    )
											self.assertEqual(image_processor.size							, {'''height''': 30, '''width''': 30}    )
											self.assertEqual(image_processor.do_reduce_labels							, lowerCamelCase__    )

											A_ :     List[Any] =							self.image_processing_class.from_dict(self.image_processor_dict							, size=42							, reduce_labels=lowerCamelCase__    )
											self.assertEqual(image_processor.size							, {'''height''': 42, '''width''': 42}    )
											self.assertEqual(image_processor.do_reduce_labels							, lowerCamelCase__    )







						def       _a    (     self      :       int    ):





											"""simple docstring"""







											pass







						def       _a    (     self      :       Optional[Any]    ):





											"""simple docstring"""







											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=lowerCamelCase__    )
											for image in image_inputs:
																self.assertIsInstance(lowerCamelCase__							, Image.Image    )

											# Test not batched input
											A_ :     Optional[Any] =							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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )

											# Test batched
											A_ :     Any =							image_processing(lowerCamelCase__							, 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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )







						def       _a    (     self      :       Tuple    ):





											"""simple docstring"""







											A_ :     Any =							self.image_processing_class(**self.image_processor_dict    )
											# create random numpy tensors
											A_ :     str =							prepare_image_inputs(self.image_processor_tester							, equal_resolution=lowerCamelCase__							, numpify=lowerCamelCase__    )
											for image in image_inputs:
																self.assertIsInstance(lowerCamelCase__							, 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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )

											# Test batched
											A_ :     str =							image_processing(lowerCamelCase__							, 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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )







						def       _a    (     self      :       Tuple    ):





											"""simple docstring"""







											A_ :     Tuple =							self.image_processing_class(**self.image_processor_dict    )
											# create random PyTorch tensors
											A_ :     Optional[Any] =							prepare_image_inputs(self.image_processor_tester							, equal_resolution=lowerCamelCase__							, torchify=lowerCamelCase__    )
											for image in image_inputs:
																self.assertIsInstance(lowerCamelCase__							, 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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )

											# Test batched
											A_ :     Any =							image_processing(lowerCamelCase__							, 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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )







						def       _a    (     self      :       Optional[Any]    ):





											"""simple docstring"""







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

											# Test not batched input
											A_ :     Union[str, Any] =							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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )
											self.assertEqual(
											    encoding['''labels'''].shape							, (
											        1,
											        self.image_processor_tester.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )
											self.assertEqual(encoding['''labels'''].dtype							, torch.long    )
											self.assertTrue(encoding['''labels'''].min().item() >= 0    )
											self.assertTrue(encoding['''labels'''].max().item() <= 255    )

											# Test batched
											A_ :     int =							image_processing(lowerCamelCase__							, lowerCamelCase__							, 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.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )
											self.assertEqual(
											    encoding['''labels'''].shape							, (
											        self.image_processor_tester.batch_size,
											        self.image_processor_tester.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )
											self.assertEqual(encoding['''labels'''].dtype							, torch.long    )
											self.assertTrue(encoding['''labels'''].min().item() >= 0    )
											self.assertTrue(encoding['''labels'''].max().item() <= 255    )

											# Test not batched input (PIL images)
											A_ :     List[Any] =							prepare_semantic_single_inputs()

											A_ :     Union[str, Any] =							image_processing(lowerCamelCase__							, lowerCamelCase__							, return_tensors='''pt'''    )
											self.assertEqual(
											    encoding['''pixel_values'''].shape							, (
											        1,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )
											self.assertEqual(
											    encoding['''labels'''].shape							, (
											        1,
											        self.image_processor_tester.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )
											self.assertEqual(encoding['''labels'''].dtype							, torch.long    )
											self.assertTrue(encoding['''labels'''].min().item() >= 0    )
											self.assertTrue(encoding['''labels'''].max().item() <= 255    )

											# Test batched input (PIL images)
											A_ :     Any =							prepare_semantic_batch_inputs()

											A_ :     List[Any] =							image_processing(lowerCamelCase__							, lowerCamelCase__							, return_tensors='''pt'''    )
											self.assertEqual(
											    encoding['''pixel_values'''].shape							, (
											        2,
											        self.image_processor_tester.num_channels,
											        self.image_processor_tester.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )
											self.assertEqual(
											    encoding['''labels'''].shape							, (
											        2,
											        self.image_processor_tester.size['''height'''],
											        self.image_processor_tester.size['''width'''],
											    )							, )
											self.assertEqual(encoding['''labels'''].dtype							, torch.long    )
											self.assertTrue(encoding['''labels'''].min().item() >= 0    )
											self.assertTrue(encoding['''labels'''].max().item() <= 255    )







						def       _a    (     self      :       Dict    ):





											"""simple docstring"""







											A_ :     Dict =							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_ :     str =							prepare_semantic_single_inputs()
											A_ :     Union[str, Any] =							image_processing(lowerCamelCase__							, lowerCamelCase__							, return_tensors='''pt'''    )
											self.assertTrue(encoding['''labels'''].min().item() >= 0    )
											self.assertTrue(encoding['''labels'''].max().item() <= 150    )

											A_ :     Tuple =							True
											A_ :     str =							image_processing(lowerCamelCase__							, lowerCamelCase__							, return_tensors='''pt'''    )
											self.assertTrue(encoding['''labels'''].min().item() >= 0    )
											self.assertTrue(encoding['''labels'''].max().item() <= 255    )









'''simple docstring'''




import logging
import random

import ray

from transformers import RagConfig, RagRetriever, RagTokenizer
from transformers.models.rag.retrieval_rag import CustomHFIndex


snake_case__          =						logging.getLogger(__name__)






class     UpperCamelCase_     :







						"""simple docstring"""


						def __init__(     self      :       Tuple    ):





											"""simple docstring"""







											A_ :     Optional[int] =							False







						def       _a    (     self      :       Any							, _lowerCamelCase      :       List[str]							, _lowerCamelCase      :       Any							, _lowerCamelCase      :       str							, _lowerCamelCase      :       int    ):





											"""simple docstring"""







											if not self.initialized:
																A_ :     Optional[Any] =							RagRetriever(
																    _lowerCamelCase							, question_encoder_tokenizer=_lowerCamelCase							, generator_tokenizer=_lowerCamelCase							, index=_lowerCamelCase							, init_retrieval=_lowerCamelCase							, )
																A_ :     Optional[Any] =							True







						def       _a    (     self      :       int    ):





											"""simple docstring"""







											self.retriever.index.init_index()







						def       _a    (     self      :       Any    ):





											"""simple docstring"""







											del self.retriever
											A_ :     List[Any] =							False







						def       _a    (     self      :       Optional[int]							, _lowerCamelCase      :       Union[str, Any]							, _lowerCamelCase      :       Union[str, Any]    ):





											"""simple docstring"""







											A_ ,A_ :     Dict =							self.retriever._main_retrieve(_lowerCamelCase							, _lowerCamelCase    )
											A_ :     int =							self.retriever.index.get_doc_dicts(_lowerCamelCase    )
											return doc_ids, retrieved_doc_embeds, doc_dicts






class     UpperCamelCase_     (a__     ):







						"""simple docstring"""


						def __init__(     self      :       int							, _lowerCamelCase      :       Optional[Any]							, _lowerCamelCase      :       Dict							, _lowerCamelCase      :       Optional[int]							, _lowerCamelCase      :       Dict							, _lowerCamelCase      :       Tuple=None    ):





											"""simple docstring"""







											if index is not None and index.is_initialized() and len(_lowerCamelCase    ) > 0:
																raise ValueError(
																    '''When using Ray for distributed fine-tuning, '''
																    '''you\'ll need to provide the paths instead, '''
																    '''as the dataset and the index are loaded '''
																    '''separately. More info in examples/rag/use_own_knowledge_dataset.py '''    )

											super().__init__(
											    _lowerCamelCase							, question_encoder_tokenizer=_lowerCamelCase							, generator_tokenizer=_lowerCamelCase							, index=_lowerCamelCase							, init_retrieval=_lowerCamelCase							, )

											A_ :     Tuple =							retrieval_workers
											A_ :     Any =							question_encoder_tokenizer
											A_ :     Dict =							generator_tokenizer
											if len(self.retrieval_workers    ) > 0:
																ray.get(
																    [
																        worker.create_rag_retriever.remote(_lowerCamelCase							, _lowerCamelCase							, _lowerCamelCase							, _lowerCamelCase    )
																        for worker in self.retrieval_workers
																    ]    )







						def       _a    (     self      :       Dict    ):





											"""simple docstring"""







											logger.info('''initializing retrieval'''    )

											if len(self.retrieval_workers    ) > 0:
																ray.get([worker.init_retrieval.remote() for worker in self.retrieval_workers]    )
											else:
																# Non-distributed training. Load index into this same process.
																self.index.init_index()







						def       _a    (     self      :       Dict							, _lowerCamelCase      :       Tuple							, _lowerCamelCase      :       int    ):





											"""simple docstring"""







											if len(self.retrieval_workers    ) > 0:
																# Select a random retrieval actor.
																A_ :     Optional[int] =							self.retrieval_workers[random.randint(0							, len(self.retrieval_workers    ) - 1    )]
																A_ ,A_ ,A_ :     Union[str, Any] =							ray.get(
																    random_worker.retrieve.remote(_lowerCamelCase							, _lowerCamelCase    )    )
											else:
																A_ ,A_ :     List[str] =							self._main_retrieve(_lowerCamelCase							, _lowerCamelCase    )
																A_ :     Union[str, Any] =							self.index.get_doc_dicts(_lowerCamelCase    )
											return retrieved_doc_embeds, doc_ids, doc_dicts







						@classmethod
						def       _a    (     cls      :       List[str]							, _lowerCamelCase      :       Union[str, Any]							, _lowerCamelCase      :       Optional[Any]=None							, **_lowerCamelCase      :       str    ):





											"""simple docstring"""







											return super(_lowerCamelCase							, cls    ).get_tokenizers(_lowerCamelCase							, _lowerCamelCase							, **_lowerCamelCase    )







						@classmethod
						def       _a    (     cls      :       Optional[Any]							, _lowerCamelCase      :       Any							, _lowerCamelCase      :       List[Any]							, _lowerCamelCase      :       str=None							, **_lowerCamelCase      :       Tuple    ):





											"""simple docstring"""







											A_ :     Tuple =							kwargs.pop('''config'''							, _lowerCamelCase    ) or RagConfig.from_pretrained(_lowerCamelCase							, **_lowerCamelCase    )
											A_ :     List[Any] =							RagTokenizer.from_pretrained(_lowerCamelCase							, config=_lowerCamelCase    )
											A_ :     Union[str, Any] =							rag_tokenizer.question_encoder
											A_ :     Dict =							rag_tokenizer.generator

											if indexed_dataset is not None:
																A_ :     Any =							'''custom'''
																A_ :     int =							CustomHFIndex(config.retrieval_vector_size							, _lowerCamelCase    )
											else:
																A_ :     Optional[int] =							cls._build_index(_lowerCamelCase    )

											return cls(
											    _lowerCamelCase							, question_encoder_tokenizer=_lowerCamelCase							, generator_tokenizer=_lowerCamelCase							, retrieval_workers=_lowerCamelCase							, index=_lowerCamelCase							, )







						def       _a    (     self      :       Optional[Any]    ):





											"""simple docstring"""







											logger.info('''re-loading the new dataset with embeddings'''    )
											# access from the training loop

											ray.get([worker.clear_object.remote() for worker in self.retrieval_workers]    )

											# build the index object again
											A_ :     Dict =							self._build_index(self.config    )

											ray.get(
											    [
											        worker.create_rag_retriever.remote(
											            self.config							, self.question_encoder_tokenizer							, self.generator_tokenizer							, _lowerCamelCase    )
											        for worker in self.retrieval_workers
											    ]    )



