



import unittest

import numpy as np
from datasets import load_dataset

from transformers.testing_utils import require_torch, require_vision
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 SegformerImageProcessor


class    _lowercase (unittest.TestCase   ):
				'''simple docstring'''







				def __init__(			self  ,			snake_case__  ,			snake_case__=7  ,			snake_case__=3  ,			snake_case__=30  ,			snake_case__=400  ,			snake_case__=True  ,			snake_case__=None  ,			snake_case__=True  ,			snake_case__=[0.5, 0.5, 0.5]  ,			snake_case__=[0.5, 0.5, 0.5]  ,			snake_case__=False  ,			):

									'''simple docstring'''


									UpperCamelCase_			= size if size is not None else {"height": 30, "width": 30}
									UpperCamelCase_			= parent
									UpperCamelCase_			= batch_size
									UpperCamelCase_			= num_channels
									UpperCamelCase_			= min_resolution
									UpperCamelCase_			= max_resolution
									UpperCamelCase_			= do_resize
									UpperCamelCase_			= size
									UpperCamelCase_			= do_normalize
									UpperCamelCase_			= image_mean
									UpperCamelCase_			= image_std
									UpperCamelCase_			= do_reduce_labels

				def    _lowerCamelCase       (			self     ):

									'''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_reduce_labels": self.do_reduce_labels,
									}




def  _lowerCAmelCase     ():
					UpperCamelCase_			= load_dataset("hf-internal-testing/fixtures_ade20k" ,						split="test")

					UpperCamelCase_			= Image.open(dataset[0]["file"])
					UpperCamelCase_			= Image.open(dataset[1]["file"])

					return image, map



def  _lowerCAmelCase     ():
					UpperCamelCase_			= load_dataset("hf-internal-testing/fixtures_ade20k" ,						split="test")

					UpperCamelCase_			= Image.open(dataset[0]["file"])
					UpperCamelCase_			= Image.open(dataset[1]["file"])
					UpperCamelCase_			= Image.open(dataset[2]["file"])
					UpperCamelCase_			= Image.open(dataset[3]["file"])

					return [imagea, imagea], [mapa, mapa]






@require_torch
@require_vision
class    _lowercase (a_   , unittest.TestCase   ):
				'''simple docstring'''







				lowercase__     =    SegformerImageProcessor if is_vision_available() else None

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									UpperCamelCase_			= SegformerImageProcessingTester(self     )

				@property
				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									return self.image_processor_tester.prepare_image_processor_dict()

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									UpperCamelCase_			= self.image_processing_class(**self.image_processor_dict     )
									self.assertTrue(hasattr(snake_case__  ,			"do_resize"     )     )
									self.assertTrue(hasattr(snake_case__  ,			"size"     )     )
									self.assertTrue(hasattr(snake_case__  ,			"do_normalize"     )     )
									self.assertTrue(hasattr(snake_case__  ,			"image_mean"     )     )
									self.assertTrue(hasattr(snake_case__  ,			"image_std"     )     )
									self.assertTrue(hasattr(snake_case__  ,			"do_reduce_labels"     )     )

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									UpperCamelCase_			= self.image_processing_class.from_dict(self.image_processor_dict     )
									self.assertEqual(image_processor.size  ,			{"height": 30, "width": 30}     )
									self.assertEqual(image_processor.do_reduce_labels  ,			snake_case__     )

									UpperCamelCase_			= self.image_processing_class.from_dict(self.image_processor_dict  ,			size=42  ,			reduce_labels=snake_case__     )
									self.assertEqual(image_processor.size  ,			{"height": 42, "width": 42}     )
									self.assertEqual(image_processor.do_reduce_labels  ,			snake_case__     )

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									pass

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


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

									# Test not batched input
									UpperCamelCase_			= image_processing(image_inputs[0]  ,			return_tensors="pt"     ).pixel_values
									self.assertEqual(
									    encoded_images.shape  ,			(
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)

									# Test batched
									UpperCamelCase_			= image_processing(snake_case__  ,			return_tensors="pt"     ).pixel_values
									self.assertEqual(
									    encoded_images.shape  ,			(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


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

									# Test not batched input
									UpperCamelCase_			= image_processing(image_inputs[0]  ,			return_tensors="pt"     ).pixel_values
									self.assertEqual(
									    encoded_images.shape  ,			(
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)

									# Test batched
									UpperCamelCase_			= image_processing(snake_case__  ,			return_tensors="pt"     ).pixel_values
									self.assertEqual(
									    encoded_images.shape  ,			(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


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

									# Test not batched input
									UpperCamelCase_			= image_processing(image_inputs[0]  ,			return_tensors="pt"     ).pixel_values
									self.assertEqual(
									    encoded_images.shape  ,			(
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)

									# Test batched
									UpperCamelCase_			= image_processing(snake_case__  ,			return_tensors="pt"     ).pixel_values
									self.assertEqual(
									    encoded_images.shape  ,			(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									UpperCamelCase_			= self.image_processing_class(**self.image_processor_dict     )
									# create random PyTorch tensors
									UpperCamelCase_			= prepare_image_inputs(self.image_processor_tester  ,			equal_resolution=snake_case__  ,			torchify=snake_case__     )
									UpperCamelCase_			= []
									for image in image_inputs:
														self.assertIsInstance(snake_case__  ,			torch.Tensor     )
														maps.append(torch.zeros(image.shape[-2:]     ).long()     )

									# Test not batched input
									UpperCamelCase_			= image_processing(image_inputs[0]  ,			maps[0]  ,			return_tensors="pt"     )
									self.assertEqual(
									    encoding["pixel_values"].shape  ,			(
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)
									self.assertEqual(
									    encoding["labels"].shape  ,			(
									        1,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)
									self.assertEqual(encoding["labels"].dtype  ,			torch.long     )
									self.assertTrue(encoding["labels"].min().item() >= 0     )
									self.assertTrue(encoding["labels"].max().item() <= 255     )

									# Test batched
									UpperCamelCase_			= image_processing(snake_case__  ,			snake_case__  ,			return_tensors="pt"     )
									self.assertEqual(
									    encoding["pixel_values"].shape  ,			(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)
									self.assertEqual(
									    encoding["labels"].shape  ,			(
									        self.image_processor_tester.batch_size,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)
									self.assertEqual(encoding["labels"].dtype  ,			torch.long     )
									self.assertTrue(encoding["labels"].min().item() >= 0     )
									self.assertTrue(encoding["labels"].max().item() <= 255     )

									# Test not batched input (PIL images)
									UpperCamelCase_	,     UpperCamelCase_			= prepare_semantic_single_inputs()

									UpperCamelCase_			= image_processing(snake_case__  ,			snake_case__  ,			return_tensors="pt"     )
									self.assertEqual(
									    encoding["pixel_values"].shape  ,			(
									        1,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)
									self.assertEqual(
									    encoding["labels"].shape  ,			(
									        1,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)
									self.assertEqual(encoding["labels"].dtype  ,			torch.long     )
									self.assertTrue(encoding["labels"].min().item() >= 0     )
									self.assertTrue(encoding["labels"].max().item() <= 255     )

									# Test batched input (PIL images)
									UpperCamelCase_	,     UpperCamelCase_			= prepare_semantic_batch_inputs()

									UpperCamelCase_			= image_processing(snake_case__  ,			snake_case__  ,			return_tensors="pt"     )
									self.assertEqual(
									    encoding["pixel_values"].shape  ,			(
									        2,
									        self.image_processor_tester.num_channels,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)
									self.assertEqual(
									    encoding["labels"].shape  ,			(
									        2,
									        self.image_processor_tester.size["height"],
									        self.image_processor_tester.size["width"],
									    )  ,			)
									self.assertEqual(encoding["labels"].dtype  ,			torch.long     )
									self.assertTrue(encoding["labels"].min().item() >= 0     )
									self.assertTrue(encoding["labels"].max().item() <= 255     )

				def    _lowerCamelCase       (			self     ):

									'''simple docstring'''


									UpperCamelCase_			= self.image_processing_class(**self.image_processor_dict     )

									# ADE20k has 150 classes, and the background is included, so labels should be between 0 and 150
									UpperCamelCase_	,     UpperCamelCase_			= prepare_semantic_single_inputs()
									UpperCamelCase_			= image_processing(snake_case__  ,			snake_case__  ,			return_tensors="pt"     )
									self.assertTrue(encoding["labels"].min().item() >= 0     )
									self.assertTrue(encoding["labels"].max().item() <= 150     )

									UpperCamelCase_			= True
									UpperCamelCase_			= image_processing(snake_case__  ,			snake_case__  ,			return_tensors="pt"     )
									self.assertTrue(encoding["labels"].min().item() >= 0     )
									self.assertTrue(encoding["labels"].max().item() <= 255     )








"""simple docstring"""


import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
       import torch

       from transformers import GPTaLMHeadModel


@require_torch
@require_sentencepiece
@require_tokenizers
class    snake_case						(				unittest.TestCase		):


   '''simple docstring'''

   @slow
   @unittest.skip('''Model is not available.'''    )
   def 							_SCREAMING_SNAKE_CASE							(      self					:	int    ):

     '''simple docstring'''
     __A		     =					'''nvidia/megatron-gpt2-345m/'''
     if "MYDIR" in os.environ:
       __A		     =					os.path.join(os.environ['''MYDIR'''],			_lowerCamelCase    )
     __A		     =					GPTaLMHeadModel.from_pretrained(_lowerCamelCase    )
     model.to(_lowerCamelCase    )
     model.half()

     __A		     =					torch.tensor(
         [[1_01, 71_10, 10_05, 10_56, 20_23, 1_13_33, 1_74_13, 10_29, 1_02]],			device=_lowerCamelCase,			dtype=torch.long,			)

     with torch.no_grad():
       __A		     =					model(_lowerCamelCase    ).logits

     __A		     =					torch.Size((1, 9, 5_02_57)    )
     self.assertEqual(output.shape,			_lowerCamelCase    )

     __A		     =					torch.tensor(
         [
             4.94_14,
             -0.29_20,
             -1.21_48,
             -4.02_73,
             -0.51_61,
             -5.21_09,
             -1.24_12,
             -1.83_01,
             -1.77_34,
             -4.71_48,
             -0.23_17,
             -1.08_11,
             -2.17_77,
             0.41_41,
             -3.79_69,
             -4.05_86,
             -2.53_32,
             -3.38_09,
             4.38_67,
         ],			device=_lowerCamelCase,			dtype=torch.half,			)

     for i in range(19    ):
       __A     ,       __A		     =					8 * i // 17, 27_92 * i  # along the diagonal
       __A     ,       __A		     =					output[0, r, c], expected_diag[i]
       __A		     =					f'row={r} col={c} computed={computed} expected={expected}'
       self.assertAlmostEqual(_lowerCamelCase,			_lowerCamelCase,			delta=1e-4,			msg=_lowerCamelCase    )




