


# Copyright 2023 The HuggingFace Team. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


SCREAMING_SNAKE_CASE       :		str         =				{"configuration_timm_backbone": ["TimmBackboneConfig"]}


try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   SCREAMING_SNAKE_CASE       :		List[str]         =				["TimmBackbone"]


if TYPE_CHECKING:
   from .configuration_timm_backbone import TimmBackboneConfig

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_timm_backbone import TimmBackbone

else:
   import sys

   SCREAMING_SNAKE_CASE       :		Dict         =				_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)





import unittest

import numpy as np

from transformers import BertConfig, 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.bert.modeling_flax_bert import (
       FlaxBertForMaskedLM,
       FlaxBertForMultipleChoice,
       FlaxBertForNextSentencePrediction,
       FlaxBertForPreTraining,
       FlaxBertForQuestionAnswering,
       FlaxBertForSequenceClassification,
       FlaxBertForTokenClassification,
       FlaxBertModel,
   )






class      UpperCamelCase					(		unittest.TestCase     ):


   '''simple docstring'''







   def __init__(     self   ,							UpperCamelCase_   ,							UpperCamelCase_=13   ,							UpperCamelCase_=7   ,							UpperCamelCase_=True   ,							UpperCamelCase_=True   ,							UpperCamelCase_=True   ,							UpperCamelCase_=True   ,							UpperCamelCase_=99   ,							UpperCamelCase_=32   ,							UpperCamelCase_=5   ,							UpperCamelCase_=4   ,							UpperCamelCase_=37   ,							UpperCamelCase_="gelu"   ,							UpperCamelCase_=0.1   ,							UpperCamelCase_=0.1   ,							UpperCamelCase_=512   ,							UpperCamelCase_=16   ,							UpperCamelCase_=2   ,							UpperCamelCase_=0.02   ,							UpperCamelCase_=4   ,							):
        lowercase_       :Union[str, Any]     =       parent
        lowercase_       :int     =       batch_size
        lowercase_       :int     =       seq_length
        lowercase_       :str     =       is_training
        lowercase_       :Dict     =       use_attention_mask
        lowercase_       :List[Any]     =       use_token_type_ids
        lowercase_       :str     =       use_labels
        lowercase_       :str     =       vocab_size
        lowercase_       :Optional[int]     =       hidden_size
        lowercase_       :Dict     =       num_hidden_layers
        lowercase_       :List[str]     =       num_attention_heads
        lowercase_       :int     =       intermediate_size
        lowercase_       :Union[str, Any]     =       hidden_act
        lowercase_       :Optional[int]     =       hidden_dropout_prob
        lowercase_       :Tuple     =       attention_probs_dropout_prob
        lowercase_       :int     =       max_position_embeddings
        lowercase_       :List[Any]     =       type_vocab_size
        lowercase_       :Any     =       type_sequence_label_size
        lowercase_       :Tuple     =       initializer_range
        lowercase_       :Any     =       num_choices
   def 		UpperCamelCase  (     self		):
        lowercase_       :Optional[int]     =       ids_tensor([self.batch_size, self.seq_length]   ,							self.vocab_size		)

        lowercase_       :List[str]     =       None
        if self.use_attention_mask:
             lowercase_       :Any     =       random_attention_mask([self.batch_size, self.seq_length]		)

        lowercase_       :List[Any]     =       None
        if self.use_token_type_ids:
             lowercase_       :Union[str, Any]     =       ids_tensor([self.batch_size, self.seq_length]   ,							self.type_vocab_size		)

        lowercase_       :Union[str, Any]     =       BertConfig(
            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=UpperCamelCase_   ,							initializer_range=self.initializer_range   ,							)

        return config, input_ids, token_type_ids, attention_mask
   def 		UpperCamelCase  (     self		):
        lowercase_       :Optional[Any]     =       self.prepare_config_and_inputs()
        lowercase_    ,      lowercase_    ,      lowercase_    ,      lowercase_       :List[str]     =       config_and_inputs
        lowercase_       :Union[str, Any]     =       {'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
        return config, inputs_dict




   def 		UpperCamelCase  (     self		):
        lowercase_       :Dict     =       self.prepare_config_and_inputs()
        lowercase_    ,      lowercase_    ,      lowercase_    ,      lowercase_       :Dict     =       config_and_inputs

        lowercase_       :str     =       True
        lowercase_       :Dict     =       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]		)
        lowercase_       :Optional[Any]     =       ids_tensor([self.batch_size, self.seq_length]   ,							vocab_size=2		)

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






@require_flax
class      UpperCamelCase					(		lowercase__						,      unittest.TestCase     ):


   '''simple docstring'''







   lowercase					:	int									=True

   lowercase					:	Dict									=(
       (
           FlaxBertModel,
           FlaxBertForPreTraining,
           FlaxBertForMaskedLM,
           FlaxBertForMultipleChoice,
           FlaxBertForQuestionAnswering,
           FlaxBertForNextSentencePrediction,
           FlaxBertForSequenceClassification,
           FlaxBertForTokenClassification,
           FlaxBertForQuestionAnswering,
       )
       if is_flax_available()
       else ()
   )
   def 		UpperCamelCase  (     self		):
        lowercase_       :Union[str, Any]     =       FlaxBertModelTester(self		)




   @slow
   def 		UpperCamelCase  (     self		):
        # Only check this for base model, not necessary for all model classes.
        # This will also help speed-up tests.
        lowercase_       :Dict     =       FlaxBertModel.from_pretrained('''bert-base-cased'''		)
        lowercase_       :str     =       model(np.ones((1, 1)		)		)
        self.assertIsNotNone(UpperCamelCase_		)

