





'''simple docstring'''



import json
import pathlib
import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision, slow
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
	import torch

if is_vision_available():
	from PIL import Image

	from transformers import YolosImageProcessor






class 							__lowercase    ( unittest.TestCase  ):







	'''simple docstring'''

	def __init__(self       ,_lowerCamelCase       ,_lowerCamelCase=7       ,_lowerCamelCase=3       ,_lowerCamelCase=30       ,_lowerCamelCase=400       ,_lowerCamelCase=True       ,_lowerCamelCase=None       ,_lowerCamelCase=True       ,_lowerCamelCase=[0.5, 0.5, 0.5]       ,_lowerCamelCase=[0.5, 0.5, 0.5]       ,_lowerCamelCase=True       ,_lowerCamelCase=1 / 255       ,_lowerCamelCase=True       ,)							->							Dict:







				'''simple docstring'''





				__lowercase			  =							size if size is not None else {'''shortest_edge''': 18, '''longest_edge''': 1333}
				__lowercase			  =							parent
				__lowercase			  =							batch_size
				__lowercase			  =							num_channels
				__lowercase			  =							min_resolution
				__lowercase			  =							max_resolution
				__lowercase			  =							do_resize
				__lowercase			  =							size
				__lowercase			  =							do_normalize
				__lowercase			  =							image_mean
				__lowercase			  =							image_std
				__lowercase			  =							do_rescale
				__lowercase			  =							rescale_factor
				__lowercase			  =							do_pad


	def   _UpperCAmelCase       (self	)							->							List[Any]:







				'''simple docstring'''





				return {
				    "do_resize": self.do_resize,
				    "size": self.size,
				    "do_normalize": self.do_normalize,
				    "image_mean": self.image_mean,
				    "image_std": self.image_std,
				    "do_rescale": self.do_rescale,
				    "rescale_factor": self.rescale_factor,
				    "do_pad": self.do_pad,
				}


	def   _UpperCAmelCase       (self       ,_lowerCamelCase       ,_lowerCamelCase=False	)							->							List[Any]:







				'''simple docstring'''





				if not batched:
							__lowercase			  =							image_inputs[0]
							if isinstance(_lowerCamelCase       ,Image.Image	):
										__lowercase  ,						__lowercase			  =							image.size
							else:
										__lowercase  ,						__lowercase			  =							image.shape[1], image.shape[2]
							if w < h:
										__lowercase			  =							int(self.size['''shortest_edge'''] * h / w	)
										__lowercase			  =							self.size['''shortest_edge''']
							elif w > h:
										__lowercase			  =							self.size['''shortest_edge''']
										__lowercase			  =							int(self.size['''shortest_edge'''] * w / h	)
							else:
										__lowercase			  =							self.size['''shortest_edge''']
										__lowercase			  =							self.size['''shortest_edge''']

				else:
							__lowercase			  =							[]
							for image in image_inputs:
										__lowercase  ,						__lowercase			  =							self.get_expected_values([image]	)
										expected_values.append((expected_height, expected_width)	)
							__lowercase			  =							max(_lowerCamelCase       ,key=lambda _lowerCamelCase	: item[0]	)[0]
							__lowercase			  =							max(_lowerCamelCase       ,key=lambda _lowerCamelCase	: item[1]	)[1]

				return expected_height, expected_width






