


import tempfile
import unittest

import numpy as np

import transformers
from transformers import GPTaConfig, GPTaTokenizer, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
			import jax
			import jax.numpy as jnp

			from transformers.modeling_flax_pytorch_utils import (
			    convert_pytorch_state_dict_to_flax,
			    load_flax_weights_in_pytorch_model,
			)
			from transformers.models.gpta.modeling_flax_gpta import FlaxGPTaLMHeadModel, FlaxGPTaModel

if is_torch_available():
			import torch



class     snake_case__  :
				def __init__(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase=14     ,      _UpperCAmelCase=7     ,      _UpperCAmelCase=True     ,      _UpperCAmelCase=True     ,      _UpperCAmelCase=False     ,      _UpperCAmelCase=True     ,      _UpperCAmelCase=99     ,      _UpperCAmelCase=32     ,      _UpperCAmelCase=5     ,      _UpperCAmelCase=4     ,      _UpperCAmelCase=37     ,      _UpperCAmelCase="gelu"     ,      _UpperCAmelCase=0.1     ,      _UpperCAmelCase=0.1     ,      _UpperCAmelCase=512     ,      _UpperCAmelCase=0.02     ,      ):




										"""simple docstring"""




										__magic_name__				:							Optional[Any]				=      parent
										__magic_name__				:							Optional[Any]				=      batch_size
										__magic_name__				:							int				=      seq_length
										__magic_name__				:							Optional[int]				=      is_training
										__magic_name__				:							Optional[Any]				=      use_input_mask
										__magic_name__				:							Optional[Any]				=      use_token_type_ids
										__magic_name__				:							int				=      use_labels
										__magic_name__				:							Optional[int]				=      vocab_size
										__magic_name__				:							int				=      hidden_size
										__magic_name__				:							int				=      num_hidden_layers
										__magic_name__				:							Dict				=      num_attention_heads
										__magic_name__				:							str				=      intermediate_size
										__magic_name__				:							str				=      hidden_act
										__magic_name__				:							Union[str, Any]				=      hidden_dropout_prob
										__magic_name__				:							Tuple				=      attention_probs_dropout_prob
										__magic_name__				:							List[Any]				=      max_position_embeddings
										__magic_name__				:							Union[str, Any]				=      initializer_range
										__magic_name__				:							Tuple				=      None
										__magic_name__				:							Tuple				=      vocab_size - 1
										__magic_name__				:							List[str]				=      vocab_size - 1
										__magic_name__				:							Any				=      vocab_size - 1
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							List[str]				=      ids_tensor([self.batch_size, self.seq_length]     ,      self.vocab_size      )

										__magic_name__				:							Dict				=      None
										if self.use_input_mask:
																__magic_name__				:							Optional[int]				=      random_attention_mask([self.batch_size, self.seq_length]      )

										__magic_name__				:							Any				=      GPTaConfig(
										    vocab_size=self.vocab_size     ,      n_embd=self.hidden_size     ,      n_layer=self.num_hidden_layers     ,      n_head=self.num_attention_heads     ,      n_positions=self.max_position_embeddings     ,      use_cache=_UpperCAmelCase     ,      bos_token_id=self.bos_token_id     ,      eos_token_id=self.eos_token_id     ,      pad_token_id=self.pad_token_id     ,      )

										return (config, input_ids, input_mask)
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Optional[Any]				=      self.prepare_config_and_inputs()
										__magic_name__		, __magic_name__		, __magic_name__				:							Optional[Any]				=      config_and_inputs
										__magic_name__				:							Optional[Any]				=      {'input_ids': input_ids, 'attention_mask': attention_mask}
										return config, inputs_dict
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__		, __magic_name__		, __magic_name__				:							Union[str, Any]				=      self.prepare_config_and_inputs()

										__magic_name__				:							Union[str, Any]				=      floats_tensor([self.batch_size, self.seq_length, self.hidden_size]      )
										__magic_name__				:							int				=      ids_tensor([self.batch_size, self.seq_length]     ,      vocab_size=2      )

										return (
										    config,
										    input_ids,
										    attention_mask,
										    encoder_hidden_states,
										    encoder_attention_mask,
										)
				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							int				=      20
										__magic_name__				:							Optional[Any]				=      model_class_name(_UpperCAmelCase      )

										__magic_name__				:							Dict				=      model.init_cache(input_ids.shape[0]     ,      _UpperCAmelCase      )
										__magic_name__				:							int				=      jnp.ones((input_ids.shape[0], max_decoder_length)     ,      dtype='i4'      )

										__magic_name__				:							str				=      jnp.broadcast_to(
										    jnp.arange(input_ids.shape[-1] - 1      )[None, :]     ,      (input_ids.shape[0], input_ids.shape[-1] - 1)      )
										__magic_name__				:							Tuple				=      model(
										    input_ids[:, :-1]     ,      attention_mask=_UpperCAmelCase     ,      past_key_values=_UpperCAmelCase     ,      position_ids=_UpperCAmelCase     ,      )

										__magic_name__				:							Tuple				=      jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]     ,      dtype='i4'      )
										__magic_name__				:							str				=      model(
										    input_ids[:, -1:]     ,      attention_mask=_UpperCAmelCase     ,      past_key_values=outputs_cache.past_key_values     ,      position_ids=_UpperCAmelCase     ,      )

										__magic_name__				:							Any				=      model(_UpperCAmelCase      )

										__magic_name__				:							Any				=      np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])      )      )
										self.parent.assertTrue(diff < 1E-3     ,      msg=F"Max diff is {diff}"      )





				def  UpperCamelCase_						(  self     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      ):




										"""simple docstring"""




										__magic_name__				:							List[Any]				=      20
										__magic_name__				:							Dict				=      model_class_name(_UpperCAmelCase      )

										__magic_name__				:							List[str]				=      jnp.concatenate(
										    [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1])      )]     ,      axis=-1     ,      )

										__magic_name__				:							List[Any]				=      model.init_cache(input_ids.shape[0]     ,      _UpperCAmelCase      )
										__magic_name__				:							Union[str, Any]				=      jnp.broadcast_to(
										    jnp.arange(input_ids.shape[-1] - 1      )[None, :]     ,      (input_ids.shape[0], input_ids.shape[-1] - 1)      )

										__magic_name__				:							List[Any]				=      model(
										    input_ids[:, :-1]     ,      attention_mask=_UpperCAmelCase     ,      past_key_values=_UpperCAmelCase     ,      position_ids=_UpperCAmelCase     ,      )
										__magic_name__				:							Tuple				=      jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]     ,      dtype='i4'      )
										__magic_name__				:							Dict				=      model(
										    input_ids[:, -1:]     ,      past_key_values=outputs_cache.past_key_values     ,      attention_mask=_UpperCAmelCase     ,      position_ids=_UpperCAmelCase     ,      )

										__magic_name__				:							int				=      model(_UpperCAmelCase     ,      attention_mask=_UpperCAmelCase      )

										__magic_name__				:							Optional[Any]				=      np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])      )      )
										self.parent.assertTrue(diff < 1E-3     ,      msg=F"Max diff is {diff}"      )






