



"""simple docstring"""

import unittest

import numpy as np
import torch

from diffusers import KarrasVePipeline, KarrasVeScheduler, UNetaDModel
from diffusers.utils.testing_utils import enable_full_determinism, require_torch, slow, torch_device


enable_full_determinism()




class  _UpperCAmelCase (    unittest.TestCase):

 @property
 def 		_snake_case      (  self	: str       ):
     torch.manual_seed(0       )
     snake_case_				:  List[str]       =			UNetaDModel(
         block_out_channels=(32, 64)						,  layers_per_block=2						,  sample_size=32						,  in_channels=3						,  out_channels=3						,  down_block_types=('''DownBlock2D''', '''AttnDownBlock2D''')						,  up_block_types=('''AttnUpBlock2D''', '''UpBlock2D''')						,  )
     return model






 def 		_snake_case      (  self	: Any       ):
     snake_case_				:  List[Any]       =			self.dummy_uncond_unet
     snake_case_				:  Dict       =			KarrasVeScheduler()

     snake_case_				:  List[Any]       =			KarrasVePipeline(unet=lowercase_						,  scheduler=lowercase_       )
     pipe.to(lowercase_       )
     pipe.set_progress_bar_config(disable=lowercase_       )

     snake_case_				:  List[Any]       =			torch.manual_seed(0       )
     snake_case_				:  Tuple       =			pipe(num_inference_steps=2						,  generator=lowercase_						,  output_type='''numpy'''       ).images

     snake_case_				:  List[str]       =			torch.manual_seed(0       )
     snake_case_				:  List[Any]       =			pipe(num_inference_steps=2						,  generator=lowercase_						,  output_type='''numpy'''						,  return_dict=lowercase_       )[0]

     snake_case_				:  Union[str, Any]       =			image[0, -3:, -3:, -1]
     snake_case_				:  List[str]       =			image_from_tuple[0, -3:, -3:, -1]

     assert image.shape == (1, 32, 32, 3)
     snake_case_				:  Any       =			np.array([0.0, 1.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0]       )

     assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2
     assert np.abs(image_from_tuple_slice.flatten() - expected_slice       ).max() < 1E-2





@slow
@require_torch
class  _UpperCAmelCase (    unittest.TestCase):

 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  Any       =			'''google/ncsnpp-celebahq-256'''
     snake_case_				:  Optional[int]       =			UNetaDModel.from_pretrained(lowercase_       )
     snake_case_				:  str       =			KarrasVeScheduler()

     snake_case_				:  Optional[int]       =			KarrasVePipeline(unet=lowercase_						,  scheduler=lowercase_       )
     pipe.to(lowercase_       )
     pipe.set_progress_bar_config(disable=lowercase_       )

     snake_case_				:  Tuple       =			torch.manual_seed(0       )
     snake_case_				:  List[Any]       =			pipe(num_inference_steps=20						,  generator=lowercase_						,  output_type='''numpy'''       ).images

     snake_case_				:  Optional[Any]       =			image[0, -3:, -3:, -1]
     assert image.shape == (1, 256, 256, 3)
     snake_case_				:  Union[str, Any]       =			np.array([0.5_78, 0.58_11, 0.59_24, 0.58_09, 0.5_87, 0.58_86, 0.58_61, 0.58_02, 0.5_86]       )

     assert np.abs(image_slice.flatten() - expected_slice       ).max() < 1E-2










"""simple docstring"""

import unittest

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


if is_torch_available():
     import torch

     from transformers import AutoModelForImageClassification

if is_vision_available():
     from transformers import AutoImageProcessor




@require_torch
@require_vision
class  _UpperCAmelCase (    unittest.TestCase):

 @slow
 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  List[Any]       =			AutoImageProcessor.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''       )
     snake_case_				:  List[Any]       =			AutoModelForImageClassification.from_pretrained('''microsoft/dit-base-finetuned-rvlcdip'''       )
     model.to(lowercase_       )

     from datasets import load_dataset

     snake_case_				:  str       =			load_dataset('''nielsr/rvlcdip-demo'''       )

     snake_case_				:  Tuple       =			dataset['''train'''][0]['''image'''].convert('''RGB'''       )

     snake_case_				:  Union[str, Any]       =			image_processor(lowercase_						,  return_tensors='''pt'''       ).to(lowercase_       )

     # forward pass
     with torch.no_grad():
         snake_case_				:  List[Any]       =			model(**lowercase_       )
         snake_case_				:  int       =			outputs.logits

     snake_case_				:  Any       =			torch.Size((1, 16)       )
     self.assertEqual(logits.shape						,  lowercase_       )

     snake_case_				:  Any       =			torch.tensor(
         [-0.41_58, -0.40_92, -0.43_47]						,  device=lowercase_						,  dtype=torch.float						,  )
     self.assertTrue(torch.allclose(logits[0, :3]						,  lowercase_						,  atol=1E-4       )       )





