



"""simple docstring"""

import unittest

from parameterized import parameterized

from transformers import OpenLlamaConfig, is_torch_available, set_seed
from transformers.testing_utils import require_torch, torch_device

from ...generation.test_utils import GenerationTesterMixin
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 OpenLlamaForCausalLM, OpenLlamaForSequenceClassification, OpenLlamaModel







class  __lowercase  :







						"""simple docstring"""







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







						def     __UpperCamelCase				(self							):
											snake_case_       :		List[str]			       =       ids_tensor([self.batch_size, self.seq_length] ,		self.vocab_size							)

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

											snake_case_       :		List[str]			       =       None
											if self.use_token_type_ids:
																snake_case_       :		Union[str, Any]			       =       ids_tensor([self.batch_size, self.seq_length] ,		self.type_vocab_size							)

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

											snake_case_       :		List[str]			       =       self.get_config()

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







						def     __UpperCamelCase				(self							):
											return OpenLlamaConfig(
											    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 ,		type_vocab_size=self.type_vocab_size ,		is_decoder=snake_case__ ,		initializer_range=self.initializer_range ,		use_stable_embedding=snake_case__ ,		)







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__							):
											snake_case_       :		Dict			       =       OpenLlamaModel(config=snake_case__							)
											model.to(snake_case__							)
											model.eval()
											snake_case_       :		int			       =       model(snake_case__ ,		attention_mask=snake_case__							)
											snake_case_       :		Tuple			       =       model(snake_case__							)
											self.parent.assertEqual(result.last_hidden_state.shape ,		(self.batch_size, self.seq_length, self.hidden_size)							)







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		):
											snake_case_       :		Union[str, Any]			       =       True
											snake_case_       :		Union[str, Any]			       =       OpenLlamaModel(snake_case__							)
											model.to(snake_case__							)
											model.eval()
											snake_case_       :		List[str]			       =       model(
											    snake_case__ ,		attention_mask=snake_case__ ,		encoder_hidden_states=snake_case__ ,		encoder_attention_mask=snake_case__ ,		)
											snake_case_       :		List[str]			       =       model(
											    snake_case__ ,		attention_mask=snake_case__ ,		encoder_hidden_states=snake_case__ ,		)
											snake_case_       :		int			       =       model(snake_case__ ,		attention_mask=snake_case__							)
											self.parent.assertEqual(result.last_hidden_state.shape ,		(self.batch_size, self.seq_length, self.hidden_size)							)







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		):
											snake_case_       :		Optional[Any]			       =       OpenLlamaForCausalLM(config=snake_case__							)
											model.to(snake_case__							)
											model.eval()
											snake_case_       :		List[Any]			       =       model(snake_case__ ,		attention_mask=snake_case__ ,		labels=snake_case__							)
											self.parent.assertEqual(result.logits.shape ,		(self.batch_size, self.seq_length, self.vocab_size)							)







						def     __UpperCamelCase				(self ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		lowercase__ ,		):
											snake_case_       :		Dict			       =       True
											snake_case_       :		Tuple			       =       True
											snake_case_       :		Dict			       =       OpenLlamaForCausalLM(config=snake_case__							)
											model.to(snake_case__							)
											model.eval()

											# first forward pass
											snake_case_       :		Union[str, Any]			       =       model(
											    snake_case__ ,		attention_mask=snake_case__ ,		encoder_hidden_states=snake_case__ ,		encoder_attention_mask=snake_case__ ,		use_cache=snake_case__ ,		)
											snake_case_       :		Tuple			       =       outputs.past_key_values

											# create hypothetical multiple next token and extent to next_input_ids
											snake_case_       :		str			       =       ids_tensor((self.batch_size, 3) ,		config.vocab_size							)
											snake_case_       :		Union[str, Any]			       =       ids_tensor((self.batch_size, 3) ,		vocab_size=2							)

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

											snake_case_       :		Any			       =       model(
											    snake_case__ ,		attention_mask=snake_case__ ,		encoder_hidden_states=snake_case__ ,		encoder_attention_mask=snake_case__ ,		output_hidden_states=snake_case__ ,		)["""hidden_states"""][0]
											snake_case_       :		Tuple			       =       model(
											    snake_case__ ,		attention_mask=snake_case__ ,		encoder_hidden_states=snake_case__ ,		encoder_attention_mask=snake_case__ ,		past_key_values=snake_case__ ,		output_hidden_states=snake_case__ ,		)["""hidden_states"""][0]

											# select random slice
											snake_case_       :		str			       =       ids_tensor((1,) ,		output_from_past.shape[-1]							).item()
											snake_case_       :		Union[str, Any]			       =       output_from_no_past[:, -3:, random_slice_idx].detach()
											snake_case_       :		Tuple			       =       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(snake_case__ ,		snake_case__ ,		atol=1e-3							)							)







						def     __UpperCamelCase				(self							):
											snake_case_       :		Tuple			       =       self.prepare_config_and_inputs()
											(
											    (
											    snake_case_				
											)       ,      (
											    snake_case_				
											)       ,      (
											    snake_case_				
											)       ,      (
											    snake_case_				
											)       ,      (
											    snake_case_				
											)       ,      (
											    snake_case_				
											)       ,      (
											    snake_case_				
											)       ,      				
											)       :		Tuple			       =       config_and_inputs
											snake_case_       :		int			       =       {"""input_ids""": input_ids, """attention_mask""": input_mask}
											return config, inputs_dict







