


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




							"""simple docstring"""


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

							def 					__snake_case      (							self :						Optional[Any]       )			->					List[str]:
														__snake_case	:    Tuple											=     ids_tensor([self.batch_size, self.seq_length]					,			self.vocab_size       )

														__snake_case	:    List[str]											=     None
														if self.use_input_mask:
																					__snake_case	:    Dict											=     random_attention_mask([self.batch_size, self.seq_length]       )

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

														__snake_case	:    Optional[Any]											=     self.get_config()

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

							def 					__snake_case      (							self :						List[Any]       )			->					List[str]:
														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 					__snake_case      (							self :						Dict					,			lowerCamelCase :						List[Any]					,			lowerCamelCase :						str					,			lowerCamelCase :						Union[str, Any]					,			lowerCamelCase :						str					,			lowerCamelCase :						Tuple					,			lowerCamelCase :						List[Any]       )			->					Dict:
														__snake_case	:    Any											=     DistilBertModel(config=_A       )
														model.to(_A       )
														model.eval()
														__snake_case	:    Optional[int]											=     model(_A					,			_A       )
														__snake_case	:    List[Any]											=     model(_A       )
														self.parent.assertEqual(result.last_hidden_state.shape					,			(self.batch_size, self.seq_length, self.hidden_size)       )

							def 					__snake_case      (							self :						Dict					,			lowerCamelCase :						str					,			lowerCamelCase :						Any					,			lowerCamelCase :						str					,			lowerCamelCase :						str					,			lowerCamelCase :						int					,			lowerCamelCase :						str       )			->					List[str]:
														__snake_case	:    Tuple											=     DistilBertForMaskedLM(config=_A       )
														model.to(_A       )
														model.eval()
														__snake_case	:    str											=     model(_A					,			attention_mask=_A					,			labels=_A       )
														self.parent.assertEqual(result.logits.shape					,			(self.batch_size, self.seq_length, self.vocab_size)       )

							def 					__snake_case      (							self :						List[Any]					,			lowerCamelCase :						str					,			lowerCamelCase :						Any					,			lowerCamelCase :						Dict					,			lowerCamelCase :						str					,			lowerCamelCase :						Tuple					,			lowerCamelCase :						Optional[Any]       )			->					Dict:
														__snake_case	:    Union[str, Any]											=     DistilBertForQuestionAnswering(config=_A       )
														model.to(_A       )
														model.eval()
														__snake_case	:    List[Any]											=     model(
														    _A					,			attention_mask=_A					,			start_positions=_A					,			end_positions=_A       )
														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 					__snake_case      (							self :						Dict					,			lowerCamelCase :						Optional[Any]					,			lowerCamelCase :						Optional[int]					,			lowerCamelCase :						Optional[int]					,			lowerCamelCase :						List[str]					,			lowerCamelCase :						List[Any]					,			lowerCamelCase :						Tuple       )			->					List[str]:
														__snake_case	:    Dict											=     self.num_labels
														__snake_case	:    Any											=     DistilBertForSequenceClassification(_A       )
														model.to(_A       )
														model.eval()
														__snake_case	:    Tuple											=     model(_A					,			attention_mask=_A					,			labels=_A       )
														self.parent.assertEqual(result.logits.shape					,			(self.batch_size, self.num_labels)       )

							def 					__snake_case      (							self :						List[Any]					,			lowerCamelCase :						List[Any]					,			lowerCamelCase :						Dict					,			lowerCamelCase :						Optional[Any]					,			lowerCamelCase :						Optional[Any]					,			lowerCamelCase :						List[str]					,			lowerCamelCase :						Any       )			->					Union[str, Any]:
														__snake_case	:    Tuple											=     self.num_labels
														__snake_case	:    Any											=     DistilBertForTokenClassification(config=_A       )
														model.to(_A       )
														model.eval()

														__snake_case	:    str											=     model(_A					,			attention_mask=_A					,			labels=_A       )
														self.parent.assertEqual(result.logits.shape					,			(self.batch_size, self.seq_length, self.num_labels)       )

							def 					__snake_case      (							self :						Union[str, Any]					,			lowerCamelCase :						Tuple					,			lowerCamelCase :						str					,			lowerCamelCase :						str					,			lowerCamelCase :						Optional[int]					,			lowerCamelCase :						Optional[int]					,			lowerCamelCase :						List[str]       )			->					str:
														__snake_case	:    Tuple											=     self.num_choices
														__snake_case	:    int											=     DistilBertForMultipleChoice(config=_A       )
														model.to(_A       )
														model.eval()
														__snake_case	:    Tuple											=     input_ids.unsqueeze(1       ).expand(-1					,			self.num_choices					,			-1       ).contiguous()
														__snake_case	:    Any											=     input_mask.unsqueeze(1       ).expand(-1					,			self.num_choices					,			-1       ).contiguous()
														__snake_case	:    List[Any]											=     model(
														    _A					,			attention_mask=_A					,			labels=_A					,			)
														self.parent.assertEqual(result.logits.shape					,			(self.batch_size, self.num_choices)       )






							def 					__snake_case      (							self :						Tuple       )			->					Dict:
														__snake_case	:    Optional[int]											=     self.prepare_config_and_inputs()
														(__snake_case)	:    Union[str, Any]											=     config_and_inputs
														__snake_case	:    Union[str, Any]											=     {'input_ids': input_ids, 'attention_mask': input_mask}
														return config, inputs_dict

