






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__ :







		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__=512       ,A__=16       ,A__=2       ,A__=0.02       ,A__=3       ,A__=4       ,A__=None       ,):
						_A	:				Union[str, Any]   =      parent
						_A	:				Union[str, Any]   =      batch_size
						_A	:				Tuple   =      seq_length
						_A	:				List[Any]   =      is_training
						_A	:				Optional[int]   =      use_input_mask
						_A	:				Optional[int]   =      use_token_type_ids
						_A	:				int   =      use_labels
						_A	:				int   =      vocab_size
						_A	:				Union[str, Any]   =      hidden_size
						_A	:				Optional[int]   =      num_hidden_layers
						_A	:				Dict   =      num_attention_heads
						_A	:				Optional[int]   =      intermediate_multiple_size
						_A	:				Optional[int]   =      hidden_act
						_A	:				Any   =      hidden_dropout
						_A	:				Optional[Any]   =      attention_dropout
						_A	:				Dict   =      weight_tying
						_A	:				Union[str, Any]   =      max_position_embeddings
						_A	:				Optional[int]   =      type_vocab_size
						_A	:				Tuple   =      type_sequence_label_size
						_A	:				Dict   =      initializer_range
						_A	:				List[Any]   =      num_labels
						_A	:				Union[str, Any]   =      num_choices
						_A	:				List[Any]   =      scope







		def  A__      (					self		):
						_A	:				List[str]   =      ids_tensor([self.batch_size, self.seq_length]       ,self.vocab_size		)

						_A	:				List[str]   =      None
						if self.use_input_mask:
										_A	:				Optional[Any]   =      random_attention_mask([self.batch_size, self.seq_length]		)

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

						_A	:				List[str]   =      self.get_config()

						return config, input_ids, input_mask, token_labels







		def  A__      (					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  A__      (					self		):
						_A  ,				_A  ,				_A  ,				_A	:				Any   =      self.prepare_config_and_inputs()

						_A	:				Union[str, Any]   =      True

						return config, input_ids, input_mask, token_labels







		def  A__      (					self       ,A__       ,A__       ,A__		):
						_A	:				Tuple   =      GPTNeoXJapaneseModel(config=A__		)
						model.to(A__		)
						model.eval()
						_A	:				int   =      model(A__       ,attention_mask=A__		)
						_A	:				str   =      model(A__		)
						self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, self.seq_length, self.hidden_size)		)







		def  A__      (					self       ,A__       ,A__       ,A__		):
						_A	:				Optional[Any]   =      True
						_A	:				Optional[int]   =      GPTNeoXJapaneseModel(A__		)
						model.to(A__		)
						model.eval()
						_A	:				Optional[Any]   =      model(A__       ,attention_mask=A__		)
						self.parent.assertEqual(result.last_hidden_state.shape       ,(self.batch_size, self.seq_length, self.hidden_size)		)







		def  A__      (					self       ,A__       ,A__       ,A__       ,A__		):
						_A	:				Dict   =      GPTNeoXJapaneseForCausalLM(config=A__		)
						model.to(A__		)
						model.eval()
						_A	:				Optional[Any]   =      model(A__       ,attention_mask=A__       ,labels=A__		)
						self.parent.assertEqual(result.logits.shape       ,(self.batch_size, self.seq_length, self.vocab_size)		)







		def  A__      (					self       ,A__       ,A__       ,A__		):
						_A	:				Optional[Any]   =      True
						_A	:				Optional[int]   =      GPTNeoXJapaneseForCausalLM(config=A__		)
						model.to(A__		)
						model.eval()

						# first forward pass
						_A	:				Optional[int]   =      model(A__       ,attention_mask=A__       ,use_cache=A__		)
						_A	:				Optional[int]   =      outputs.past_key_values

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

						# append to next input_ids and
						_A	:				Optional[Any]   =      torch.cat([input_ids, next_tokens]       ,dim=-1		)
						_A	:				Union[str, Any]   =      torch.cat([input_mask, next_mask]       ,dim=-1		)

						_A	:				Optional[int]   =      model(A__       ,attention_mask=A__       ,output_hidden_states=A__		)
						_A	:				str   =      output_from_no_past['''hidden_states'''][0]
						_A	:				Dict   =      model(
						    A__       ,attention_mask=A__       ,past_key_values=A__       ,output_hidden_states=A__       ,)['''hidden_states'''][0]

						# select random slice
						_A	:				Optional[Any]   =      ids_tensor((1,)       ,output_from_past.shape[-1]		).item()
						_A	:				str   =      output_from_no_past[:, -3:, random_slice_idx].detach()
						_A	:				List[Any]   =      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  A__      (					self		):
						_A	:				str   =      self.prepare_config_and_inputs()
						_A  ,				_A  ,				_A  ,				_A	:				Optional[Any]   =      config_and_inputs
						_A	:				int   =      {'''input_ids''': input_ids, '''attention_mask''': input_mask}
						return config, inputs_dict






