




"""simple docstring"""


from abc import ABC, abstractmethod
from typing import List, Optional






class       lowercase(  __a				):







 '''simple docstring'''





 def __init__(       self:	Dict	):




   '''simple docstring'''



   self.test()


 def 				UpperCamelCase_     (       self:	Optional[Any]	):




   '''simple docstring'''



   _snake_case							:	Tuple			=						0
   _snake_case							:	List[Any]			=						False
   while not completed:
     if counter == 1:
       self.reset()
     _snake_case							:	List[str]			=						self.advance()
     if not self.does_advance(a_	):
       raise Exception(
           """Custom Constraint is not defined correctly. self.does_advance(self.advance()) must be true."""	)

     _snake_case							:	Union[str, Any]			=						self.update(a_	)
     counter += 1

     if counter > 10_000:
       raise Exception("""update() does not fulfill the constraint."""	)

   if self.remaining() != 0:
     raise Exception("""Custom Constraint is not defined correctly."""	)


 @abstractmethod
 def 				UpperCamelCase_     (       self:	Optional[int]	):




   '''simple docstring'''



   raise NotImplementedError(
       f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."	)


 @abstractmethod
 def 				UpperCamelCase_     (       self:	Optional[int],   a_:	int	):




   '''simple docstring'''



   raise NotImplementedError(
       f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."	)


 @abstractmethod
 def 				UpperCamelCase_     (       self:	Optional[int],   a_:	int	):




   '''simple docstring'''



   raise NotImplementedError(
       f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."	)


 @abstractmethod
 def 				UpperCamelCase_     (       self:	Tuple	):




   '''simple docstring'''



   raise NotImplementedError(
       f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."	)


 @abstractmethod
 def 				UpperCamelCase_     (       self:	int	):




   '''simple docstring'''



   raise NotImplementedError(
       f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."	)


 @abstractmethod
 def 				UpperCamelCase_     (       self:	List[str],   a_:	Tuple=False	):




   '''simple docstring'''



   raise NotImplementedError(
       f"{self.__class__} is an abstract class. Only classes inheriting this class can be called."	)







class       lowercase(  __a				):







 '''simple docstring'''





 def __init__(       self:	Tuple,   a_:	List[int]	):




   '''simple docstring'''



   super(a_,   self	).__init__()

   if not isinstance(a_,   a_	) or len(a_	) == 0:
     raise ValueError(f"`token_ids` has to be a non-empty list, but is {token_ids}."	)
   if any((not isinstance(a_,   a_	) or token_id < 0) for token_id in token_ids	):
     raise ValueError(f"Each list in `token_ids` has to be a list of positive integers, but is {token_ids}."	)

   _snake_case							:	Optional[int]			=						token_ids

   _snake_case							:	Dict			=						len(self.token_ids	)
   _snake_case							:	Union[str, Any]			=						-1  # the index of the currently fulfilled step
   _snake_case							:	str			=						False


 def 				UpperCamelCase_     (       self:	Optional[int]	):




   '''simple docstring'''



   if self.completed:
     return None
   return self.token_ids[self.fulfilled_idx + 1]


 def 				UpperCamelCase_     (       self:	List[Any],   a_:	int	):




   '''simple docstring'''



   if not isinstance(a_,   a_	):
     raise ValueError(f"`token_id` has to be an `int`, but is {token_id} of type {type(a_	)}"	)

   if self.completed:
     return False

   return token_id == self.token_ids[self.fulfilled_idx + 1]


 def 				UpperCamelCase_     (       self:	List[str],   a_:	int	):




   '''simple docstring'''



   if not isinstance(a_,   a_	):
     raise ValueError(f"`token_id` has to be an `int`, but is {token_id} of type {type(a_	)}"	)

   _snake_case							:	Optional[int]			=						False
   _snake_case							:	List[str]			=						False
   _snake_case							:	List[Any]			=						False

   if self.does_advance(a_	):
     self.fulfilled_idx += 1
     _snake_case							:	List[str]			=						True
     if self.fulfilled_idx == (self.seqlen - 1):
       _snake_case							:	str			=						True
     _snake_case							:	Optional[int]			=						completed
   else:
     # failed to make progress.
     _snake_case							:	Union[str, Any]			=						True
     self.reset()
   return stepped, completed, reset


 def 				UpperCamelCase_     (       self:	Union[str, Any]	):




   '''simple docstring'''



   _snake_case							:	int			=						False
   _snake_case							:	Dict			=						0


 def 				UpperCamelCase_     (       self:	Tuple	):




   '''simple docstring'''



   return self.seqlen - (self.fulfilled_idx + 1)


 def 				UpperCamelCase_     (       self:	Union[str, Any],   a_:	str=False	):




   '''simple docstring'''



   _snake_case							:	Dict			=						PhrasalConstraint(self.token_ids	)

   if stateful:
     _snake_case							:	List[Any]			=						self.seqlen
     _snake_case							:	str			=						self.fulfilled_idx
     _snake_case							:	Union[str, Any]			=						self.completed

   return new_constraint







class       lowercase:







 '''simple docstring'''





 def __init__(       self:	int,   a_:	List[List[int]],   a_:	int=True	):




   '''simple docstring'''



   _snake_case							:	List[str]			=						max([len(a_	) for one in nested_token_ids]	)

   _snake_case							:	int			=						{}
   for token_ids in nested_token_ids:
     _snake_case							:	List[str]			=						root
     for tidx, token_id in enumerate(a_	):
       if token_id not in level:
         _snake_case							:	int			=						{}

       _snake_case							:	List[Any]			=						level[token_id]

   if no_subsets and self.has_subsets(a_,   a_	):
     raise ValueError(
         """Each list in `nested_token_ids` can't be a complete subset of another list, but is"""
         f" {nested_token_ids}."	)

   _snake_case							:	int			=						root


 def 				UpperCamelCase_     (       self:	Tuple,   a_:	Tuple	):




   '''simple docstring'''



   _snake_case							:	Union[str, Any]			=						self.trie

   for current_token in current_seq:
     _snake_case							:	Any			=						start[current_token]

   _snake_case							:	Any			=						list(start.keys()	)

   return next_tokens


 def 				UpperCamelCase_     (       self:	List[Any],   a_:	Optional[int]	):




   '''simple docstring'''



   _snake_case							:	Any			=						self.next_tokens(a_	)

   return len(a_	) == 0


 def 				UpperCamelCase_     (       self:	Dict,   a_:	List[Any]	):




   '''simple docstring'''



   _snake_case							:	int			=						list(root.values()	)
   if len(a_	) == 0:
     return 1
   else:
     return sum([self.count_leaves(a_	) for nn in next_nodes]	)


 def 				UpperCamelCase_     (       self:	str,   a_:	List[Any],   a_:	int	):




   '''simple docstring'''



   _snake_case							:	str			=						self.count_leaves(a_	)
   return len(a_	) != leaf_count







class       lowercase(  __a				):







 '''simple docstring'''





 def __init__(       self:	Tuple,   a_:	List[List[int]]	):




   '''simple docstring'''



   super(a_,   self	).__init__()

   if not isinstance(a_,   a_	) or len(a_	) == 0:
     raise ValueError(f"`nested_token_ids` has to be a non-empty list, but is {nested_token_ids}."	)
   if any(not isinstance(a_,   a_	) for token_ids in nested_token_ids	):
     raise ValueError(f"`nested_token_ids` has to be a list of lists, but is {nested_token_ids}."	)
   if any(
       any((not isinstance(a_,   a_	) or token_id < 0) for token_id in token_ids	)
       for token_ids in nested_token_ids	):
     raise ValueError(
         f"Each list in `nested_token_ids` has to be a list of positive integers, but is {nested_token_ids}."	)

   _snake_case							:	Optional[Any]			=						DisjunctiveTrie(a_	)
   _snake_case							:	Optional[int]			=						nested_token_ids

   _snake_case							:	Tuple			=						self.trie.max_height
   _snake_case							:	List[str]			=						[]
   _snake_case							:	Union[str, Any]			=						False


 def 				UpperCamelCase_     (       self:	Any	):




   '''simple docstring'''



   _snake_case							:	List[Any]			=						self.trie.next_tokens(self.current_seq	)

   if len(a_	) == 0:
     return None
   else:
     return token_list


 def 				UpperCamelCase_     (       self:	Tuple,   a_:	int	):




   '''simple docstring'''



   if not isinstance(a_,   a_	):
     raise ValueError(f"`token_id` is supposed to be type `int`, but is {token_id} of type {type(a_	)}"	)

   _snake_case							:	Optional[Any]			=						self.trie.next_tokens(self.current_seq	)

   return token_id in next_tokens


 def 				UpperCamelCase_     (       self:	Any,   a_:	int	):




   '''simple docstring'''



   if not isinstance(a_,   a_	):
     raise ValueError(f"`token_id` is supposed to be type `int`, but is {token_id} of type {type(a_	)}"	)

   _snake_case							:	Any			=						False
   _snake_case							:	Union[str, Any]			=						False
   _snake_case							:	Tuple			=						False

   if self.does_advance(a_	):
     self.current_seq.append(a_	)
     _snake_case							:	Optional[Any]			=						True
   else:
     _snake_case							:	Tuple			=						True
     self.reset()

   _snake_case							:	Any			=						self.trie.reached_leaf(self.current_seq	)
   _snake_case							:	Tuple			=						completed

   return stepped, completed, reset


 def 				UpperCamelCase_     (       self:	str	):




   '''simple docstring'''



   _snake_case							:	Any			=						False
   _snake_case							:	Tuple			=						[]


 def 				UpperCamelCase_     (       self:	str	):




   '''simple docstring'''



   if self.completed:
     # since this can be completed without reaching max height
     return 0
   else:
     return self.seqlen - len(self.current_seq	)


 def 				UpperCamelCase_     (       self:	Dict,   a_:	List[Any]=False	):




   '''simple docstring'''



   _snake_case							:	Union[str, Any]			=						DisjunctiveConstraint(self.token_ids	)

   if stateful:
     _snake_case							:	Tuple			=						self.seqlen
     _snake_case							:	List[str]			=						self.current_seq
     _snake_case							:	List[Any]			=						self.completed

   return new_constraint







class       lowercase:







 '''simple docstring'''





 def __init__(       self:	Any,   a_:	List[Constraint]	):




   '''simple docstring'''



   _snake_case							:	Any			=						constraints

   # max # of steps required to fulfill a given constraint
   _snake_case							:	Optional[int]			=						max([c.seqlen for c in constraints]	)
   _snake_case							:	Optional[Any]			=						len(a_	)
   _snake_case							:	List[Any]			=						False

   self.init_state()


 def 				UpperCamelCase_     (       self:	Optional[int]	):




   '''simple docstring'''



   _snake_case							:	List[Any]			=						[]
   _snake_case							:	Union[str, Any]			=						None
   _snake_case							:	Optional[int]			=						[constraint.copy(stateful=a_	) for constraint in self.constraints]


 def 				UpperCamelCase_     (       self:	Union[str, Any]	):




   '''simple docstring'''



   _snake_case							:	Any			=						0
   if self.inprogress_constraint:
     # extra points for having a constraint mid-fulfilled
     add += self.max_seqlen - self.inprogress_constraint.remaining()

   return (len(self.complete_constraints	) * self.max_seqlen) + add


 def 				UpperCamelCase_     (       self:	Optional[int]	):




   '''simple docstring'''



   _snake_case							:	Union[str, Any]			=						[]
   if self.inprogress_constraint is None:
     for constraint in self.pending_constraints:  # "pending" == "unfulfilled yet"
       _snake_case							:	str			=						constraint.advance()
       if isinstance(a_,   a_	):
         token_list.append(a_	)
       elif isinstance(a_,   a_	):
         token_list.extend(a_	)
   else:
     _snake_case							:	List[str]			=						self.inprogress_constraint.advance()
     if isinstance(a_,   a_	):
       token_list.append(a_	)
     elif isinstance(a_,   a_	):
       token_list.extend(a_	)

   if len(a_	) == 0:
     return None
   else:
     return token_list


 def 				UpperCamelCase_     (       self:	int,   a_:	Optional[List[int]]	):




   '''simple docstring'''



   self.init_state()

   if token_ids is not None:
     for token in token_ids:
       # completes or steps **one** constraint
       _snake_case							:	int			=						self.add(a_	)

       # the entire list of constraints are fulfilled
       if self.completed:
         break


 def 				UpperCamelCase_     (       self:	List[Any],   a_:	int	):




   '''simple docstring'''



   if not isinstance(a_,   a_	):
     raise ValueError(f"`token_id` should be an `int`, but is `{token_id}`."	)

   _snake_case							:	List[str]			=						False, False

   if self.completed:
     _snake_case							:	Dict			=						True
     _snake_case							:	List[Any]			=						False
     return complete, stepped

   if self.inprogress_constraint is not None:
     # In the middle of fulfilling a constraint. If the `token_id` *does* makes an incremental progress to current
     # job, simply update the state

     _snake_case							:	Optional[int]			=						self.inprogress_constraint.update(a_	)
     if reset:
       # 1. If the next token breaks the progress, then we must restart.
       #     e.g. constraint = "I love pies" and sequence so far is "I love" but `token_id` == "books".

       #     But that doesn't mean we self.init_state(), since we only reset the state for this particular
       #     constraint, not the full list of constraints.

       self.pending_constraints.append(self.inprogress_constraint.copy(stateful=a_	)	)
       _snake_case							:	Union[str, Any]			=						None

     if complete:
       # 2. If the next token completes the constraint, move it to completed list, set
       #     inprogress to None. If there are no pending constraints either, then this full list of constraints
       #     is complete.

       self.complete_constraints.append(self.inprogress_constraint	)
       _snake_case							:	Dict			=						None

       if len(self.pending_constraints	) == 0:
         # we're done!
         _snake_case							:	Union[str, Any]			=						True

   else:
     # Not in the middle of fulfilling a constraint. So does this `token_id` helps us step towards any of our list
     # of constraints?

     for cidx, pending_constraint in enumerate(self.pending_constraints	):
       if pending_constraint.does_advance(a_	):
         _snake_case							:	Any			=						pending_constraint.update(a_	)

         if not stepped:
           raise Exception(
               """`constraint.update(token_id)` is not yielding incremental progress, """
               """even though `constraint.does_advance(token_id)` is true."""	)

         if complete:
           self.complete_constraints.append(a_	)
           _snake_case							:	Optional[Any]			=						None

         if not complete and stepped:
           _snake_case							:	List[Any]			=						pending_constraint

         if complete or stepped:
           # If we made any progress at all, then it's at least not a "pending constraint".

           _snake_case							:	Any			=						(
               self.pending_constraints[:cidx] + self.pending_constraints[cidx + 1 :]
           )

           if len(self.pending_constraints	) == 0 and self.inprogress_constraint is None:
             # If there's no longer any pending after this and no inprogress either, then we must be
             # complete.

             _snake_case							:	List[str]			=						True

           break  # prevent accidentally stepping through multiple constraints with just one token.

   return complete, stepped


 def 				UpperCamelCase_     (       self:	Dict,   a_:	int=True	):




   '''simple docstring'''



   _snake_case							:	Dict			=						ConstraintListState(self.constraints	)  # we actually never though self.constraints objects
   # throughout this process. So it's at initialization state.

   if stateful:
     _snake_case							:	List[Any]			=						[
         constraint.copy(stateful=a_	) for constraint in self.complete_constraints
     ]
     if self.inprogress_constraint is not None:
       _snake_case							:	Dict			=						self.inprogress_constraint.copy(stateful=a_	)
     _snake_case							:	List[str]			=						[constraint.copy() for constraint in self.pending_constraints]

   return new_state








"""simple docstring"""


import inspect
import unittest

from datasets import load_dataset
from packaging import version

from transformers import BeitConfig
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_torch_multi_gpu, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
     import torch
     from torch import nn

     from transformers import (
         MODEL_MAPPING,
         BeitForImageClassification,
         BeitForMaskedImageModeling,
         BeitForSemanticSegmentation,
         BeitModel,
     )
     from transformers.models.beit.modeling_beit import BEIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
     import PIL
     from PIL import Image

     from transformers import BeitImageProcessor






class       lowercase:







 '''simple docstring'''





 def __init__(       self:	Optional[Any],   a_:	Union[str, Any],   a_:	int=100,   a_:	int=13,   a_:	List[Any]=30,   a_:	str=2,   a_:	Optional[Any]=3,   a_:	Optional[int]=True,   a_:	Any=True,   a_:	Optional[Any]=32,   a_:	Tuple=4,   a_:	str=4,   a_:	List[Any]=37,   a_:	List[str]="gelu",   a_:	str=0.1,   a_:	Optional[int]=0.1,   a_:	Any=10,   a_:	List[str]=0.02,   a_:	Dict=3,   a_:	str=None,   a_:	Optional[int]=[0, 1, 2, 3],   ):




   '''simple docstring'''



   _snake_case							:	Optional[int]			=						parent
   _snake_case							:	Optional[Any]			=						100
   _snake_case							:	Any			=						batch_size
   _snake_case							:	List[Any]			=						image_size
   _snake_case							:	Optional[Any]			=						patch_size
   _snake_case							:	str			=						num_channels
   _snake_case							:	Tuple			=						is_training
   _snake_case							:	Tuple			=						use_labels
   _snake_case							:	Any			=						hidden_size
   _snake_case							:	Optional[int]			=						num_hidden_layers
   _snake_case							:	List[str]			=						num_attention_heads
   _snake_case							:	Union[str, Any]			=						intermediate_size
   _snake_case							:	Dict			=						hidden_act
   _snake_case							:	str			=						hidden_dropout_prob
   _snake_case							:	Optional[int]			=						attention_probs_dropout_prob
   _snake_case							:	Optional[Any]			=						type_sequence_label_size
   _snake_case							:	Any			=						initializer_range
   _snake_case							:	List[str]			=						scope
   _snake_case							:	int			=						out_indices
   _snake_case							:	Optional[Any]			=						num_labels

   # in BeiT, the seq length equals the number of patches + 1 (we add 1 for the [CLS] token)
   _snake_case							:	Dict			=						(image_size // patch_size) ** 2
   _snake_case							:	str			=						num_patches + 1


 def 				UpperCamelCase_     (       self:	Any	):




   '''simple docstring'''



   _snake_case							:	int			=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]	)

   _snake_case							:	List[Any]			=						None
   _snake_case							:	Tuple			=						None
   if self.use_labels:
     _snake_case							:	str			=						ids_tensor([self.batch_size],   self.type_sequence_label_size	)
     _snake_case							:	List[str]			=						ids_tensor([self.batch_size, self.image_size, self.image_size],   self.num_labels	)

   _snake_case							:	List[str]			=						self.get_config()

   return config, pixel_values, labels, pixel_labels


 def 				UpperCamelCase_     (       self:	List[Any]	):




   '''simple docstring'''



   return BeitConfig(
       vocab_size=self.vocab_size,   image_size=self.image_size,   patch_size=self.patch_size,   num_channels=self.num_channels,   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,   is_decoder=a_,   initializer_range=self.initializer_range,   out_indices=self.out_indices,   )


 def 				UpperCamelCase_     (       self:	List[Any],   a_:	List[Any],   a_:	Any,   a_:	Optional[Any],   a_:	List[str]	):




   '''simple docstring'''



   _snake_case							:	str			=						BeitModel(config=a_	)
   model.to(a_	)
   model.eval()
   _snake_case							:	Dict			=						model(a_	)
   self.parent.assertEqual(result.last_hidden_state.shape,   (self.batch_size, self.seq_length, self.hidden_size)	)


 def 				UpperCamelCase_     (       self:	str,   a_:	List[Any],   a_:	Optional[Any],   a_:	Optional[int],   a_:	List[Any]	):




   '''simple docstring'''



   _snake_case							:	List[str]			=						BeitForMaskedImageModeling(config=a_	)
   model.to(a_	)
   model.eval()
   _snake_case							:	Union[str, Any]			=						model(a_	)
   self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.seq_length - 1, self.vocab_size)	)


 def 				UpperCamelCase_     (       self:	Any,   a_:	List[str],   a_:	Any,   a_:	List[Any],   a_:	Optional[Any]	):




   '''simple docstring'''



   _snake_case							:	Any			=						self.type_sequence_label_size
   _snake_case							:	Any			=						BeitForImageClassification(a_	)
   model.to(a_	)
   model.eval()
   _snake_case							:	List[Any]			=						model(a_,   labels=a_	)
   self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.type_sequence_label_size)	)

   # test greyscale images
   _snake_case							:	Any			=						1
   _snake_case							:	str			=						BeitForImageClassification(a_	)
   model.to(a_	)
   model.eval()

   _snake_case							:	Dict			=						floats_tensor([self.batch_size, 1, self.image_size, self.image_size]	)
   _snake_case							:	Optional[Any]			=						model(a_,   labels=a_	)
   self.parent.assertEqual(result.logits.shape,   (self.batch_size, self.type_sequence_label_size)	)


 def 				UpperCamelCase_     (       self:	List[Any],   a_:	Optional[int],   a_:	List[Any],   a_:	str,   a_:	int	):




   '''simple docstring'''



   _snake_case							:	List[str]			=						self.num_labels
   _snake_case							:	List[Any]			=						BeitForSemanticSegmentation(a_	)
   model.to(a_	)
   model.eval()
   _snake_case							:	List[str]			=						model(a_	)
   self.parent.assertEqual(
       result.logits.shape,   (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)	)
   _snake_case							:	str			=						model(a_,   labels=a_	)
   self.parent.assertEqual(
       result.logits.shape,   (self.batch_size, self.num_labels, self.image_size * 2, self.image_size * 2)	)


 def 				UpperCamelCase_     (       self:	List[str]	):




   '''simple docstring'''



   _snake_case							:	Tuple			=						self.prepare_config_and_inputs()
   _snake_case	,      _snake_case	,      _snake_case	,      _snake_case							:	Any			=						config_and_inputs
   _snake_case							:	Optional[int]			=						{"""pixel_values""": pixel_values}
   return config, inputs_dict







