


"""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     _UpperCamelCase    :
							'''simple docstring'''







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

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     ids_tensor([self.batch_size, self.seq_length]       ,  self.vocab_size      )

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

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

										__lowerCAmelCase							       =     self.get_config()

										return config, input_ids, input_mask, token_labels

							def      snake_case							(	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=__a       ,  initializer_range=self.initializer_range       ,  )

							def      snake_case							(	self      ):
										__lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase							       =     self.prepare_config_and_inputs()

										__lowerCAmelCase							       =     True

										return config, input_ids, input_mask, token_labels

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     GPTNeoXJapaneseModel(config=__a      )
										model.to(__a      )
										model.eval()
										__lowerCAmelCase							       =     model(__a       ,  attention_mask=__a      )
										__lowerCAmelCase							       =     model(__a      )
										self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.hidden_size)      )

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     True
										__lowerCAmelCase							       =     GPTNeoXJapaneseModel(__a      )
										model.to(__a      )
										model.eval()
										__lowerCAmelCase							       =     model(__a       ,  attention_mask=__a      )
										self.parent.assertEqual(result.last_hidden_state.shape       ,  (self.batch_size, self.seq_length, self.hidden_size)      )

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     GPTNeoXJapaneseForCausalLM(config=__a      )
										model.to(__a      )
										model.eval()
										__lowerCAmelCase							       =     model(__a       ,  attention_mask=__a       ,  labels=__a      )
										self.parent.assertEqual(result.logits.shape       ,  (self.batch_size, self.seq_length, self.vocab_size)      )

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     True
										__lowerCAmelCase							       =     GPTNeoXJapaneseForCausalLM(config=__a      )
										model.to(__a      )
										model.eval()

										# first forward pass
										__lowerCAmelCase							       =     model(__a       ,  attention_mask=__a       ,  use_cache=__a      )
										__lowerCAmelCase							       =     outputs.past_key_values

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

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

										__lowerCAmelCase							       =     model(__a       ,  attention_mask=__a       ,  output_hidden_states=__a      )
										__lowerCAmelCase							       =     output_from_no_past["hidden_states"][0]
										__lowerCAmelCase							       =     model(
										    __a       ,  attention_mask=__a       ,  past_key_values=__a       ,  output_hidden_states=__a       ,  )["hidden_states"][0]

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



							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.prepare_config_and_inputs()
										__lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase							       =     config_and_inputs
										__lowerCAmelCase							       =     {"input_ids": input_ids, "attention_mask": input_mask}
										return config, inputs_dict
@require_torch
class     _UpperCamelCase    (  lowerCAmelCase__			,lowerCAmelCase__			,unittest.TestCase						):
							'''simple docstring'''







							__UpperCAmelCase		:       Dict							 =(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
							__UpperCAmelCase		:       int							 =(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
							__UpperCAmelCase		:       int							 =(
							    {"""feature-extraction""": GPTNeoXJapaneseModel, """text-generation""": GPTNeoXJapaneseForCausalLM}
							    if is_torch_available()
							    else {}
							)
							__UpperCAmelCase		:       int							 =False
							__UpperCAmelCase		:       Dict							 =False
							__UpperCAmelCase		:       List[str]							 =False
							__UpperCAmelCase		:       Optional[Any]							 =False

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     GPTNeoXJapaneseModelTester(self      )
										__lowerCAmelCase							       =     ConfigTester(self       ,  config_class=__a       ,  hidden_size=37      )

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

							def      snake_case							(	self      ):
										__lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_model(__a       ,  __a       ,  __a      )

							def      snake_case							(	self      ):
										__lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs_for_decoder()
										self.model_tester.create_and_check_model_as_decoder(__a       ,  __a       ,  __a      )

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

										__lowerCAmelCase							       =     None

										self.model_tester.create_and_check_model_as_decoder(__a       ,  __a       ,  __a      )

							def      snake_case							(	self      ):
										__lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase       ,      __lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_decoder_model_past_large_inputs(__a       ,  __a       ,  __a      )

							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_for_causal_lm(*__a      )



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

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

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

										__lowerCAmelCase							       =     GPTNeoXJapaneseTokenizer.from_pretrained(__a      )
										__lowerCAmelCase							       =     GPTNeoXJapaneseForCausalLM.from_pretrained(__a      )

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








"""simple docstring"""


import unittest

from transformers import MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING, is_vision_available
from transformers.pipelines import pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
	from PIL import Image
else:
	class     _UpperCamelCase    :
								'''simple docstring'''







								@staticmethod
								def      snake_case							(	*__a       ,  **__a      ):
											pass
@is_pipeline_test
@require_torch
@require_vision
class     _UpperCamelCase    (  unittest.TestCase						):
							'''simple docstring'''







							__UpperCAmelCase		:       str							 =MODEL_FOR_VISUAL_QUESTION_ANSWERING_MAPPING

							def      snake_case							(	self       ,  __a       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     pipeline("visual-question-answering"       ,  model="hf-internal-testing/tiny-vilt-random-vqa"      )
										__lowerCAmelCase							       =     [
										    {
										        "image": Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      ),
										        "question": "How many cats are there?",
										    },
										    {
										        "image": "./tests/fixtures/tests_samples/COCO/000000039769.png",
										        "question": "How many cats are there?",
										    },
										]
										return vqa_pipeline, examples

							def      snake_case							(	self       ,  __a       ,  __a      ):
										__lowerCAmelCase							       =     vqa_pipeline(__a       ,  top_k=1      )
										self.assertEqual(
										    __a       ,  [
										        [{"score": ANY(__a      ), "answer": ANY(__a      )}],
										        [{"score": ANY(__a      ), "answer": ANY(__a      )}],
										    ]       ,  )

							@require_torch
							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     pipeline("visual-question-answering"       ,  model="hf-internal-testing/tiny-vilt-random-vqa"      )
										__lowerCAmelCase							       =     "./tests/fixtures/tests_samples/COCO/000000039769.png"
										__lowerCAmelCase							       =     "How many cats are there?"

										__lowerCAmelCase							       =     vqa_pipeline(image=__a       ,  question="How many cats are there?"       ,  top_k=2      )
										self.assertEqual(
										    __a       ,  [{"score": ANY(__a      ), "answer": ANY(__a      )}, {"score": ANY(__a      ), "answer": ANY(__a      )}]      )

										__lowerCAmelCase							       =     vqa_pipeline({"image": image, "question": question}       ,  top_k=2      )
										self.assertEqual(
										    __a       ,  [{"score": ANY(__a      ), "answer": ANY(__a      )}, {"score": ANY(__a      ), "answer": ANY(__a      )}]      )

							@slow
							@require_torch
							def      snake_case							(	self      ):
										__lowerCAmelCase							       =     pipeline("visual-question-answering"       ,  model="dandelin/vilt-b32-finetuned-vqa"      )
										__lowerCAmelCase							       =     "./tests/fixtures/tests_samples/COCO/000000039769.png"
										__lowerCAmelCase							       =     "How many cats are there?"

										__lowerCAmelCase							       =     vqa_pipeline(image=__a       ,  question=__a       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  decimals=4      )       ,  [{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]      )

										__lowerCAmelCase							       =     vqa_pipeline({"image": image, "question": question}       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  decimals=4      )       ,  [{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]      )

										__lowerCAmelCase							       =     vqa_pipeline(
										    [{"image": image, "question": question}, {"image": image, "question": question}]       ,  top_k=2      )
										self.assertEqual(
										    nested_simplify(__a       ,  decimals=4      )       ,  [[{"score": 0.8_7_9_9, "answer": "2"}, {"score": 0.2_9_6, "answer": "1"}]] * 2       ,  )



							@require_tf
							@unittest.skip("Visual question answering not implemented in TF"      )
							def      snake_case							(	self      ):
										pass




