




'''simple docstring'''


import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import BertGenerationDecoder, BertGenerationEncoder

class      A       :







							def __init__(    self			:    str				,     lowerCAmelCase_			:    List[Any]				,     lowerCAmelCase_			:    List[Any]=13				,     lowerCAmelCase_			:    Union[str, Any]=7				,     lowerCAmelCase_			:    List[str]=True				,     lowerCAmelCase_			:    Optional[Any]=True				,     lowerCAmelCase_			:    Optional[int]=99				,     lowerCAmelCase_			:    Any=32				,     lowerCAmelCase_			:    List[str]=5				,     lowerCAmelCase_			:    str=4				,     lowerCAmelCase_			:    Union[str, Any]=37				,     lowerCAmelCase_			:    Tuple="gelu"				,     lowerCAmelCase_			:    Optional[int]=0.1				,     lowerCAmelCase_			:    Tuple=0.1				,     lowerCAmelCase_			:    int=50				,     lowerCAmelCase_			:    Optional[Any]=0.0_2				,     lowerCAmelCase_			:    int=True				,     lowerCAmelCase_			:    str=None				,     )     ->				Optional[Any]:


														"""simple docstring"""


														_a      	=							parent
														_a      	=							batch_size
														_a      	=							seq_length
														_a      	=							is_training
														_a      	=							use_input_mask
														_a      	=							vocab_size
														_a      	=							hidden_size
														_a      	=							num_hidden_layers
														_a      	=							num_attention_heads
														_a      	=							intermediate_size
														_a      	=							hidden_act
														_a      	=							hidden_dropout_prob
														_a      	=							attention_probs_dropout_prob
														_a      	=							max_position_embeddings
														_a      	=							initializer_range
														_a      	=							use_labels
														_a      	=							scope







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


														"""simple docstring"""


														_a      	=							ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size   )

														_a      	=							None
														if self.use_input_mask:
																					_a      	=							random_attention_mask([self.batch_size, self.seq_length]   )

														if self.use_labels:
																					_a      	=							ids_tensor([self.batch_size, self.seq_length]				,     self.vocab_size   )

														_a      	=							self.get_config()

														return config, input_ids, input_mask, token_labels







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


														"""simple docstring"""


														return BertGenerationConfig(
														    vocab_size=self.vocab_size				,     hidden_size=self.hidden_size				,     num_hidden_layers=self.num_hidden_layers				,     num_attention_heads=self.num_attention_heads				,     intermediate_size=self.intermediate_size				,     hidden_act=self.hidden_act				,     hidden_dropout_prob=self.hidden_dropout_prob				,     attention_probs_dropout_prob=self.attention_probs_dropout_prob				,     max_position_embeddings=self.max_position_embeddings				,     is_decoder=lowerCAmelCase_				,     initializer_range=self.initializer_range				,     )







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


														"""simple docstring"""


														(
														    (
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				
														)      	=							self.prepare_config_and_inputs()

														_a      	=							True
														_a      	=							floats_tensor([self.batch_size, self.seq_length, self.hidden_size]   )
														_a      	=							ids_tensor([self.batch_size, self.seq_length]				,     vocab_size=2   )

														return (
														    config,
														    input_ids,
														    input_mask,
														    token_labels,
														    encoder_hidden_states,
														    encoder_attention_mask,
														)







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


														"""simple docstring"""


														_a      	=							BertGenerationEncoder(config=lowerCAmelCase_   )
														model.to(lowerCAmelCase_   )
														model.eval()
														_a      	=							model(lowerCAmelCase_				,     attention_mask=lowerCAmelCase_   )
														_a      	=							model(lowerCAmelCase_   )
														self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)   )







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


														"""simple docstring"""


														_a      	=							True
														_a      	=							BertGenerationEncoder(config=lowerCAmelCase_   )
														model.to(lowerCAmelCase_   )
														model.eval()
														_a      	=							model(
														    lowerCAmelCase_				,     attention_mask=lowerCAmelCase_				,     encoder_hidden_states=lowerCAmelCase_				,     encoder_attention_mask=lowerCAmelCase_				,     )
														_a      	=							model(
														    lowerCAmelCase_				,     attention_mask=lowerCAmelCase_				,     encoder_hidden_states=lowerCAmelCase_				,     )
														self.parent.assertEqual(result.last_hidden_state.shape				,     (self.batch_size, self.seq_length, self.hidden_size)   )







							def   __lowerCAmelCase     (    self			:    Optional[Any]				,     lowerCAmelCase_			:    Optional[int]				,     lowerCAmelCase_			:    Any				,     lowerCAmelCase_			:    Tuple				,     lowerCAmelCase_			:    Dict				,     lowerCAmelCase_			:    Any				,     lowerCAmelCase_			:    List[Any]				,     **lowerCAmelCase_			:    Optional[int]				,     )     ->				Dict:


														"""simple docstring"""


														_a      	=							True
														_a      	=							True
														_a      	=							BertGenerationDecoder(config=lowerCAmelCase_   ).to(lowerCAmelCase_   ).eval()

														# first forward pass
														_a      	=							model(
														    lowerCAmelCase_				,     attention_mask=lowerCAmelCase_				,     encoder_hidden_states=lowerCAmelCase_				,     encoder_attention_mask=lowerCAmelCase_				,     use_cache=lowerCAmelCase_				,     )
														_a      	=							outputs.past_key_values

														# create hypothetical multiple next token and extent to next_input_ids
														_a      	=							ids_tensor((self.batch_size, 3)				,     config.vocab_size   )
														_a      	=							ids_tensor((self.batch_size, 3)				,     vocab_size=2   )

														# append to next input_ids and
														_a      	=							torch.cat([input_ids, next_tokens]				,     dim=-1   )
														_a      	=							torch.cat([input_mask, next_mask]				,     dim=-1   )

														_a      	=							model(
														    lowerCAmelCase_				,     attention_mask=lowerCAmelCase_				,     encoder_hidden_states=lowerCAmelCase_				,     encoder_attention_mask=lowerCAmelCase_				,     output_hidden_states=lowerCAmelCase_				,     )['''hidden_states'''][0]
														_a      	=							model(
														    lowerCAmelCase_				,     attention_mask=lowerCAmelCase_				,     encoder_hidden_states=lowerCAmelCase_				,     encoder_attention_mask=lowerCAmelCase_				,     past_key_values=lowerCAmelCase_				,     output_hidden_states=lowerCAmelCase_				,     )['''hidden_states'''][0]

														# select random slice
														_a      	=							ids_tensor((1,)				,     output_from_past.shape[-1]   ).item()
														_a      	=							output_from_no_past[:, -3:, random_slice_idx].detach()
														_a      	=							output_from_past[:, :, random_slice_idx].detach()

														self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]   )

														# test that outputs are equal for slice
														self.parent.assertTrue(torch.allclose(lowerCAmelCase_				,     lowerCAmelCase_				,     atol=1e-3   )   )







							def   __lowerCAmelCase     (    self			:    Tuple				,     lowerCAmelCase_			:    Optional[int]				,     lowerCAmelCase_			:    Any				,     lowerCAmelCase_			:    Optional[Any]				,     lowerCAmelCase_			:    Dict				,     *lowerCAmelCase_			:    Optional[Any]				,     )     ->				List[Any]:


														"""simple docstring"""


														_a      	=							BertGenerationDecoder(lowerCAmelCase_   )
														model.to(lowerCAmelCase_   )
														model.eval()
														_a      	=							model(lowerCAmelCase_				,     attention_mask=lowerCAmelCase_				,     labels=lowerCAmelCase_   )
														self.parent.assertEqual(result.logits.shape				,     (self.batch_size, self.seq_length, self.vocab_size)   )







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


														"""simple docstring"""


														_a	,				_a	,				_a	,				_a      	=							self.prepare_config_and_inputs()
														_a      	=							{'''input_ids''': input_ids, '''attention_mask''': input_mask}
														return config, inputs_dict