@require_torch
class       lowercase(  __a ,    __a ,    unittest.TestCase				):







 '''simple docstring'''





 lowercase__				      =      (
     (BeitModel, BeitForImageClassification, BeitForMaskedImageModeling, BeitForSemanticSegmentation)
     if is_torch_available()
     else ()
 )
 lowercase__				      =      (
     {
         "feature-extraction": BeitModel,
         "image-classification": BeitForImageClassification,
         "image-segmentation": BeitForSemanticSegmentation,
     }
     if is_torch_available()
     else {}
 )

 lowercase__				      =      False
 lowercase__				      =      False
 lowercase__				      =      False


 def 				UpperCamelCase_     (       self:	Union[str, Any]	):




   '''simple docstring'''



   _snake_case							:	Dict			=						BeitModelTester(self	)
   _snake_case							:	int			=						ConfigTester(self,   config_class=a_,   has_text_modality=a_,   hidden_size=37	)


 def 				UpperCamelCase_     (       self:	str	):




   '''simple docstring'''



   self.config_tester.run_common_tests()


 @unittest.skip(reason="""BEiT does not use inputs_embeds"""	)
 def 				UpperCamelCase_     (       self:	Optional[int]	):




   '''simple docstring'''



   pass


 @require_torch_multi_gpu
 @unittest.skip(reason="""BEiT has some layers using `add_module` which doesn't work well with `nn.DataParallel`"""	)
 def 				UpperCamelCase_     (       self:	str	):




   '''simple docstring'''



   pass


 def 				UpperCamelCase_     (       self:	List[Any]	):




   '''simple docstring'''



   _snake_case	,      _snake_case							:	Any			=						self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     _snake_case							:	List[str]			=						model_class(a_	)
     self.assertIsInstance(model.get_input_embeddings(),   (nn.Module)	)
     _snake_case							:	Tuple			=						model.get_output_embeddings()
     self.assertTrue(x is None or isinstance(a_,   nn.Linear	)	)


 def 				UpperCamelCase_     (       self:	Optional[Any]	):




   '''simple docstring'''



   _snake_case	,      _snake_case							:	Optional[Any]			=						self.model_tester.prepare_config_and_inputs_for_common()

   for model_class in self.all_model_classes:
     _snake_case							:	Any			=						model_class(a_	)
     _snake_case							:	List[str]			=						inspect.signature(model.forward	)
     # signature.parameters is an OrderedDict => so arg_names order is deterministic
     _snake_case							:	List[Any]			=						[*signature.parameters.keys()]

     _snake_case							:	List[Any]			=						["""pixel_values"""]
     self.assertListEqual(arg_names[:1],   a_	)


 def 				UpperCamelCase_     (       self:	List[str]	):




   '''simple docstring'''



   _snake_case							:	Optional[int]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_model(*a_	)


 def 				UpperCamelCase_     (       self:	int	):




   '''simple docstring'''



   _snake_case							:	Optional[Any]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_masked_lm(*a_	)


 def 				UpperCamelCase_     (       self:	Dict	):




   '''simple docstring'''



   _snake_case							:	Dict			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_image_classification(*a_	)


 def 				UpperCamelCase_     (       self:	Optional[int]	):




   '''simple docstring'''



   _snake_case							:	List[Any]			=						self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_for_semantic_segmentation(*a_	)


 def 				UpperCamelCase_     (       self:	int	):




   '''simple docstring'''



   if not self.model_tester.is_training:
     return

   _snake_case	,      _snake_case							:	List[str]			=						self.model_tester.prepare_config_and_inputs_for_common()
   _snake_case							:	Any			=						True

   for model_class in self.all_model_classes:
     # we don't test BeitForMaskedImageModeling
     if model_class in [*get_values(a_	), BeitForMaskedImageModeling]:
       continue

     _snake_case							:	List[Any]			=						model_class(a_	)
     model.to(a_	)
     model.train()
     _snake_case							:	Dict			=						self._prepare_for_class(a_,   a_,   return_labels=a_	)
     _snake_case							:	List[Any]			=						model(**a_	).loss
     loss.backward()


 def 				UpperCamelCase_     (       self:	Any	):




   '''simple docstring'''



   _snake_case	,      _snake_case							:	List[Any]			=						self.model_tester.prepare_config_and_inputs_for_common()
   if not self.model_tester.is_training:
     return

   _snake_case							:	Dict			=						False
   _snake_case							:	Optional[Any]			=						True

   for model_class in self.all_model_classes:
     # we don't test BeitForMaskedImageModeling
     if (
         model_class in [*get_values(a_	), BeitForMaskedImageModeling]
         or not model_class.supports_gradient_checkpointing
     ):
       continue

     _snake_case							:	Any			=						model_class(a_	)
     model.gradient_checkpointing_enable()
     model.to(a_	)
     model.train()
     _snake_case							:	Any			=						self._prepare_for_class(a_,   a_,   return_labels=a_	)
     _snake_case							:	int			=						model(**a_	).loss
     loss.backward()


 def 				UpperCamelCase_     (       self:	Tuple	):




   '''simple docstring'''



   _snake_case	,      _snake_case							:	List[str]			=						self.model_tester.prepare_config_and_inputs_for_common()

   _snake_case							:	int			=						_config_zero_init(a_	)
   for model_class in self.all_model_classes:
     _snake_case							:	Tuple			=						model_class(config=a_	)
     for name, param in model.named_parameters():
       # we skip lambda parameters as these require special initial values
       # determined by config.layer_scale_init_value
       if "lambda" in name:
         continue
       if param.requires_grad:
         self.assertIn(
             ((param.data.mean() * 1E9).round() / 1E9).item(),   [0.0, 1.0],   msg=f"Parameter {name} of model {model_class} seems not properly initialized",   )


 @slow
 def 				UpperCamelCase_     (       self:	int	):




   '''simple docstring'''



   for model_name in BEIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
     _snake_case							:	Optional[int]			=						BeitModel.from_pretrained(a_	)
     self.assertIsNotNone(a_	)