@require_torch
@require_vision
class 							__lowercase    ( lowerCAmelCase__	, unittest.TestCase  ):







	'''simple docstring'''

	a      :			List[str]     =    YolosImageProcessor if is_vision_available() else None


	def   _UpperCAmelCase       (self	)							->							Any:







				'''simple docstring'''





				__lowercase			  =							YolosImageProcessingTester(self	)


	@property
	def   _UpperCAmelCase       (self	)							->							Optional[int]:







				'''simple docstring'''





				return self.image_processor_tester.prepare_image_processor_dict()


	def   _UpperCAmelCase       (self	)							->							List[str]:







				'''simple docstring'''





				__lowercase			  =							self.image_processing_class(**self.image_processor_dict	)
				self.assertTrue(hasattr(_lowerCamelCase       ,'''image_mean'''	)	)
				self.assertTrue(hasattr(_lowerCamelCase       ,'''image_std'''	)	)
				self.assertTrue(hasattr(_lowerCamelCase       ,'''do_normalize'''	)	)
				self.assertTrue(hasattr(_lowerCamelCase       ,'''do_resize'''	)	)
				self.assertTrue(hasattr(_lowerCamelCase       ,'''size'''	)	)


	def   _UpperCAmelCase       (self	)							->							Any:







				'''simple docstring'''





				__lowercase			  =							self.image_processing_class.from_dict(self.image_processor_dict	)
				self.assertEqual(image_processor.size       ,{'''shortest_edge''': 18, '''longest_edge''': 1333}	)
				self.assertEqual(image_processor.do_pad       ,_lowerCamelCase	)

				__lowercase			  =							self.image_processing_class.from_dict(
				    self.image_processor_dict       ,size=42       ,max_size=84       ,pad_and_return_pixel_mask=_lowerCamelCase	)
				self.assertEqual(image_processor.size       ,{'''shortest_edge''': 42, '''longest_edge''': 84}	)
				self.assertEqual(image_processor.do_pad       ,_lowerCamelCase	)


	def   _UpperCAmelCase       (self	)							->							int:







				'''simple docstring'''





				pass


	def   _UpperCAmelCase       (self	)							->							Any:







				'''simple docstring'''





				__lowercase			  =							self.image_processing_class(**self.image_processor_dict	)
				# create random PIL images
				__lowercase			  =							prepare_image_inputs(self.image_processor_tester       ,equal_resolution=_lowerCamelCase	)
				for image in image_inputs:
							self.assertIsInstance(_lowerCamelCase       ,Image.Image	)

				# Test not batched input
				__lowercase			  =							image_processing(image_inputs[0]       ,return_tensors='''pt'''	).pixel_values

				__lowercase  ,						__lowercase			  =							self.image_processor_tester.get_expected_values(_lowerCamelCase	)

				self.assertEqual(
				    encoded_images.shape       ,(1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,)

				# Test batched
				__lowercase  ,						__lowercase			  =							self.image_processor_tester.get_expected_values(_lowerCamelCase       ,batched=_lowerCamelCase	)

				__lowercase			  =							image_processing(_lowerCamelCase       ,return_tensors='''pt'''	).pixel_values
				self.assertEqual(
				    encoded_images.shape       ,(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        expected_height,
				        expected_width,
				    )       ,)


	def   _UpperCAmelCase       (self	)							->							str:







				'''simple docstring'''





				__lowercase			  =							self.image_processing_class(**self.image_processor_dict	)
				# create random numpy tensors
				__lowercase			  =							prepare_image_inputs(self.image_processor_tester       ,equal_resolution=_lowerCamelCase       ,numpify=_lowerCamelCase	)
				for image in image_inputs:
							self.assertIsInstance(_lowerCamelCase       ,np.ndarray	)

				# Test not batched input
				__lowercase			  =							image_processing(image_inputs[0]       ,return_tensors='''pt'''	).pixel_values

				__lowercase  ,						__lowercase			  =							self.image_processor_tester.get_expected_values(_lowerCamelCase	)

				self.assertEqual(
				    encoded_images.shape       ,(1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,)

				# Test batched
				__lowercase			  =							image_processing(_lowerCamelCase       ,return_tensors='''pt'''	).pixel_values

				__lowercase  ,						__lowercase			  =							self.image_processor_tester.get_expected_values(_lowerCamelCase       ,batched=_lowerCamelCase	)

				self.assertEqual(
				    encoded_images.shape       ,(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        expected_height,
				        expected_width,
				    )       ,)


	def   _UpperCAmelCase       (self	)							->							Tuple:







				'''simple docstring'''





				__lowercase			  =							self.image_processing_class(**self.image_processor_dict	)
				# create random PyTorch tensors
				__lowercase			  =							prepare_image_inputs(self.image_processor_tester       ,equal_resolution=_lowerCamelCase       ,torchify=_lowerCamelCase	)
				for image in image_inputs:
							self.assertIsInstance(_lowerCamelCase       ,torch.Tensor	)

				# Test not batched input
				__lowercase			  =							image_processing(image_inputs[0]       ,return_tensors='''pt'''	).pixel_values

				__lowercase  ,						__lowercase			  =							self.image_processor_tester.get_expected_values(_lowerCamelCase	)

				self.assertEqual(
				    encoded_images.shape       ,(1, self.image_processor_tester.num_channels, expected_height, expected_width)       ,)

				# Test batched
				__lowercase			  =							image_processing(_lowerCamelCase       ,return_tensors='''pt'''	).pixel_values

				__lowercase  ,						__lowercase			  =							self.image_processor_tester.get_expected_values(_lowerCamelCase       ,batched=_lowerCamelCase	)

				self.assertEqual(
				    encoded_images.shape       ,(
				        self.image_processor_tester.batch_size,
				        self.image_processor_tester.num_channels,
				        expected_height,
				        expected_width,
				    )       ,)


	def   _UpperCAmelCase       (self	)							->							Any:







				'''simple docstring'''





				__lowercase			  =							self.image_processing_class(**self.image_processor_dict	)
				__lowercase			  =							self.image_processing_class(do_resize=_lowerCamelCase       ,do_normalize=_lowerCamelCase       ,do_rescale=_lowerCamelCase	)
				# create random PyTorch tensors
				__lowercase			  =							prepare_image_inputs(self.image_processor_tester       ,equal_resolution=_lowerCamelCase       ,torchify=_lowerCamelCase	)
				for image in image_inputs:
							self.assertIsInstance(_lowerCamelCase       ,torch.Tensor	)

				# Test whether the method "pad" and calling the image processor return the same tensors
				__lowercase			  =							image_processing_a.pad(_lowerCamelCase       ,return_tensors='''pt'''	)
				__lowercase			  =							image_processing_a(_lowerCamelCase       ,return_tensors='''pt'''	)

				self.assertTrue(
				    torch.allclose(encoded_images_with_method['''pixel_values''']       ,encoded_images['''pixel_values''']       ,atol=1E-4	)	)


	@slow
	def   _UpperCAmelCase       (self	)							->							int:







				'''simple docstring'''





				__lowercase			  =							Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
				with open('''./tests/fixtures/tests_samples/COCO/coco_annotations.txt'''       ,'''r'''	) as f:
							__lowercase			  =							json.loads(f.read()	)

				__lowercase			  =							{'''image_id''': 39769, '''annotations''': target}

				# encode them
				__lowercase			  =							YolosImageProcessor.from_pretrained('''hustvl/yolos-small'''	)
				__lowercase			  =							image_processing(images=_lowerCamelCase       ,annotations=_lowerCamelCase       ,return_tensors='''pt'''	)

				# verify pixel values
				__lowercase			  =							torch.Size([1, 3, 800, 1066]	)
				self.assertEqual(encoding['''pixel_values'''].shape       ,_lowerCamelCase	)

				__lowercase			  =							torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1]	)
				self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]       ,_lowerCamelCase       ,atol=1E-4	)	)

				# verify area
				__lowercase			  =							torch.tensor([5_8_8_7.9_6_0_0, 1_1_2_5_0.2_0_6_1, 4_8_9_3_5_3.8_4_3_8, 8_3_7_1_2_2.7_5_0_0, 1_4_7_9_6_7.5_1_5_6, 1_6_5_7_3_2.3_4_3_8]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']       ,_lowerCamelCase	)	)
				# verify boxes
				__lowercase			  =							torch.Size([6, 4]	)
				self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape       ,_lowerCamelCase	)
				__lowercase			  =							torch.tensor([0.5_5_0_3, 0.2_7_6_5, 0.0_6_0_4, 0.2_2_1_5]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]       ,_lowerCamelCase       ,atol=1E-3	)	)
				# verify image_id
				__lowercase			  =							torch.tensor([39769]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']       ,_lowerCamelCase	)	)
				# verify is_crowd
				__lowercase			  =							torch.tensor([0, 0, 0, 0, 0, 0]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']       ,_lowerCamelCase	)	)
				# verify class_labels
				__lowercase			  =							torch.tensor([75, 75, 63, 65, 17, 17]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']       ,_lowerCamelCase	)	)
				# verify orig_size
				__lowercase			  =							torch.tensor([480, 640]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']       ,_lowerCamelCase	)	)
				# verify size
				__lowercase			  =							torch.tensor([800, 1066]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']       ,_lowerCamelCase	)	)


	@slow
	def   _UpperCAmelCase       (self	)							->							Any:







				'''simple docstring'''





				__lowercase			  =							Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''	)
				with open('''./tests/fixtures/tests_samples/COCO/coco_panoptic_annotations.txt'''       ,'''r'''	) as f:
							__lowercase			  =							json.loads(f.read()	)

				__lowercase			  =							{'''file_name''': '''000000039769.png''', '''image_id''': 39769, '''segments_info''': target}

				__lowercase			  =							pathlib.Path('''./tests/fixtures/tests_samples/COCO/coco_panoptic'''	)

				# encode them
				__lowercase			  =							YolosImageProcessor(format='''coco_panoptic'''	)
				__lowercase			  =							image_processing(images=_lowerCamelCase       ,annotations=_lowerCamelCase       ,masks_path=_lowerCamelCase       ,return_tensors='''pt'''	)

				# verify pixel values
				__lowercase			  =							torch.Size([1, 3, 800, 1066]	)
				self.assertEqual(encoding['''pixel_values'''].shape       ,_lowerCamelCase	)

				__lowercase			  =							torch.tensor([0.2_7_9_6, 0.3_1_3_8, 0.3_4_8_1]	)
				self.assertTrue(torch.allclose(encoding['''pixel_values'''][0, 0, 0, :3]       ,_lowerCamelCase       ,atol=1E-4	)	)

				# verify area
				__lowercase			  =							torch.tensor([1_4_7_9_7_9.6_8_7_5, 1_6_5_5_2_7.0_4_6_9, 4_8_4_6_3_8.5_9_3_8, 1_1_2_9_2.9_3_7_5, 5_8_7_9.6_5_6_2, 7_6_3_4.1_1_4_7]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''area''']       ,_lowerCamelCase	)	)
				# verify boxes
				__lowercase			  =							torch.Size([6, 4]	)
				self.assertEqual(encoding['''labels'''][0]['''boxes'''].shape       ,_lowerCamelCase	)
				__lowercase			  =							torch.tensor([0.2_6_2_5, 0.5_4_3_7, 0.4_6_8_8, 0.8_6_2_5]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''boxes'''][0]       ,_lowerCamelCase       ,atol=1E-3	)	)
				# verify image_id
				__lowercase			  =							torch.tensor([39769]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''image_id''']       ,_lowerCamelCase	)	)
				# verify is_crowd
				__lowercase			  =							torch.tensor([0, 0, 0, 0, 0, 0]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''iscrowd''']       ,_lowerCamelCase	)	)
				# verify class_labels
				__lowercase			  =							torch.tensor([17, 17, 63, 75, 75, 93]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''class_labels''']       ,_lowerCamelCase	)	)
				# verify masks
				__lowercase			  =							822873
				self.assertEqual(encoding['''labels'''][0]['''masks'''].sum().item()       ,_lowerCamelCase	)
				# verify orig_size
				__lowercase			  =							torch.tensor([480, 640]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''orig_size''']       ,_lowerCamelCase	)	)
				# verify size
				__lowercase			  =							torch.tensor([800, 1066]	)
				self.assertTrue(torch.allclose(encoding['''labels'''][0]['''size''']       ,_lowerCamelCase	)	)












