




'''simple docstring'''







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						:	List[str]   ,				lowercase						:	Optional[Any]   ,				lowercase						:	int=13   ,				lowercase						:	Dict=7   ,				lowercase						:	Any=True   ,				lowercase						:	int=True   ,				lowercase						:	Optional[int]=False   ,				lowercase						:	List[str]=True   ,				lowercase						:	str=99   ,				lowercase						:	int=32   ,				lowercase						:	Tuple=5   ,				lowercase						:	List[str]=4   ,				lowercase						:	Optional[Any]=37   ,				lowercase						:	Optional[int]="gelu"   ,				lowercase						:	Optional[int]=0.1   ,				lowercase						:	str=0.1   ,				lowercase						:	Union[str, Any]=512   ,				lowercase						:	List[str]=16   ,				lowercase						:	Optional[Any]=2   ,				lowercase						:	str=0.02   ,				lowercase						:	str=3   ,				lowercase						:	List[Any]=4   ,				lowercase						:	Union[str, Any]=None   ,				):



           '''simple docstring'''

           UpperCAmelCase			       =		parent
           UpperCAmelCase			       =		batch_size
           UpperCAmelCase			       =		seq_length
           UpperCAmelCase			       =		is_training
           UpperCAmelCase			       =		use_input_mask
           UpperCAmelCase			       =		use_token_type_ids
           UpperCAmelCase			       =		use_labels
           UpperCAmelCase			       =		vocab_size
           UpperCAmelCase			       =		hidden_size
           UpperCAmelCase			       =		num_hidden_layers
           UpperCAmelCase			       =		num_attention_heads
           UpperCAmelCase			       =		intermediate_size
           UpperCAmelCase			       =		hidden_act
           UpperCAmelCase			       =		hidden_dropout_prob
           UpperCAmelCase			       =		attention_probs_dropout_prob
           UpperCAmelCase			       =		max_position_embeddings
           UpperCAmelCase			       =		type_vocab_size
           UpperCAmelCase			       =		type_sequence_label_size
           UpperCAmelCase			       =		initializer_range
           UpperCAmelCase			       =		num_labels
           UpperCAmelCase			       =		num_choices
           UpperCAmelCase			       =		scope
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		ids_tensor([self.batch_size, self.seq_length]   ,				self.vocab_size				)

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

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

           UpperCAmelCase			       =		self.get_config()

           return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels
       def     A      (	self						:	List[str]				):



           '''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     A      (	self						:	Union[str, Any]   ,				lowercase						:	int   ,				lowercase						:	Optional[Any]   ,				lowercase						:	Optional[int]   ,				lowercase						:	Any   ,				lowercase						:	Any   ,				lowercase						:	Optional[int]				):



           '''simple docstring'''

           UpperCAmelCase			       =		DistilBertModel(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		model(lowercase   ,				lowercase				)
           UpperCAmelCase			       =		model(lowercase				)
           self.parent.assertEqual(result.last_hidden_state.shape   ,				(self.batch_size, self.seq_length, self.hidden_size)				)
       def     A      (	self						:	Any   ,				lowercase						:	Dict   ,				lowercase						:	List[str]   ,				lowercase						:	List[Any]   ,				lowercase						:	Tuple   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	int				):



           '''simple docstring'''

           UpperCAmelCase			       =		DistilBertForMaskedLM(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		model(lowercase   ,				attention_mask=lowercase   ,				labels=lowercase				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.vocab_size)				)
       def     A      (	self						:	Tuple   ,				lowercase						:	int   ,				lowercase						:	Tuple   ,				lowercase						:	Optional[Any]   ,				lowercase						:	str   ,				lowercase						:	List[Any]   ,				lowercase						:	str				):



           '''simple docstring'''

           UpperCAmelCase			       =		DistilBertForQuestionAnswering(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		model(
               lowercase   ,				attention_mask=lowercase   ,				start_positions=lowercase   ,				end_positions=lowercase				)
           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     A      (	self						:	Tuple   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Optional[Any]   ,				lowercase						:	List[str]   ,				lowercase						:	Optional[int]   ,				lowercase						:	Optional[int]   ,				lowercase						:	int				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.num_labels
           UpperCAmelCase			       =		DistilBertForSequenceClassification(lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		model(lowercase   ,				attention_mask=lowercase   ,				labels=lowercase				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_labels)				)
       def     A      (	self						:	List[str]   ,				lowercase						:	Optional[Any]   ,				lowercase						:	Tuple   ,				lowercase						:	Any   ,				lowercase						:	str   ,				lowercase						:	Optional[Any]   ,				lowercase						:	Dict				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.num_labels
           UpperCAmelCase			       =		DistilBertForTokenClassification(config=lowercase				)
           model.to(lowercase				)
           model.eval()

           UpperCAmelCase			       =		model(lowercase   ,				attention_mask=lowercase   ,				labels=lowercase				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.seq_length, self.num_labels)				)
       def     A      (	self						:	Any   ,				lowercase						:	Tuple   ,				lowercase						:	List[str]   ,				lowercase						:	Dict   ,				lowercase						:	Union[str, Any]   ,				lowercase						:	Any   ,				lowercase						:	str				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.num_choices
           UpperCAmelCase			       =		DistilBertForMultipleChoice(config=lowercase				)
           model.to(lowercase				)
           model.eval()
           UpperCAmelCase			       =		input_ids.unsqueeze(1				).expand(-1   ,				self.num_choices   ,				-1				).contiguous()
           UpperCAmelCase			       =		input_mask.unsqueeze(1				).expand(-1   ,				self.num_choices   ,				-1				).contiguous()
           UpperCAmelCase			       =		model(
               lowercase   ,				attention_mask=lowercase   ,				labels=lowercase   ,				)
           self.parent.assertEqual(result.logits.shape   ,				(self.batch_size, self.num_choices)				)







       def     A      (	self						:	Any				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.prepare_config_and_inputs()
           ((UpperCAmelCase)       ,     (UpperCAmelCase)       ,     (UpperCAmelCase)       ,     (UpperCAmelCase)       ,     (UpperCAmelCase)       ,     (UpperCAmelCase))			       =		config_and_inputs
           UpperCAmelCase			       =		{'''input_ids''': input_ids, '''attention_mask''': input_mask}
           return config, inputs_dict






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



           '''simple docstring'''

           UpperCAmelCase			       =		DistilBertModelTester(self				)
           UpperCAmelCase			       =		ConfigTester(self   ,				config_class=lowercase   ,				dim=37				)
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       def     A      (	self						:	str				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_model(*lowercase				)
       def     A      (	self						:	Optional[Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_masked_lm(*lowercase				)
       def     A      (	self						:	List[str]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_question_answering(*lowercase				)
       def     A      (	self						:	str				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_sequence_classification(*lowercase				)
       def     A      (	self						:	Optional[int]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_token_classification(*lowercase				)
       def     A      (	self						:	List[str]				):



           '''simple docstring'''

           UpperCAmelCase			       =		self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_distilbert_for_multiple_choice(*lowercase				)
       @slow
       def     A      (	self						:	Tuple				):



           '''simple docstring'''

           for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               UpperCAmelCase			       =		DistilBertModel.from_pretrained(lowercase				)
               self.assertIsNotNone(lowercase				)







       @slow
       @require_torch_gpu
       def     A      (	self						:	str				):



           '''simple docstring'''

           UpperCAmelCase       ,     UpperCAmelCase			       =		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

               UpperCAmelCase			       =		True
               UpperCAmelCase			       =		model_class(config=lowercase				)

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

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






@require_torch
class 			_a  (	unittest.TestCase ):
       @slow
       def     A      (	self						:	Union[str, Any]				):



           '''simple docstring'''

           UpperCAmelCase			       =		DistilBertModel.from_pretrained('''distilbert-base-uncased'''				)
           UpperCAmelCase			       =		torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]				)
           UpperCAmelCase			       =		torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]				)
           with torch.no_grad():
               UpperCAmelCase			       =		model(lowercase   ,				attention_mask=lowercase				)[0]
           UpperCAmelCase			       =		torch.Size((1, 11, 768)				)
           self.assertEqual(output.shape   ,				lowercase				)
           UpperCAmelCase			       =		torch.tensor(
               [[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]				)

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









"""simple docstring"""






from __future__ import annotations

import time

import numpy as np

__snake_case							:   Optional[Any]      =    [8, 5, 9, 7]
__snake_case							:   List[Any]      =    [
    [2, 0, 1, 1],
    [0, 1, 2, 1],
    [4, 0, 0, 3],
    [0, 2, 1, 0],
    [1, 0, 3, 0],
]
__snake_case							:   Optional[int]      =    [
    [3, 2, 1, 4],
    [0, 2, 5, 2],
    [5, 1, 0, 5],
    [1, 5, 3, 0],
    [3, 0, 3, 3],
]



class 			A__   :




	'''simple docstring'''





	def __init__(     self:		Any       ,			_SCREAMING_SNAKE_CASE:		list[int]       ,			_SCREAMING_SNAKE_CASE:		list[list[int]]       ,			_SCREAMING_SNAKE_CASE:		list[list[int]]       ,			)				->       None:

				"""simple docstring"""

				__lowerCAmelCase      :  Any												=       claim_vector
				__lowerCAmelCase      :  Tuple												=       allocated_resources_table
				__lowerCAmelCase      :  Tuple												=       maximum_claim_table

	def 							_SCREAMING_SNAKE_CASE   (     self:		Union[str, Any])				->       list[int]:

				"""simple docstring"""

				return [
				    sum(p_item[i] for p_item in self.__allocated_resources_table)
				    for i in range(len(self.__allocated_resources_table[0]))
				]

	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       list[int]:

				"""simple docstring"""

				return np.array(self.__claim_vector) - np.array(
				    self.__processes_resource_summation())

	def 							_SCREAMING_SNAKE_CASE   (     self:		int)				->       list[list[int]]:

				"""simple docstring"""

				return [
				    list(np.array(self.__maximum_claim_table[i]) - np.array(_SCREAMING_SNAKE_CASE))
				    for i, allocated_resource in enumerate(self.__allocated_resources_table)
				]

	def 							_SCREAMING_SNAKE_CASE   (     self:		List[Any])				->       dict[int, list[int]]:

				"""simple docstring"""

				return {self.__need().index(_SCREAMING_SNAKE_CASE): i for i in self.__need()}

	def 							_SCREAMING_SNAKE_CASE   (     self:		Optional[Any]       ,			**_SCREAMING_SNAKE_CASE:		List[Any])				->       None:

				"""simple docstring"""

				__lowerCAmelCase      :  Optional[int]												=       self.__need()
				__lowerCAmelCase      :  int												=       self.__allocated_resources_table
				__lowerCAmelCase      :  Dict												=       self.__available_resources()
				__lowerCAmelCase      :  str												=       self.__need_index_manager()
				for kw, val in kwargs.items():
							if kw and val is True:
										self.__pretty_data()
				print("_" * 50 + "\n")
				while need_list:
							__lowerCAmelCase      :  int												=       False
							for each_need in need_list:
										__lowerCAmelCase      :  Dict												=       True
										for index, need in enumerate(_SCREAMING_SNAKE_CASE):
													if need > available_resources[index]:
																__lowerCAmelCase      :  Dict												=       False
																break
										if execution:
													__lowerCAmelCase      :  Any												=       True
													# get the original index of the process from ind_ctrl db
													for original_need_index, need_clone in need_index_manager.items():
																if each_need == need_clone:
																			__lowerCAmelCase      :  Union[str, Any]												=       original_need_index
													print(F"""Process {process_number + 1} is executing.""")
													# remove the process run from stack
													need_list.remove(_SCREAMING_SNAKE_CASE)
													# update available/freed resources stack
													__lowerCAmelCase      :  Dict												=       np.array(_SCREAMING_SNAKE_CASE) + np.array(
													    alloc_resources_table[process_number])
													print(
													    "Updated available resource stack for processes: "
													    + " ".join([str(_SCREAMING_SNAKE_CASE) for x in available_resources]))
													break
							if safe:
										print("The process is in a safe state.\n")
							else:
										print("System in unsafe state. Aborting...\n")
										break







	def 							_SCREAMING_SNAKE_CASE   (     self:		Union[str, Any])				->       List[Any]:

				"""simple docstring"""

				print(" " * 9 + "Allocated Resource Table")
				for item in self.__allocated_resources_table:
							print(
							    F"""P{self.__allocated_resources_table.index(_SCREAMING_SNAKE_CASE) + 1}"""
							    + " ".join(F"""{it:>8}""" for it in item)
							    + "\n")
				print(" " * 9 + "System Resource Table")
				for item in self.__maximum_claim_table:
							print(
							    F"""P{self.__maximum_claim_table.index(_SCREAMING_SNAKE_CASE) + 1}"""
							    + " ".join(F"""{it:>8}""" for it in item)
							    + "\n")
				print(
				    "Current Usage by Active Processes: "
				    + " ".join(str(_SCREAMING_SNAKE_CASE) for x in self.__claim_vector))
				print(
				    "Initial Available Resources:       "
				    + " ".join(str(_SCREAMING_SNAKE_CASE) for x in self.__available_resources()))
				time.sleep(1)


if __name__ == "__main__":
	import doctest

	doctest.testmod()