
'''simple docstring'''






import unittest

import numpy as np

from transformers.file_utils import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision

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 DPTImageProcessor

class  UpperCAmelCase__						(       unittest.TestCase ):



    def __init__(	self      :       Optional[Any],__A      :       List[str],__A      :       List[str]=7,__A      :       Union[str, Any]=3,__A      :       Union[str, Any]=1_8,__A      :       int=3_0,__A      :       Dict=4_0_0,__A      :       Union[str, Any]=True,__A      :       List[Any]=None,__A      :       Any=True,__A      :       Any=[0.5, 0.5, 0.5],__A      :       str=[0.5, 0.5, 0.5],):
          _lowerCamelCase				:   Any								=   size if size is not None else {"height": 1_8, "width": 1_8}
          _lowerCamelCase				:   List[Any]								=   parent
          _lowerCamelCase				:   int								=   batch_size
          _lowerCamelCase				:   Any								=   num_channels
          _lowerCamelCase				:   List[Any]								=   image_size
          _lowerCamelCase				:   int								=   min_resolution
          _lowerCamelCase				:   str								=   max_resolution
          _lowerCamelCase				:   Dict								=   do_resize
          _lowerCamelCase				:   str								=   size
          _lowerCamelCase				:   Optional[int]								=   do_normalize
          _lowerCamelCase				:   Dict								=   image_mean
          _lowerCamelCase				:   int								=   image_std



    def   lowerCamelCase_  (	self      :       List[Any]     ):
          return {
              "image_mean": self.image_mean,
              "image_std": self.image_std,
              "do_normalize": self.do_normalize,
              "do_resize": self.do_resize,
              "size": self.size,
          }


@require_torch
@require_vision
class  UpperCAmelCase__						(       A						,	unittest.TestCase ):
    lowerCAmelCase_				    =		DPTImageProcessor if is_vision_available() else None



    def   lowerCamelCase_  (	self      :       Tuple     ):
          _lowerCamelCase				:   List[Any]								=   DPTImageProcessingTester(self     )



    @property
    def   lowerCamelCase_  (	self      :       List[Any]     ):
          return self.image_processor_tester.prepare_image_processor_dict()



    def   lowerCamelCase_  (	self      :       Tuple     ):
          _lowerCamelCase				:   Tuple								=   self.image_processing_class(**self.image_processor_dict     )
          self.assertTrue(hasattr(__A,"image_mean"     )     )
          self.assertTrue(hasattr(__A,"image_std"     )     )
          self.assertTrue(hasattr(__A,"do_normalize"     )     )
          self.assertTrue(hasattr(__A,"do_resize"     )     )
          self.assertTrue(hasattr(__A,"size"     )     )



    def   lowerCamelCase_  (	self      :       Tuple     ):
          _lowerCamelCase				:   Union[str, Any]								=   self.image_processing_class.from_dict(self.image_processor_dict     )
          self.assertEqual(image_processor.size,{"height": 1_8, "width": 1_8}     )

          _lowerCamelCase				:   Tuple								=   self.image_processing_class.from_dict(self.image_processor_dict,size=4_2     )
          self.assertEqual(image_processor.size,{"height": 4_2, "width": 4_2}     )



    def   lowerCamelCase_  (	self      :       List[Any]     ):
          # Initialize image_processing
          _lowerCamelCase				:   str								=   self.image_processing_class(**self.image_processor_dict     )
          # create random PIL images
          _lowerCamelCase				:   Optional[int]								=   prepare_image_inputs(self.image_processor_tester,equal_resolution=__A     )
          for image in image_inputs:
                self.assertIsInstance(__A,Image.Image     )

          # Test not batched input
          _lowerCamelCase				:   str								=   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
          _lowerCamelCase				:   Optional[int]								=   image_processing(__A,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      :       Dict     ):
          # Initialize image_processing
          _lowerCamelCase				:   List[Any]								=   self.image_processing_class(**self.image_processor_dict     )
          # create random numpy tensors
          _lowerCamelCase				:   int								=   prepare_image_inputs(self.image_processor_tester,equal_resolution=__A,numpify=__A     )
          for image in image_inputs:
                self.assertIsInstance(__A,np.ndarray     )

          # Test not batched input
          _lowerCamelCase				:   str								=   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
          _lowerCamelCase				:   str								=   image_processing(__A,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      :       Union[str, Any]     ):
          # Initialize image_processing
          _lowerCamelCase				:   List[str]								=   self.image_processing_class(**self.image_processor_dict     )
          # create random PyTorch tensors
          _lowerCamelCase				:   int								=   prepare_image_inputs(self.image_processor_tester,equal_resolution=__A,torchify=__A     )
          for image in image_inputs:
                self.assertIsInstance(__A,torch.Tensor     )

          # Test not batched input
          _lowerCamelCase				:   Optional[int]								=   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
          _lowerCamelCase				:   int								=   image_processing(__A,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"],
              ),)

'''simple docstring'''






import unittest

import numpy as np

from transformers import RobertaConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
 from transformers.models.roberta.modeling_flax_roberta import (
     FlaxRobertaForCausalLM,
     FlaxRobertaForMaskedLM,
     FlaxRobertaForMultipleChoice,
     FlaxRobertaForQuestionAnswering,
     FlaxRobertaForSequenceClassification,
     FlaxRobertaForTokenClassification,
     FlaxRobertaModel,
 )