@require_torch
class        a	(a__  ,       a__  ,       unittest.TestCase   ):




							"""simple docstring"""


							__UpperCAmelCase  						=						(
							    (
							        DistilBertModel,
							        DistilBertForMaskedLM,
							        DistilBertForMultipleChoice,
							        DistilBertForQuestionAnswering,
							        DistilBertForSequenceClassification,
							        DistilBertForTokenClassification,
							    )
							    if is_torch_available()
							    else None
							)
							__UpperCAmelCase  						=						(
							    {
							        "feature-extraction": DistilBertModel,
							        "fill-mask": DistilBertForMaskedLM,
							        "question-answering": DistilBertForQuestionAnswering,
							        "text-classification": DistilBertForSequenceClassification,
							        "token-classification": DistilBertForTokenClassification,
							        "zero-shot": DistilBertForSequenceClassification,
							    }
							    if is_torch_available()
							    else {}
							)
							__UpperCAmelCase  						=						True
							__UpperCAmelCase  						=						True
							__UpperCAmelCase  						=						True
							__UpperCAmelCase  						=						True

							def 					__snake_case      (							self :						int       )			->					Any:
														__snake_case	:    int											=     DistilBertModelTester(self       )
														__snake_case	:    List[Any]											=     ConfigTester(self					,			config_class=_A					,			dim=37       )

							def 					__snake_case      (							self :						Dict       )			->					Union[str, Any]:
														self.config_tester.run_common_tests()

							def 					__snake_case      (							self :						Any       )			->					Any:
														__snake_case	:    Dict											=     self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_model(*_A       )

							def 					__snake_case      (							self :						List[Any]       )			->					List[Any]:
														__snake_case	:    List[str]											=     self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_for_masked_lm(*_A       )

							def 					__snake_case      (							self :						List[str]       )			->					Dict:
														__snake_case	:    Optional[Any]											=     self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_for_question_answering(*_A       )

							def 					__snake_case      (							self :						List[str]       )			->					Optional[int]:
														__snake_case	:    Dict											=     self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_for_sequence_classification(*_A       )

							def 					__snake_case      (							self :						Optional[Any]       )			->					Union[str, Any]:
														__snake_case	:    Any											=     self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_for_token_classification(*_A       )

							def 					__snake_case      (							self :						Optional[Any]       )			->					Any:
														__snake_case	:    Tuple											=     self.model_tester.prepare_config_and_inputs()
														self.model_tester.create_and_check_distilbert_for_multiple_choice(*_A       )

							@slow
							def 					__snake_case      (							self :						str       )			->					List[str]:

														for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																					__snake_case	:    List[str]											=     DistilBertModel.from_pretrained(_A       )
																					self.assertIsNotNone(_A       )






							@slow
							@require_torch_gpu
							def 					__snake_case      (							self :						int       )			->					int:
														__snake_case	:    Union[str, Any]											=     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

																					__snake_case	:    Dict											=     True
																					__snake_case	:    Tuple											=     model_class(config=_A       )

																					__snake_case	:    List[Any]											=     self._prepare_for_class(_A					,			_A       )
																					__snake_case	:    Dict											=     torch.jit.trace(
																					    _A					,			(inputs_dict["input_ids"].to("cpu"       ), inputs_dict["attention_mask"].to("cpu"       ))       )

																					with tempfile.TemporaryDirectory() as tmp:
																												torch.jit.save(_A					,			os.path.join(_A					,			"traced_model.pt"       )       )
																												__snake_case	:    int											=     torch.jit.load(os.path.join(_A					,			"traced_model.pt"       )					,			map_location=_A       )
																												loaded(inputs_dict["input_ids"].to(_A       )					,			inputs_dict["attention_mask"].to(_A       )       )

