


import tempfile
import unittest

import numpy as np

import transformers
from transformers import GPTaTokenizer, GPTJConfig, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, tooslow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, 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.gptj.modeling_flax_gptj import FlaxGPTJForCausalLM, FlaxGPTJModel

if is_torch_available():
			import torch


class        _a							:

							def __init__(							self:	List[Any]    ,						UpperCamelCase_:	Union[str, Any]    ,						UpperCamelCase_:	List[Any]=14    ,						UpperCamelCase_:	Dict=7    ,						UpperCamelCase_:	str=True    ,						UpperCamelCase_:	str=True    ,						UpperCamelCase_:	Tuple=False    ,						UpperCamelCase_:	List[Any]=True    ,						UpperCamelCase_:	int=99    ,						UpperCamelCase_:	int=32    ,						UpperCamelCase_:	str=4    ,						UpperCamelCase_:	List[Any]=4    ,						UpperCamelCase_:	List[Any]=4    ,						UpperCamelCase_:	Optional[int]=37    ,						UpperCamelCase_:	Any="gelu"    ,						UpperCamelCase_:	Optional[int]=0.1    ,						UpperCamelCase_:	Any=0.1    ,						UpperCamelCase_:	Tuple=512    ,						UpperCamelCase_:	int=0.02    ,						)			->   Tuple:





									"""simple docstring"""







									lowercase__            =      parent
									lowercase__            =      batch_size
									lowercase__            =      seq_length
									lowercase__            =      is_training
									lowercase__            =      use_input_mask
									lowercase__            =      use_token_type_ids
									lowercase__            =      use_labels
									lowercase__            =      vocab_size
									lowercase__            =      hidden_size
									lowercase__            =      rotary_dim
									lowercase__            =      num_hidden_layers
									lowercase__            =      num_attention_heads
									lowercase__            =      intermediate_size
									lowercase__            =      hidden_act
									lowercase__            =      hidden_dropout_prob
									lowercase__            =      attention_probs_dropout_prob
									lowercase__            =      max_position_embeddings
									lowercase__            =      initializer_range
									lowercase__            =      None
									lowercase__            =      vocab_size - 1
									lowercase__            =      vocab_size - 1
									lowercase__            =      vocab_size - 1

							def       lowerCamelCase_ (							self:	str							)			->   str:





									"""simple docstring"""







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

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

									lowercase__            =      GPTJConfig(
									    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    ,						rotary_dim=self.rotary_dim    ,						)

									return (config, input_ids, input_mask)

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





									"""simple docstring"""







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

							def       lowerCamelCase_ (							self:	Optional[int]    ,						UpperCamelCase_:	Optional[int]    ,						UpperCamelCase_:	List[Any]    ,						UpperCamelCase_:	int    ,						UpperCamelCase_:	int							)			->   Union[str, Any]:





									"""simple docstring"""







									lowercase__            =      20
									lowercase__            =      model_class_name(UpperCamelCase_							)

									lowercase__            =      model.init_cache(input_ids.shape[0]    ,						UpperCamelCase_							)
									lowercase__            =      jnp.ones((input_ids.shape[0], max_decoder_length)    ,						dtype='''i4'''							)

									lowercase__            =      jnp.broadcast_to(
									    jnp.arange(input_ids.shape[-1] - 1							)[None, :]    ,						(input_ids.shape[0], input_ids.shape[-1] - 1)							)
									lowercase__            =      model(
									    input_ids[:, :-1]    ,						attention_mask=UpperCamelCase_    ,						past_key_values=UpperCamelCase_    ,						position_ids=UpperCamelCase_    ,						)

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

									lowercase__            =      model(UpperCamelCase_							)

									lowercase__            =      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       lowerCamelCase_ (							self:	Dict    ,						UpperCamelCase_:	List[Any]    ,						UpperCamelCase_:	Any    ,						UpperCamelCase_:	str    ,						UpperCamelCase_:	Tuple							)			->   Any:





									"""simple docstring"""







									lowercase__            =      20
									lowercase__            =      model_class_name(UpperCamelCase_							)

									lowercase__            =      jnp.concatenate(
									    [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1])							)]    ,						axis=-1    ,						)

									lowercase__            =      model.init_cache(input_ids.shape[0]    ,						UpperCamelCase_							)
									lowercase__            =      jnp.broadcast_to(
									    jnp.arange(input_ids.shape[-1] - 1							)[None, :]    ,						(input_ids.shape[0], input_ids.shape[-1] - 1)							)

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

									lowercase__            =      model(UpperCamelCase_    ,						attention_mask=UpperCamelCase_							)

									lowercase__            =      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        _a							(			UpperCamelCase__     ,	UpperCamelCase__     ,	unittest.TestCase			):
							_lowercase	:			List[Any]        = (FlaxGPTJModel, FlaxGPTJForCausalLM) if is_flax_available() else ()
							_lowercase	:			List[Any]        = (FlaxGPTJForCausalLM,) if is_flax_available() else ()

							def       lowerCamelCase_ (							self:	Tuple							)			->   Optional[int]:





									"""simple docstring"""







									lowercase__            =      FlaxGPTJModelTester(self							)

							def       lowerCamelCase_ (							self:	Any							)			->   Tuple:





									"""simple docstring"""







									for model_class_name in self.all_model_classes:
											lowercase__     ,		lowercase__     ,		lowercase__            =      self.model_tester.prepare_config_and_inputs()
											self.model_tester.check_use_cache_forward(UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_							)

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





									"""simple docstring"""







									for model_class_name in self.all_model_classes:
											lowercase__     ,		lowercase__     ,		lowercase__            =      self.model_tester.prepare_config_and_inputs()
											self.model_tester.check_use_cache_forward_with_attn_mask(
											    UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_    ,						UpperCamelCase_							)

							@tooslow
							def       lowerCamelCase_ (							self:	Optional[int]							)			->   List[str]:





									"""simple docstring"""







									lowercase__            =      GPTaTokenizer.from_pretrained('''gpt2'''    ,						pad_token='''<|endoftext|>'''    ,						padding_side='''left'''							)
									lowercase__            =      tokenizer(['''Hello this is a long string''', '''Hey''']    ,						return_tensors='''np'''    ,						padding=UpperCamelCase_    ,						truncation=UpperCamelCase_							)

									lowercase__            =      FlaxGPTJForCausalLM.from_pretrained('''EleutherAI/gpt-j-6B'''							)
									lowercase__            =      False
									lowercase__            =      model.config.eos_token_id

									lowercase__            =      jax.jit(model.generate							)

									lowercase__            =      jit_generate(
									    inputs['''input_ids''']    ,						attention_mask=inputs['''attention_mask''']    ,						pad_token_id=tokenizer.pad_token_id							).sequences

									lowercase__            =      tokenizer.batch_decode(UpperCamelCase_    ,						skip_special_tokens=UpperCamelCase_							)

									lowercase__            =      [
									    '''Hello this is a long string of text.\n\nI\'m trying to get the text of the''',
									    '''Hey, I\'m a little late to the party. I\'m going to''',
									]

									self.assertListEqual(UpperCamelCase_    ,						UpperCamelCase_							)

							@is_pt_flax_cross_test
							def       lowerCamelCase_ (							self:	List[str]							)			->   str:





									"""simple docstring"""







									lowercase__     ,		lowercase__            =      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
													lowercase__            =      self._prepare_for_class(UpperCamelCase_    ,						UpperCamelCase_							)
													lowercase__            =      {k: torch.tensor(v.tolist()							) for k, v in prepared_inputs_dict.items()}

													# load corresponding PyTorch class
													lowercase__            =      model_class.__name__[4:]  # Skip the "Flax" at the beginning
													lowercase__            =      getattr(UpperCamelCase_    ,						UpperCamelCase_							)

													lowercase__     ,		lowercase__            =      pt_inputs['''input_ids'''].shape
													lowercase__            =      np.random.randint(0    ,						seq_length - 1    ,						size=(batch_size,)							)
													for batch_idx, start_index in enumerate(UpperCamelCase_							):
															lowercase__            =      0
															lowercase__            =      1
															lowercase__            =      0
															lowercase__            =      1
													lowercase__            =      pt_model_class(UpperCamelCase_							).eval()
													lowercase__            =      model_class(UpperCamelCase_    ,						dtype=jnp.floataa							)

													lowercase__            =      convert_pytorch_state_dict_to_flax(pt_model.state_dict()    ,						UpperCamelCase_							)
													lowercase__            =      fx_state

													with torch.no_grad():
															lowercase__            =      pt_model(**UpperCamelCase_							).to_tuple()

													lowercase__            =      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_							)
															lowercase__            =      model_class.from_pretrained(UpperCamelCase_    ,						from_pt=UpperCamelCase_							)

													lowercase__            =      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       lowerCamelCase_ (							self:	Dict							)			->   str:





									"""simple docstring"""







									lowercase__     ,		lowercase__            =      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
													lowercase__            =      self._prepare_for_class(UpperCamelCase_    ,						UpperCamelCase_							)
													lowercase__            =      {k: torch.tensor(v.tolist()							) for k, v in prepared_inputs_dict.items()}

													# load corresponding PyTorch class
													lowercase__            =      model_class.__name__[4:]  # Skip the "Flax" at the beginning
													lowercase__            =      getattr(UpperCamelCase_    ,						UpperCamelCase_							)

													lowercase__            =      pt_model_class(UpperCamelCase_							).eval()
													lowercase__            =      model_class(UpperCamelCase_    ,						dtype=jnp.floataa							)

													lowercase__            =      load_flax_weights_in_pytorch_model(UpperCamelCase_    ,						fx_model.params							)
													lowercase__     ,		lowercase__            =      pt_inputs['''input_ids'''].shape
													lowercase__            =      np.random.randint(0    ,						seq_length - 1    ,						size=(batch_size,)							)
													for batch_idx, start_index in enumerate(UpperCamelCase_							):
															lowercase__            =      0
															lowercase__            =      1
															lowercase__            =      0
															lowercase__            =      1

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

													with torch.no_grad():
															lowercase__            =      pt_model(**UpperCamelCase_							).to_tuple()

													lowercase__            =      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_							)
															lowercase__            =      pt_model_class.from_pretrained(UpperCamelCase_    ,						from_flax=UpperCamelCase_							)

													with torch.no_grad():
															lowercase__            =      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							)


							@tooslow
							def       lowerCamelCase_ (							self:	Optional[Any]							)			->   int:





									"""simple docstring"""







									for model_class_name in self.all_model_classes:
											lowercase__            =      model_class_name.from_pretrained('''EleutherAI/gpt-j-6B'''							)
											lowercase__            =      model(np.ones((1, 1)							)							)
											self.assertIsNotNone(UpperCamelCase_							)






from __future__ import annotations
def 			_a (      SCREAMING_SNAKE_CASE						,				SCREAMING_SNAKE_CASE     ):


		"""simple docstring"""




		lowercase__            =      0
		lowercase__            =      len(SCREAMING_SNAKE_CASE     ) - 1

		while i < j:
				if nums[i] + nums[j] == target:
						return [i, j]
				elif nums[i] + nums[j] < target:
						lowercase__            =      i + 1
				else:
						lowercase__            =      j - 1

		return []


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			print(f"""{two_pointer([2, 7, 11, 15], 9) = }""")


