




import unittest

from transformers import BertGenerationConfig, is_torch_available
from transformers.testing_utils import require_torch, 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 BertGenerationDecoder, BertGenerationEncoder




class 					snake_case__							:
				"""simple docstring"""



				def __init__(   self       :  Optional[int]      ,   __lowerCamelCase       :  Union[str, Any]      ,   __lowerCamelCase       :  Union[str, Any]=13      ,   __lowerCamelCase       :  Optional[int]=7      ,   __lowerCamelCase       :  Optional[Any]=True      ,   __lowerCamelCase       :  Tuple=True      ,   __lowerCamelCase       :  str=99      ,   __lowerCamelCase       :  Optional[Any]=32      ,   __lowerCamelCase       :  Union[str, Any]=5      ,   __lowerCamelCase       :  Dict=4      ,   __lowerCamelCase       :  List[Any]=37      ,   __lowerCamelCase       :  Tuple="gelu"      ,   __lowerCamelCase       :  List[Any]=0.1      ,   __lowerCamelCase       :  Optional[Any]=0.1      ,   __lowerCamelCase       :  int=50      ,   __lowerCamelCase       :  List[str]=0.02      ,   __lowerCamelCase       :  int=True      ,   __lowerCamelCase       :  Optional[int]=None      ,   )      ->  Union[str, Any]:
							a					=    parent
							a					=    batch_size
							a					=    seq_length
							a					=    is_training
							a					=    use_input_mask
							a					=    vocab_size
							a					=    hidden_size
							a					=    num_hidden_layers
							a					=    num_attention_heads
							a					=    intermediate_size
							a					=    hidden_act
							a					=    hidden_dropout_prob
							a					=    attention_probs_dropout_prob
							a					=    max_position_embeddings
							a					=    initializer_range
							a					=    use_labels
							a					=    scope





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  Tuple:
							a					=    ids_tensor([self.batch_size, self.seq_length]      ,   self.vocab_size							)

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

							if self.use_labels:
										a					=    ids_tensor([self.batch_size, self.seq_length]      ,   self.vocab_size							)

							a					=    self.get_config()

							return config, input_ids, input_mask, token_labels





				def 							__UpperCAmelCase							(   self       :  Tuple							)      ->  int:
							return BertGenerationConfig(
							    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      ,   is_decoder=__lowerCamelCase      ,   initializer_range=self.initializer_range      ,   )





				def 							__UpperCAmelCase							(   self       :  Union[str, Any]							)      ->  List[Any]:
							(
							    (
							    a
							)					,	(
							    a
							)					,	(
							    a
							)					,	(
							    a
							)					,	
							)					=    self.prepare_config_and_inputs()

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

							return (
							    config,
							    input_ids,
							    input_mask,
							    token_labels,
							    encoder_hidden_states,
							    encoder_attention_mask,
							)





				def 							__UpperCAmelCase							(   self       :  int      ,   __lowerCamelCase       :  List[Any]      ,   __lowerCamelCase       :  str      ,   __lowerCamelCase       :  Tuple      ,   __lowerCamelCase       :  Optional[Any]      ,   **__lowerCamelCase       :  Any      ,   )      ->  Optional[int]:
							a					=    BertGenerationEncoder(config=__lowerCamelCase							)
							model.to(__lowerCamelCase							)
							model.eval()
							a					=    model(__lowerCamelCase      ,   attention_mask=__lowerCamelCase							)
							a					=    model(__lowerCamelCase							)
							self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, self.seq_length, self.hidden_size)							)





				def 							__UpperCAmelCase							(   self       :  str      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  Union[str, Any]      ,   __lowerCamelCase       :  Dict      ,   __lowerCamelCase       :  Optional[Any]      ,   __lowerCamelCase       :  Optional[Any]      ,   **__lowerCamelCase       :  Union[str, Any]      ,   )      ->  List[Any]:
							a					=    True
							a					=    BertGenerationEncoder(config=__lowerCamelCase							)
							model.to(__lowerCamelCase							)
							model.eval()
							a					=    model(
							    __lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   encoder_hidden_states=__lowerCamelCase      ,   encoder_attention_mask=__lowerCamelCase      ,   )
							a					=    model(
							    __lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   encoder_hidden_states=__lowerCamelCase      ,   )
							self.parent.assertEqual(result.last_hidden_state.shape      ,   (self.batch_size, self.seq_length, self.hidden_size)							)





				def 							__UpperCAmelCase							(   self       :  List[Any]      ,   __lowerCamelCase       :  str      ,   __lowerCamelCase       :  Optional[Any]      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  List[Any]      ,   __lowerCamelCase       :  str      ,   __lowerCamelCase       :  str      ,   **__lowerCamelCase       :  str      ,   )      ->  Any:
							a					=    True
							a					=    True
							a					=    BertGenerationDecoder(config=__lowerCamelCase							).to(__lowerCamelCase							).eval()

							# first forward pass
							a					=    model(
							    __lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   encoder_hidden_states=__lowerCamelCase      ,   encoder_attention_mask=__lowerCamelCase      ,   use_cache=__lowerCamelCase      ,   )
							a					=    outputs.past_key_values

							# create hypothetical multiple next token and extent to next_input_ids
							a					=    ids_tensor((self.batch_size, 3)      ,   config.vocab_size							)
							a					=    ids_tensor((self.batch_size, 3)      ,   vocab_size=2							)

							# append to next input_ids and
							a					=    torch.cat([input_ids, next_tokens]      ,   dim=-1							)
							a					=    torch.cat([input_mask, next_mask]      ,   dim=-1							)

							a					=    model(
							    __lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   encoder_hidden_states=__lowerCamelCase      ,   encoder_attention_mask=__lowerCamelCase      ,   output_hidden_states=__lowerCamelCase      ,   )["hidden_states"][0]
							a					=    model(
							    __lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   encoder_hidden_states=__lowerCamelCase      ,   encoder_attention_mask=__lowerCamelCase      ,   past_key_values=__lowerCamelCase      ,   output_hidden_states=__lowerCamelCase      ,   )["hidden_states"][0]

							# select random slice
							a					=    ids_tensor((1,)      ,   output_from_past.shape[-1]							).item()
							a					=    output_from_no_past[:, -3:, random_slice_idx].detach()
							a					=    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 							__UpperCAmelCase							(   self       :  Union[str, Any]      ,   __lowerCamelCase       :  Optional[Any]      ,   __lowerCamelCase       :  Optional[int]      ,   __lowerCamelCase       :  Tuple      ,   __lowerCamelCase       :  List[Any]      ,   *__lowerCamelCase       :  Dict      ,   )      ->  List[str]:
							a					=    BertGenerationDecoder(__lowerCamelCase							)
							model.to(__lowerCamelCase							)
							model.eval()
							a					=    model(__lowerCamelCase      ,   attention_mask=__lowerCamelCase      ,   labels=__lowerCamelCase							)
							self.parent.assertEqual(result.logits.shape      ,   (self.batch_size, self.seq_length, self.vocab_size)							)





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  Optional[int]:
							a					,	a					,	a					,	a					=    self.prepare_config_and_inputs()
							a					=    {"input_ids": input_ids, "attention_mask": input_mask}
							return config, inputs_dict