@require_torch
class        a	(unittest.TestCase   ):




							"""simple docstring"""


							@slow
							def 					__snake_case      (							self :						str       )			->					Optional[int]:
														__snake_case	:    str											=     DistilBertModel.from_pretrained("distilbert-base-uncased"       )
														__snake_case	:    Tuple											=     torch.tensor([[0, 345, 232, 328, 740, 140, 1695, 69, 6078, 1588, 2]]       )
														__snake_case	:    int											=     torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]       )
														with torch.no_grad():
																					__snake_case	:    Union[str, Any]											=     model(_A					,			attention_mask=_A       )[0]
														__snake_case	:    Tuple											=     torch.Size((1, 11, 768)       )
														self.assertEqual(output.shape					,			_A       )
														__snake_case	:    int											=     torch.tensor(
														    [[[-0.16_39, 0.32_99, 0.16_48], [-0.17_46, 0.32_89, 0.17_10], [-0.18_84, 0.33_57, 0.18_10]]]       )

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





import unittest

import numpy as np







def        lowerCAmelCase_  (      __lowerCamelCase							,			__lowerCamelCase							,			__lowerCamelCase							,			__lowerCamelCase = None							,			):
							__snake_case	:    List[str]											=     np.shape(__lowerCamelCase			)
							__snake_case	:    Optional[Any]											=     np.shape(__lowerCamelCase			)
							__snake_case	:    List[str]											=     np.shape(__lowerCamelCase			)

							if shape_a[0] != shape_b[0]:
														__snake_case	:    Any											=     (
														    "Expected the same number of rows for A and B. "
														    F'Instead found A of size {shape_a} and B of size {shape_b}'
														)
														raise ValueError(__lowerCamelCase			)

							if shape_b[1] != shape_c[1]:
														__snake_case	:    int											=     (
														    "Expected the same number of columns for B and C. "
														    F'Instead found B of size {shape_b} and C of size {shape_c}'
														)
														raise ValueError(__lowerCamelCase			)

							__snake_case	:    str											=     pseudo_inv
							if a_inv is None:
														try:
																					__snake_case	:    Optional[Any]											=     np.linalg.inv(__lowerCamelCase			)
														except np.linalg.LinAlgError:
																					raise ValueError(
																					    "Input matrix A is not invertible. Cannot compute Schur complement."			)

							return mat_c - mat_b.T @ a_inv @ mat_b

class        a	(unittest.TestCase   ):




							"""simple docstring"""


							def 					__snake_case      (							self :						Tuple       )			->					None:
														__snake_case	:    str											=     np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]       )
														__snake_case	:    str											=     np.array([[0, 3], [3, 0], [2, 3]]       )
														__snake_case	:    Dict											=     np.array([[2, 1], [6, 3]]       )

														__snake_case	:    Dict											=     schur_complement(lowerCamelCase					,			lowerCamelCase					,			lowerCamelCase       )

														__snake_case	:    int											=     np.block([[a, b], [b.T, c]]       )

														__snake_case	:    Optional[int]											=     np.linalg.det(lowerCamelCase       )
														__snake_case	:    Any											=     np.linalg.det(lowerCamelCase       )
														__snake_case	:    Tuple											=     np.linalg.det(lowerCamelCase       )

														self.assertAlmostEqual(lowerCamelCase					,			det_a * det_s       )

							def 					__snake_case      (							self :						int       )			->					None:
														__snake_case	:    str											=     np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]       )
														__snake_case	:    Dict											=     np.array([[0, 3], [3, 0], [2, 3]]       )
														__snake_case	:    Tuple											=     np.array([[2, 1], [6, 3]]       )

														with self.assertRaises(lowerCamelCase       ):
																					schur_complement(lowerCamelCase					,			lowerCamelCase					,			lowerCamelCase       )






							def 					__snake_case      (							self :						List[Any]       )			->					None:
														__snake_case	:    List[Any]											=     np.array([[1, 2, 1], [2, 1, 2], [3, 2, 4]]       )
														__snake_case	:    Tuple											=     np.array([[0, 3], [3, 0], [2, 3]]       )
														__snake_case	:    List[Any]											=     np.array([[2, 1, 3], [6, 3, 5]]       )

														with self.assertRaises(lowerCamelCase       ):
																					schur_complement(lowerCamelCase					,			lowerCamelCase					,			lowerCamelCase       )


if __name__ == "__main__":
			import doctest

			doctest.testmod()
			unittest.main()

