



"""simple docstring"""

import unittest

import torch
from torch import nn

from accelerate.test_utils import require_cuda
from accelerate.utils.memory import find_executable_batch_size, release_memory







def 							SCREAMING_SNAKE_CASE__      (		):



					"""simple docstring"""





					raise RuntimeError("""CUDA out of memory."""			)







class    __lowercase     (					nn.Module):







						"""simple docstring"""







						def __init__(self       ):
											super().__init__()
											snake_case_					:		Union[str, Any] 					=					nn.Linear(3   ,		4       )
											snake_case_					:		Union[str, Any] 					=					nn.BatchNormad(4       )
											snake_case_					:		Optional[Any] 					=					nn.Linear(4   ,		5       )







						def 						__UpperCamelCase							(self   ,		lowercase__       ):
											return self.lineara(self.batchnorm(self.lineara(lowercase__       )       )       )







class    __lowercase     (					unittest.TestCase):







						"""simple docstring"""







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Any 					=					[]

											@find_executable_batch_size(starting_batch_size=1_28       )
											def mock_training_loop_function(lowercase__       ):
																nonlocal batch_sizes
																batch_sizes.append(lowercase__       )
																if batch_size != 8:
																					raise_fake_out_of_memory()

											mock_training_loop_function()
											self.assertListEqual(lowercase__   ,		[1_28, 64, 32, 16, 8]       )







						def 						__UpperCamelCase							(self       ):
											snake_case_					:		List[str] 					=					[]

											@find_executable_batch_size(starting_batch_size=1_28       )
											def mock_training_loop_function(lowercase__   ,		lowercase__       ):
																nonlocal batch_sizes
																batch_sizes.append(lowercase__       )
																if batch_size != 8:
																					raise_fake_out_of_memory()
																return batch_size, arga

											snake_case_					,			snake_case_					:		Tuple 					=					mock_training_loop_function("""hello"""       )
											self.assertListEqual(lowercase__   ,		[1_28, 64, 32, 16, 8]       )
											self.assertListEqual([bs, arga]   ,		[8, """hello"""]       )







						def 						__UpperCamelCase							(self       ):
											@find_executable_batch_size(starting_batch_size=0       )
											def mock_training_loop_function(lowercase__       ):
																pass

											with self.assertRaises(lowercase__       ) as cm:
																mock_training_loop_function()
																self.assertIn("""No executable batch size found, reached zero."""   ,		cm.exception.args[0]       )







						def 						__UpperCamelCase							(self       ):
											@find_executable_batch_size(starting_batch_size=16       )
											def mock_training_loop_function(lowercase__       ):
																if batch_size > 0:
																					raise_fake_out_of_memory()
																pass

											with self.assertRaises(lowercase__       ) as cm:
																mock_training_loop_function()
																self.assertIn("""No executable batch size found, reached zero."""   ,		cm.exception.args[0]       )







						def 						__UpperCamelCase							(self       ):
											@find_executable_batch_size(starting_batch_size=1_28       )
											def mock_training_loop_function(lowercase__   ,		lowercase__   ,		lowercase__       ):
																if batch_size != 8:
																					raise raise_fake_out_of_memory()

											with self.assertRaises(lowercase__       ) as cm:
																mock_training_loop_function(1_28   ,		"""hello"""   ,		"""world"""       )
																self.assertIn("""Batch size was passed into `f`"""   ,		cm.exception.args[0]       )
																self.assertIn("""`f(arg1='hello', arg2='world')"""   ,		cm.exception.args[0]       )







						def 						__UpperCamelCase							(self       ):
											@find_executable_batch_size(starting_batch_size=16       )
											def mock_training_loop_function(lowercase__       ):
																raise ValueError("""Oops, we had an error!"""       )

											with self.assertRaises(lowercase__       ) as cm:
																mock_training_loop_function()
																self.assertIn("""Oops, we had an error!"""   ,		cm.exception.args[0]       )







						@require_cuda
						def 						__UpperCamelCase							(self       ):
											snake_case_					:		Tuple 					=					torch.cuda.memory_allocated()
											snake_case_					:		int 					=					ModelForTest()
											model.cuda()
											self.assertGreater(torch.cuda.memory_allocated()   ,		lowercase__       )
											snake_case_					:		int 					=					release_memory(lowercase__       )
											self.assertEqual(torch.cuda.memory_allocated()   ,		lowercase__       )









"""simple docstring"""

from ...configuration_utils import PretrainedConfig
from ...utils import logging


a_			   =					logging.get_logger(__name__)

a_			   =					{
    '''facebook/dpr-ctx_encoder-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-ctx_encoder-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-question_encoder-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-question_encoder-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-reader-single-nq-base''': (
        '''https://huggingface.co/facebook/dpr-reader-single-nq-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-ctx_encoder-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-ctx_encoder-multiset-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-question_encoder-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-question_encoder-multiset-base/resolve/main/config.json'''
    ),
    '''facebook/dpr-reader-multiset-base''': (
        '''https://huggingface.co/facebook/dpr-reader-multiset-base/resolve/main/config.json'''
    ),
}







class    __lowercase     (					_UpperCAmelCase):







						"""simple docstring"""







						_A		:							Optional[int]							       =		"""dpr"""







						def __init__(self   ,		lowercase__=3_05_22   ,		lowercase__=7_68   ,		lowercase__=12   ,		lowercase__=12   ,		lowercase__=30_72   ,		lowercase__="gelu"   ,		lowercase__=0.1   ,		lowercase__=0.1   ,		lowercase__=5_12   ,		lowercase__=2   ,		lowercase__=0.02   ,		lowercase__=1e-12   ,		lowercase__=0   ,		lowercase__="absolute"   ,		lowercase__ = 0   ,		**lowercase__   ,		):
											super().__init__(pad_token_id=lowercase__   ,		**lowercase__       )

											snake_case_					:		List[Any] 					=					vocab_size
											snake_case_					:		List[str] 					=					hidden_size
											snake_case_					:		Tuple 					=					num_hidden_layers
											snake_case_					:		List[Any] 					=					num_attention_heads
											snake_case_					:		int 					=					hidden_act
											snake_case_					:		Dict 					=					intermediate_size
											snake_case_					:		int 					=					hidden_dropout_prob
											snake_case_					:		Any 					=					attention_probs_dropout_prob
											snake_case_					:		str 					=					max_position_embeddings
											snake_case_					:		List[str] 					=					type_vocab_size
											snake_case_					:		List[str] 					=					initializer_range
											snake_case_					:		Optional[int] 					=					layer_norm_eps
											snake_case_					:		Union[str, Any] 					=					projection_dim
											snake_case_					:		str 					=					position_embedding_type




