
'''simple docstring'''
import unittest

from transformers import GPTNeoXJapaneseConfig, is_torch_available
from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
				import torch

				from transformers import GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel


class       _lowerCAmelCase   :







							'''simple docstring'''

							def __init__(self  ,  UpperCAmelCase  ,  UpperCAmelCase=13  ,  UpperCAmelCase=7  ,  UpperCAmelCase=True  ,  UpperCAmelCase=True  ,  UpperCAmelCase=True  ,  UpperCAmelCase=True  ,  UpperCAmelCase=99  ,  UpperCAmelCase=32  ,  UpperCAmelCase=5  ,  UpperCAmelCase=4  ,  UpperCAmelCase=4  ,  UpperCAmelCase="gelu"  ,  UpperCAmelCase=0.0  ,  UpperCAmelCase=0.1  ,  UpperCAmelCase=True  ,  UpperCAmelCase=512  ,  UpperCAmelCase=16  ,  UpperCAmelCase=2  ,  UpperCAmelCase=0.02  ,  UpperCAmelCase=3  ,  UpperCAmelCase=4  ,  UpperCAmelCase=None  ,  ):
														_snake_case       =  parent
														_snake_case       =  batch_size
														_snake_case       =  seq_length
														_snake_case       =  is_training
														_snake_case       =  use_input_mask
														_snake_case       =  use_token_type_ids
														_snake_case       =  use_labels
														_snake_case       =  vocab_size
														_snake_case       =  hidden_size
														_snake_case       =  num_hidden_layers
														_snake_case       =  num_attention_heads
														_snake_case       =  intermediate_multiple_size
														_snake_case       =  hidden_act
														_snake_case       =  hidden_dropout
														_snake_case       =  attention_dropout
														_snake_case       =  weight_tying
														_snake_case       =  max_position_embeddings
														_snake_case       =  type_vocab_size
														_snake_case       =  type_sequence_label_size
														_snake_case       =  initializer_range
														_snake_case       =  num_labels
														_snake_case       =  num_choices
														_snake_case       =  scope

							def 	lowercase							(self  ):
														_snake_case       =  ids_tensor([self.batch_size, self.seq_length]  ,  self.vocab_size  )

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

														_snake_case       =  None
														if self.use_labels:
																					_snake_case       =  ids_tensor([self.batch_size, self.seq_length]  ,  self.num_labels  )

														_snake_case       =  self.get_config()

														return config, input_ids, input_mask, token_labels

							def 	lowercase							(self  ):
														return GPTNeoXJapaneseConfig(
														    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_multiple_size=self.intermediate_multiple_size  ,  hidden_act=self.hidden_act  ,  hidden_dropout=self.hidden_dropout  ,  attention_dropout=self.attention_dropout  ,  weight_tying=self.weight_tying  ,  max_position_embeddings=self.max_position_embeddings  ,  type_vocab_size=self.type_vocab_size  ,  is_decoder=UpperCAmelCase  ,  initializer_range=self.initializer_range  ,  )

							def 	lowercase							(self  ):
														_snake_case,							_snake_case,							_snake_case,							_snake_case       =  self.prepare_config_and_inputs()

														_snake_case       =  True

														return config, input_ids, input_mask, token_labels

							def 	lowercase							(self  ,  UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  ):
														_snake_case       =  GPTNeoXJapaneseModel(config=UpperCAmelCase  )
														model.to(UpperCAmelCase  )
														model.eval()
														_snake_case       =  model(UpperCAmelCase  ,  attention_mask=UpperCAmelCase  )
														_snake_case       =  model(UpperCAmelCase  )
														self.parent.assertEqual(result.last_hidden_state.shape  ,  (self.batch_size, self.seq_length, self.hidden_size)  )

							def 	lowercase							(self  ,  UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  ):
														_snake_case       =  True
														_snake_case       =  GPTNeoXJapaneseModel(UpperCAmelCase  )
														model.to(UpperCAmelCase  )
														model.eval()
														_snake_case       =  model(UpperCAmelCase  ,  attention_mask=UpperCAmelCase  )
														self.parent.assertEqual(result.last_hidden_state.shape  ,  (self.batch_size, self.seq_length, self.hidden_size)  )

							def 	lowercase							(self  ,  UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  ):
														_snake_case       =  GPTNeoXJapaneseForCausalLM(config=UpperCAmelCase  )
														model.to(UpperCAmelCase  )
														model.eval()
														_snake_case       =  model(UpperCAmelCase  ,  attention_mask=UpperCAmelCase  ,  labels=UpperCAmelCase  )
														self.parent.assertEqual(result.logits.shape  ,  (self.batch_size, self.seq_length, self.vocab_size)  )

							def 	lowercase							(self  ,  UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  ):
														_snake_case       =  True
														_snake_case       =  GPTNeoXJapaneseForCausalLM(config=UpperCAmelCase  )
														model.to(UpperCAmelCase  )
														model.eval()

														# first forward pass
														_snake_case       =  model(UpperCAmelCase  ,  attention_mask=UpperCAmelCase  ,  use_cache=UpperCAmelCase  )
														_snake_case       =  outputs.past_key_values

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

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

														_snake_case       =  model(UpperCAmelCase  ,  attention_mask=UpperCAmelCase  ,  output_hidden_states=UpperCAmelCase  )
														_snake_case       =  output_from_no_past["""hidden_states"""][0]
														_snake_case       =  model(
														    UpperCAmelCase  ,  attention_mask=UpperCAmelCase  ,  past_key_values=UpperCAmelCase  ,  output_hidden_states=UpperCAmelCase  ,  )["""hidden_states"""][0]

														# select random slice
														_snake_case       =  ids_tensor((1,)  ,  output_from_past.shape[-1]  ).item()
														_snake_case       =  output_from_no_past[:, -3:, random_slice_idx].detach()
														_snake_case       =  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(UpperCAmelCase  ,  UpperCAmelCase  ,  atol=1e-3  )  )





							def 	lowercase							(self  ):
														_snake_case       =  self.prepare_config_and_inputs()
														_snake_case,							_snake_case,							_snake_case,							_snake_case       =  config_and_inputs
														_snake_case       =  {"""input_ids""": input_ids, """attention_mask""": input_mask}
														return config, inputs_dict


