code: 
  
  
  
  
  
  '''simple docstring'''
  
  
  from ...configuration_utils import PretrainedConfig
  from ...utils import is_detectrona_available, logging
  
  
  _lowerCamelCase				:	Any				    =       logging.get_logger(__name__)
  
  _lowerCamelCase				:	Tuple				    =       {
      'layoutlmv2-base-uncased': 'https://huggingface.co/microsoft/layoutlmv2-base-uncased/resolve/main/config.json',
      'layoutlmv2-large-uncased': 'https://huggingface.co/microsoft/layoutlmv2-large-uncased/resolve/main/config.json',
      # See all LayoutLMv2 models at https://huggingface.co/models?filter=layoutlmv2
  }
  
  # soft dependency
  if is_detectrona_available():
  				import detectrona
  
  
  
  class 							__UpperCAmelCase  (       A__						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		__lowerCAmelCase 				=    '''layoutlmv2'''
  
  
  
  
  
  
  
  		def __init__(self						:      List[str]      ,      _lowerCAmelCase						:      str=3_0522      ,      _lowerCAmelCase						:      Union[str, Any]=768      ,      _lowerCAmelCase						:      List[Any]=12      ,      _lowerCAmelCase						:      Tuple=12      ,      _lowerCAmelCase						:      str=3072      ,      _lowerCAmelCase						:      Any="gelu"      ,      _lowerCAmelCase						:      Optional[Any]=0.1      ,      _lowerCAmelCase						:      Optional[int]=0.1      ,      _lowerCAmelCase						:      List[str]=512      ,      _lowerCAmelCase						:      Tuple=2      ,      _lowerCAmelCase						:      Union[str, Any]=0.02      ,      _lowerCAmelCase						:      Dict=1e-12      ,      _lowerCAmelCase						:      Dict=0      ,      _lowerCAmelCase						:      Tuple=1024      ,      _lowerCAmelCase						:      str=128      ,      _lowerCAmelCase						:      Tuple=32      ,      _lowerCAmelCase						:      Tuple=True      ,      _lowerCAmelCase						:      List[Any]=256      ,      _lowerCAmelCase						:      Optional[int]=64      ,      _lowerCAmelCase						:      Dict=True      ,      _lowerCAmelCase						:      List[Any]=[7, 7, 256]      ,      _lowerCAmelCase						:      int=128      ,      _lowerCAmelCase						:      Dict=128      ,      _lowerCAmelCase						:      Any=True      ,      _lowerCAmelCase						:      Union[str, Any]=True      ,      _lowerCAmelCase						:      str=False      ,      _lowerCAmelCase						:      Optional[Any]=None      ,      **_lowerCAmelCase						:      Optional[int]      ,      ):
  			super().__init__(
  			    vocab_size=_lowerCAmelCase      ,      hidden_size=_lowerCAmelCase      ,      num_hidden_layers=_lowerCAmelCase      ,      num_attention_heads=_lowerCAmelCase      ,      intermediate_size=_lowerCAmelCase      ,      hidden_act=_lowerCAmelCase      ,      hidden_dropout_prob=_lowerCAmelCase      ,      attention_probs_dropout_prob=_lowerCAmelCase      ,      max_position_embeddings=_lowerCAmelCase      ,      type_vocab_size=_lowerCAmelCase      ,      initializer_range=_lowerCAmelCase      ,      layer_norm_eps=_lowerCAmelCase      ,      pad_token_id=_lowerCAmelCase      ,      **_lowerCAmelCase      ,      )
  			A							  =      max_ad_position_embeddings
  			A							  =      max_rel_pos
  			A							  =      rel_pos_bins
  			A							  =      fast_qkv
  			A							  =      max_rel_ad_pos
  			A							  =      rel_ad_pos_bins
  			A							  =      convert_sync_batchnorm
  			A							  =      image_feature_pool_shape
  			A							  =      coordinate_size
  			A							  =      shape_size
  			A							  =      has_relative_attention_bias
  			A							  =      has_spatial_attention_bias
  			A							  =      has_visual_segment_embedding
  			A							  =      (
  			    detectrona_config_args if detectrona_config_args is not None else self.get_default_detectrona_config()
  			)
  
  
  
  
  
  
  
  		@classmethod
  		def        A     (self						:      List[str]      ):
  			return {
  			    "MODEL.MASK_ON": True,
  			    "MODEL.PIXEL_STD": [57.375, 57.120, 58.395],
  			    "MODEL.BACKBONE.NAME": "build_resnet_fpn_backbone",
  			    "MODEL.FPN.IN_FEATURES": ["res2", "res3", "res4", "res5"],
  			    "MODEL.ANCHOR_GENERATOR.SIZES": [[32], [64], [128], [256], [512]],
  			    "MODEL.RPN.IN_FEATURES": ["p2", "p3", "p4", "p5", "p6"],
  			    "MODEL.RPN.PRE_NMS_TOPK_TRAIN": 2000,
  			    "MODEL.RPN.PRE_NMS_TOPK_TEST": 1000,
  			    "MODEL.RPN.POST_NMS_TOPK_TRAIN": 1000,
  			    "MODEL.POST_NMS_TOPK_TEST": 1000,
  			    "MODEL.ROI_HEADS.NAME": "StandardROIHeads",
  			    "MODEL.ROI_HEADS.NUM_CLASSES": 5,
  			    "MODEL.ROI_HEADS.IN_FEATURES": ["p2", "p3", "p4", "p5"],
  			    "MODEL.ROI_BOX_HEAD.NAME": "FastRCNNConvFCHead",
  			    "MODEL.ROI_BOX_HEAD.NUM_FC": 2,
  			    "MODEL.ROI_BOX_HEAD.POOLER_RESOLUTION": 14,
  			    "MODEL.ROI_MASK_HEAD.NAME": "MaskRCNNConvUpsampleHead",
  			    "MODEL.ROI_MASK_HEAD.NUM_CONV": 4,
  			    "MODEL.ROI_MASK_HEAD.POOLER_RESOLUTION": 7,
  			    "MODEL.RESNETS.DEPTH": 101,
  			    "MODEL.RESNETS.SIZES": [[32], [64], [128], [256], [512]],
  			    "MODEL.RESNETS.ASPECT_RATIOS": [[0.5, 1.0, 2.0]],
  			    "MODEL.RESNETS.OUT_FEATURES": ["res2", "res3", "res4", "res5"],
  			    "MODEL.RESNETS.NUM_GROUPS": 32,
  			    "MODEL.RESNETS.WIDTH_PER_GROUP": 8,
  			    "MODEL.RESNETS.STRIDE_IN_1X1": False,
  			}
  
  
  
  
  
  
  
  		def        A     (self						:      Union[str, Any]      ):
  			A							  =      detectrona.config.get_cfg()
  			for k, v in self.detectrona_config_args.items():
  				A							  =      k.split("""."""      )
  				A							  =      detectrona_config
  				for attribute in attributes[:-1]:
  					A							  =      getattr(_lowerCAmelCase      ,      _lowerCAmelCase      )
  				setattr(_lowerCAmelCase      ,      attributes[-1]      ,      _lowerCAmelCase      )
  
  			return detectrona_config
  
  
style_context: 
  
  
  
  
  
  '''simple docstring'''
  
  
  import logging
  import random
  
  import ray
  
  from transformers import RagConfig, RagRetriever, RagTokenizer
  from transformers.models.rag.retrieval_rag import CustomHFIndex
  
  
  _lowerCamelCase				:	Tuple				    =       logging.getLogger(__name__)
  
  
  
  class 							__UpperCAmelCase  :
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def __init__(self						:      Union[str, Any]      ):
  			A							  =      False
  
  
  
  
  
  
  
  		def        A     (self						:      Any      ,      _lowerCAmelCase						:      str      ,      _lowerCAmelCase						:      List[str]      ,      _lowerCAmelCase						:      str      ,      _lowerCAmelCase						:      Any      ):
  			if not self.initialized:
  				A							  =      RagRetriever(
  				    _lowerCAmelCase      ,      question_encoder_tokenizer=_lowerCAmelCase      ,      generator_tokenizer=_lowerCAmelCase      ,      index=_lowerCAmelCase      ,      init_retrieval=_lowerCAmelCase      ,      )
  				A							  =      True
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ):
  			self.retriever.index.init_index()
  
  
  
  
  
  
  
  		def        A     (self						:      Optional[int]      ):
  			# delete the old self.retriever object before assigning the new index
  			del self.retriever
  			A							  =      False
  
  
  
  
  
  
  
  		def        A     (self						:      List[str]      ,      _lowerCAmelCase						:      int      ,      _lowerCAmelCase						:      List[str]      ):
  			A  ,    A							  =      self.retriever._main_retrieve(_lowerCAmelCase      ,      _lowerCAmelCase      )
  			A							  =      self.retriever.index.get_doc_dicts(_lowerCAmelCase      )
  			return doc_ids, retrieved_doc_embeds, doc_dicts
  
  
  
  
  
  
  class 							__UpperCAmelCase  (       A__						):
  
  
  
  
  
  
  
  		'''simple docstring'''
  
  
  
  
  
  
  
  		def __init__(self						:      Tuple      ,      _lowerCAmelCase						:      int      ,      _lowerCAmelCase						:      int      ,      _lowerCAmelCase						:      List[str]      ,      _lowerCAmelCase						:      Tuple      ,      _lowerCAmelCase						:      str=None      ):
  			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							  =      retrieval_workers
  			A							  =      question_encoder_tokenizer
  			A							  =      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						:      List[Any]      ):
  			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						:      str      ,      _lowerCAmelCase						:      Dict      ,      _lowerCAmelCase						:      Tuple      ):
  			if len(self.retrieval_workers      ) > 0:
  				# Select a random retrieval actor.
  				A							  =      self.retrieval_workers[random.randint(0      ,      len(self.retrieval_workers      ) - 1      )]
  				A  ,    A  ,    A							  =      ray.get(
  				    random_worker.retrieve.remote(_lowerCAmelCase      ,      _lowerCAmelCase      )      )
  			else:
  				A  ,    A							  =      self._main_retrieve(_lowerCAmelCase      ,      _lowerCAmelCase      )
  				A							  =      self.index.get_doc_dicts(_lowerCAmelCase      )
  			return retrieved_doc_embeds, doc_ids, doc_dicts
  
  
  
  
  
  
  
  		@classmethod
  		def        A     (cls						:      Union[str, Any]      ,      _lowerCAmelCase						:      int      ,      _lowerCAmelCase						:      Optional[Any]=None      ,      **_lowerCAmelCase						:      Union[str, Any]      ):
  			return super(_lowerCAmelCase      ,      cls      ).get_tokenizers(_lowerCAmelCase      ,      _lowerCAmelCase      ,      **_lowerCAmelCase      )
  
  
  
  
  
  
  
  		@classmethod
  		def        A     (cls						:      List[Any]      ,      _lowerCAmelCase						:      Optional[int]      ,      _lowerCAmelCase						:      List[str]      ,      _lowerCAmelCase						:      str=None      ,      **_lowerCAmelCase						:      List[str]      ):
  			A							  =      kwargs.pop("""config"""      ,      _lowerCAmelCase      ) or RagConfig.from_pretrained(_lowerCAmelCase      ,      **_lowerCAmelCase      )
  			A							  =      RagTokenizer.from_pretrained(_lowerCAmelCase      ,      config=_lowerCAmelCase      )
  			A							  =      rag_tokenizer.question_encoder
  			A							  =      rag_tokenizer.generator
  
  			if indexed_dataset is not None:
  				A							  =      """custom"""
  				A							  =      CustomHFIndex(config.retrieval_vector_size      ,      _lowerCAmelCase      )
  			else:
  				A							  =      cls._build_index(_lowerCAmelCase      )
  
  			return cls(
  			    _lowerCAmelCase      ,      question_encoder_tokenizer=_lowerCAmelCase      ,      generator_tokenizer=_lowerCAmelCase      ,      retrieval_workers=_lowerCAmelCase      ,      index=_lowerCAmelCase      ,      )
  
  
  
  
  
  
  
  		def        A     (self						:      List[Any]      ):
  			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							  =      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
  			    ]      )
  
  