'''simple docstring'''



from ...configuration_utils import PretrainedConfig
from ...utils import logging


_SCREAMING_SNAKE_CASE   =      logging.get_logger(__name__)

_SCREAMING_SNAKE_CASE   =      {
    '''s-JoL/Open-Llama-V1''': '''https://huggingface.co/s-JoL/Open-Llama-V1/blob/main/config.json''',
}






class 							__lowercase    ( lowerCAmelCase__  ):







	'''simple docstring'''

	a      :			Dict     =    "open-llama"


	def __init__(self       ,_lowerCamelCase=100000       ,_lowerCamelCase=4096       ,_lowerCamelCase=11008       ,_lowerCamelCase=32       ,_lowerCamelCase=32       ,_lowerCamelCase="silu"       ,_lowerCamelCase=2048       ,_lowerCamelCase=0.0_2       ,_lowerCamelCase=1E-6       ,_lowerCamelCase=True       ,_lowerCamelCase=0       ,_lowerCamelCase=1       ,_lowerCamelCase=2       ,_lowerCamelCase=False       ,_lowerCamelCase=True       ,_lowerCamelCase=0.1       ,_lowerCamelCase=0.1       ,_lowerCamelCase=True       ,_lowerCamelCase=True       ,_lowerCamelCase=None       ,**_lowerCamelCase       ,)							->							Union[str, Any]:







				'''simple docstring'''





				__lowercase			  =							vocab_size
				__lowercase			  =							max_position_embeddings
				__lowercase			  =							hidden_size
				__lowercase			  =							intermediate_size
				__lowercase			  =							num_hidden_layers
				__lowercase			  =							num_attention_heads
				__lowercase			  =							hidden_act
				__lowercase			  =							initializer_range
				__lowercase			  =							rms_norm_eps
				__lowercase			  =							use_cache
				__lowercase			  =							kwargs.pop(
				    '''use_memorry_efficient_attention'''       ,_lowerCamelCase	)
				__lowercase			  =							hidden_dropout_prob
				__lowercase			  =							attention_dropout_prob
				__lowercase			  =							use_stable_embedding
				__lowercase			  =							shared_input_output_embedding
				__lowercase			  =							rope_scaling
				self._rope_scaling_validation()

				super().__init__(
				    pad_token_id=_lowerCamelCase       ,bos_token_id=_lowerCamelCase       ,eos_token_id=_lowerCamelCase       ,tie_word_embeddings=_lowerCamelCase       ,**_lowerCamelCase       ,)


	def   _UpperCAmelCase       (self	)							->							List[Any]:







				'''simple docstring'''





				if self.rope_scaling is None:
							return

				if not isinstance(self.rope_scaling       ,_lowerCamelCase	) or len(self.rope_scaling	) != 2:
							raise ValueError(
							    '''`rope_scaling` must be a dictionary with with two fields, `name` and `factor`, '''
							    f"got {self.rope_scaling}"	)
				__lowercase			  =							self.rope_scaling.get('''type'''       ,_lowerCamelCase	)
				__lowercase			  =							self.rope_scaling.get('''factor'''       ,_lowerCamelCase	)
				if rope_scaling_type is None or rope_scaling_type not in ["linear", "dynamic"]:
							raise ValueError(
							    f"`rope_scaling`'s name field must be one of ['linear', 'dynamic'], got {rope_scaling_type}"	)
				if rope_scaling_factor is None or not isinstance(_lowerCamelCase       ,_lowerCamelCase	) or rope_scaling_factor <= 1.0:
							raise ValueError(f"`rope_scaling`'s factor field must be an float > 1, got {rope_scaling_factor}"	)





