


"""simple docstring"""




import logging
import os
from typing import List, Tuple

import numpy as np
import psutil
import torch
import torch.distributed as dist

from transformers import RagRetriever


__magic_name__						: int    =				logging.getLogger(__name__)




class 			__snake_case   (lowerCamelCase  ):




							def __init__(     self:    List[str]     ,							A_:    List[str]     ,							A_:    Optional[Any]     ,							A_:    str     ,							A_:    str=None   ):
										super().__init__(
										    A_     ,							question_encoder_tokenizer=A_     ,							generator_tokenizer=A_     ,							index=A_     ,							init_retrieval=A_     ,							)
										__lowerCamelCase          =      None




							def   __a   (     self:    Union[str, Any]     ,							A_:    int   ):
										logger.info("""initializing retrieval"""   )

										# initializing a separate process group for retrieval as the default
										# nccl backend doesn't support gather/scatter operations while gloo
										# is too slow to replace nccl for the core gpu communication
										if dist.is_initialized():
													logger.info("""dist initialized"""   )
													# needs to be set manually
													__lowerCamelCase          =      self._infer_socket_ifname()
													# avoid clash with the NCCL port
													__lowerCamelCase          =      str(distributed_port + 1   )
													__lowerCamelCase          =      dist.new_group(ranks=A_     ,							backend="""gloo"""   )

										# initialize retriever only on the main worker
										if not dist.is_initialized() or self._is_main():
													logger.info("""dist not initialized / main"""   )
													self.index.init_index()

										# all processes wait untill the retriever is initialized by the main process
										if dist.is_initialized():
													torch.distributed.barrier(group=self.process_group   )




							def   __a   (     self:    List[str]   ):
										return dist.get_rank(group=self.process_group   ) == 0




							def   __a   (     self:    List[str]     ,							A_:    Tuple     ,							A_:    List[Any]     ,							A_:    str=torch.floataa   ):
										__lowerCamelCase          =      torch.empty(A_     ,							dtype=A_   )
										dist.scatter(A_     ,							src=0     ,							scatter_list=A_     ,							group=self.process_group   )
										return target_tensor




							def   __a   (     self:    Tuple   ):
										__lowerCamelCase          =      psutil.net_if_addrs()
										# a hacky way to deal with varying network interface names
										__lowerCamelCase          =      next((addr for addr in addrs if addr.startswith("""e"""   ))     ,							A_   )
										return ifname




							def   __a   (     self:    Tuple     ,							A_:    np.ndarray     ,							A_:    int   ):

										# single GPU training
										if not dist.is_initialized():
													__lowerCamelCase     ,__lowerCamelCase          =      self._main_retrieve(A_     ,							A_   )
													return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(A_   )

										# distributed training
										__lowerCamelCase          =      dist.get_world_size(group=self.process_group   )

										# gather logic
										__lowerCamelCase          =      None
										if self._is_main():
													__lowerCamelCase          =      [torch.empty(question_hidden_states.shape     ,							dtype=torch.floataa   ) for _ in range(A_   )]
										dist.gather(torch.tensor(A_   )     ,							dst=0     ,							gather_list=A_     ,							group=self.process_group   )

										# scatter logic
										__lowerCamelCase          =      question_hidden_states.shape[0]
										__lowerCamelCase          =      []
										__lowerCamelCase          =      []
										if self._is_main():
													assert len(A_   ) == world_size
													__lowerCamelCase     ,__lowerCamelCase          =      self._main_retrieve(torch.cat(A_   ).numpy()     ,							A_   )
													__lowerCamelCase     ,__lowerCamelCase          =      torch.tensor(A_   ), torch.tensor(A_   )
													__lowerCamelCase          =      self._chunk_tensor(A_     ,							A_   )
													__lowerCamelCase          =      self._chunk_tensor(A_     ,							A_   )
										__lowerCamelCase          =      self._scattered(A_     ,							[n_queries, n_docs]     ,							target_type=torch.intaa   )
										__lowerCamelCase          =      self._scattered(A_     ,							[n_queries, n_docs, question_hidden_states.shape[1]]   )

										return retrieved_doc_embeds.numpy(), doc_ids.numpy(), self.index.get_doc_dicts(A_   )








"""simple docstring"""




def       a_ (	lowercase__ :int = 10**9     ):
			__lowerCamelCase          =      1
			__lowerCamelCase          =      2

			__lowerCamelCase          =      0
			__lowerCamelCase          =      0
			__lowerCamelCase          =      0
			while perimeter <= max_perimeter:
						perimeters_sum += perimeter

						prev_value += 2 * value
						value += prev_value

						__lowerCamelCase          =      2 * value + 2 if i % 2 == 0 else 2 * value - 2
						i += 1

			return perimeters_sum


if __name__ == "__main__":
					print(f"""{solution() = }""")