@require_torch
class      A       (						_a	,_a	,_a	,unittest.TestCase       ):
							lowercase_							     =							(BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
							lowercase_							     =							(BertGenerationDecoder,) if is_torch_available() else ()
							lowercase_							     =							(
							    {'feature-extraction': BertGenerationEncoder, 'text-generation': BertGenerationDecoder}
							    if is_torch_available()
							    else {}
							)







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


														"""simple docstring"""


														_a      	=							BertGenerationEncoderTester(self   )
														_a      	=							ConfigTester(self				,     config_class=lowerCAmelCase_				,     hidden_size=37   )







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


														"""simple docstring"""


														self.config_tester.run_common_tests()







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


														"""simple docstring"""


														_a      	=							self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(*lowerCAmelCase_   )







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


														"""simple docstring"""


														_a	,				_a	,				_a	,				_a      	=							self.model_tester.prepare_config_and_inputs()
														_a      	=							'''bert'''
														self.model_tester.create_and_check_model(lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_   )







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


														"""simple docstring"""


														_a      	=							self.model_tester.prepare_config_and_inputs_for_decoder()
														self.model_tester.create_and_check_model_as_decoder(*lowerCAmelCase_   )







							def   __lowerCAmelCase     (    self			:    Any   )     ->				Any:


														"""simple docstring"""


														_a      	=							self.model_tester.prepare_config_and_inputs_for_decoder()
														self.model_tester.create_and_check_decoder_model_past_large_inputs(*lowerCAmelCase_   )







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


														"""simple docstring"""


														(
														    (
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				(
														    _a
														)	,				
														)      	=							self.model_tester.prepare_config_and_inputs_for_decoder()

														_a      	=							None

														self.model_tester.create_and_check_model_as_decoder(
														    lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     lowerCAmelCase_				,     )







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


														"""simple docstring"""


														_a      	=							self.model_tester.prepare_config_and_inputs_for_decoder()
														self.model_tester.create_and_check_for_causal_lm(*lowerCAmelCase_   )







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


														"""simple docstring"""


														_a      	=							BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''   )
														self.assertIsNotNone(lowerCAmelCase_   )



@require_torch
class      A       (						unittest.TestCase       ):







							@slow
							def   __lowerCAmelCase     (    self			:    str   )     ->				Tuple:


														"""simple docstring"""


														_a      	=							BertGenerationEncoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''   )
														_a      	=							torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]   )
														with torch.no_grad():
																					_a      	=							model(lowerCAmelCase_   )[0]
														_a      	=							torch.Size([1, 8, 10_24]   )
														self.assertEqual(output.shape				,     lowerCAmelCase_   )
														_a      	=							torch.tensor(
														    [[[0.1_7_7_5, 0.0_0_8_3, -0.0_3_2_1], [1.6_0_0_2, 0.1_2_8_7, 0.3_9_1_2], [2.1_4_7_3, 0.5_7_9_1, 0.6_0_6_6]]]   )
														self.assertTrue(torch.allclose(output[:, :3, :3]				,     lowerCAmelCase_				,     atol=1e-4   )   )



@require_torch
class      A       (						unittest.TestCase       ):







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


														"""simple docstring"""


														_a      	=							BertGenerationDecoder.from_pretrained('''google/bert_for_seq_generation_L-24_bbc_encoder'''   )
														_a      	=							torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]   )
														with torch.no_grad():
																					_a      	=							model(lowerCAmelCase_   )[0]
														_a      	=							torch.Size([1, 8, 5_03_58]   )
														self.assertEqual(output.shape				,     lowerCAmelCase_   )
														_a      	=							torch.tensor(
														    [[[-0.5_7_8_8, -2.5_9_9_4, -3.7_0_5_4], [0.0_4_3_8, 4.7_9_9_7, 1.8_7_9_5], [1.5_8_6_2, 6.6_4_0_9, 4.4_6_3_8]]]   )
														self.assertTrue(torch.allclose(output[:, :3, :3]				,     lowerCAmelCase_				,     atol=1e-4   )   )







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

