




import os
import tempfile
import unittest

from transformers import NezhaConfig, is_torch_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import (
       MODEL_FOR_PRETRAINING_MAPPING,
       NezhaForMaskedLM,
       NezhaForMultipleChoice,
       NezhaForNextSentencePrediction,
       NezhaForPreTraining,
       NezhaForQuestionAnswering,
       NezhaForSequenceClassification,
       NezhaForTokenClassification,
       NezhaModel,
   )
   from transformers.models.nezha.modeling_nezha import NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST





class 	lowerCamelCase_							:


 '''simple docstring'''







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



 def 				lowerCAmelCase_  (							self  :		Tuple						):
       SCREAMING_SNAKE_CASE_     						=      ids_tensor([self.batch_size, self.seq_length]    ,    self.vocab_size						)

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

       SCREAMING_SNAKE_CASE_     						=      None
       if self.use_token_type_ids:
             SCREAMING_SNAKE_CASE_     						=      ids_tensor([self.batch_size, self.seq_length]    ,    self.type_vocab_size						)

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

       SCREAMING_SNAKE_CASE_     						=      self.get_config()

       return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels



 def 				lowerCAmelCase_  (							self  :		Any						):
       return NezhaConfig(
           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_size=self.intermediate_size    ,    hidden_act=self.hidden_act    ,    hidden_dropout_prob=self.hidden_dropout_prob    ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob    ,    max_position_embeddings=self.max_position_embeddings    ,    type_vocab_size=self.type_vocab_size    ,    is_decoder=_lowerCAmelCase    ,    initializer_range=self.initializer_range    ,    )



 def 				lowerCAmelCase_  (							self  :		Any						):
       (
           (
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			
       )     						=      self.prepare_config_and_inputs()

       SCREAMING_SNAKE_CASE_     						=      True
       SCREAMING_SNAKE_CASE_     						=      floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
       SCREAMING_SNAKE_CASE_     						=      ids_tensor([self.batch_size, self.seq_length]    ,    vocab_size=2						)

       return (
           config,
           input_ids,
           token_type_ids,
           input_mask,
           sequence_labels,
           token_labels,
           choice_labels,
           encoder_hidden_states,
           encoder_attention_mask,
       )



 def 				lowerCAmelCase_  (							self  :		int    ,    _lowerCAmelCase  :		List[str]    ,    _lowerCAmelCase  :		str    ,    _lowerCAmelCase  :		Optional[int]    ,    _lowerCAmelCase  :		int    ,    _lowerCAmelCase  :		Optional[Any]    ,    _lowerCAmelCase  :		Optional[Any]    ,    _lowerCAmelCase  :		Optional[int]						):
       SCREAMING_SNAKE_CASE_     						=      NezhaModel(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_     						=      model(_lowerCAmelCase    ,    attention_mask=_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_     						=      model(_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_     						=      model(_lowerCAmelCase						)
       self.parent.assertEqual(result.last_hidden_state.shape    ,    (self.batch_size, self.seq_length, self.hidden_size)						)
       self.parent.assertEqual(result.pooler_output.shape    ,    (self.batch_size, self.hidden_size)						)



 def 				lowerCAmelCase_  (							self  :		Optional[int]    ,    _lowerCAmelCase  :		str    ,    _lowerCAmelCase  :		List[Any]    ,    _lowerCAmelCase  :		List[str]    ,    _lowerCAmelCase  :		int    ,    _lowerCAmelCase  :		Tuple    ,    _lowerCAmelCase  :		Any    ,    _lowerCAmelCase  :		Tuple    ,    _lowerCAmelCase  :		str    ,    _lowerCAmelCase  :		List[str]    ,    ):
       SCREAMING_SNAKE_CASE_     						=      True
       SCREAMING_SNAKE_CASE_     						=      NezhaModel(_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_     						=      model(
           _lowerCAmelCase    ,    attention_mask=_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase    ,    encoder_hidden_states=_lowerCAmelCase    ,    encoder_attention_mask=_lowerCAmelCase    ,    )
       SCREAMING_SNAKE_CASE_     						=      model(
           _lowerCAmelCase    ,    attention_mask=_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase    ,    encoder_hidden_states=_lowerCAmelCase    ,    )
       SCREAMING_SNAKE_CASE_     						=      model(_lowerCAmelCase    ,    attention_mask=_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase						)
       self.parent.assertEqual(result.last_hidden_state.shape    ,    (self.batch_size, self.seq_length, self.hidden_size)						)
       self.parent.assertEqual(result.pooler_output.shape    ,    (self.batch_size, self.hidden_size)						)



 def 				lowerCAmelCase_  (							self  :		Union[str, Any]    ,    _lowerCAmelCase  :		Optional[int]    ,    _lowerCAmelCase  :		List[Any]    ,    _lowerCAmelCase  :		Optional[int]    ,    _lowerCAmelCase  :		Dict    ,    _lowerCAmelCase  :		List[str]    ,    _lowerCAmelCase  :		Union[str, Any]    ,    _lowerCAmelCase  :		Union[str, Any]						):
       SCREAMING_SNAKE_CASE_     						=      NezhaForMaskedLM(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_     						=      model(_lowerCAmelCase    ,    attention_mask=_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase    ,    labels=_lowerCAmelCase						)
       self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.seq_length, self.vocab_size)						)



 def 				lowerCAmelCase_  (							self  :		List[str]    ,    _lowerCAmelCase  :		List[str]    ,    _lowerCAmelCase  :		Dict    ,    _lowerCAmelCase  :		Optional[int]    ,    _lowerCAmelCase  :		Union[str, Any]    ,    _lowerCAmelCase  :		int    ,    _lowerCAmelCase  :		List[Any]    ,    _lowerCAmelCase  :		Optional[int]						):
       SCREAMING_SNAKE_CASE_     						=      NezhaForNextSentencePrediction(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_     						=      model(
           _lowerCAmelCase    ,    attention_mask=_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase    ,    labels=_lowerCAmelCase    ,    )
       self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, 2)						)



 def 				lowerCAmelCase_  (							self  :		List[Any]    ,    _lowerCAmelCase  :		str    ,    _lowerCAmelCase  :		str    ,    _lowerCAmelCase  :		Optional[Any]    ,    _lowerCAmelCase  :		int    ,    _lowerCAmelCase  :		int    ,    _lowerCAmelCase  :		Tuple    ,    _lowerCAmelCase  :		Dict						):
       SCREAMING_SNAKE_CASE_     						=      NezhaForPreTraining(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_     						=      model(
           _lowerCAmelCase    ,    attention_mask=_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase    ,    labels=_lowerCAmelCase    ,    next_sentence_label=_lowerCAmelCase    ,    )
       self.parent.assertEqual(result.prediction_logits.shape    ,    (self.batch_size, self.seq_length, self.vocab_size)						)
       self.parent.assertEqual(result.seq_relationship_logits.shape    ,    (self.batch_size, 2)						)



 def 				lowerCAmelCase_  (							self  :		Union[str, Any]    ,    _lowerCAmelCase  :		List[Any]    ,    _lowerCAmelCase  :		Optional[Any]    ,    _lowerCAmelCase  :		str    ,    _lowerCAmelCase  :		Union[str, Any]    ,    _lowerCAmelCase  :		Optional[int]    ,    _lowerCAmelCase  :		Tuple    ,    _lowerCAmelCase  :		Dict						):
       SCREAMING_SNAKE_CASE_     						=      NezhaForQuestionAnswering(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_     						=      model(
           _lowerCAmelCase    ,    attention_mask=_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase    ,    start_positions=_lowerCAmelCase    ,    end_positions=_lowerCAmelCase    ,    )
       self.parent.assertEqual(result.start_logits.shape    ,    (self.batch_size, self.seq_length)						)
       self.parent.assertEqual(result.end_logits.shape    ,    (self.batch_size, self.seq_length)						)



 def 				lowerCAmelCase_  (							self  :		int    ,    _lowerCAmelCase  :		str    ,    _lowerCAmelCase  :		Dict    ,    _lowerCAmelCase  :		Tuple    ,    _lowerCAmelCase  :		List[Any]    ,    _lowerCAmelCase  :		Any    ,    _lowerCAmelCase  :		str    ,    _lowerCAmelCase  :		Tuple						):
       SCREAMING_SNAKE_CASE_     						=      self.num_labels
       SCREAMING_SNAKE_CASE_     						=      NezhaForSequenceClassification(_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_     						=      model(_lowerCAmelCase    ,    attention_mask=_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase    ,    labels=_lowerCAmelCase						)
       self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.num_labels)						)



 def 				lowerCAmelCase_  (							self  :		List[Any]    ,    _lowerCAmelCase  :		Union[str, Any]    ,    _lowerCAmelCase  :		Optional[Any]    ,    _lowerCAmelCase  :		str    ,    _lowerCAmelCase  :		Optional[Any]    ,    _lowerCAmelCase  :		List[Any]    ,    _lowerCAmelCase  :		Optional[int]    ,    _lowerCAmelCase  :		Tuple						):
       SCREAMING_SNAKE_CASE_     						=      self.num_labels
       SCREAMING_SNAKE_CASE_     						=      NezhaForTokenClassification(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_     						=      model(_lowerCAmelCase    ,    attention_mask=_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase    ,    labels=_lowerCAmelCase						)
       self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.seq_length, self.num_labels)						)



 def 				lowerCAmelCase_  (							self  :		Any    ,    _lowerCAmelCase  :		Tuple    ,    _lowerCAmelCase  :		List[Any]    ,    _lowerCAmelCase  :		Any    ,    _lowerCAmelCase  :		List[str]    ,    _lowerCAmelCase  :		Optional[Any]    ,    _lowerCAmelCase  :		Any    ,    _lowerCAmelCase  :		List[Any]						):
       SCREAMING_SNAKE_CASE_     						=      self.num_choices
       SCREAMING_SNAKE_CASE_     						=      NezhaForMultipleChoice(config=_lowerCAmelCase						)
       model.to(_lowerCAmelCase						)
       model.eval()
       SCREAMING_SNAKE_CASE_     						=      input_ids.unsqueeze(1						).expand(-1    ,    self.num_choices    ,    -1						).contiguous()
       SCREAMING_SNAKE_CASE_     						=      token_type_ids.unsqueeze(1						).expand(-1    ,    self.num_choices    ,    -1						).contiguous()
       SCREAMING_SNAKE_CASE_     						=      input_mask.unsqueeze(1						).expand(-1    ,    self.num_choices    ,    -1						).contiguous()
       SCREAMING_SNAKE_CASE_     						=      model(
           _lowerCAmelCase    ,    attention_mask=_lowerCAmelCase    ,    token_type_ids=_lowerCAmelCase    ,    labels=_lowerCAmelCase    ,    )
       self.parent.assertEqual(result.logits.shape    ,    (self.batch_size, self.num_choices)						)




 def 				lowerCAmelCase_  (							self  :		List[str]						):
       SCREAMING_SNAKE_CASE_     						=      self.prepare_config_and_inputs()
       (
           (
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			(
           SCREAMING_SNAKE_CASE_
       )						,			
       )     						=      config_and_inputs
       SCREAMING_SNAKE_CASE_     						=      {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
       return config, inputs_dict





@require_torch
class 	lowerCamelCase_							(				_SCREAMING_SNAKE_CASE	,       _SCREAMING_SNAKE_CASE	,       _SCREAMING_SNAKE_CASE	,       unittest.TestCase ):


 '''simple docstring'''







 lowercase_       =   (
     (
         NezhaModel,
         NezhaForMaskedLM,
         NezhaForMultipleChoice,
         NezhaForNextSentencePrediction,
         NezhaForPreTraining,
         NezhaForQuestionAnswering,
         NezhaForSequenceClassification,
         NezhaForTokenClassification,
     )
     if is_torch_available()
     else ()
 )
 lowercase_       =   (
     {
         "feature-extraction": NezhaModel,
         "fill-mask": NezhaForMaskedLM,
         "question-answering": NezhaForQuestionAnswering,
         "text-classification": NezhaForSequenceClassification,
         "token-classification": NezhaForTokenClassification,
         "zero-shot": NezhaForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 lowercase_       =   True



 def 				lowerCAmelCase_  (							self  :		Optional[int]    ,    _lowerCAmelCase  :		Optional[int]    ,    _lowerCAmelCase  :		str    ,    _lowerCAmelCase  :		Tuple=False						):
       SCREAMING_SNAKE_CASE_     						=      super()._prepare_for_class(_lowerCAmelCase    ,    _lowerCAmelCase    ,    return_labels=_lowerCAmelCase						)

       if return_labels:
             if model_class in get_values(_lowerCAmelCase						):
                   SCREAMING_SNAKE_CASE_     						=      torch.zeros(
                       (self.model_tester.batch_size, self.model_tester.seq_length)    ,    dtype=torch.long    ,    device=_lowerCAmelCase						)
                   SCREAMING_SNAKE_CASE_     						=      torch.zeros(
                       self.model_tester.batch_size    ,    dtype=torch.long    ,    device=_lowerCAmelCase						)
       return inputs_dict



 def 				lowerCAmelCase_  (							self  :		Union[str, Any]						):
       SCREAMING_SNAKE_CASE_     						=      NezhaModelTester(self						)
       SCREAMING_SNAKE_CASE_     						=      ConfigTester(self    ,    config_class=_lowerCAmelCase    ,    hidden_size=37						)



 def 				lowerCAmelCase_  (							self  :		Optional[int]						):
       self.config_tester.run_common_tests()



 def 				lowerCAmelCase_  (							self  :		List[str]						):
       SCREAMING_SNAKE_CASE_     						=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		str						):
       SCREAMING_SNAKE_CASE_     						=      self.model_tester.prepare_config_and_inputs_for_decoder()
       self.model_tester.create_and_check_model_as_decoder(*_lowerCAmelCase						)



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

       SCREAMING_SNAKE_CASE_     						=      None

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



 def 				lowerCAmelCase_  (							self  :		Any						):
       SCREAMING_SNAKE_CASE_     						=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_masked_lm(*_lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		str						):
       SCREAMING_SNAKE_CASE_     						=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_multiple_choice(*_lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		Optional[int]						):
       SCREAMING_SNAKE_CASE_     						=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_next_sequence_prediction(*_lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		Any						):
       SCREAMING_SNAKE_CASE_     						=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_pretraining(*_lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		Tuple						):
       SCREAMING_SNAKE_CASE_     						=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_question_answering(*_lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		int						):
       SCREAMING_SNAKE_CASE_     						=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_sequence_classification(*_lowerCAmelCase						)



 def 				lowerCAmelCase_  (							self  :		List[str]						):
       SCREAMING_SNAKE_CASE_     						=      self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_token_classification(*_lowerCAmelCase						)



 @slow
 def 				lowerCAmelCase_  (							self  :		Optional[int]						):
       for model_name in NEZHA_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             SCREAMING_SNAKE_CASE_     						=      NezhaModel.from_pretrained(_lowerCAmelCase						)
             self.assertIsNotNone(_lowerCAmelCase						)




 @slow
 @require_torch_gpu
 def 				lowerCAmelCase_  (							self  :		str						):
       SCREAMING_SNAKE_CASE_						,			SCREAMING_SNAKE_CASE_     						=      self.model_tester.prepare_config_and_inputs_for_common()
       for model_class in self.all_model_classes:
             # NezhaForMultipleChoice behaves incorrectly in JIT environments.
             if model_class == NezhaForMultipleChoice:
                   return

             SCREAMING_SNAKE_CASE_     						=      True
             SCREAMING_SNAKE_CASE_     						=      model_class(config=_lowerCAmelCase						)

             SCREAMING_SNAKE_CASE_     						=      self._prepare_for_class(_lowerCAmelCase    ,    _lowerCAmelCase						)
             SCREAMING_SNAKE_CASE_     						=      torch.jit.trace(
                 _lowerCAmelCase    ,    (inputs_dict['input_ids'].to('cpu'						), inputs_dict['attention_mask'].to('cpu'						))						)

             with tempfile.TemporaryDirectory() as tmp:
                   torch.jit.save(_lowerCAmelCase    ,    os.path.join(_lowerCAmelCase    ,    'bert.pt'						)						)
                   SCREAMING_SNAKE_CASE_     						=      torch.jit.load(os.path.join(_lowerCAmelCase    ,    'bert.pt'						)    ,    map_location=_lowerCAmelCase						)
                   loaded(inputs_dict['input_ids'].to(_lowerCAmelCase						)    ,    inputs_dict['attention_mask'].to(_lowerCAmelCase						)						)





@require_torch
class 	lowerCamelCase_							(				unittest.TestCase ):


 '''simple docstring'''







 @slow
 def 				lowerCAmelCase_  (							self  :		Any						):
       SCREAMING_SNAKE_CASE_     						=      NezhaModel.from_pretrained('sijunhe/nezha-cn-base'						)
       SCREAMING_SNAKE_CASE_     						=      torch.tensor([[0, 1, 2, 3, 4, 5]]						)
       SCREAMING_SNAKE_CASE_     						=      torch.tensor([[0, 1, 1, 1, 1, 1]]						)
       with torch.no_grad():
             SCREAMING_SNAKE_CASE_     						=      model(_lowerCAmelCase    ,    attention_mask=_lowerCAmelCase						)[0]
       SCREAMING_SNAKE_CASE_     						=      torch.Size((1, 6, 768)						)
       self.assertEqual(output.shape    ,    _lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_     						=      torch.tensor([[[0.0685, 0.2441, 0.1102], [0.0600, 0.1906, 0.1349], [0.0221, 0.0819, 0.0586]]]						)

       self.assertTrue(torch.allclose(output[:, 1:4, 1:4]    ,    _lowerCAmelCase    ,    atol=1E-4						)						)




 @slow
 def 				lowerCAmelCase_  (							self  :		Tuple						):
       SCREAMING_SNAKE_CASE_     						=      NezhaForMaskedLM.from_pretrained('sijunhe/nezha-cn-base'						)
       SCREAMING_SNAKE_CASE_     						=      torch.tensor([[0, 1, 2, 3, 4, 5]]						)
       SCREAMING_SNAKE_CASE_     						=      torch.tensor([[1, 1, 1, 1, 1, 1]]						)
       with torch.no_grad():
             SCREAMING_SNAKE_CASE_     						=      model(_lowerCAmelCase    ,    attention_mask=_lowerCAmelCase						)[0]
       SCREAMING_SNAKE_CASE_     						=      torch.Size((1, 6, 21_128)						)
       self.assertEqual(output.shape    ,    _lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_     						=      torch.tensor(
           [[-2.7939, -1.7902, -2.2189], [-2.8585, -1.8908, -2.3723], [-2.6499, -1.7750, -2.2558]]						)

       self.assertTrue(torch.allclose(output[:, 1:4, 1:4]    ,    _lowerCAmelCase    ,    atol=1E-4						)						)





from pathlib import PurePosixPath
from typing import Optional

import fsspec
from fsspec import AbstractFileSystem
from huggingface_hub.hf_api import DatasetInfo

from ..utils.file_utils import get_authentication_headers_for_url
from ..utils.hub import hf_hub_url





class 	lowerCamelCase_							(				_SCREAMING_SNAKE_CASE ):


 '''simple docstring'''







 lowercase_       =   ""
 lowercase_       =   "hf-legacy"  # "hf://"" is reserved for hffs



 def __init__(							self  :		Optional[int]    ,    _lowerCAmelCase  :		Optional[DatasetInfo] = None    ,    _lowerCAmelCase  :		Optional[str] = None    ,    **_lowerCAmelCase  :		int    ,    ):
       super().__init__(self    ,    **_lowerCAmelCase						)
       SCREAMING_SNAKE_CASE_     						=      repo_info
       SCREAMING_SNAKE_CASE_     						=      token
       SCREAMING_SNAKE_CASE_     						=      None



 def 				lowerCAmelCase_  (							self  :		Tuple						):
       if self.dir_cache is None:
             SCREAMING_SNAKE_CASE_     						=      {}
             for hf_file in self.repo_info.siblings:
                   # TODO(QL): add sizes
                   SCREAMING_SNAKE_CASE_     						=      {
                       'name': hf_file.rfilename,
                       'size': None,
                       'type': 'file',
                   }
                   self.dir_cache.update(
                       {
                           str(_lowerCAmelCase						): {'name': str(_lowerCAmelCase						), 'size': None, 'type': 'directory'}
                           for d in list(PurePosixPath(hf_file.rfilename						).parents						)[:-1]
                       }						)



 def 				lowerCAmelCase_  (							self  :		Optional[int]    ,    _lowerCAmelCase  :		str    ,    _lowerCAmelCase  :		str = "rb"    ,    **_lowerCAmelCase  :		Optional[Any]    ,    ):
       if not isinstance(self.repo_info    ,    _lowerCAmelCase						):
             raise NotImplementedError(F"Open is only implemented for dataset repositories, but got {self.repo_info}"						)
       SCREAMING_SNAKE_CASE_     						=      hf_hub_url(self.repo_info.id    ,    _lowerCAmelCase    ,    revision=self.repo_info.sha						)
       return fsspec.open(
           _lowerCAmelCase    ,    mode=_lowerCAmelCase    ,    headers=get_authentication_headers_for_url(_lowerCAmelCase    ,    use_auth_token=self.token						)    ,    client_kwargs={'trust_env': True}    ,    ).open()



 def 				lowerCAmelCase_  (							self  :		List[Any]    ,    _lowerCAmelCase  :		Any    ,    **_lowerCAmelCase  :		Dict						):
       self._get_dirs()
       SCREAMING_SNAKE_CASE_     						=      self._strip_protocol(_lowerCAmelCase						)
       if path in self.dir_cache:
             return self.dir_cache[path]
       else:
             raise FileNotFoundError(_lowerCAmelCase						)




 def 				lowerCAmelCase_  (							self  :		Optional[Any]    ,    _lowerCAmelCase  :		Any    ,    _lowerCAmelCase  :		Any=False    ,    **_lowerCAmelCase  :		str						):
       self._get_dirs()
       SCREAMING_SNAKE_CASE_     						=      PurePosixPath(path.strip('/'						)						)
       SCREAMING_SNAKE_CASE_     						=      {}
       for p, f in self.dir_cache.items():
             SCREAMING_SNAKE_CASE_     						=      PurePosixPath(p.strip('/'						)						)
             SCREAMING_SNAKE_CASE_     						=      p.parent
             if root == path:
                   SCREAMING_SNAKE_CASE_     						=      f
       SCREAMING_SNAKE_CASE_     						=      list(paths.values()						)
       if detail:
             return out
       else:
             return sorted(f['name'] for f in out						)