class  UpperCAmelCase__						(       unittest.TestCase ):



    def __init__(	self      :       Union[str, Any],__A      :       Dict,__A      :       List[str]=1_3,__A      :       Any=7,__A      :       str=True,__A      :       Optional[int]=True,__A      :       Optional[Any]=True,__A      :       Any=True,__A      :       List[str]=9_9,__A      :       str=3_2,__A      :       List[str]=5,__A      :       Optional[Any]=4,__A      :       Any=3_7,__A      :       Optional[Any]="gelu",__A      :       List[Any]=0.1,__A      :       Any=0.1,__A      :       Dict=5_1_2,__A      :       Tuple=1_6,__A      :       Tuple=2,__A      :       List[Any]=0.02,__A      :       Any=4,):
          _lowerCamelCase				:   List[Any]								=   parent
          _lowerCamelCase				:   Optional[int]								=   batch_size
          _lowerCamelCase				:   Tuple								=   seq_length
          _lowerCamelCase				:   Tuple								=   is_training
          _lowerCamelCase				:   Union[str, Any]								=   use_attention_mask
          _lowerCamelCase				:   Optional[Any]								=   use_token_type_ids
          _lowerCamelCase				:   List[Any]								=   use_labels
          _lowerCamelCase				:   str								=   vocab_size
          _lowerCamelCase				:   List[str]								=   hidden_size
          _lowerCamelCase				:   Union[str, Any]								=   num_hidden_layers
          _lowerCamelCase				:   Tuple								=   num_attention_heads
          _lowerCamelCase				:   Union[str, Any]								=   intermediate_size
          _lowerCamelCase				:   Optional[Any]								=   hidden_act
          _lowerCamelCase				:   Tuple								=   hidden_dropout_prob
          _lowerCamelCase				:   Optional[Any]								=   attention_probs_dropout_prob
          _lowerCamelCase				:   List[Any]								=   max_position_embeddings
          _lowerCamelCase				:   Union[str, Any]								=   type_vocab_size
          _lowerCamelCase				:   Union[str, Any]								=   type_sequence_label_size
          _lowerCamelCase				:   str								=   initializer_range
          _lowerCamelCase				:   List[Any]								=   num_choices



    def   lowerCamelCase_  (	self      :       int     ):
          _lowerCamelCase				:   Optional[int]								=   ids_tensor([self.batch_size, self.seq_length],self.vocab_size     )

          _lowerCamelCase				:   Dict								=   None
          if self.use_attention_mask:
                _lowerCamelCase				:   Optional[int]								=   random_attention_mask([self.batch_size, self.seq_length]     )

          _lowerCamelCase				:   List[str]								=   None
          if self.use_token_type_ids:
                _lowerCamelCase				:   Tuple								=   ids_tensor([self.batch_size, self.seq_length],self.type_vocab_size     )

          _lowerCamelCase				:   Optional[int]								=   RobertaConfig(
              vocab_size=self.vocab_size,hidden_size=self.hidden_size,num_hidden_layers=self.num_hidden_layers,num_attention_heads=self.num_attention_heads,intermediate_size=self.intermediate_size,hidden_act=self.hidden_act,hidden_dropout_prob=self.hidden_dropout_prob,attention_probs_dropout_prob=self.attention_probs_dropout_prob,max_position_embeddings=self.max_position_embeddings,type_vocab_size=self.type_vocab_size,is_decoder=__A,initializer_range=self.initializer_range,)

          return config, input_ids, token_type_ids, attention_mask



    def   lowerCamelCase_  (	self      :       Any     ):
          _lowerCamelCase				:   List[str]								=   self.prepare_config_and_inputs()
          _lowerCamelCase						,		_lowerCamelCase						,		_lowerCamelCase						,		_lowerCamelCase				:   str								=   config_and_inputs
          _lowerCamelCase				:   List[Any]								=   {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": attention_mask}
          return config, inputs_dict



    def   lowerCamelCase_  (	self      :       List[str]     ):
          _lowerCamelCase				:   Any								=   self.prepare_config_and_inputs()
          _lowerCamelCase						,		_lowerCamelCase						,		_lowerCamelCase						,		_lowerCamelCase				:   Dict								=   config_and_inputs

          _lowerCamelCase				:   int								=   True
          _lowerCamelCase				:   List[Any]								=   floats_tensor([self.batch_size, self.seq_length, self.hidden_size]     )
          _lowerCamelCase				:   Optional[int]								=   ids_tensor([self.batch_size, self.seq_length],vocab_size=2     )

          return (
              config,
              input_ids,
              token_type_ids,
              encoder_hidden_states,
              encoder_attention_mask,
          )


@require_flax
class  UpperCAmelCase__						(       A						,	unittest.TestCase ):
    lowerCAmelCase_				    =		True

    lowerCAmelCase_				    =		(
        (
            FlaxRobertaModel,
            FlaxRobertaForCausalLM,
            FlaxRobertaForMaskedLM,
            FlaxRobertaForSequenceClassification,
            FlaxRobertaForTokenClassification,
            FlaxRobertaForMultipleChoice,
            FlaxRobertaForQuestionAnswering,
        )
        if is_flax_available()
        else ()
    )



    def   lowerCamelCase_  (	self      :       Optional[int]     ):
          _lowerCamelCase				:   List[str]								=   FlaxRobertaModelTester(self     )



    @slow
    def   lowerCamelCase_  (	self      :       Any     ):
          for model_class_name in self.all_model_classes:
                _lowerCamelCase				:   Union[str, Any]								=   model_class_name.from_pretrained("roberta-base",from_pt=__A     )
                _lowerCamelCase				:   Union[str, Any]								=   model(np.ones((1, 1)     )     )
                self.assertIsNotNone(__A     )