

import sys
import tempfile
import unittest
import unittest.mock as mock
from pathlib import Path

from huggingface_hub import HfFolder, delete_repo
from requests.exceptions import HTTPError

from transformers import AutoImageProcessor, ViTImageProcessor
from transformers.testing_utils import TOKEN, USER, get_tests_dir, is_staging_test


sys.path.append(str(Path(__file__).parent.parent / '''utils'''))

from test_module.custom_image_processing import CustomImageProcessor  # noqa E402


snake_case_			  = get_tests_dir('''fixtures''')


class       SCREAMING_SNAKE_CASE__					(unittest.TestCase     ):





    def       snake_case_				(      self):
         # A mock response for an HTTP head request to emulate server down
         lowercase__    :   Dict      =       mock.Mock()
         lowercase__    :   Any      =       500
         lowercase__    :   List[str]      =       {}
         lowercase__    :   int      =       HTTPError
         lowercase__    :   Union[str, Any]      =       {}

         # Download this model to make sure it's in the cache.
         lowercase__    :   List[str]      =       ViTImageProcessor.from_pretrained('hf-internal-testing/tiny-random-vit')
         # Under the mock environment we get a 500 error when trying to reach the model.
         with mock.patch('requests.Session.request'						,   return_value=_lowerCAmelCase) as mock_head:
              lowercase__    :   Tuple      =       ViTImageProcessor.from_pretrained('hf-internal-testing/tiny-random-vit')
              # This check we did call the fake head request
              mock_head.assert_called()





    def       snake_case_				(      self):
         # This test is for deprecated behavior and can be removed in v5
         lowercase__    :   int      =       ViTImageProcessor.from_pretrained(
             'https://huggingface.co/hf-internal-testing/tiny-random-vit/resolve/main/preprocessor_config.json')





    def       snake_case_				(      self):
         with self.assertRaises(_lowerCAmelCase):
              # config is in subfolder, the following should not work without specifying the subfolder
              lowercase__    :   str      =       AutoImageProcessor.from_pretrained('hf-internal-testing/stable-diffusion-all-variants')

         lowercase__    :   Dict      =       AutoImageProcessor.from_pretrained(
             'hf-internal-testing/stable-diffusion-all-variants'						,   subfolder='feature_extractor')

         self.assertIsNotNone(_lowerCAmelCase)



