




'''simple docstring'''




from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


snake_case__          =						logging.get_logger(__name__)






class     UpperCamelCase_     (a__     ):







						"""simple docstring"""


						_lowerCAmelCase    						=					['pixel_values']







						def __init__(     self      :       str							, _lowerCamelCase      :       bool = True							, _lowerCamelCase      :       Optional[Dict[str, int]] = None							, _lowerCamelCase      :       PILImageResampling = PILImageResampling.BILINEAR							, _lowerCamelCase      :       bool = True							, _lowerCamelCase      :       Union[int, float] = 1 / 255							, _lowerCamelCase      :       bool = True							, _lowerCamelCase      :       Optional[Union[float, List[float]]] = None							, _lowerCamelCase      :       Optional[Union[float, List[float]]] = None							, **_lowerCamelCase      :       str							, ):





											"""simple docstring"""







											super().__init__(**_lowerCamelCase    )
											A_ :     Tuple =							size if size is not None else {'''height''': 224, '''width''': 224}
											A_ :     Any =							get_size_dict(_lowerCamelCase    )
											A_ :     Optional[Any] =							do_resize
											A_ :     str =							do_rescale
											A_ :     Any =							do_normalize
											A_ :     Optional[Any] =							size
											A_ :     Optional[Any] =							resample
											A_ :     Union[str, Any] =							rescale_factor
											A_ :     Union[str, Any] =							image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
											A_ :     Union[str, Any] =							image_std if image_std is not None else IMAGENET_STANDARD_STD







						def       _a    (     self      :       str							, _lowerCamelCase      :       np.ndarray							, _lowerCamelCase      :       Dict[str, int]							, _lowerCamelCase      :       PILImageResampling = PILImageResampling.BILINEAR							, _lowerCamelCase      :       Optional[Union[str, ChannelDimension]] = None							, **_lowerCamelCase      :       Optional[Any]							, ):





											"""simple docstring"""







											A_ :     Dict =							get_size_dict(_lowerCamelCase    )
											if "height" not in size or "width" not in size:
																raise ValueError(f'The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}'    )
											return resize(
											    _lowerCamelCase							, size=(size['''height'''], size['''width'''])							, resample=_lowerCamelCase							, data_format=_lowerCamelCase							, **_lowerCamelCase    )







						def       _a    (     self      :       List[Any]							, _lowerCamelCase      :       np.ndarray							, _lowerCamelCase      :       float							, _lowerCamelCase      :       Optional[Union[str, ChannelDimension]] = None							, **_lowerCamelCase      :       Any    ):





											"""simple docstring"""







											return rescale(_lowerCamelCase							, scale=_lowerCamelCase							, data_format=_lowerCamelCase							, **_lowerCamelCase    )







						def       _a    (     self      :       Any							, _lowerCamelCase      :       np.ndarray							, _lowerCamelCase      :       Union[float, List[float]]							, _lowerCamelCase      :       Union[float, List[float]]							, _lowerCamelCase      :       Optional[Union[str, ChannelDimension]] = None							, **_lowerCamelCase      :       List[Any]							, ):





											"""simple docstring"""







											return normalize(_lowerCamelCase							, mean=_lowerCamelCase							, std=_lowerCamelCase							, data_format=_lowerCamelCase							, **_lowerCamelCase    )







						def       _a    (     self      :       Union[str, Any]							, _lowerCamelCase      :       ImageInput							, _lowerCamelCase      :       Optional[bool] = None							, _lowerCamelCase      :       Dict[str, int] = None							, _lowerCamelCase      :       PILImageResampling = None							, _lowerCamelCase      :       Optional[bool] = None							, _lowerCamelCase      :       Optional[float] = None							, _lowerCamelCase      :       Optional[bool] = None							, _lowerCamelCase      :       Optional[Union[float, List[float]]] = None							, _lowerCamelCase      :       Optional[Union[float, List[float]]] = None							, _lowerCamelCase      :       Optional[Union[str, TensorType]] = None							, _lowerCamelCase      :       Union[str, ChannelDimension] = ChannelDimension.FIRST							, **_lowerCamelCase      :       Tuple							, ):





											"""simple docstring"""







											A_ :     List[str] =							do_resize if do_resize is not None else self.do_resize
											A_ :     Any =							do_rescale if do_rescale is not None else self.do_rescale
											A_ :     List[str] =							do_normalize if do_normalize is not None else self.do_normalize
											A_ :     int =							resample if resample is not None else self.resample
											A_ :     List[str] =							rescale_factor if rescale_factor is not None else self.rescale_factor
											A_ :     Optional[int] =							image_mean if image_mean is not None else self.image_mean
											A_ :     Any =							image_std if image_std is not None else self.image_std

											A_ :     Tuple =							size if size is not None else self.size
											A_ :     int =							get_size_dict(_lowerCamelCase    )

											A_ :     Dict =							make_list_of_images(_lowerCamelCase    )

											if not valid_images(_lowerCamelCase    ):
																raise ValueError(
																    '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
																    '''torch.Tensor, tf.Tensor or jax.ndarray.'''    )

											if do_resize and size is None:
																raise ValueError('''Size must be specified if do_resize is True.'''    )

											if do_rescale and rescale_factor is None:
																raise ValueError('''Rescale factor must be specified if do_rescale is True.'''    )

											# All transformations expect numpy arrays.
											A_ :     List[Any] =							[to_numpy_array(_lowerCamelCase    ) for image in images]

											if do_resize:
																A_ :     Dict =							[self.resize(image=_lowerCamelCase							, size=_lowerCamelCase							, resample=_lowerCamelCase    ) for image in images]

											if do_rescale:
																A_ :     Union[str, Any] =							[self.rescale(image=_lowerCamelCase							, scale=_lowerCamelCase    ) for image in images]

											if do_normalize:
																A_ :     List[Any] =							[self.normalize(image=_lowerCamelCase							, mean=_lowerCamelCase							, std=_lowerCamelCase    ) for image in images]

											A_ :     Dict =							[to_channel_dimension_format(_lowerCamelCase							, _lowerCamelCase    ) for image in images]

											A_ :     str =							{'''pixel_values''': images}
											return BatchFeature(data=_lowerCamelCase							, tensor_type=_lowerCamelCase    )









'''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
											    ]    )



