


import os
import tempfile
import unittest

from transformers import DistilBertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, 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 (
     DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
     DistilBertForMaskedLM,
     DistilBertForMultipleChoice,
     DistilBertForQuestionAnswering,
     DistilBertForSequenceClassification,
     DistilBertForTokenClassification,
     DistilBertModel,
 )





class   __a			(    A_      ):
 def __init__(   self				, lowerCAmelCase__				, lowerCAmelCase__=13				, lowerCAmelCase__=7				, lowerCAmelCase__=True				, lowerCAmelCase__=True				, lowerCAmelCase__=False				, lowerCAmelCase__=True				, lowerCAmelCase__=99				, lowerCAmelCase__=32				, lowerCAmelCase__=5				, lowerCAmelCase__=4				, lowerCAmelCase__=37				, lowerCAmelCase__="gelu"				, lowerCAmelCase__=0.1				, lowerCAmelCase__=0.1				, lowerCAmelCase__=512				, lowerCAmelCase__=16				, lowerCAmelCase__=2				, lowerCAmelCase__=0.0_2				, lowerCAmelCase__=3				, lowerCAmelCase__=4				, lowerCAmelCase__=None				, )					->				str:




        '''simple docstring'''
        lowercase__:   str  			=   parent
        lowercase__:   List[str]  			=   batch_size
        lowercase__:   Tuple  			=   seq_length
        lowercase__:   int  			=   is_training
        lowercase__:   str  			=   use_input_mask
        lowercase__:   Optional[int]  			=   use_token_type_ids
        lowercase__:   Union[str, Any]  			=   use_labels
        lowercase__:   Tuple  			=   vocab_size
        lowercase__:   List[Any]  			=   hidden_size
        lowercase__:   List[Any]  			=   num_hidden_layers
        lowercase__:   List[str]  			=   num_attention_heads
        lowercase__:   Optional[int]  			=   intermediate_size
        lowercase__:   str  			=   hidden_act
        lowercase__:   Union[str, Any]  			=   hidden_dropout_prob
        lowercase__:   Tuple  			=   attention_probs_dropout_prob
        lowercase__:   Optional[Any]  			=   max_position_embeddings
        lowercase__:   Any  			=   type_vocab_size
        lowercase__:   str  			=   type_sequence_label_size
        lowercase__:   Tuple  			=   initializer_range
        lowercase__:   List[Any]  			=   num_labels
        lowercase__:   Dict  			=   num_choices
        lowercase__:   List[Any]  			=   scope
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   Dict  			=   ids_tensor([self.batch_size, self.seq_length]				, self.vocab_size )

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

        lowercase__:   str  			=   None
        lowercase__:   Optional[Any]  			=   None
        lowercase__:   Tuple  			=   None
        if self.use_labels:
               lowercase__:   Optional[Any]  			=   ids_tensor([self.batch_size]				, self.type_sequence_label_size )
               lowercase__:   Optional[Any]  			=   ids_tensor([self.batch_size, self.seq_length]				, self.num_labels )
               lowercase__:   int  			=   ids_tensor([self.batch_size]				, self.num_choices )

        lowercase__:   Any  			=   self.get_config()

        return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




        '''simple docstring'''
        return DistilBertConfig(
            vocab_size=self.vocab_size				, dim=self.hidden_size				, n_layers=self.num_hidden_layers				, n_heads=self.num_attention_heads				, hidden_dim=self.intermediate_size				, hidden_act=self.hidden_act				, dropout=self.hidden_dropout_prob				, attention_dropout=self.attention_probs_dropout_prob				, max_position_embeddings=self.max_position_embeddings				, initializer_range=self.initializer_range				, )
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				List[str]:




        '''simple docstring'''
        lowercase__:   Any  			=   DistilBertModel(config=lowerCAmelCase__ )
        model.to(lowerCAmelCase__ )
        model.eval()
        lowercase__:   int  			=   model(lowerCAmelCase__				, lowerCAmelCase__ )
        lowercase__:   Union[str, Any]  			=   model(lowerCAmelCase__ )
        self.parent.assertEqual(result.last_hidden_state.shape				, (self.batch_size, self.seq_length, self.hidden_size) )
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				Tuple:




        '''simple docstring'''
        lowercase__:   Tuple  			=   DistilBertForMaskedLM(config=lowerCAmelCase__ )
        model.to(lowerCAmelCase__ )
        model.eval()
        lowercase__:   Dict  			=   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				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   List[str]  			=   DistilBertForQuestionAnswering(config=lowerCAmelCase__ )
        model.to(lowerCAmelCase__ )
        model.eval()
        lowercase__:   Union[str, Any]  			=   model(
            lowerCAmelCase__				, attention_mask=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 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				List[Any]:




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   self.num_labels
        lowercase__:   Tuple  			=   DistilBertForSequenceClassification(lowerCAmelCase__ )
        model.to(lowerCAmelCase__ )
        model.eval()
        lowercase__:   Any  			=   model(lowerCAmelCase__				, attention_mask=lowerCAmelCase__				, labels=lowerCAmelCase__ )
        self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.num_labels) )
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				Any:




        '''simple docstring'''
        lowercase__:   int  			=   self.num_labels
        lowercase__:   Union[str, Any]  			=   DistilBertForTokenClassification(config=lowerCAmelCase__ )
        model.to(lowerCAmelCase__ )
        model.eval()

        lowercase__:   Optional[Any]  			=   model(lowerCAmelCase__				, attention_mask=lowerCAmelCase__				, labels=lowerCAmelCase__ )
        self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.seq_length, self.num_labels) )
 def 		SCREAMING_SNAKE_CASE__   (   self				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__				, lowerCAmelCase__ )					->				str:




        '''simple docstring'''
        lowercase__:   Union[str, Any]  			=   self.num_choices
        lowercase__:   List[Any]  			=   DistilBertForMultipleChoice(config=lowerCAmelCase__ )
        model.to(lowerCAmelCase__ )
        model.eval()
        lowercase__:   Optional[Any]  			=   input_ids.unsqueeze(1 ).expand(-1				, self.num_choices				, -1 ).contiguous()
        lowercase__:   int  			=   input_mask.unsqueeze(1 ).expand(-1				, self.num_choices				, -1 ).contiguous()
        lowercase__:   Any  			=   model(
            lowerCAmelCase__				, attention_mask=lowerCAmelCase__				, labels=lowerCAmelCase__				, )
        self.parent.assertEqual(result.logits.shape				, (self.batch_size, self.num_choices) )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Dict:




        '''simple docstring'''
        lowercase__:   Tuple  			=   self.prepare_config_and_inputs()
        ((lowercase__)			,						(lowercase__)			,						(lowercase__)			,						(lowercase__)			,						(lowercase__)			,						(lowercase__)):   Tuple  			=   config_and_inputs
        lowercase__:   List[str]  			=   {'input_ids': input_ids, 'attention_mask': input_mask}
        return config, inputs_dict