@is_staging_test
class       SCREAMING_SNAKE_CASE__					(unittest.TestCase     ):





    @classmethod
    def       snake_case_				(      cls):
         lowercase__    :   Optional[int]      =       TOKEN
         HfFolder.save_token(_lowerCAmelCase)





    @classmethod
    def       snake_case_				(      cls):
         try:
              delete_repo(token=cls._token						,   repo_id='test-image-processor')
         except HTTPError:
              pass

         try:
              delete_repo(token=cls._token						,   repo_id='valid_org/test-image-processor-org')
         except HTTPError:
              pass

         try:
              delete_repo(token=cls._token						,   repo_id='test-dynamic-image-processor')
         except HTTPError:
              pass





    def       snake_case_				(      self):
         lowercase__    :   Optional[int]      =       ViTImageProcessor.from_pretrained(_lowerCAmelCase)
         image_processor.push_to_hub('test-image-processor'						,   use_auth_token=self._token)

         lowercase__    :   str      =       ViTImageProcessor.from_pretrained(f"""{USER}/test-image-processor""")
         for k, v in image_processor.__dict__.items():
              self.assertEqual(_lowerCAmelCase						,   getattr(_lowerCAmelCase						,   _lowerCAmelCase))

         # Reset repo
         delete_repo(token=self._token						,   repo_id='test-image-processor')

         # Push to hub via save_pretrained
         with tempfile.TemporaryDirectory() as tmp_dir:
              image_processor.save_pretrained(
                  _lowerCAmelCase						,   repo_id='test-image-processor'						,   push_to_hub=_lowerCAmelCase						,   use_auth_token=self._token)

         lowercase__    :   List[Any]      =       ViTImageProcessor.from_pretrained(f"""{USER}/test-image-processor""")
         for k, v in image_processor.__dict__.items():
              self.assertEqual(_lowerCAmelCase						,   getattr(_lowerCAmelCase						,   _lowerCAmelCase))





    def       snake_case_				(      self):
         lowercase__    :   Tuple      =       ViTImageProcessor.from_pretrained(_lowerCAmelCase)
         image_processor.push_to_hub('valid_org/test-image-processor'						,   use_auth_token=self._token)

         lowercase__    :   int      =       ViTImageProcessor.from_pretrained('valid_org/test-image-processor')
         for k, v in image_processor.__dict__.items():
              self.assertEqual(_lowerCAmelCase						,   getattr(_lowerCAmelCase						,   _lowerCAmelCase))

         # Reset repo
         delete_repo(token=self._token						,   repo_id='valid_org/test-image-processor')

         # Push to hub via save_pretrained
         with tempfile.TemporaryDirectory() as tmp_dir:
              image_processor.save_pretrained(
                  _lowerCAmelCase						,   repo_id='valid_org/test-image-processor-org'						,   push_to_hub=_lowerCAmelCase						,   use_auth_token=self._token)

         lowercase__    :   Optional[int]      =       ViTImageProcessor.from_pretrained('valid_org/test-image-processor-org')
         for k, v in image_processor.__dict__.items():
              self.assertEqual(_lowerCAmelCase						,   getattr(_lowerCAmelCase						,   _lowerCAmelCase))





    def       snake_case_				(      self):
         CustomImageProcessor.register_for_auto_class()
         lowercase__    :   Optional[int]      =       CustomImageProcessor.from_pretrained(_lowerCAmelCase)

         image_processor.push_to_hub('test-dynamic-image-processor'						,   use_auth_token=self._token)

         # This has added the proper auto_map field to the config
         self.assertDictEqual(
             image_processor.auto_map						,   {'AutoImageProcessor': 'custom_image_processing.CustomImageProcessor'}						,   )

         lowercase__    :   List[str]      =       AutoImageProcessor.from_pretrained(
             f"""{USER}/test-dynamic-image-processor"""						,   trust_remote_code=_lowerCAmelCase)
         # Can't make an isinstance check because the new_image_processor is from the CustomImageProcessor class of a dynamic module
         self.assertEqual(new_image_processor.__class__.__name__						,   'CustomImageProcessor')







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				:






       def __init__(      self:    List[str]				,    _lowerCAmelCase:    Dict				,    _lowerCAmelCase:    List[str]=13				,    _lowerCAmelCase:    Dict=7				,    _lowerCAmelCase:    Tuple=True				,    _lowerCAmelCase:    Dict=True				,    _lowerCAmelCase:    Optional[Any]=True				,    _lowerCAmelCase:    Union[str, Any]=True				,    _lowerCAmelCase:    str=99				,    _lowerCAmelCase:    Dict=32				,    _lowerCAmelCase:    Dict=5				,    _lowerCAmelCase:    int=4				,    _lowerCAmelCase:    Optional[Any]=4				,    _lowerCAmelCase:    Any="gelu"				,    _lowerCAmelCase:    Any=0.0				,    _lowerCAmelCase:    List[Any]=0.1				,    _lowerCAmelCase:    Optional[int]=True				,    _lowerCAmelCase:    Any=5_12				,    _lowerCAmelCase:    Optional[Any]=16				,    _lowerCAmelCase:    str=2				,    _lowerCAmelCase:    Tuple=0.02				,    _lowerCAmelCase:    str=3				,    _lowerCAmelCase:    List[str]=4				,    _lowerCAmelCase:    Optional[Any]=None				,    ):
         lowercase			:List[str]							=  parent
         lowercase			:List[str]							=  batch_size
         lowercase			:List[str]							=  seq_length
         lowercase			:Dict							=  is_training
         lowercase			:Union[str, Any]							=  use_input_mask
         lowercase			:Optional[int]							=  use_token_type_ids
         lowercase			:Dict							=  use_labels
         lowercase			:Any							=  vocab_size
         lowercase			:List[Any]							=  hidden_size
         lowercase			:Optional[int]							=  num_hidden_layers
         lowercase			:Union[str, Any]							=  num_attention_heads
         lowercase			:Any							=  intermediate_multiple_size
         lowercase			:str							=  hidden_act
         lowercase			:List[Any]							=  hidden_dropout
         lowercase			:Optional[Any]							=  attention_dropout
         lowercase			:int							=  weight_tying
         lowercase			:Tuple							=  max_position_embeddings
         lowercase			:str							=  type_vocab_size
         lowercase			:Union[str, Any]							=  type_sequence_label_size
         lowercase			:Dict							=  initializer_range
         lowercase			:Dict							=  num_labels
         lowercase			:Dict							=  num_choices
         lowercase			:Any							=  scope






       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         lowercase			:List[Any]							=  ids_tensor([self.batch_size, self.seq_length]				,    self.vocab_size )

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

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

         lowercase			:Tuple							=  self.get_config()

         return config, input_ids, input_mask, token_labels






       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         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=_lowerCAmelCase				,    initializer_range=self.initializer_range				,    )






       def     SCREAMING_SNAKE_CASE			(      self:    Optional[int] ):
         lowercase		,			lowercase		,			lowercase		,			lowercase			:Any							=  self.prepare_config_and_inputs()

         lowercase			:Tuple							=  True

         return config, input_ids, input_mask, token_labels






       def     SCREAMING_SNAKE_CASE			(      self:    List[Any]				,    _lowerCAmelCase:    Optional[Any]				,    _lowerCAmelCase:    Dict				,    _lowerCAmelCase:    List[str] ):
         lowercase			:List[str]							=  GPTNeoXJapaneseModel(config=_lowerCAmelCase )
         model.to(_lowerCAmelCase )
         model.eval()
         lowercase			:Optional[int]							=  model(_lowerCAmelCase				,    attention_mask=_lowerCAmelCase )
         lowercase			:Dict							=  model(_lowerCAmelCase )
         self.parent.assertEqual(result.last_hidden_state.shape				,    (self.batch_size, self.seq_length, self.hidden_size) )






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






       def     SCREAMING_SNAKE_CASE			(      self:    int				,    _lowerCAmelCase:    str				,    _lowerCAmelCase:    Dict				,    _lowerCAmelCase:    int				,    _lowerCAmelCase:    List[Any] ):
         lowercase			:str							=  GPTNeoXJapaneseForCausalLM(config=_lowerCAmelCase )
         model.to(_lowerCAmelCase )
         model.eval()
         lowercase			:Tuple							=  model(_lowerCAmelCase				,    attention_mask=_lowerCAmelCase				,    labels=_lowerCAmelCase )
         self.parent.assertEqual(result.logits.shape				,    (self.batch_size, self.seq_length, self.vocab_size) )






       def     SCREAMING_SNAKE_CASE			(      self:    Dict				,    _lowerCAmelCase:    List[Any]				,    _lowerCAmelCase:    int				,    _lowerCAmelCase:    Any ):
         lowercase			:Optional[Any]							=  True
         lowercase			:List[str]							=  GPTNeoXJapaneseForCausalLM(config=_lowerCAmelCase )
         model.to(_lowerCAmelCase )
         model.eval()

         # first forward pass
         lowercase			:str							=  model(_lowerCAmelCase				,    attention_mask=_lowerCAmelCase				,    use_cache=_lowerCAmelCase )
         lowercase			:Tuple							=  outputs.past_key_values

         # create hypothetical multiple next token and extent to next_input_ids
         lowercase			:Optional[int]							=  ids_tensor((self.batch_size, 3)				,    config.vocab_size )
         lowercase			:Tuple							=  ids_tensor((self.batch_size, 3)				,    vocab_size=2 )

         # append to next input_ids and
         lowercase			:Tuple							=  torch.cat([input_ids, next_tokens]				,    dim=-1 )
         lowercase			:List[Any]							=  torch.cat([input_mask, next_mask]				,    dim=-1 )

         lowercase			:Dict							=  model(_lowerCAmelCase				,    attention_mask=_lowerCAmelCase				,    output_hidden_states=_lowerCAmelCase )
         lowercase			:List[str]							=  output_from_no_past["hidden_states"][0]
         lowercase			:str							=  model(
             _lowerCAmelCase				,    attention_mask=_lowerCAmelCase				,    past_key_values=_lowerCAmelCase				,    output_hidden_states=_lowerCAmelCase				,    )["hidden_states"][0]

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






       def     SCREAMING_SNAKE_CASE			(      self:    Dict ):
         lowercase			:str							=  self.prepare_config_and_inputs()
         lowercase		,			lowercase		,			lowercase		,			lowercase			:Tuple							=  config_and_inputs
         lowercase			:Union[str, Any]							=  {"input_ids": input_ids, "attention_mask": input_mask}
         return config, inputs_dict