def       UpperCAmelCase__ ():
  """simple docstring"""

  _snake_case							:	Union[str, Any]			=						Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png"""					)
  return image






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







 '''simple docstring'''





 @cached_property
 def 				UpperCamelCase_     (       self:	Dict	):




   '''simple docstring'''



   return BeitImageProcessor.from_pretrained("""microsoft/beit-base-patch16-224"""	) if is_vision_available() else None


 @slow
 def 				UpperCamelCase_     (       self:	Union[str, Any]	):




   '''simple docstring'''



   _snake_case							:	str			=						BeitForMaskedImageModeling.from_pretrained("""microsoft/beit-base-patch16-224-pt22k"""	).to(a_	)

   _snake_case							:	Dict			=						self.default_image_processor
   _snake_case							:	Dict			=						prepare_img()
   _snake_case							:	List[str]			=						image_processor(images=a_,   return_tensors="""pt"""	).pixel_values.to(a_	)

   # prepare bool_masked_pos
   _snake_case							:	Optional[int]			=						torch.ones((1, 196),   dtype=torch.bool	).to(a_	)

   # forward pass
   with torch.no_grad():
     _snake_case							:	int			=						model(pixel_values=a_,   bool_masked_pos=a_	)
   _snake_case							:	Dict			=						outputs.logits

   # verify the logits
   _snake_case							:	Optional[int]			=						torch.Size((1, 196, 8_192)	)
   self.assertEqual(logits.shape,   a_	)

   _snake_case							:	Optional[Any]			=						torch.tensor(
       [[-3.2_437, 0.5_072, -13.9_174], [-3.2_456, 0.4_948, -13.9_401], [-3.2_033, 0.5_121, -13.8_550]]	).to(a_	)

   self.assertTrue(torch.allclose(logits[bool_masked_pos][:3, :3],   a_,   atol=1E-2	)	)


 @slow
 def 				UpperCamelCase_     (       self:	Optional[int]	):




   '''simple docstring'''



   _snake_case							:	Dict			=						BeitForImageClassification.from_pretrained("""microsoft/beit-base-patch16-224"""	).to(a_	)

   _snake_case							:	List[Any]			=						self.default_image_processor
   _snake_case							:	Any			=						prepare_img()
   _snake_case							:	Any			=						image_processor(images=a_,   return_tensors="""pt"""	).to(a_	)

   # forward pass
   with torch.no_grad():
     _snake_case							:	int			=						model(**a_	)
   _snake_case							:	Optional[int]			=						outputs.logits

   # verify the logits
   _snake_case							:	Tuple			=						torch.Size((1, 1_000)	)
   self.assertEqual(logits.shape,   a_	)

   _snake_case							:	Any			=						torch.tensor([-1.2_385, -1.0_987, -1.0_108]	).to(a_	)

   self.assertTrue(torch.allclose(logits[0, :3],   a_,   atol=1E-4	)	)

   _snake_case							:	str			=						281
   self.assertEqual(logits.argmax(-1	).item(),   a_	)


 @slow
 def 				UpperCamelCase_     (       self:	Dict	):




   '''simple docstring'''



   _snake_case							:	int			=						BeitForImageClassification.from_pretrained("""microsoft/beit-large-patch16-224-pt22k-ft22k"""	).to(
       a_	)

   _snake_case							:	int			=						self.default_image_processor
   _snake_case							:	Optional[Any]			=						prepare_img()
   _snake_case							:	Union[str, Any]			=						image_processor(images=a_,   return_tensors="""pt"""	).to(a_	)

   # forward pass
   with torch.no_grad():
     _snake_case							:	Union[str, Any]			=						model(**a_	)
   _snake_case							:	Dict			=						outputs.logits

   # verify the logits
   _snake_case							:	Tuple			=						torch.Size((1, 21_841)	)
   self.assertEqual(logits.shape,   a_	)

   _snake_case							:	Optional[int]			=						torch.tensor([1.6_881, -0.2_787, 0.5_901]	).to(a_	)

   self.assertTrue(torch.allclose(logits[0, :3],   a_,   atol=1E-4	)	)

   _snake_case							:	List[str]			=						2_396
   self.assertEqual(logits.argmax(-1	).item(),   a_	)


 @slow
 def 				UpperCamelCase_     (       self:	Dict	):




   '''simple docstring'''



   _snake_case							:	List[str]			=						BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""	)
   _snake_case							:	int			=						model.to(a_	)

   _snake_case							:	List[str]			=						BeitImageProcessor(do_resize=a_,   size=640,   do_center_crop=a_	)

   _snake_case							:	Optional[int]			=						load_dataset("""hf-internal-testing/fixtures_ade20k""",   split="""test"""	)
   _snake_case							:	Union[str, Any]			=						Image.open(ds[0]["""file"""]	)
   _snake_case							:	List[Any]			=						image_processor(images=a_,   return_tensors="""pt"""	).to(a_	)

   # forward pass
   with torch.no_grad():
     _snake_case							:	Optional[int]			=						model(**a_	)
   _snake_case							:	Union[str, Any]			=						outputs.logits

   # verify the logits
   _snake_case							:	List[str]			=						torch.Size((1, 150, 160, 160)	)
   self.assertEqual(logits.shape,   a_	)

   _snake_case							:	Optional[int]			=						version.parse(PIL.__version__	) < version.parse("""9.0.0"""	)

   if is_pillow_less_than_a:
     _snake_case							:	Any			=						torch.tensor(
         [
             [[-4.9_225, -2.3_954, -3.0_522], [-2.8_822, -1.0_046, -1.7_561], [-2.9_549, -1.3_228, -2.1_347]],
             [[-5.8_168, -3.4_129, -4.0_778], [-3.8_651, -2.2_214, -3.0_277], [-3.8_356, -2.4_643, -3.3_535]],
             [[-0.0_078, 3.9_952, 4.0_754], [2.9_856, 4.6_944, 5.0_035], [3.2_413, 4.7_813, 4.9_969]],
         ],   device=a_,   )
   else:
     _snake_case							:	Optional[Any]			=						torch.tensor(
         [
             [[-4.8_960, -2.3_688, -3.0_355], [-2.8_478, -0.9_836, -1.7_418], [-2.9_449, -1.3_332, -2.1_456]],
             [[-5.8_081, -3.4_124, -4.1_006], [-3.8_561, -2.2_081, -3.0_323], [-3.8_365, -2.4_601, -3.3_669]],
             [[-0.0_309, 3.9_868, 4.0_540], [2.9_640, 4.6_877, 4.9_976], [3.2_081, 4.7_690, 4.9_942]],
         ],   device=a_,   )

   self.assertTrue(torch.allclose(logits[0, :3, :3, :3],   a_,   atol=1E-4	)	)


 @slow
 def 				UpperCamelCase_     (       self:	Optional[Any]	):




   '''simple docstring'''



   _snake_case							:	int			=						BeitForSemanticSegmentation.from_pretrained("""microsoft/beit-base-finetuned-ade-640-640"""	)
   _snake_case							:	List[Any]			=						model.to(a_	)

   _snake_case							:	Tuple			=						BeitImageProcessor(do_resize=a_,   size=640,   do_center_crop=a_	)

   _snake_case							:	Union[str, Any]			=						load_dataset("""hf-internal-testing/fixtures_ade20k""",   split="""test"""	)
   _snake_case							:	str			=						Image.open(ds[0]["""file"""]	)
   _snake_case							:	Tuple			=						image_processor(images=a_,   return_tensors="""pt"""	).to(a_	)

   # forward pass
   with torch.no_grad():
     _snake_case							:	Optional[int]			=						model(**a_	)

   _snake_case							:	Union[str, Any]			=						outputs.logits.detach().cpu()

   _snake_case							:	Union[str, Any]			=						image_processor.post_process_semantic_segmentation(outputs=a_,   target_sizes=[(500, 300)]	)
   _snake_case							:	Optional[int]			=						torch.Size((500, 300)	)
   self.assertEqual(segmentation[0].shape,   a_	)

   _snake_case							:	List[str]			=						image_processor.post_process_semantic_segmentation(outputs=a_	)
   _snake_case							:	List[str]			=						torch.Size((160, 160)	)
   self.assertEqual(segmentation[0].shape,   a_	)


