




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 						UpperCAmelCase  :







						'''simple docstring'''





						def __init__(		self				:	List[Any]			,      __lowercase				:	str			,      __lowercase				:	Any=13			,      __lowercase				:	Optional[Any]=7			,      __lowercase				:	str=True			,      __lowercase				:	Tuple=True			,      __lowercase				:	Tuple=99			,      __lowercase				:	Dict=32			,      __lowercase				:	int=5			,      __lowercase				:	List[str]=4			,      __lowercase				:	int=37			,      __lowercase				:	Dict="gelu"			,      __lowercase				:	str=0.1			,      __lowercase				:	List[str]=0.1			,      __lowercase				:	Optional[int]=50			,      __lowercase				:	Optional[int]=0.02			,      __lowercase				:	Dict=True			,      __lowercase				:	int=None			,      ):






							"""simple docstring"""






							snake_case_						       =	parent
							snake_case_						       =	batch_size
							snake_case_						       =	seq_length
							snake_case_						       =	is_training
							snake_case_						       =	use_input_mask
							snake_case_						       =	vocab_size
							snake_case_						       =	hidden_size
							snake_case_						       =	num_hidden_layers
							snake_case_						       =	num_attention_heads
							snake_case_						       =	intermediate_size
							snake_case_						       =	hidden_act
							snake_case_						       =	hidden_dropout_prob
							snake_case_						       =	attention_probs_dropout_prob
							snake_case_						       =	max_position_embeddings
							snake_case_						       =	initializer_range
							snake_case_						       =	use_labels
							snake_case_						       =	scope






						def 			snake_case__		(		self				:	int		):






							"""simple docstring"""






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

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

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

							snake_case_						       =	self.get_config()

							return config, input_ids, input_mask, token_labels






						def 			snake_case__		(		self				:	Optional[int]		):






							"""simple docstring"""






							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=__lowercase			,      initializer_range=self.initializer_range			,      )






						def 			snake_case__		(		self				:	Tuple		):






							"""simple docstring"""






							(
							    (
							    snake_case_
							)       , (
							    snake_case_
							)       , (
							    snake_case_
							)       , (
							    snake_case_
							)       , 
							)						       =	self.prepare_config_and_inputs()

							snake_case_						       =	True
							snake_case_						       =	floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
							snake_case_						       =	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 			snake_case__		(		self				:	Any			,      __lowercase				:	List[str]			,      __lowercase				:	Union[str, Any]			,      __lowercase				:	List[str]			,      __lowercase				:	List[str]			,      **__lowercase				:	Any			,      ):






							"""simple docstring"""






							snake_case_						       =	BertGenerationEncoder(config=__lowercase		)
							model.to(__lowercase		)
							model.eval()
							snake_case_						       =	model(__lowercase			,      attention_mask=__lowercase		)
							snake_case_						       =	model(__lowercase		)
							self.parent.assertEqual(result.last_hidden_state.shape			,      (self.batch_size, self.seq_length, self.hidden_size)		)






						def 			snake_case__		(		self				:	Optional[Any]			,      __lowercase				:	Optional[int]			,      __lowercase				:	str			,      __lowercase				:	Dict			,      __lowercase				:	Optional[int]			,      __lowercase				:	Tuple			,      __lowercase				:	str			,      **__lowercase				:	List[Any]			,      ):






							"""simple docstring"""






							snake_case_						       =	True
							snake_case_						       =	BertGenerationEncoder(config=__lowercase		)
							model.to(__lowercase		)
							model.eval()
							snake_case_						       =	model(
							    __lowercase			,      attention_mask=__lowercase			,      encoder_hidden_states=__lowercase			,      encoder_attention_mask=__lowercase			,      )
							snake_case_						       =	model(
							    __lowercase			,      attention_mask=__lowercase			,      encoder_hidden_states=__lowercase			,      )
							self.parent.assertEqual(result.last_hidden_state.shape			,      (self.batch_size, self.seq_length, self.hidden_size)		)






						def 			snake_case__		(		self				:	Dict			,      __lowercase				:	Tuple			,      __lowercase				:	Dict			,      __lowercase				:	List[str]			,      __lowercase				:	str			,      __lowercase				:	List[Any]			,      __lowercase				:	Union[str, Any]			,      **__lowercase				:	Dict			,      ):






							"""simple docstring"""






							snake_case_						       =	True
							snake_case_						       =	True
							snake_case_						       =	BertGenerationDecoder(config=__lowercase		).to(__lowercase		).eval()

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

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

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

							snake_case_						       =	model(
							    __lowercase			,      attention_mask=__lowercase			,      encoder_hidden_states=__lowercase			,      encoder_attention_mask=__lowercase			,      output_hidden_states=__lowercase			,      )["hidden_states"][0]
							snake_case_						       =	model(
							    __lowercase			,      attention_mask=__lowercase			,      encoder_hidden_states=__lowercase			,      encoder_attention_mask=__lowercase			,      past_key_values=__lowercase			,      output_hidden_states=__lowercase			,      )["hidden_states"][0]

							# select random slice
							snake_case_						       =	ids_tensor((1,)			,      output_from_past.shape[-1]		).item()
							snake_case_						       =	output_from_no_past[:, -3:, random_slice_idx].detach()
							snake_case_						       =	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(__lowercase			,      __lowercase			,      atol=1E-3		)		)






						def 			snake_case__		(		self				:	Any			,      __lowercase				:	Dict			,      __lowercase				:	str			,      __lowercase				:	Optional[Any]			,      __lowercase				:	int			,      *__lowercase				:	List[str]			,      ):






							"""simple docstring"""






							snake_case_						       =	BertGenerationDecoder(__lowercase		)
							model.to(__lowercase		)
							model.eval()
							snake_case_						       =	model(__lowercase			,      attention_mask=__lowercase			,      labels=__lowercase		)
							self.parent.assertEqual(result.logits.shape			,      (self.batch_size, self.seq_length, self.vocab_size)		)






						def 			snake_case__		(		self				:	Optional[Any]		):






							"""simple docstring"""






							snake_case_       , snake_case_       , snake_case_       , snake_case_						       =	self.prepare_config_and_inputs()
							snake_case_						       =	{"input_ids": input_ids, "attention_mask": input_mask}
							return config, inputs_dict