@require_torch
class 					snake_case__							(_UpperCamelCase					, _UpperCamelCase					, _UpperCamelCase					, unittest.TestCase     ):
				"""simple docstring"""



				SCREAMING_SNAKE_CASE_ :							List[Any]											=      (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE_ :							Optional[int]											=      (BertGenerationDecoder,) if is_torch_available() else ()
				SCREAMING_SNAKE_CASE_ :							Optional[Any]											=      (
				    {"""feature-extraction""": BertGenerationEncoder, """text-generation""": BertGenerationDecoder}
				    if is_torch_available()
				    else {}
				)





				def 							__UpperCAmelCase							(   self       :  Optional[int]							)      ->  Optional[Any]:
							a					=    BertGenerationEncoderTester(self							)
							a					=    ConfigTester(self      ,   config_class=__lowerCamelCase      ,   hidden_size=37							)





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  Dict:
							self.config_tester.run_common_tests()





				def 							__UpperCAmelCase							(   self       :  Dict							)      ->  str:
							a					=    self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  List[str]:
							a					,	a					,	a					,	a					=    self.model_tester.prepare_config_and_inputs()
							a					=    "bert"
							self.model_tester.create_and_check_model(__lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  str							)      ->  Optional[Any]:
							a					=    self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  List[Any]							)      ->  List[Any]:
							a					=    self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_decoder_model_past_large_inputs(*__lowerCamelCase							)





				def 							__UpperCAmelCase							(   self       :  Optional[Any]							)      ->  Union[str, Any]:
							# This regression test was failing with PyTorch < 1.3
							(
							    (
							    a
							)					,	(
							    a
							)					,	(
							    a
							)					,	(
							    a
							)					,	(
							    a
							)					,	(
							    a
							)					,	
							)					=    self.model_tester.prepare_config_and_inputs_for_decoder()

							a					=    None

							self.model_tester.create_and_check_model_as_decoder(
							    __lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase      ,   __lowerCamelCase      ,   )





				def 							__UpperCAmelCase							(   self       :  Union[str, Any]							)      ->  Any:
							a					=    self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_for_causal_lm(*__lowerCamelCase							)





				@slow
				def 							__UpperCAmelCase							(   self       :  Any							)      ->  Optional[int]:
							a					=    BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"							)
							self.assertIsNotNone(__lowerCamelCase							)




