


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 ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE=13 ,					__SCREAMING_SNAKE_CASE=7 ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=99 ,					__SCREAMING_SNAKE_CASE=32 ,					__SCREAMING_SNAKE_CASE=5 ,					__SCREAMING_SNAKE_CASE=4 ,					__SCREAMING_SNAKE_CASE=4 ,					__SCREAMING_SNAKE_CASE="gelu" ,					__SCREAMING_SNAKE_CASE=0.0 ,					__SCREAMING_SNAKE_CASE=0.1 ,					__SCREAMING_SNAKE_CASE=True ,					__SCREAMING_SNAKE_CASE=512 ,					__SCREAMING_SNAKE_CASE=16 ,					__SCREAMING_SNAKE_CASE=2 ,					__SCREAMING_SNAKE_CASE=0.02 ,					__SCREAMING_SNAKE_CASE=3 ,					__SCREAMING_SNAKE_CASE=4 ,					__SCREAMING_SNAKE_CASE=None ,					):




              """simple docstring"""




              UpperCamelCase    :	Tuple   	=		parent
              UpperCamelCase    :	Tuple   	=		batch_size
              UpperCamelCase    :	Union[str, Any]   	=		seq_length
              UpperCamelCase    :	Union[str, Any]   	=		is_training
              UpperCamelCase    :	Union[str, Any]   	=		use_input_mask
              UpperCamelCase    :	str   	=		use_token_type_ids
              UpperCamelCase    :	Union[str, Any]   	=		use_labels
              UpperCamelCase    :	int   	=		vocab_size
              UpperCamelCase    :	Union[str, Any]   	=		hidden_size
              UpperCamelCase    :	Tuple   	=		num_hidden_layers
              UpperCamelCase    :	Optional[int]   	=		num_attention_heads
              UpperCamelCase    :	Union[str, Any]   	=		intermediate_multiple_size
              UpperCamelCase    :	List[Any]   	=		hidden_act
              UpperCamelCase    :	Union[str, Any]   	=		hidden_dropout
              UpperCamelCase    :	Any   	=		attention_dropout
              UpperCamelCase    :	Optional[int]   	=		weight_tying
              UpperCamelCase    :	int   	=		max_position_embeddings
              UpperCamelCase    :	Any   	=		type_vocab_size
              UpperCamelCase    :	Tuple   	=		type_sequence_label_size
              UpperCamelCase    :	Optional[Any]   	=		initializer_range
              UpperCamelCase    :	Tuple   	=		num_labels
              UpperCamelCase    :	Union[str, Any]   	=		num_choices
              UpperCamelCase    :	Optional[int]   	=		scope






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	int   	=		ids_tensor([self.batch_size, self.seq_length] ,					self.vocab_size						)

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

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

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

              return config, input_ids, input_mask, token_labels






       def  _lowercase   (     self						):




              """simple docstring"""




              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=__SCREAMING_SNAKE_CASE ,					initializer_range=self.initializer_range ,					)






       def  _lowercase   (     self						):




              """simple docstring"""




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

              UpperCamelCase    :	str   	=		True

              return config, input_ids, input_mask, token_labels






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	List[str]   	=		GPTNeoXJapaneseModel(config=__SCREAMING_SNAKE_CASE						)
              model.to(__SCREAMING_SNAKE_CASE						)
              model.eval()
              UpperCamelCase    :	Tuple   	=		model(__SCREAMING_SNAKE_CASE ,					attention_mask=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Union[str, Any]   	=		model(__SCREAMING_SNAKE_CASE						)
              self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Optional[Any]   	=		True
              UpperCamelCase    :	List[str]   	=		GPTNeoXJapaneseModel(__SCREAMING_SNAKE_CASE						)
              model.to(__SCREAMING_SNAKE_CASE						)
              model.eval()
              UpperCamelCase    :	int   	=		model(__SCREAMING_SNAKE_CASE ,					attention_mask=__SCREAMING_SNAKE_CASE						)
              self.parent.assertEqual(result.last_hidden_state.shape ,					(self.batch_size, self.seq_length, self.hidden_size)						)






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




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






       def  _lowercase   (     self ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						):




              """simple docstring"""




              UpperCamelCase    :	Optional[int]   	=		True
              UpperCamelCase    :	str   	=		GPTNeoXJapaneseForCausalLM(config=__SCREAMING_SNAKE_CASE						)
              model.to(__SCREAMING_SNAKE_CASE						)
              model.eval()

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

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

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

              UpperCamelCase    :	Optional[int]   	=		model(__SCREAMING_SNAKE_CASE ,					attention_mask=__SCREAMING_SNAKE_CASE ,					output_hidden_states=__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Union[str, Any]   	=		output_from_no_past['''hidden_states'''][0]
              UpperCamelCase    :	List[Any]   	=		model(
                  __SCREAMING_SNAKE_CASE ,					attention_mask=__SCREAMING_SNAKE_CASE ,					past_key_values=__SCREAMING_SNAKE_CASE ,					output_hidden_states=__SCREAMING_SNAKE_CASE ,					)['''hidden_states'''][0]

              # select random slice
              UpperCamelCase    :	Any   	=		ids_tensor((1,) ,					output_from_past.shape[-1]						).item()
              UpperCamelCase    :	List[str]   	=		output_from_no_past[:, -3:, random_slice_idx].detach()
              UpperCamelCase    :	Optional[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(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					atol=1e-3						)						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Tuple   	=		self.prepare_config_and_inputs()
              UpperCamelCase	, UpperCamelCase	, UpperCamelCase	, UpperCamelCase    :	Union[str, Any]   	=		config_and_inputs
              UpperCamelCase    :	Any   	=		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
              return config, inputs_dict




@require_torch
class    UpperCAmelCase_	(							_a,     _a,     unittest.TestCase):
       '''simple docstring'''
       __UpperCamelCase      :     Optional[Any]							=					(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
       __UpperCamelCase      :     Any							=					(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
       __UpperCamelCase      :     List[str]							=					(
           {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM}
           if is_torch_available()
           else {}
       )
       __UpperCamelCase      :     Tuple							=					False
       __UpperCamelCase      :     List[str]							=					False
       __UpperCamelCase      :     List[Any]							=					False
       __UpperCamelCase      :     List[Any]							=					False






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Tuple   	=		GPTNeoXJapaneseModelTester(self						)
              UpperCamelCase    :	Union[str, Any]   	=		ConfigTester(self ,					config_class=__SCREAMING_SNAKE_CASE ,					hidden_size=37						)






       def  _lowercase   (     self						):




              """simple docstring"""




              self.config_tester.run_common_tests()






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase	, UpperCamelCase	, UpperCamelCase	, UpperCamelCase    :	Dict   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_model(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase	, UpperCamelCase	, UpperCamelCase	, UpperCamelCase    :	Tuple   	=		self.model_tester.prepare_config_and_inputs_for_decoder()
              self.model_tester.create_and_check_model_as_decoder(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase	, UpperCamelCase	, UpperCamelCase	, UpperCamelCase    :	Optional[Any]   	=		self.model_tester.prepare_config_and_inputs_for_decoder()

              UpperCamelCase    :	List[str]   	=		None

              self.model_tester.create_and_check_model_as_decoder(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase	, UpperCamelCase	, UpperCamelCase	, UpperCamelCase    :	int   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_decoder_model_past_large_inputs(__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE ,					__SCREAMING_SNAKE_CASE						)






       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Any   	=		self.model_tester.prepare_config_and_inputs()
              self.model_tester.create_and_check_for_causal_lm(*__SCREAMING_SNAKE_CASE						)






       @slow
       def  _lowercase   (     self						):




              """simple docstring"""




              UpperCamelCase    :	Union[str, Any]   	=		'''abeja/gpt-neox-japanese-2.7b'''

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

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

              UpperCamelCase    :	Optional[int]   	=		GPTNeoXJapaneseTokenizer.from_pretrained(__SCREAMING_SNAKE_CASE						)
              UpperCamelCase    :	Optional[int]   	=		GPTNeoXJapaneseForCausalLM.from_pretrained(__SCREAMING_SNAKE_CASE						)

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





from ...utils import (
    OptionalDependencyNotAvailable,
    is_torch_available,
    is_transformers_available,
    is_transformers_version,
)


try:
  if not (is_transformers_available() and is_torch_available()):
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  from ...utils.dummy_torch_and_transformers_objects import ShapEPipeline
else:
  from .camera import create_pan_cameras
  from .pipeline_shap_e import ShapEPipeline
  from .pipeline_shap_e_img2img import ShapEImgaImgPipeline
  from .renderer import (
      BoundingBoxVolume,
      ImportanceRaySampler,
      MLPNeRFModelOutput,
      MLPNeRSTFModel,
      ShapEParamsProjModel,
      ShapERenderer,
      StratifiedRaySampler,
      VoidNeRFModel,
  )