@require_torch
class   __a			(    A_       ,       A_       ,       unittest.TestCase      ):
 __lowercase :							Any        = (
     (
         DistilBertModel,
         DistilBertForMaskedLM,
         DistilBertForMultipleChoice,
         DistilBertForQuestionAnswering,
         DistilBertForSequenceClassification,
         DistilBertForTokenClassification,
     )
     if is_torch_available()
     else None
 )
 __lowercase :							Optional[int]        = (
     {
         '''feature-extraction''': DistilBertModel,
         '''fill-mask''': DistilBertForMaskedLM,
         '''question-answering''': DistilBertForQuestionAnswering,
         '''text-classification''': DistilBertForSequenceClassification,
         '''token-classification''': DistilBertForTokenClassification,
         '''zero-shot''': DistilBertForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 __lowercase :							str        = True
 __lowercase :							str        = True
 __lowercase :							Union[str, Any]        = True
 __lowercase :							Optional[int]        = True
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				int:




        '''simple docstring'''
        lowercase__:   int  			=   DistilBertModelTester(self )
        lowercase__:   Dict  			=   ConfigTester(self				, config_class=lowerCAmelCase__				, dim=37 )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[int]:




        '''simple docstring'''
        self.config_tester.run_common_tests()
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[Any]:




        '''simple docstring'''
        lowercase__:   Optional[Any]  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_model(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Tuple:




        '''simple docstring'''
        lowercase__:   Optional[int]  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_masked_lm(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[Any]:




        '''simple docstring'''
        lowercase__:   List[Any]  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_question_answering(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[str]:




        '''simple docstring'''
        lowercase__:   str  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Optional[Any]:




        '''simple docstring'''
        lowercase__:   str  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_token_classification(*lowerCAmelCase__ )
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				Union[str, Any]:




        '''simple docstring'''
        lowercase__:   int  			=   self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowerCAmelCase__ )
 @slow
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[str]:




        '''simple docstring'''
        for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__:   Union[str, Any]  			=   DistilBertModel.from_pretrained(lowerCAmelCase__ )
               self.assertIsNotNone(lowerCAmelCase__ )
 @slow
 @require_torch_gpu
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				int:




        '''simple docstring'''
        lowercase__			,						lowercase__:   int  			=   self.model_tester.prepare_config_and_inputs_for_common()
        for model_class in self.all_model_classes:
               # BertForMultipleChoice behaves incorrectly in JIT environments.
               if model_class == DistilBertForMultipleChoice:
                      return

               lowercase__:   Any  			=   True
               lowercase__:   List[str]  			=   model_class(config=lowerCAmelCase__ )

               lowercase__:   Any  			=   self._prepare_for_class(lowerCAmelCase__				, lowerCAmelCase__ )
               lowercase__:   Optional[int]  			=   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__				, 'traced_model.pt' ) )
                      lowercase__:   Any  			=   torch.jit.load(os.path.join(lowerCAmelCase__				, 'traced_model.pt' )				, map_location=lowerCAmelCase__ )
                      loaded(inputs_dict['input_ids'].to(lowerCAmelCase__ )				, inputs_dict['attention_mask'].to(lowerCAmelCase__ ) )





@require_torch
class   __a			(    unittest.TestCase      ):
 @slow
 def 		SCREAMING_SNAKE_CASE__   (   self )					->				List[Any]:




        '''simple docstring'''
        lowercase__:   Optional[int]  			=   DistilBertModel.from_pretrained('distilbert-base-uncased' )
        lowercase__:   Optional[Any]  			=   torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]] )
        lowercase__:   Dict  			=   torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]] )
        with torch.no_grad():
               lowercase__:   Optional[Any]  			=   model(lowerCAmelCase__				, attention_mask=lowerCAmelCase__ )[0]
        lowercase__:   Optional[Any]  			=   torch.Size((1, 11, 768) )
        self.assertEqual(output.shape				, lowerCAmelCase__ )
        lowercase__:   Optional[Any]  			=   torch.tensor(
            [[[-0.1_6_3_9, 0.3_2_9_9, 0.1_6_4_8], [-0.1_7_4_6, 0.3_2_8_9, 0.1_7_1_0], [-0.1_8_8_4, 0.3_3_5_7, 0.1_8_1_0]]] )

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






from collections.abc import Sequence







def        _lowerCamelCase(	lowercase__   , lowercase__ = False	)						->	float:

		'''simple docstring'''







		if not arr:
				return 0

		__lowercase=			0 if allow_empty_subarrays else float('-inf'	)
		__lowercase=			0.0
		for num in arr:
				__lowercase=			max(0 if allow_empty_subarrays else num   , curr_sum + num	)
				__lowercase=			max(lowercase__   , lowercase__	)

		return max_sum


if __name__ == "__main__":
	from doctest import testmod

	testmod()

	lowerCAmelCase           =		[-2, 1, -3, 4, -1, 2, 1, -5, 4]
	print(F'{max_subarray_sum(nums) = }')