@require_torch
class  __lowercase  (       SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       ,     SCREAMING_SNAKE_CASE__       ,     unittest.TestCase):







						"""simple docstring"""







						_A		:				Optional[Any]											=		(
						    (OpenLlamaModel, OpenLlamaForCausalLM, OpenLlamaForSequenceClassification) if is_torch_available() else ()
						)
						_A		:				Optional[Any]											=		(OpenLlamaForCausalLM,) if is_torch_available() else ()
						_A		:				List[str]											=		(
						    {
						        """feature-extraction""": OpenLlamaModel,
						        """text-classification""": OpenLlamaForSequenceClassification,
						        """text-generation""": OpenLlamaForCausalLM,
						        """zero-shot""": OpenLlamaForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						_A		:				Tuple											=		False
						_A		:				List[Any]											=		False







						def     __UpperCamelCase				(self							):
											snake_case_       :		Optional[Any]			       =       OpenLlamaModelTester(self							)
											snake_case_       :		Optional[int]			       =       ConfigTester(self ,		config_class=snake_case__ ,		hidden_size=37							)







						def     __UpperCamelCase				(self							):
											self.config_tester.run_common_tests()







						def     __UpperCamelCase				(self							):
											snake_case_       :		str			       =       self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*snake_case__							)







						def     __UpperCamelCase				(self							):
											snake_case_       :		Union[str, Any]			       =       self.model_tester.prepare_config_and_inputs()
											for type in ["absolute", "relative_key", "relative_key_query"]:
																snake_case_       :		Union[str, Any]			       =       type
																self.model_tester.create_and_check_model(*snake_case__							)







						def     __UpperCamelCase				(self							):
											snake_case_       ,      snake_case_       :		Optional[int]			       =       self.model_tester.prepare_config_and_inputs_for_common()
											snake_case_       :		int			       =       3
											snake_case_       :		Union[str, Any]			       =       input_dict["""input_ids"""]
											snake_case_       :		Union[str, Any]			       =       input_ids.ne(1							).to(snake_case__							)
											snake_case_       :		Union[str, Any]			       =       ids_tensor([self.model_tester.batch_size] ,		self.model_tester.type_sequence_label_size							)
											snake_case_       :		Dict			       =       OpenLlamaForSequenceClassification(snake_case__							)
											model.to(snake_case__							)
											model.eval()
											snake_case_       :		Union[str, Any]			       =       model(snake_case__ ,		attention_mask=snake_case__ ,		labels=snake_case__							)
											self.assertEqual(result.logits.shape ,		(self.model_tester.batch_size, self.model_tester.num_labels)							)







						def     __UpperCamelCase				(self							):
											snake_case_       ,      snake_case_       :		str			       =       self.model_tester.prepare_config_and_inputs_for_common()
											snake_case_       :		List[str]			       =       3
											snake_case_       :		int			       =       """single_label_classification"""
											snake_case_       :		int			       =       input_dict["""input_ids"""]
											snake_case_       :		Optional[Any]			       =       input_ids.ne(1							).to(snake_case__							)
											snake_case_       :		Any			       =       ids_tensor([self.model_tester.batch_size] ,		self.model_tester.type_sequence_label_size							)
											snake_case_       :		List[str]			       =       OpenLlamaForSequenceClassification(snake_case__							)
											model.to(snake_case__							)
											model.eval()
											snake_case_       :		int			       =       model(snake_case__ ,		attention_mask=snake_case__ ,		labels=snake_case__							)
											self.assertEqual(result.logits.shape ,		(self.model_tester.batch_size, self.model_tester.num_labels)							)







						def     __UpperCamelCase				(self							):
											snake_case_       ,      snake_case_       :		Any			       =       self.model_tester.prepare_config_and_inputs_for_common()
											snake_case_       :		List[Any]			       =       3
											snake_case_       :		Optional[Any]			       =       """multi_label_classification"""
											snake_case_       :		Optional[Any]			       =       input_dict["""input_ids"""]
											snake_case_       :		List[Any]			       =       input_ids.ne(1							).to(snake_case__							)
											snake_case_       :		Tuple			       =       ids_tensor(
											    [self.model_tester.batch_size, config.num_labels] ,		self.model_tester.type_sequence_label_size							).to(torch.float							)
											snake_case_       :		List[Any]			       =       OpenLlamaForSequenceClassification(snake_case__							)
											model.to(snake_case__							)
											model.eval()
											snake_case_       :		List[str]			       =       model(snake_case__ ,		attention_mask=snake_case__ ,		labels=snake_case__							)
											self.assertEqual(result.logits.shape ,		(self.model_tester.batch_size, self.model_tester.num_labels)							)







						@unittest.skip("""Open-Llama buffers include complex numbers, which breaks this test"""							)
						def     __UpperCamelCase				(self							):
											pass







						@parameterized.expand([("""linear""",), ("""dynamic""",)]							)
						def     __UpperCamelCase				(self ,		lowercase__							):
											snake_case_       ,      snake_case_       :		List[str]			       =       self.model_tester.prepare_config_and_inputs_for_common()
											snake_case_       :		Tuple			       =       ids_tensor([1, 10] ,		config.vocab_size							)
											snake_case_       :		Optional[int]			       =       ids_tensor([1, int(config.max_position_embeddings * 1.5							)] ,		config.vocab_size							)

											set_seed(42							)  # Fixed seed at init time so the two models get the same random weights
											snake_case_       :		str			       =       OpenLlamaModel(snake_case__							)
											original_model.to(snake_case__							)
											original_model.eval()
											snake_case_       :		Union[str, Any]			       =       original_model(snake_case__							).last_hidden_state
											snake_case_       :		Dict			       =       original_model(snake_case__							).last_hidden_state

											set_seed(42							)  # Fixed seed at init time so the two models get the same random weights
											snake_case_       :		Union[str, Any]			       =       {"""type""": scaling_type, """factor""": 10.0}
											snake_case_       :		List[Any]			       =       OpenLlamaModel(snake_case__							)
											scaled_model.to(snake_case__							)
											scaled_model.eval()
											snake_case_       :		Union[str, Any]			       =       scaled_model(snake_case__							).last_hidden_state
											snake_case_       :		Any			       =       scaled_model(snake_case__							).last_hidden_state

											# Dynamic scaling does not change the RoPE embeddings until it receives an input longer than the original
											# maximum sequence length, so the outputs for the short input should match.
											if scaling_type == "dynamic":
																self.assertTrue(torch.allclose(snake_case__ ,		snake_case__ ,		atol=1e-5							)							)
											else:
																self.assertFalse(torch.allclose(snake_case__ ,		snake_case__ ,		atol=1e-5							)							)

											# The output should be different for long inputs
											self.assertFalse(torch.allclose(snake_case__ ,		snake_case__ ,		atol=1e-5							)							)










from dataclasses import dataclass
from typing import Tuple

import numpy as np
import torch



@dataclass
class    lowercase_		:
						lowerCAmelCase__			      =42  # [batch_size x 3]
						lowerCAmelCase__			      =42  # [batch_size x 3]
						lowerCAmelCase__			      =42  # [batch_size x 3]
						lowerCAmelCase__			      =42  # [batch_size x 3]
						lowerCAmelCase__			      =42
						lowerCAmelCase__			      =42
						lowerCAmelCase__			      =42
						lowerCAmelCase__			      =42
						lowerCAmelCase__			      =42



						def 	__a					(			self	:   Optional[Any]  ):

								"""simple docstring"""

								assert self.x.shape[0] == self.y.shape[0] == self.z.shape[0] == self.origin.shape[0]
								assert self.x.shape[1] == self.y.shape[1] == self.z.shape[1] == self.origin.shape[1] == 3
								assert len(self.x.shape  ) == len(self.y.shape  ) == len(self.z.shape  ) == len(self.origin.shape  ) == 2



						def 	__a					(			self	:   Union[str, Any]  ):

								"""simple docstring"""

								return torch.from_numpy(np.array([self.width, self.height]					,							dtype=np.floataa  )  )



						def 	__a					(			self	:   Optional[int]  ):

								"""simple docstring"""

								return torch.from_numpy(np.array([self.x_fov, self.y_fov]					,							dtype=np.floataa  )  )



						def 	__a					(			self	:   int  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_					      =							torch.arange(self.height * self.width  )
								SCREAMING_SNAKE_CASE_					      =							torch.stack(
								    [
								        pixel_indices % self.width,
								        torch.div(snake_case__					,							self.width					,							rounding_mode='trunc'  ),
								    ]					,							axis=1					,							)
								return coords



						@property
						def 	__a					(			self	:   List[Any]  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_      ,				*SCREAMING_SNAKE_CASE_					      =							self.shape
								SCREAMING_SNAKE_CASE_					      =							int(np.prod(snake_case__  )  )

								SCREAMING_SNAKE_CASE_					      =							self.get_image_coords()
								SCREAMING_SNAKE_CASE_					      =							torch.broadcast_to(coords.unsqueeze(0  )					,							[batch_size * inner_batch_size, *coords.shape]  )
								SCREAMING_SNAKE_CASE_					      =							self.get_camera_rays(snake_case__  )

								SCREAMING_SNAKE_CASE_					      =							rays.view(snake_case__					,							inner_batch_size * self.height * self.width					,							2					,							3  )

								return rays



						def 	__a					(			self	:   Optional[Any]					,							snake_case__	:   torch.Tensor  ):

								"""simple docstring"""

								SCREAMING_SNAKE_CASE_      ,				*SCREAMING_SNAKE_CASE_      ,				SCREAMING_SNAKE_CASE_					      =							coords.shape
								assert n_coords == 2
								assert batch_size == self.origin.shape[0]

								SCREAMING_SNAKE_CASE_					      =							coords.view(snake_case__					,							-1					,							2  )

								SCREAMING_SNAKE_CASE_					      =							self.resolution()
								SCREAMING_SNAKE_CASE_					      =							self.fov()

								SCREAMING_SNAKE_CASE_					      =							(flat.float() / (res - 1)) * 2 - 1
								SCREAMING_SNAKE_CASE_					      =							fracs * torch.tan(fov / 2  )

								SCREAMING_SNAKE_CASE_					      =							fracs.view(snake_case__					,							-1					,							2  )
								SCREAMING_SNAKE_CASE_					      =							(
								    self.z.view(snake_case__					,							1					,							3  )
								    + self.x.view(snake_case__					,							1					,							3  ) * fracs[:, :, :1]
								    + self.y.view(snake_case__					,							1					,							3  ) * fracs[:, :, 1:]
								)
								SCREAMING_SNAKE_CASE_					      =							directions / directions.norm(dim=-1					,							keepdim=snake_case__  )
								SCREAMING_SNAKE_CASE_					      =							torch.stack(
								    [
								        torch.broadcast_to(self.origin.view(snake_case__					,							1					,							3  )					,							[batch_size, directions.shape[1], 3]  ),
								        directions,
								    ]					,							dim=2					,							)
								return rays.view(snake_case__					,							*snake_case__					,							2					,							3  )






						def 	__a					(			self	:   Optional[int]					,							snake_case__	:   int					,							snake_case__	:   int  ):

								"""simple docstring"""

								assert width * self.height == height * self.width, "The aspect ratio should not change."
								return DifferentiableProjectiveCamera(
								    origin=self.origin					,							x=self.x					,							y=self.y					,							z=self.z					,							width=snake_case__					,							height=snake_case__					,							x_fov=self.x_fov					,							y_fov=self.y_fov					,							)


def 					_a							(					lowerCAmelCase )->	DifferentiableProjectiveCamera:
		SCREAMING_SNAKE_CASE_					      =							[]
		SCREAMING_SNAKE_CASE_					      =							[]
		SCREAMING_SNAKE_CASE_					      =							[]
		SCREAMING_SNAKE_CASE_					      =							[]
		for theta in np.linspace(0    ,						2 * np.pi    ,						num=20 ):
				SCREAMING_SNAKE_CASE_					      =							np.array([np.sin(lowerCAmelCase ), np.cos(lowerCAmelCase ), -0.5] )
				z /= np.sqrt(np.sum(z**2 ) )
				SCREAMING_SNAKE_CASE_					      =							-z * 4
				SCREAMING_SNAKE_CASE_					      =							np.array([np.cos(lowerCAmelCase ), -np.sin(lowerCAmelCase ), 0.0] )
				SCREAMING_SNAKE_CASE_					      =							np.cross(lowerCAmelCase    ,						lowerCAmelCase )
				origins.append(lowerCAmelCase )
				xs.append(lowerCAmelCase )
				ys.append(lowerCAmelCase )
				zs.append(lowerCAmelCase )
		return DifferentiableProjectiveCamera(
		    origin=torch.from_numpy(np.stack(lowerCAmelCase    ,						axis=0 ) ).float()    ,						x=torch.from_numpy(np.stack(lowerCAmelCase    ,						axis=0 ) ).float()    ,						y=torch.from_numpy(np.stack(lowerCAmelCase    ,						axis=0 ) ).float()    ,						z=torch.from_numpy(np.stack(lowerCAmelCase    ,						axis=0 ) ).float()    ,						width=lowerCAmelCase    ,						height=lowerCAmelCase    ,						x_fov=0.7    ,						y_fov=0.7    ,						shape=(1, len(lowerCAmelCase ))    ,						)