





"""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


__UpperCamelCase     :						int						       =  logging.getLogger(__name__)







class       a							(   a__  ):



 def __init__(     self				,       _snake_case				,       _snake_case				,       _snake_case				,       _snake_case=None     ):







  """simple docstring"""






  super().__init__(
      _snake_case				,       question_encoder_tokenizer=_snake_case				,       generator_tokenizer=_snake_case				,       index=_snake_case				,       init_retrieval=_snake_case				,       )
  lowerCAmelCase    				= None



 def    UpperCamelCase__  (     self				,       _snake_case     ):







  """simple docstring"""






  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=_snake_case				,       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    UpperCamelCase__  (     self     ):







  """simple docstring"""






  return dist.get_rank(group=self.process_group     ) == 0



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case				,       _snake_case=torch.floataa     ):







  """simple docstring"""






  lowerCAmelCase    				= torch.empty(_snake_case				,       dtype=_snake_case     )
  dist.scatter(_snake_case				,       src=0				,       scatter_list=_snake_case				,       group=self.process_group     )
  return target_tensor



 def    UpperCamelCase__  (     self     ):







  """simple docstring"""






  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'     ))				,       _snake_case     )
  return ifname



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case     ):







  """simple docstring"""






  if not dist.is_initialized():
   lowerCAmelCase				,lowerCAmelCase    				= self._main_retrieve(_snake_case				,       _snake_case     )
   return retrieved_doc_embeds, doc_ids, self.index.get_doc_dicts(_snake_case     )

  # 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(_snake_case     )]
  dist.gather(torch.tensor(_snake_case     )				,       dst=0				,       gather_list=_snake_case				,       group=self.process_group     )

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

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












"""simple docstring"""
from dataclasses import dataclass
from typing import Optional, Tuple, Union

import torch
import torch.nn as nn

from ..configuration_utils import ConfigMixin, register_to_config
from ..utils import BaseOutput, apply_forward_hook
from .modeling_utils import ModelMixin
from .vae import Decoder, DecoderOutput, Encoder, VectorQuantizer







@dataclass
class       a							(   a__  ):
 snake_case__             =       42







class       a							(   a__   , a__  ):



 @register_to_config
 def __init__(     self				,       _snake_case = 3				,       _snake_case = 3				,       _snake_case = ("DownEncoderBlock2D",)				,       _snake_case = ("UpDecoderBlock2D",)				,       _snake_case = (64,)				,       _snake_case = 1				,       _snake_case = "silu"				,       _snake_case = 3				,       _snake_case = 32				,       _snake_case = 2_56				,       _snake_case = 32				,       _snake_case = None				,       _snake_case = 0.18_215				,       _snake_case = "group"				,       ):







  """simple docstring"""






  super().__init__()

  # pass init params to Encoder
  lowerCAmelCase    				= Encoder(
      in_channels=_snake_case				,       out_channels=_snake_case				,       down_block_types=_snake_case				,       block_out_channels=_snake_case				,       layers_per_block=_snake_case				,       act_fn=_snake_case				,       norm_num_groups=_snake_case				,       double_z=_snake_case				,       )

  lowerCAmelCase    				= vq_embed_dim if vq_embed_dim is not None else latent_channels

  lowerCAmelCase    				= nn.Convad(_snake_case				,       _snake_case				,       1     )
  lowerCAmelCase    				= VectorQuantizer(_snake_case				,       _snake_case				,       beta=0.25				,       remap=_snake_case				,       sane_index_shape=_snake_case     )
  lowerCAmelCase    				= nn.Convad(_snake_case				,       _snake_case				,       1     )

  # pass init params to Decoder
  lowerCAmelCase    				= Decoder(
      in_channels=_snake_case				,       out_channels=_snake_case				,       up_block_types=_snake_case				,       block_out_channels=_snake_case				,       layers_per_block=_snake_case				,       act_fn=_snake_case				,       norm_num_groups=_snake_case				,       norm_type=_snake_case				,       )



 @apply_forward_hook
 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case = True     ):







  """simple docstring"""






  lowerCAmelCase    				= self.encoder(_snake_case     )
  lowerCAmelCase    				= self.quant_conv(_snake_case     )

  if not return_dict:
   return (h,)

  return VQEncoderOutput(latents=_snake_case     )



 @apply_forward_hook
 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case = False				,       _snake_case = True     ):







  """simple docstring"""






  if not force_not_quantize:
   lowerCAmelCase				,lowerCAmelCase				,lowerCAmelCase    				= self.quantize(_snake_case     )
  else:
   lowerCAmelCase    				= h
  lowerCAmelCase    				= self.post_quant_conv(_snake_case     )
  lowerCAmelCase    				= self.decoder(_snake_case				,       quant if self.config.norm_type == 'spatial' else None     )

  if not return_dict:
   return (dec,)

  return DecoderOutput(sample=_snake_case     )



 def    UpperCamelCase__  (     self				,       _snake_case				,       _snake_case = True     ):







  """simple docstring"""






  lowerCAmelCase    				= sample
  lowerCAmelCase    				= self.encode(_snake_case     ).latents
  lowerCAmelCase    				= self.decode(_snake_case     ).sample

  if not return_dict:
   return (dec,)

  return DecoderOutput(sample=_snake_case     )





