




'''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 						snake_case_       ():





							'''simple docstring'''


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

class      A       (						nn.Module       ):







							def __init__(    self			:    List[Any]   )     ->				List[str]:


														"""simple docstring"""


														super().__init__()
														_a      	=							nn.Linear(3				,     4   )
														_a      	=							nn.BatchNormad(4   )
														_a      	=							nn.Linear(4				,     5   )







							def   __lowerCAmelCase     (    self			:    Union[str, Any]				,     lowerCAmelCase_			:    Dict   )     ->				List[str]:


														"""simple docstring"""


														return self.lineara(self.batchnorm(self.lineara(lowerCAmelCase_   )   )   )



class      A       (						unittest.TestCase       ):







							def   __lowerCAmelCase     (    self			:    Dict   )     ->				List[Any]:


														"""simple docstring"""


														_a      	=							[]

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

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







							def   __lowerCAmelCase     (    self			:    List[Any]   )     ->				List[Any]:


														"""simple docstring"""


														_a      	=							[]

														@find_executable_batch_size(starting_batch_size=1_28   )
														def mock_training_loop_function(lowerCAmelCase_			:    List[str]				,     lowerCAmelCase_			:    List[Any]   ):
																					nonlocal batch_sizes
																					batch_sizes.append(lowerCAmelCase_   )
																					if batch_size != 8:
																												raise_fake_out_of_memory()
																					return batch_size, arga

														_a	,				_a      	=							mock_training_loop_function('''hello'''   )
														self.assertListEqual(lowerCAmelCase_				,     [1_28, 64, 32, 16, 8]   )
														self.assertListEqual([bs, arga]				,     [8, '''hello''']   )







							def   __lowerCAmelCase     (    self			:    Union[str, Any]   )     ->				Union[str, Any]:


														"""simple docstring"""


														@find_executable_batch_size(starting_batch_size=0   )
														def mock_training_loop_function(lowerCAmelCase_			:    Tuple   ):
																					pass

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







							def   __lowerCAmelCase     (    self			:    int   )     ->				Optional[int]:


														"""simple docstring"""


														@find_executable_batch_size(starting_batch_size=16   )
														def mock_training_loop_function(lowerCAmelCase_			:    Union[str, Any]   ):
																					if batch_size > 0:
																												raise_fake_out_of_memory()
																					pass

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







							def   __lowerCAmelCase     (    self			:    int   )     ->				Tuple:


														"""simple docstring"""


														@find_executable_batch_size(starting_batch_size=1_28   )
														def mock_training_loop_function(lowerCAmelCase_			:    int				,     lowerCAmelCase_			:    Dict				,     lowerCAmelCase_			:    str   ):
																					if batch_size != 8:
																												raise raise_fake_out_of_memory()

														with self.assertRaises(lowerCAmelCase_   ) 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   __lowerCAmelCase     (    self			:    Any   )     ->				Optional[Any]:


														"""simple docstring"""


														@find_executable_batch_size(starting_batch_size=16   )
														def mock_training_loop_function(lowerCAmelCase_			:    Any   ):
																					raise ValueError('''Oops, we had an error!'''   )

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







							@require_cuda
							def   __lowerCAmelCase     (    self			:    List[Any]   )     ->				Optional[int]:


														"""simple docstring"""


														_a      	=							torch.cuda.memory_allocated()
														_a      	=							ModelForTest()
														model.cuda()
														self.assertGreater(torch.cuda.memory_allocated()				,     lowerCAmelCase_   )
														_a      	=							release_memory(lowerCAmelCase_   )
														self.assertEqual(torch.cuda.memory_allocated()				,     lowerCAmelCase_   )







'''simple docstring'''


import numpy as np
from sklearn.datasets import fetch_california_housing
from sklearn.metrics import mean_absolute_error, mean_squared_error
from sklearn.model_selection import train_test_split
from xgboost import XGBRegressor
def 						snake_case_       (UpperCamelCase			:    dict ):





							'''simple docstring'''


							return (data["data"], data["target"])
def 						snake_case_       (UpperCamelCase			:    np.ndarray					,		UpperCamelCase			:    np.ndarray					,		UpperCamelCase			:    np.ndarray ):





							'''simple docstring'''


							_a      	=							XGBRegressor(verbosity=0					,		random_state=42 )
							xgb.fit(UpperCamelCase					,		UpperCamelCase )
							# Predict target for test data
							_a      	=							xgb.predict(UpperCamelCase )
							_a      	=							predictions.reshape(len(UpperCamelCase )					,		1 )
							return predictions
def 						snake_case_       ():





							'''simple docstring'''


							_a      	=							fetch_california_housing()
							_a	,				_a      	=							data_handling(UpperCamelCase )
							_a	,				_a	,				_a	,				_a      	=							train_test_split(
							    UpperCamelCase					,		UpperCamelCase					,		test_size=0.25					,		random_state=1 )
							_a      	=							xgboost(UpperCamelCase					,		UpperCamelCase					,		UpperCamelCase )
							# Error printing
							print(f'Mean Absolute Error : {mean_absolute_error(UpperCamelCase					,		UpperCamelCase )}' )
							print(f'Mean Square Error  : {mean_squared_error(UpperCamelCase					,		UpperCamelCase )}' )


if __name__ == "__main__":
				import doctest

				doctest.testmod(verbose=True)
				main()