@require_flax
class     snake_case__  (      _A						,       _A						,       unittest.TestCase  ):
				__lowerCAmelCase		=      (FlaxGPTaModel, FlaxGPTaLMHeadModel) if is_flax_available() else ()
				__lowerCAmelCase		=      (FlaxGPTaLMHeadModel,) if is_flax_available() else ()
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Optional[int]				=      FlaxGPTaModelTester(self      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										for model_class_name in self.all_model_classes:
																__magic_name__		, __magic_name__		, __magic_name__				:							List[str]				=      self.model_tester.prepare_config_and_inputs()
																self.model_tester.check_use_cache_forward(_UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      )
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										for model_class_name in self.all_model_classes:
																__magic_name__		, __magic_name__		, __magic_name__				:							Optional[int]				=      self.model_tester.prepare_config_and_inputs()
																self.model_tester.check_use_cache_forward_with_attn_mask(
																    _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase     ,      _UpperCAmelCase      )
				@slow
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__				:							Any				=      GPTaTokenizer.from_pretrained('gpt2'     ,      pad_token='</s>'     ,      padding_side='left'      )
										__magic_name__				:							Optional[Any]				=      tokenizer(['Hello this is a long string', 'Hey']     ,      return_tensors='np'     ,      padding=_UpperCAmelCase     ,      truncation=_UpperCAmelCase      )

										__magic_name__				:							Any				=      FlaxGPTaLMHeadModel.from_pretrained('gpt2'      )
										__magic_name__				:							int				=      False
										__magic_name__				:							Optional[int]				=      model.config.eos_token_id

										__magic_name__				:							Optional[Any]				=      jax.jit(model.generate      )

										__magic_name__				:							Tuple				=      jit_generate(inputs['input_ids']     ,      attention_mask=inputs['attention_mask']      ).sequences

										__magic_name__				:							Union[str, Any]				=      tokenizer.batch_decode(_UpperCAmelCase     ,      skip_special_tokens=_UpperCAmelCase      )

										__magic_name__				:							List[str]				=      [
										    'Hello this is a long string of words. I\'m going to start with the first one.\n',
										    'Hey, I\'m not sure if I\'m going to be able to do',
										]

										self.assertListEqual(_UpperCAmelCase     ,      _UpperCAmelCase      )
				@is_pt_flax_cross_test
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__		, __magic_name__				:							Any				=      self.model_tester.prepare_config_and_inputs_for_common()

										for model_class in self.all_model_classes:
																with self.subTest(model_class.__name__      ):
																						# prepare inputs
																						__magic_name__				:							List[Any]				=      self._prepare_for_class(_UpperCAmelCase     ,      _UpperCAmelCase      )
																						__magic_name__				:							Tuple				=      {k: torch.tensor(v.tolist()      ) for k, v in prepared_inputs_dict.items()}

																						# load corresponding PyTorch class
																						__magic_name__				:							int				=      model_class.__name__[4:]  # Skip the "Flax" at the beginning
																						__magic_name__				:							Optional[int]				=      getattr(_UpperCAmelCase     ,      _UpperCAmelCase      )

																						__magic_name__		, __magic_name__				:							Any				=      pt_inputs['input_ids'].shape
																						__magic_name__				:							int				=      np.random.randint(0     ,      seq_length - 1     ,      size=(batch_size,)      )
																						for batch_idx, start_index in enumerate(_UpperCAmelCase      ):
																												__magic_name__				:							Tuple				=      0
																												__magic_name__				:							str				=      1
																												__magic_name__				:							Dict				=      0
																												__magic_name__				:							List[str]				=      1
																						__magic_name__				:							List[Any]				=      pt_model_class(_UpperCAmelCase      ).eval()
																						__magic_name__				:							Union[str, Any]				=      model_class(_UpperCAmelCase     ,      dtype=jnp.floataa      )

																						__magic_name__				:							List[str]				=      convert_pytorch_state_dict_to_flax(pt_model.state_dict()     ,      _UpperCAmelCase      )
																						__magic_name__				:							Dict				=      fx_state

																						with torch.no_grad():
																												__magic_name__				:							Optional[int]				=      pt_model(**_UpperCAmelCase      ).to_tuple()

																						__magic_name__				:							str				=      fx_model(**_UpperCAmelCase      ).to_tuple()
																						self.assertEqual(len(_UpperCAmelCase      )     ,      len(_UpperCAmelCase      )     ,      'Output lengths differ between Flax and PyTorch'      )
																						for fx_output, pt_output in zip(_UpperCAmelCase     ,      _UpperCAmelCase      ):
																												self.assert_almost_equals(fx_output[:, -1]     ,      pt_output[:, -1].numpy()     ,      4E-2      )

																						with tempfile.TemporaryDirectory() as tmpdirname:
																												pt_model.save_pretrained(_UpperCAmelCase      )
																												__magic_name__				:							List[Any]				=      model_class.from_pretrained(_UpperCAmelCase     ,      from_pt=_UpperCAmelCase      )

																						__magic_name__				:							Any				=      fx_model_loaded(**_UpperCAmelCase      ).to_tuple()
																						self.assertEqual(
																						    len(_UpperCAmelCase      )     ,      len(_UpperCAmelCase      )     ,      'Output lengths differ between Flax and PyTorch'      )
																						for fx_output_loaded, pt_output in zip(_UpperCAmelCase     ,      _UpperCAmelCase      ):
																												self.assert_almost_equals(fx_output_loaded[:, -1]     ,      pt_output[:, -1].numpy()     ,      4E-2      )
				@is_pt_flax_cross_test
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										__magic_name__		, __magic_name__				:							List[str]				=      self.model_tester.prepare_config_and_inputs_for_common()
										for model_class in self.all_model_classes:
																with self.subTest(model_class.__name__      ):
																						# prepare inputs
																						__magic_name__				:							Optional[Any]				=      self._prepare_for_class(_UpperCAmelCase     ,      _UpperCAmelCase      )
																						__magic_name__				:							Dict				=      {k: torch.tensor(v.tolist()      ) for k, v in prepared_inputs_dict.items()}

																						# load corresponding PyTorch class
																						__magic_name__				:							str				=      model_class.__name__[4:]  # Skip the "Flax" at the beginning
																						__magic_name__				:							List[str]				=      getattr(_UpperCAmelCase     ,      _UpperCAmelCase      )

																						__magic_name__				:							Dict				=      pt_model_class(_UpperCAmelCase      ).eval()
																						__magic_name__				:							Optional[int]				=      model_class(_UpperCAmelCase     ,      dtype=jnp.floataa      )

																						__magic_name__				:							List[str]				=      load_flax_weights_in_pytorch_model(_UpperCAmelCase     ,      fx_model.params      )
																						__magic_name__		, __magic_name__				:							str				=      pt_inputs['input_ids'].shape
																						__magic_name__				:							List[Any]				=      np.random.randint(0     ,      seq_length - 1     ,      size=(batch_size,)      )
																						for batch_idx, start_index in enumerate(_UpperCAmelCase      ):
																												__magic_name__				:							Any				=      0
																												__magic_name__				:							Tuple				=      1
																												__magic_name__				:							Any				=      0
																												__magic_name__				:							Union[str, Any]				=      1

																						# make sure weights are tied in PyTorch
																						pt_model.tie_weights()

																						with torch.no_grad():
																												__magic_name__				:							Any				=      pt_model(**_UpperCAmelCase      ).to_tuple()

																						__magic_name__				:							List[Any]				=      fx_model(**_UpperCAmelCase      ).to_tuple()
																						self.assertEqual(len(_UpperCAmelCase      )     ,      len(_UpperCAmelCase      )     ,      'Output lengths differ between Flax and PyTorch'      )
																						for fx_output, pt_output in zip(_UpperCAmelCase     ,      _UpperCAmelCase      ):
																												self.assert_almost_equals(fx_output[:, -1]     ,      pt_output[:, -1].numpy()     ,      4E-2      )

																						with tempfile.TemporaryDirectory() as tmpdirname:
																												fx_model.save_pretrained(_UpperCAmelCase      )
																												__magic_name__				:							List[str]				=      pt_model_class.from_pretrained(_UpperCAmelCase     ,      from_flax=_UpperCAmelCase      )

																						with torch.no_grad():
																												__magic_name__				:							int				=      pt_model_loaded(**_UpperCAmelCase      ).to_tuple()

																						self.assertEqual(
																						    len(_UpperCAmelCase      )     ,      len(_UpperCAmelCase      )     ,      'Output lengths differ between Flax and PyTorch'      )
																						for fx_output, pt_output in zip(_UpperCAmelCase     ,      _UpperCAmelCase      ):
																												self.assert_almost_equals(fx_output[:, -1]     ,      pt_output[:, -1].numpy()     ,      4E-2      )





				@slow
				def  UpperCamelCase_						(  self      ):




										"""simple docstring"""




										for model_class_name in self.all_model_classes:
																__magic_name__				:							Dict				=      model_class_name.from_pretrained('gpt2'     ,      from_pt=_UpperCAmelCase      )
																__magic_name__				:							Any				=      model(np.ones((1, 1)      )      )
																self.assertIsNotNone(_UpperCAmelCase      )






from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.neighbors import KNeighborsClassifier

# Load iris file
snake_case__     							= load_iris()
iris.keys()


print(F"""Target names: \n {iris.target_names} """)
print(F"""\n Features: \n {iris.feature_names}""")

# Train set e Test set
snake_case__							,     snake_case__							,     snake_case__							,     snake_case__     							= train_test_split(
    iris['data'], iris['target'], random_state=4
)

# KNN

snake_case__     							= KNeighborsClassifier(n_neighbors=1)
knn.fit(X_train, y_train)

# new array to test
snake_case__     							= [[1, 2, 1, 4], [2, 3, 4, 5]]

snake_case__     							= knn.predict(X_new)

print(
    F"""\nNew array: \n {X_new}\n\nTarget Names Prediction: \n"""
    F""" {iris['target_names'][prediction]}"""
)


