






"""simple docstring"""


import unittest

import numpy as np
import PIL
import torch

from diffusers.image_processor import VaeImageProcessor


class    snake_case						(				unittest.TestCase		):


   '''simple docstring'''

   @property
   def 							_SCREAMING_SNAKE_CASE							(      self					:	Any    ):

     '''simple docstring'''
     __A		     =					1
     __A		     =					3
     __A		     =					8
     __A		     =					8

     __A		     =					torch.rand((batch_size, num_channels, height, width)    )

     return sample


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Dict,			_lowerCamelCase					:	Union[str, Any]    ):

     '''simple docstring'''
     if isinstance(image[0],			PIL.Image.Image    ):
       return np.stack([np.array(_lowerCamelCase    ) for i in image],			axis=0    )
     elif isinstance(_lowerCamelCase,			torch.Tensor    ):
       return image.cpu().numpy().transpose(0,			2,			3,			1    )
     return image


   def 							_SCREAMING_SNAKE_CASE							(      self					:	List[str]    ):

     '''simple docstring'''
     __A		     =					VaeImageProcessor(do_resize=_lowerCamelCase,			do_normalize=_lowerCamelCase    )

     __A		     =					self.dummy_sample
     __A		     =					self.to_np(_lowerCamelCase    )

     for output_type in ["pt", "np", "pil"]:
       __A		     =					image_processor.postprocess(
           image_processor.preprocess(_lowerCamelCase    ),			output_type=_lowerCamelCase,			)
       __A		     =					self.to_np(_lowerCamelCase    )
       __A		     =					(input_np * 2_55).round() if output_type == '''pil''' else input_np
       assert (
           np.abs(in_np - out_np    ).max() < 1e-6
       ), f'decoded output does not match input for output_type {output_type}'


   def 							_SCREAMING_SNAKE_CASE							(      self					:	Dict    ):

     '''simple docstring'''
     __A		     =					VaeImageProcessor(do_resize=_lowerCamelCase,			do_normalize=_lowerCamelCase    )
     __A		     =					self.dummy_sample.cpu().numpy().transpose(0,			2,			3,			1    )

     for output_type in ["pt", "np", "pil"]:
       __A		     =					image_processor.postprocess(image_processor.preprocess(_lowerCamelCase    ),			output_type=_lowerCamelCase    )

       __A		     =					self.to_np(_lowerCamelCase    )
       __A		     =					(input_np * 2_55).round() if output_type == '''pil''' else input_np
       assert (
           np.abs(in_np - out_np    ).max() < 1e-6
       ), f'decoded output does not match input for output_type {output_type}'


   def 							_SCREAMING_SNAKE_CASE							(      self					:	List[Any]    ):

     '''simple docstring'''
     __A		     =					VaeImageProcessor(do_resize=_lowerCamelCase,			do_normalize=_lowerCamelCase    )

     __A		     =					self.dummy_sample.cpu().numpy().transpose(0,			2,			3,			1    )
     __A		     =					image_processor.numpy_to_pil(_lowerCamelCase    )

     for output_type in ["pt", "np", "pil"]:
       __A		     =					image_processor.postprocess(image_processor.preprocess(_lowerCamelCase    ),			output_type=_lowerCamelCase    )
       for i, o in zip(_lowerCamelCase,			_lowerCamelCase    ):
         __A		     =					np.array(_lowerCamelCase    )
         __A		     =					self.to_np(_lowerCamelCase    ) if output_type == '''pil''' else (self.to_np(_lowerCamelCase    ) * 2_55).round()
         assert (
             np.abs(in_np - out_np    ).max() < 1e-6
         ), f'decoded output does not match input for output_type {output_type}'


   def 							_SCREAMING_SNAKE_CASE							(      self					:	List[Any]    ):

     '''simple docstring'''
     __A		     =					VaeImageProcessor(do_resize=_lowerCamelCase,			do_normalize=_lowerCamelCase    )

     __A		     =					self.dummy_sample
     __A		     =					input_pt_ad.squeeze(0    )

     __A		     =					image_processor.postprocess(
         image_processor.preprocess(_lowerCamelCase    ),			output_type='''np''',			)
     __A		     =					image_processor.postprocess(
         image_processor.preprocess(_lowerCamelCase    ),			output_type='''np''',			)

     __A		     =					self.to_np(self.dummy_sample    )
     __A		     =					input_np_ad.squeeze(0    )

     __A		     =					image_processor.postprocess(
         image_processor.preprocess(_lowerCamelCase    ),			output_type='''np''',			)
     __A		     =					image_processor.postprocess(
         image_processor.preprocess(_lowerCamelCase    ),			output_type='''np''',			)

     assert np.abs(out_pt_ad - out_pt_ad    ).max() < 1e-6
     assert np.abs(out_np_ad - out_np_ad    ).max() < 1e-6






   def 							_SCREAMING_SNAKE_CASE							(      self					:	Tuple    ):

     '''simple docstring'''
     __A		     =					VaeImageProcessor(do_resize=_lowerCamelCase,			do_normalize=_lowerCamelCase    )

     __A		     =					self.dummy_sample
     __A		     =					list(_lowerCamelCase    )

     __A		     =					image_processor.postprocess(
         image_processor.preprocess(_lowerCamelCase    ),			output_type='''np''',			)

     __A		     =					image_processor.postprocess(
         image_processor.preprocess(_lowerCamelCase    ),			output_type='''np''',			)

     __A		     =					self.to_np(self.dummy_sample    )
     list(_lowerCamelCase    )

     __A		     =					image_processor.postprocess(
         image_processor.preprocess(_lowerCamelCase    ),			output_type='''np''',			)

     __A		     =					image_processor.postprocess(
         image_processor.preprocess(_lowerCamelCase    ),			output_type='''np''',			)

     assert np.abs(out_pt_ad - out_pt_list    ).max() < 1e-6
     assert np.abs(out_np_ad - out_np_list    ).max() < 1e-6












"""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    )




