



"""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 		UpperCAmelCase__      (      unittest.TestCase       ):




			'''simple docstring'''






			@slow
			@unittest.skip('''Model is not available.'''			)
			def 			snake_case__					(       self							:			Optional[Any]			):
					'''simple docstring'''





					__UpperCAmelCase   :					int													=     '''nvidia/megatron-gpt2-345m/'''
					if "MYDIR" in os.environ:
							__UpperCAmelCase   :					List[str]													=     os.path.join(os.environ['''MYDIR''']	,	a_			)
					__UpperCAmelCase   :					List[str]													=     GPTaLMHeadModel.from_pretrained(a_			)
					model.to(a_			)
					model.half()

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

					with torch.no_grad():
							__UpperCAmelCase   :					Tuple													=     model(a_			).logits

					__UpperCAmelCase   :					Optional[int]													=     torch.Size((1, 9, 5_02_57)			)
					self.assertEqual(output.shape	,	a_			)

					__UpperCAmelCase   :					str													=     torch.tensor(
					    [
					        4.9_4_1_4,
					        -0.2_9_2_0,
					        -1.2_1_4_8,
					        -4.0_2_7_3,
					        -0.5_1_6_1,
					        -5.2_1_0_9,
					        -1.2_4_1_2,
					        -1.8_3_0_1,
					        -1.7_7_3_4,
					        -4.7_1_4_8,
					        -0.2_3_1_7,
					        -1.0_8_1_1,
					        -2.1_7_7_7,
					        0.4_1_4_1,
					        -3.7_9_6_9,
					        -4.0_5_8_6,
					        -2.5_3_3_2,
					        -3.3_8_0_9,
					        4.3_8_6_7,
					    ]	,	device=a_	,	dtype=torch.half	,	)

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










import unittest

from transformers import (
    MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
    PreTrainedTokenizer,
    is_vision_available,
)
from transformers.pipelines import ImageClassificationPipeline, pipeline
from transformers.testing_utils import (
    is_pipeline_test,
    nested_simplify,
    require_tf,
    require_torch,
    require_torch_or_tf,
    require_vision,
    slow,
)

from .test_pipelines_common import ANY


if is_vision_available():
				from PIL import Image
else:

				class 		UpperCAmelCase__      :




							'''simple docstring'''






							@staticmethod
							def 			snake_case__					(       *a_							:			Union[str, Any]	,	**a_							:			Any			):
									'''simple docstring'''





									pass

@is_pipeline_test
@require_torch_or_tf
@require_vision
class 		UpperCAmelCase__      (      unittest.TestCase       ):




			'''simple docstring'''






			UpperCamelCase      						=	MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING
			UpperCamelCase      						=	TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING




			def 			snake_case__					(       self							:			Any	,	a_							:			str	,	a_							:			Dict	,	a_							:			Dict			):
					'''simple docstring'''





					__UpperCAmelCase   :					Any													=     ImageClassificationPipeline(model=a_	,	image_processor=a_	,	top_k=2			)
					__UpperCAmelCase   :					int													=     [
					    Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			),
					    '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					]
					return image_classifier, examples




			def 			snake_case__					(       self							:			str	,	a_							:			Optional[int]	,	a_							:			List[str]			):
					'''simple docstring'''





					__UpperCAmelCase   :					Tuple													=     image_classifier('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			)

					self.assertEqual(
					    a_	,	[
					        {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					    ]	,	)

					import datasets

					__UpperCAmelCase   :					Optional[Any]													=     datasets.load_dataset('''hf-internal-testing/fixtures_image_utils'''	,	'''image'''	,	split='''test'''			)

					# Accepts URL + PIL.Image + lists
					__UpperCAmelCase   :					Any													=     image_classifier(
					    [
					        Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''			),
					        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					        # RGBA
					        dataset[0]['''file'''],
					        # LA
					        dataset[1]['''file'''],
					        # L
					        dataset[2]['''file'''],
					    ]			)
					self.assertEqual(
					    a_	,	[
					        [
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        ],
					        [
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        ],
					        [
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        ],
					        [
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        ],
					        [
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					            {'''score''': ANY(a_			), '''label''': ANY(a_			)},
					        ],
					    ]	,	)




			@require_torch
			def 			snake_case__					(       self							:			int			):
					'''simple docstring'''





					__UpperCAmelCase   :					Dict													=     '''hf-internal-testing/tiny-random-vit'''
					__UpperCAmelCase   :					Optional[int]													=     pipeline('''image-classification'''	,	model=a_			)

					__UpperCAmelCase   :					Optional[int]													=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''			)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}]	,	)

					__UpperCAmelCase   :					Dict													=     image_classifier(
					    [
					        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					    ]	,	top_k=2	,	)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[
					        [{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}],
					        [{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}],
					    ]	,	)




			@require_tf
			def 			snake_case__					(       self							:			Optional[int]			):
					'''simple docstring'''





					__UpperCAmelCase   :					Tuple													=     '''hf-internal-testing/tiny-random-vit'''
					__UpperCAmelCase   :					Optional[Any]													=     pipeline('''image-classification'''	,	model=a_	,	framework='''tf'''			)

					__UpperCAmelCase   :					Optional[Any]													=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''			)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}]	,	)

					__UpperCAmelCase   :					Any													=     image_classifier(
					    [
					        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					        '''http://images.cocodataset.org/val2017/000000039769.jpg''',
					    ]	,	top_k=2	,	)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[
					        [{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}],
					        [{'''label''': '''LABEL_1''', '''score''': 0.5_7_4}, {'''label''': '''LABEL_0''', '''score''': 0.4_2_6}],
					    ]	,	)




			def 			snake_case__					(       self							:			Optional[int]			):
					'''simple docstring'''





					__UpperCAmelCase   :					Union[str, Any]													=     PreTrainedTokenizer()

					# Assert that the pipeline can be initialized with a feature extractor that is not in any mapping
					__UpperCAmelCase   :					Dict													=     pipeline(
					    '''image-classification'''	,	model='''hf-internal-testing/tiny-random-vit'''	,	tokenizer=a_			)

					self.assertIs(image_classifier.tokenizer	,	a_			)




			@slow
			@require_torch
			def 			snake_case__					(       self							:			Optional[int]			):
					'''simple docstring'''





					__UpperCAmelCase   :					List[str]													=     pipeline('''image-classification'''	,	model='''deepmind/vision-perceiver-conv'''			)
					__UpperCAmelCase   :					List[Any]													=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''			)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[
					        {'''score''': 0.4_3_8_5, '''label''': '''tabby, tabby cat'''},
					        {'''score''': 0.3_2_1, '''label''': '''tiger cat'''},
					        {'''score''': 0.0_5_0_2, '''label''': '''Egyptian cat'''},
					        {'''score''': 0.0_1_3_7, '''label''': '''crib, cot'''},
					        {'''score''': 0.0_0_7, '''label''': '''radiator'''},
					    ]	,	)

					__UpperCAmelCase   :					List[str]													=     pipeline('''image-classification'''	,	model='''deepmind/vision-perceiver-fourier'''			)
					__UpperCAmelCase   :					List[Any]													=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''			)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[
					        {'''score''': 0.5_6_5_8, '''label''': '''tabby, tabby cat'''},
					        {'''score''': 0.1_3_0_9, '''label''': '''tiger cat'''},
					        {'''score''': 0.0_7_2_2, '''label''': '''Egyptian cat'''},
					        {'''score''': 0.0_7_0_7, '''label''': '''remote control, remote'''},
					        {'''score''': 0.0_0_8_2, '''label''': '''computer keyboard, keypad'''},
					    ]	,	)

					__UpperCAmelCase   :					List[str]													=     pipeline('''image-classification'''	,	model='''deepmind/vision-perceiver-learned'''			)
					__UpperCAmelCase   :					Union[str, Any]													=     image_classifier('''http://images.cocodataset.org/val2017/000000039769.jpg'''			)
					self.assertEqual(
					    nested_simplify(a_	,	decimals=4			)	,	[
					        {'''score''': 0.3_0_2_2, '''label''': '''tabby, tabby cat'''},
					        {'''score''': 0.2_3_6_2, '''label''': '''Egyptian cat'''},
					        {'''score''': 0.1_8_5_6, '''label''': '''tiger cat'''},
					        {'''score''': 0.0_3_2_4, '''label''': '''remote control, remote'''},
					        {'''score''': 0.0_0_9_6, '''label''': '''quilt, comforter, comfort, puff'''},
					    ]	,	)