@require_torch
class       __lowerCAmelCase				(							lowerCAmelCase					,       lowerCAmelCase					,       unittest.TestCase):
       _a           =  (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
       _a           =  (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
       _a           =  (
           {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
           if is_torch_available()
           else {}
       )
       _a           =  False
       _a           =  False
       _a           =  False
       _a           =  False






       def     SCREAMING_SNAKE_CASE			(      self:    List[str] ):
         lowercase			:Any							=  GPTNeoXJapaneseModelTester(self )
         lowercase			:List[str]							=  ConfigTester(self				,    config_class=_lowerCAmelCase				,    hidden_size=37 )






       def     SCREAMING_SNAKE_CASE			(      self:    Tuple ):
         self.config_tester.run_common_tests()






       def     SCREAMING_SNAKE_CASE			(      self:    Dict ):
         lowercase		,			lowercase		,			lowercase		,			lowercase			:Tuple							=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(_lowerCAmelCase				,    _lowerCAmelCase				,    _lowerCAmelCase )






       def     SCREAMING_SNAKE_CASE			(      self:    Tuple ):
         lowercase		,			lowercase		,			lowercase		,			lowercase			:Optional[Any]							=  self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_model_as_decoder(_lowerCAmelCase				,    _lowerCAmelCase				,    _lowerCAmelCase )






       def     SCREAMING_SNAKE_CASE			(      self:    Optional[int] ):
         # This regression test was failing with PyTorch < 1.3
         lowercase		,			lowercase		,			lowercase		,			lowercase			:Tuple							=  self.model_tester.prepare_config_and_inputs_for_decoder()

         lowercase			:Union[str, Any]							=  None

         self.model_tester.create_and_check_model_as_decoder(_lowerCAmelCase				,    _lowerCAmelCase				,    _lowerCAmelCase )






       def     SCREAMING_SNAKE_CASE			(      self:    Union[str, Any] ):
         lowercase		,			lowercase		,			lowercase		,			lowercase			:List[str]							=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_decoder_model_past_large_inputs(_lowerCAmelCase				,    _lowerCAmelCase				,    _lowerCAmelCase )






       def     SCREAMING_SNAKE_CASE			(      self:    str ):
         lowercase			:Optional[int]							=  self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_causal_lm(*_lowerCAmelCase )






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

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

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

         lowercase			:List[str]							=  GPTNeoXJapaneseTokenizer.from_pretrained(_lowerCAmelCase )
         lowercase			:Tuple							=  GPTNeoXJapaneseForCausalLM.from_pretrained(_lowerCAmelCase )

         lowercase			:List[str]							=  []
         for prompt in prompts:
           lowercase			:Optional[int]							=  tokenizer(_lowerCAmelCase				,    return_tensors="pt" ).input_ids
           lowercase			:Union[str, Any]							=  model.generate(_lowerCAmelCase				,    max_length=50 )
           lowercase			:List[str]							=  tokenizer.batch_decode(_lowerCAmelCase				,    skip_special_tokens=_lowerCAmelCase )
           predicted_outputs += generated_string
         self.assertListEqual(_lowerCAmelCase				,    _lowerCAmelCase )