@require_torch
class 					snake_case__							(unittest.TestCase     ):
				"""simple docstring"""



				@slow
				def 							__UpperCAmelCase							(   self       :  str							)      ->  List[Any]:
							a					=    BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"							)
							a					=    torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]							)
							with torch.no_grad():
										a					=    model(__lowerCamelCase							)[0]
							a					=    torch.Size([1, 8, 10_24]							)
							self.assertEqual(output.shape      ,   __lowerCamelCase							)
							a					=    torch.tensor(
							    [[[0.1_775, 0.0_083, -0.0_321], [1.6_002, 0.1_287, 0.3_912], [2.1_473, 0.5_791, 0.6_066]]]							)
							self.assertTrue(torch.allclose(output[:, :3, :3]      ,   __lowerCamelCase      ,   atol=1e-4							)							)




@require_torch
class 					snake_case__							(unittest.TestCase     ):
				"""simple docstring"""



				@slow
				def 							__UpperCAmelCase							(   self       :  List[Any]							)      ->  Dict:
							a					=    BertGenerationDecoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"							)
							a					=    torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]							)
							with torch.no_grad():
										a					=    model(__lowerCamelCase							)[0]
							a					=    torch.Size([1, 8, 5_03_58]							)
							self.assertEqual(output.shape      ,   __lowerCamelCase							)
							a					=    torch.tensor(
							    [[[-0.5_788, -2.5_994, -3.7_054], [0.0_438, 4.7_997, 1.8_795], [1.5_862, 6.6_409, 4.4_638]]]							)
							self.assertTrue(torch.allclose(output[:, :3, :3]      ,   __lowerCamelCase      ,   atol=1e-4							)							)












from __future__ import annotations

import time

import numpy as np

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




class 					snake_case__							:
				"""simple docstring"""



				def __init__(   self       :  Any      ,   __lowerCamelCase       :  list[int]      ,   __lowerCamelCase       :  list[list[int]]      ,   __lowerCamelCase       :  list[list[int]]      ,   )      ->  None:
							a					=    claim_vector
							a					=    allocated_resources_table
							a					=    maximum_claim_table





				def 							__UpperCAmelCase							(   self       :  List[str]							)      ->  list[int]:
							return [
							    sum(p_item[i] for p_item in self.__allocated_resources_table							)
							    for i in range(len(self.__allocated_resources_table[0]							)							)
							]





				def 							__UpperCAmelCase							(   self       :  str							)      ->  list[int]:
							return np.array(self.__claim_vector							) - np.array(
							    self.__processes_resource_summation()							)





				def 							__UpperCAmelCase							(   self       :  Dict							)      ->  list[list[int]]:
							return [
							    list(np.array(self.__maximum_claim_table[i]							) - np.array(__lowerCamelCase							)							)
							    for i, allocated_resource in enumerate(self.__allocated_resources_table							)
							]





				def 							__UpperCAmelCase							(   self       :  Dict							)      ->  dict[int, list[int]]:
							return {self.__need().index(__lowerCamelCase							): i for i in self.__need()}





				def 							__UpperCAmelCase							(   self       :  Optional[Any]      ,   **__lowerCamelCase       :  Any							)      ->  None:
							a					=    self.__need()
							a					=    self.__allocated_resources_table
							a					=    self.__available_resources()
							a					=    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:
										a					=    False
										for each_need in need_list:
													a					=    True
													for index, need in enumerate(__lowerCamelCase							):
																if need > available_resources[index]:
																			a					=    False
																			break
													if execution:
																a					=    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:
																						a					=    original_need_index
																print(f"""Process {process_number + 1} is executing."""							)
																# remove the process run from stack
																need_list.remove(__lowerCamelCase							)
																# update available/freed resources stack
																a					=    np.array(__lowerCamelCase							) + np.array(
																    alloc_resources_table[process_number]							)
																print(
																    "Updated available resource stack for processes: "
																    + " ".join([str(__lowerCamelCase							) 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 							__UpperCAmelCase							(   self       :  Any							)      ->  str:
							print(" " * 9 + "Allocated Resource Table"							)
							for item in self.__allocated_resources_table:
										print(
										    f"""P{self.__allocated_resources_table.index(__lowerCamelCase							) + 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(__lowerCamelCase							) + 1}"""
										    + " ".join(f"""{it:>8}""" for it in item							)
										    + "\n"							)
							print(
							    "Current Usage by Active Processes: "
							    + " ".join(str(__lowerCamelCase							) for x in self.__claim_vector							)							)
							print(
							    "Initial Available Resources:       "
							    + " ".join(str(__lowerCamelCase							) for x in self.__available_resources()							)							)
							time.sleep(1							)


if __name__ == "__main__":
		import doctest

		doctest.testmod()






