



"""simple docstring"""

import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_torch


if is_torch_available():
     import torch

     from transformers.activations import gelu_new, gelu_python, get_activation




@require_torch
class  _UpperCAmelCase (    unittest.TestCase):

 def 		_snake_case      (  self	: str       ):
     snake_case_				:  Tuple       =			torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]       )
     snake_case_				:  Dict       =			get_activation('''gelu'''       )
     self.assertTrue(torch.allclose(gelu_python(UpperCamelCase_       )						,  torch_builtin(UpperCamelCase_       )       )       )
     self.assertFalse(torch.allclose(gelu_python(UpperCamelCase_       )						,  gelu_new(UpperCamelCase_       )       )       )

 def 		_snake_case      (  self	: str       ):
     snake_case_				:  List[str]       =			torch.tensor([-100, -1, -0.1, 0, 0.1, 1.0, 100]       )
     snake_case_				:  Union[str, Any]       =			get_activation('''gelu'''       )
     snake_case_				:  Any       =			get_activation('''gelu_10'''       )

     snake_case_				:  int       =			torch_builtin(UpperCamelCase_       )
     snake_case_				:  int       =			geluaa(UpperCamelCase_       )

     snake_case_				:  Dict       =			torch.where(y_gelu_aa < 10.0						,  1						,  0       )

     self.assertTrue(torch.max(UpperCamelCase_       ).item() == 10.0       )
     self.assertTrue(torch.allclose(y_gelu * clipped_mask						,  y_gelu_aa * clipped_mask       )       )

 def 		_snake_case      (  self	: str       ):
     get_activation('''gelu'''       )
     get_activation('''gelu_10'''       )
     get_activation('''gelu_fast'''       )
     get_activation('''gelu_new'''       )
     get_activation('''gelu_python'''       )
     get_activation('''gelu_pytorch_tanh'''       )
     get_activation('''linear'''       )
     get_activation('''mish'''       )
     get_activation('''quick_gelu'''       )
     get_activation('''relu'''       )
     get_activation('''sigmoid'''       )
     get_activation('''silu'''       )
     get_activation('''swish'''       )
     get_activation('''tanh'''       )
     with self.assertRaises(UpperCamelCase_       ):
         get_activation('''bogus'''       )
     with self.assertRaises(UpperCamelCase_       ):
         get_activation(UpperCamelCase_       )






 def 		_snake_case      (  self	: List[Any]       ):
     snake_case_				:  List[Any]       =			get_activation('''gelu'''       )
     snake_case_				:  Optional[int]       =			1
     snake_case_				:  Optional[int]       =			get_activation('''gelu'''       )
     self.assertEqual(acta.a						,  1       )
     with self.assertRaises(UpperCamelCase_       ):
         snake_case_				:  int       =			acta.a










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