@require_torch
class       _lowerCAmelCase   (   __snake_case					,							__snake_case					,							unittest.TestCase				):







							'''simple docstring'''

							lowerCAmelCase_ 						=						(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
							lowerCAmelCase_ 						=						(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
							lowerCAmelCase_ 						=						(
							    {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM}
							    if is_torch_available()
							    else {}
							)
							lowerCAmelCase_ 						=						False
							lowerCAmelCase_ 						=						False
							lowerCAmelCase_ 						=						False
							lowerCAmelCase_ 						=						False

							def 	lowercase							(self  ):
														_snake_case       =  GPTNeoXJapaneseModelTester(self  )
														_snake_case       =  ConfigTester(self  ,  config_class=UpperCAmelCase  ,  hidden_size=37  )

							def 	lowercase							(self  ):
														self.config_tester.run_common_tests()

							def 	lowercase							(self  ):
														_snake_case,							_snake_case,							_snake_case,							_snake_case       =  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_model(UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  )

							def 	lowercase							(self  ):
														_snake_case,							_snake_case,							_snake_case,							_snake_case       =  self.model_tester.prepare_config_and_inputs_for_decoder()
														self.model_tester.create_and_check_model_as_decoder(UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  )

							def 	lowercase							(self  ):
														# This regression test was failing with PyTorch < 1.3
														_snake_case,							_snake_case,							_snake_case,							_snake_case       =  self.model_tester.prepare_config_and_inputs_for_decoder()

														_snake_case       =  None

														self.model_tester.create_and_check_model_as_decoder(UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  )

							def 	lowercase							(self  ):
														_snake_case,							_snake_case,							_snake_case,							_snake_case       =  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_decoder_model_past_large_inputs(UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  )

							def 	lowercase							(self  ):
														_snake_case       =  self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_for_causal_lm(*UpperCAmelCase  )





							@slow
							def 	lowercase							(self  ):
														_snake_case       =  """abeja/gpt-neox-japanese-2.7b"""

														_snake_case       =  ["""データサイエンティストとは、""", """100年後に必要とされる会社は、""", """フルリモートの環境で働くために必要なことは、""", """国境の長いトンネルを抜けると""", """美味しい日本食といえば、"""]

														_snake_case       =  [
														    """データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。""",
														    """100年後に必要とされる会社は、「人」が中心の会社です。""",
														    """フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。""",
														    """国境の長いトンネルを抜けると、そこは雪国だった。""",
														    """美味しい日本食といえば、やっぱりお寿司ですよね。""",
														]

														_snake_case       =  GPTNeoXJapaneseTokenizer.from_pretrained(UpperCAmelCase  )
														_snake_case       =  GPTNeoXJapaneseForCausalLM.from_pretrained(UpperCAmelCase  )

														_snake_case       =  []
														for prompt in prompts:
																					_snake_case       =  tokenizer(UpperCAmelCase  ,  return_tensors="""pt"""  ).input_ids
																					_snake_case       =  model.generate(UpperCAmelCase  ,  max_length=50  )
																					_snake_case       =  tokenizer.batch_decode(UpperCAmelCase  ,  skip_special_tokens=UpperCAmelCase  )
																					predicted_outputs += generated_string
														self.assertListEqual(UpperCAmelCase  ,  UpperCAmelCase  )

'''simple docstring'''
import numpy as np
from cva import destroyAllWindows, imread, imshow, waitKey


class       _lowerCAmelCase   :







							'''simple docstring'''

							def __init__(self  ,  UpperCAmelCase  ,  UpperCAmelCase  ,  UpperCAmelCase  )					->       List[str]:
														if dst_width < 0 or dst_height < 0:
																					raise ValueError("""Destination width/height should be > 0"""  )

														_snake_case       =  img
														_snake_case       =  img.shape[1]
														_snake_case       =  img.shape[0]
														_snake_case       =  dst_width
														_snake_case       =  dst_height

														_snake_case       =  self.src_w / self.dst_w
														_snake_case       =  self.src_h / self.dst_h

														_snake_case       =  _snake_case       =  (
														    np.ones((self.dst_h, self.dst_w, 3)  ,  np.uinta  ) * 255
														)

							def 	lowercase							(self  )					->       List[Any]:
														for i in range(self.dst_h  ):
																					for j in range(self.dst_w  ):
																												_snake_case       =  self.img[self.get_y(UpperCAmelCase  )][self.get_x(UpperCAmelCase  )]

							def 	lowercase							(self  ,  UpperCAmelCase  )					->       int:
														return int(self.ratio_x * x  )





							def 	lowercase							(self  ,  UpperCAmelCase  )					->       int:
														return int(self.ratio_y * y  )


if __name__ == "__main__":
				__lowerCAmelCase							,    __lowerCAmelCase												=	800, 600
				__lowerCAmelCase												=	imread('image_data/lena.jpg', 1)
				__lowerCAmelCase												=	NearestNeighbour(im, dst_w, dst_h)
				n.process()

				imshow(
				    f'''Image resized from: {im.shape[1]}x{im.shape[0]} to {dst_w}x{dst_h}''', n.output
				)
				waitKey(0)
				destroyAllWindows()