@require_torch
class 						UpperCAmelCase  (							UpperCAmelCase__			,  UpperCAmelCase__			,  UpperCAmelCase__			,  unittest.TestCase     ):







						'''simple docstring'''





						lowerCAmelCase_							=  (BertGenerationEncoder, BertGenerationDecoder) if is_torch_available() else ()
						lowerCAmelCase_							=  (BertGenerationDecoder,) if is_torch_available() else ()
						lowerCAmelCase_							=  (
						    {'''feature-extraction''': BertGenerationEncoder, '''text-generation''': BertGenerationDecoder}
						    if is_torch_available()
						    else {}
						)






						def 			snake_case__		(		self				:	Optional[int]		):






							"""simple docstring"""






							snake_case_						       =	BertGenerationEncoderTester(self		)
							snake_case_						       =	ConfigTester(self			,      config_class=__lowercase			,      hidden_size=37		)






						def 			snake_case__		(		self				:	Any		):






							"""simple docstring"""






							self.config_tester.run_common_tests()






						def 			snake_case__		(		self				:	Optional[Any]		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							self.model_tester.create_and_check_model(*__lowercase		)






						def 			snake_case__		(		self				:	int		):






							"""simple docstring"""






							snake_case_       , snake_case_       , snake_case_       , snake_case_						       =	self.model_tester.prepare_config_and_inputs()
							snake_case_						       =	"bert"
							self.model_tester.create_and_check_model(__lowercase			,      __lowercase			,      __lowercase			,      __lowercase		)






						def 			snake_case__		(		self				:	List[str]		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_model_as_decoder(*__lowercase		)






						def 			snake_case__		(		self				:	Optional[Any]		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_decoder_model_past_large_inputs(*__lowercase		)






						def 			snake_case__		(		self				:	List[str]		):






							"""simple docstring"""






							(
							    (
							    snake_case_
							)       , (
							    snake_case_
							)       , (
							    snake_case_
							)       , (
							    snake_case_
							)       , (
							    snake_case_
							)       , (
							    snake_case_
							)       , 
							)						       =	self.model_tester.prepare_config_and_inputs_for_decoder()

							snake_case_						       =	None

							self.model_tester.create_and_check_model_as_decoder(
							    __lowercase			,      __lowercase			,      __lowercase			,      __lowercase			,      __lowercase			,      __lowercase			,      )






						def 			snake_case__		(		self				:	Optional[int]		):






							"""simple docstring"""






							snake_case_						       =	self.model_tester.prepare_config_and_inputs_for_decoder()
							self.model_tester.create_and_check_for_causal_lm(*__lowercase		)






						@slow
						def 			snake_case__		(		self				:	int		):






							"""simple docstring"""






							snake_case_						       =	BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"		)
							self.assertIsNotNone(__lowercase		)







@require_torch
class 						UpperCAmelCase  (							unittest.TestCase     ):







						'''simple docstring'''





						@slow
						def 			snake_case__		(		self				:	Dict		):






							"""simple docstring"""






							snake_case_						       =	BertGenerationEncoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"		)
							snake_case_						       =	torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]		)
							with torch.no_grad():
								snake_case_						       =	model(__lowercase		)[0]
							snake_case_						       =	torch.Size([1, 8, 10_24]		)
							self.assertEqual(output.shape			,      __lowercase		)
							snake_case_						       =	torch.tensor(
							    [[[0.1775, 0.0083, -0.0321], [1.6002, 0.1287, 0.3912], [2.1473, 0.5791, 0.6066]]]		)
							self.assertTrue(torch.allclose(output[:, :3, :3]			,      __lowercase			,      atol=1E-4		)		)







@require_torch
class 						UpperCAmelCase  (							unittest.TestCase     ):







						'''simple docstring'''





						@slow
						def 			snake_case__		(		self				:	List[str]		):






							"""simple docstring"""






							snake_case_						       =	BertGenerationDecoder.from_pretrained("google/bert_for_seq_generation_L-24_bbc_encoder"		)
							snake_case_						       =	torch.tensor([[1_01, 75_92, 10_10, 20_26, 38_99, 20_03, 1_01_40, 1_02]]		)
							with torch.no_grad():
								snake_case_						       =	model(__lowercase		)[0]
							snake_case_						       =	torch.Size([1, 8, 5_03_58]		)
							self.assertEqual(output.shape			,      __lowercase		)
							snake_case_						       =	torch.tensor(
							    [[[-0.5788, -2.5994, -3.7054], [0.0438, 4.7997, 1.8795], [1.5862, 6.6409, 4.4638]]]		)
							self.assertTrue(torch.allclose(output[:, :3, :3]			,      __lowercase			,      atol=1E-4		)		)






from __future__ import annotations

from dataclasses import dataclass
@dataclass
class 						UpperCAmelCase  :







						'''simple docstring'''





						lowerCAmelCase_							=  42
						lowerCAmelCase_							=  None
						lowerCAmelCase_							=  None







def 		lowerCamelCase__       (						_A				):







	'''simple docstring'''





	def is_valid_tree(_A				) -> bool:
		if node is None:
			return True

		if not isinstance(_A	,      _A				):
			return False

		try:
			float(node.data				)
		except (TypeError, ValueError):
			return False

		return is_valid_tree(node.left				) and is_valid_tree(node.right				)

	if not is_valid_tree(_A				):
		raise ValueError(
		    "Each node should be type of TreeNode and data should be float."				)

	def is_binary_search_tree_recursive_check(
	    _A	,      _A	,      _A				) -> bool:

		if node is None:
			return True

		return (
		    left_bound < node.data < right_bound
		    and is_binary_search_tree_recursive_check(node.left	,      _A	,      node.data				)
		    and is_binary_search_tree_recursive_check(
		        node.right	,      node.data	,      _A				)
		)

	return is_binary_search_tree_recursive_check(_A	,      -float("inf"				)	,      float("inf"				)				)


if __name__ == "__main__":
		import doctest

		doctest.testmod()