@require_torch
class 						UpperCAmelCase__ (							__snake_case     ,				__snake_case     ,				unittest.TestCase							):
		__snake_case      :       int     =   (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
		__snake_case      :       Optional[int]     =   (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
		__snake_case      :       str     =   (
		    {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM}
		    if is_torch_available()
		    else {}
		)
		__snake_case      :       str     =   False
		__snake_case      :       Dict     =   False
		__snake_case      :       str     =   False
		__snake_case      :       Dict     =   False







		def  A__      (					self		):
						_A	:				Union[str, Any]   =      GPTNeoXJapaneseModelTester(self		)
						_A	:				str   =      ConfigTester(self       ,config_class=A__       ,hidden_size=37		)







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







		def  A__      (					self		):
						_A  ,				_A  ,				_A  ,				_A	:				List[str]   =      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_model(A__       ,A__       ,A__		)







		def  A__      (					self		):
						_A  ,				_A  ,				_A  ,				_A	:				List[Any]   =      self.model_tester.prepare_config_and_inputs_for_decoder()
						self.model_tester.create_and_check_model_as_decoder(A__       ,A__       ,A__		)







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

						_A	:				str   =      None

						self.model_tester.create_and_check_model_as_decoder(A__       ,A__       ,A__		)







		def  A__      (					self		):
						_A  ,				_A  ,				_A  ,				_A	:				List[str]   =      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_decoder_model_past_large_inputs(A__       ,A__       ,A__		)







		def  A__      (					self		):
						_A	:				List[Any]   =      self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_for_causal_lm(*A__		)







		@slow
		def  A__      (					self		):
						_A	:				List[Any]   =      '''abeja/gpt-neox-japanese-2.7b'''

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

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

						_A	:				str   =      GPTNeoXJapaneseTokenizer.from_pretrained(A__		)
						_A	:				Any   =      GPTNeoXJapaneseForCausalLM.from_pretrained(A__		)

						_A	:				Tuple   =      []
						for prompt in prompts:
										_A	:				Optional[int]   =      tokenizer(A__       ,return_tensors='''pt'''		).input_ids
										_A	:				str   =      model.generate(A__       ,max_length=50		)
										_A	:				Dict   =      tokenizer.batch_decode(A__       ,skip_special_tokens=A__		)
										predicted_outputs += generated_string
						self.assertListEqual(A__       ,A__		)











from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import (
    center_crop,
    convert_to_rgb,
    get_resize_output_image_size,
    normalize,
    rescale,
    resize,
    to_channel_dimension_format,
)
from ...image_utils import (
    OPENAI_CLIP_MEAN,
    OPENAI_CLIP_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, is_vision_available, logging


_UpperCamelCase			:	Any								=logging.get_logger(__name__)


if is_vision_available():
	import PIL






class 						UpperCAmelCase__ (							__snake_case							):
		__snake_case      :       Union[str, Any]     =   ["pixel_values"]







		def __init__(					self       ,A__ = True       ,A__ = None       ,A__ = PILImageResampling.BICUBIC       ,A__ = True       ,A__ = None       ,A__ = True       ,A__ = 1 / 255       ,A__ = True       ,A__ = None       ,A__ = None       ,A__ = True       ,**A__       ,):
						super().__init__(**A__		)
						_A	:				List[str]   =      size if size is not None else {'''shortest_edge''': 224}
						_A	:				int   =      get_size_dict(A__       ,default_to_square=A__		)
						_A	:				Optional[int]   =      crop_size if crop_size is not None else {'''height''': 224, '''width''': 224}
						_A	:				Dict   =      get_size_dict(A__       ,default_to_square=A__       ,param_name='''crop_size'''		)

						_A	:				List[str]   =      do_resize
						_A	:				List[str]   =      size
						_A	:				Tuple   =      resample
						_A	:				List[Any]   =      do_center_crop
						_A	:				Dict   =      crop_size
						_A	:				Any   =      do_rescale
						_A	:				Optional[Any]   =      rescale_factor
						_A	:				Dict   =      do_normalize
						_A	:				Optional[int]   =      image_mean if image_mean is not None else OPENAI_CLIP_MEAN
						_A	:				Optional[int]   =      image_std if image_std is not None else OPENAI_CLIP_STD
						_A	:				Any   =      do_convert_rgb







		def  A__      (					self       ,A__       ,A__       ,A__ = PILImageResampling.BICUBIC       ,A__ = None       ,**A__       ,):
						_A	:				List[Any]   =      get_size_dict(A__       ,default_to_square=A__		)
						if "shortest_edge" not in size:
										raise ValueError(f"""The `size` parameter must contain the key `shortest_edge`. Got {size.keys()}"""		)
						_A	:				Any   =      get_resize_output_image_size(A__       ,size=size['''shortest_edge''']       ,default_to_square=A__		)
						return resize(A__       ,size=A__       ,resample=A__       ,data_format=A__       ,**A__		)







		def  A__      (					self       ,A__       ,A__       ,A__ = None       ,**A__       ,):
						_A	:				Union[str, Any]   =      get_size_dict(A__		)
						if "height" not in size or "width" not in size:
										raise ValueError(f"""The `size` parameter must contain the keys (height, width). Got {size.keys()}"""		)
						return center_crop(A__       ,size=(size['''height'''], size['''width'''])       ,data_format=A__       ,**A__		)







		def  A__      (					self       ,A__       ,A__       ,A__ = None       ,**A__       ,):
						return rescale(A__       ,scale=A__       ,data_format=A__       ,**A__		)







		def  A__      (					self       ,A__       ,A__       ,A__       ,A__ = None       ,**A__       ,):
						return normalize(A__       ,mean=A__       ,std=A__       ,data_format=A__       ,**A__		)







		def  A__      (					self       ,A__       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = None       ,A__ = ChannelDimension.FIRST       ,**A__       ,):
						_A	:				str   =      do_resize if do_resize is not None else self.do_resize
						_A	:				List[str]   =      size if size is not None else self.size
						_A	:				List[str]   =      get_size_dict(A__       ,param_name='''size'''       ,default_to_square=A__		)
						_A	:				Optional[Any]   =      resample if resample is not None else self.resample
						_A	:				int   =      do_center_crop if do_center_crop is not None else self.do_center_crop
						_A	:				Any   =      crop_size if crop_size is not None else self.crop_size
						_A	:				int   =      get_size_dict(A__       ,param_name='''crop_size'''       ,default_to_square=A__		)
						_A	:				int   =      do_rescale if do_rescale is not None else self.do_rescale
						_A	:				List[str]   =      rescale_factor if rescale_factor is not None else self.rescale_factor
						_A	:				Tuple   =      do_normalize if do_normalize is not None else self.do_normalize
						_A	:				Union[str, Any]   =      image_mean if image_mean is not None else self.image_mean
						_A	:				int   =      image_std if image_std is not None else self.image_std
						_A	:				Optional[Any]   =      do_convert_rgb if do_convert_rgb is not None else self.do_convert_rgb

						_A	:				Optional[Any]   =      make_list_of_images(A__		)

						if not valid_images(A__		):
										raise ValueError(
										    '''Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '''
										    '''torch.Tensor, tf.Tensor or jax.ndarray.'''		)

						if do_resize and size is None:
										raise ValueError('''Size must be specified if do_resize is True.'''		)

						if do_center_crop and crop_size is None:
										raise ValueError('''Crop size must be specified if do_center_crop is True.'''		)

						if do_rescale and rescale_factor is None:
										raise ValueError('''Rescale factor must be specified if do_rescale is True.'''		)

						if do_normalize and (image_mean is None or image_std is None):
										raise ValueError('''Image mean and std must be specified if do_normalize is True.'''		)

						# PIL RGBA images are converted to RGB
						if do_convert_rgb:
										_A	:				int   =      [convert_to_rgb(A__		) for image in images]

						# All transformations expect numpy arrays.
						_A	:				int   =      [to_numpy_array(A__		) for image in images]

						if do_resize:
										_A	:				List[str]   =      [self.resize(image=A__       ,size=A__       ,resample=A__		) for image in images]

						if do_center_crop:
										_A	:				Union[str, Any]   =      [self.center_crop(image=A__       ,size=A__		) for image in images]

						if do_rescale:
										_A	:				Tuple   =      [self.rescale(image=A__       ,scale=A__		) for image in images]

						if do_normalize:
										_A	:				Dict   =      [self.normalize(image=A__       ,mean=A__       ,std=A__		) for image in images]

						_A	:				Dict   =      [to_channel_dimension_format(A__       ,A__		) for image in images]

						_A	:				Dict   =      {'''pixel_values''': images}
						return BatchFeature(data=A__       ,tensor_type=A__		)



