






import inspect
import unittest

from transformers import MobileNetVaConfig
from transformers.testing_utils import require_torch, 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, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
   import torch

   from transformers import MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation, MobileNetVaModel
   from transformers.models.mobilenet_va.modeling_mobilenet_va import MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
   from PIL import Image

   from transformers import MobileNetVaImageProcessor


class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase					):
     '''simple docstring'''


     def 				_A       (				self       :	Optional[Any]      ):
       SCREAMING_SNAKE_CASE       :      str							=    self.config_class(**self.inputs_dict      )
       self.parent.assertTrue(hasattr(UpperCAmelCase_					, "tf_padding"      )      )
       self.parent.assertTrue(hasattr(UpperCAmelCase_					, "depth_multiplier"      )      )





class  SCREAMING_SNAKE_CASE			:
     '''simple docstring'''


     def __init__(				self       :	Any					, UpperCAmelCase_       :	Union[str, Any]					, UpperCAmelCase_       :	Any=13					, UpperCAmelCase_       :	int=3					, UpperCAmelCase_       :	Tuple=32					, UpperCAmelCase_       :	Tuple=0.25					, UpperCAmelCase_       :	List[str]=8					, UpperCAmelCase_       :	List[str]=8					, UpperCAmelCase_       :	Tuple=6					, UpperCAmelCase_       :	List[Any]=32					, UpperCAmelCase_       :	Optional[int]=True					, UpperCAmelCase_       :	Dict=True					, UpperCAmelCase_       :	int=True					, UpperCAmelCase_       :	Tuple="relu6"					, UpperCAmelCase_       :	Any=1280					, UpperCAmelCase_       :	Tuple=0.1					, UpperCAmelCase_       :	Optional[Any]=0.02					, UpperCAmelCase_       :	Optional[int]=True					, UpperCAmelCase_       :	Any=True					, UpperCAmelCase_       :	List[Any]=10					, UpperCAmelCase_       :	str=None					, ):
       SCREAMING_SNAKE_CASE       :      Dict							=    parent
       SCREAMING_SNAKE_CASE       :      str							=    batch_size
       SCREAMING_SNAKE_CASE       :      Any							=    num_channels
       SCREAMING_SNAKE_CASE       :      int							=    image_size
       SCREAMING_SNAKE_CASE       :      Any							=    depth_multiplier
       SCREAMING_SNAKE_CASE       :      int							=    depth_divisible_by
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    min_depth
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    expand_ratio
       SCREAMING_SNAKE_CASE       :      int							=    tf_padding
       SCREAMING_SNAKE_CASE       :      int							=    output_stride
       SCREAMING_SNAKE_CASE       :      Dict							=    first_layer_is_expansion
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    finegrained_output
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    hidden_act
       SCREAMING_SNAKE_CASE       :      Tuple							=    last_hidden_size if finegrained_output else int(last_hidden_size * depth_multiplier      )
       SCREAMING_SNAKE_CASE       :      List[str]							=    classifier_dropout_prob
       SCREAMING_SNAKE_CASE       :      List[Any]							=    use_labels
       SCREAMING_SNAKE_CASE       :      str							=    is_training
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    num_labels
       SCREAMING_SNAKE_CASE       :      Tuple							=    initializer_range
       SCREAMING_SNAKE_CASE       :      int							=    scope


     def 				_A       (				self       :	Dict      ):
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    None
       SCREAMING_SNAKE_CASE       :      Any							=    None
       if self.use_labels:
         SCREAMING_SNAKE_CASE       :      List[str]							=    ids_tensor([self.batch_size]					, self.num_labels      )
         SCREAMING_SNAKE_CASE       :      Optional[int]							=    ids_tensor([self.batch_size, self.image_size, self.image_size]					, self.num_labels      )

       SCREAMING_SNAKE_CASE       :      Dict							=    self.get_config()

       return config, pixel_values, labels, pixel_labels


     def 				_A       (				self       :	Union[str, Any]      ):
       return MobileNetVaConfig(
           num_channels=self.num_channels					, image_size=self.image_size					, depth_multiplier=self.depth_multiplier					, depth_divisible_by=self.depth_divisible_by					, min_depth=self.min_depth					, expand_ratio=self.expand_ratio					, output_stride=self.output_stride					, first_layer_is_expansion=self.first_layer_is_expansion					, finegrained_output=self.finegrained_output					, hidden_act=self.hidden_act					, tf_padding=self.tf_padding					, classifier_dropout_prob=self.classifier_dropout_prob					, initializer_range=self.initializer_range					, )


     def 				_A       (				self       :	List[Any]					, UpperCAmelCase_       :	str					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	List[str]					, UpperCAmelCase_       :	List[Any]      ):
       SCREAMING_SNAKE_CASE       :      List[Any]							=    MobileNetVaModel(config=UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE       :      str							=    model(UpperCAmelCase_      )
       self.parent.assertEqual(
           result.last_hidden_state.shape					, (
               self.batch_size,
               self.last_hidden_size,
               self.image_size // self.output_stride,
               self.image_size // self.output_stride,
           )					, )
       self.parent.assertEqual(
           result.pooler_output.shape					, (self.batch_size, self.last_hidden_size)					, )


     def 				_A       (				self       :	Any					, UpperCAmelCase_       :	Tuple					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	Dict					, UpperCAmelCase_       :	Optional[Any]      ):
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    self.num_labels
       SCREAMING_SNAKE_CASE       :      str							=    MobileNetVaForImageClassification(UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE       :      str							=    model(UpperCAmelCase_					, labels=UpperCAmelCase_      )
       self.parent.assertEqual(result.logits.shape					, (self.batch_size, self.num_labels)      )


     def 				_A       (				self       :	int					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	Optional[Any]					, UpperCAmelCase_       :	Any					, UpperCAmelCase_       :	Any      ):
       SCREAMING_SNAKE_CASE       :      List[Any]							=    self.num_labels
       SCREAMING_SNAKE_CASE       :      Dict							=    MobileNetVaForSemanticSegmentation(UpperCAmelCase_      )
       model.to(UpperCAmelCase_      )
       model.eval()
       SCREAMING_SNAKE_CASE       :      List[str]							=    model(UpperCAmelCase_      )
       self.parent.assertEqual(
           result.logits.shape					, (
               self.batch_size,
               self.num_labels,
               self.image_size // self.output_stride,
               self.image_size // self.output_stride,
           )					, )
       SCREAMING_SNAKE_CASE       :      str							=    model(UpperCAmelCase_					, labels=UpperCAmelCase_      )
       self.parent.assertEqual(
           result.logits.shape					, (
               self.batch_size,
               self.num_labels,
               self.image_size // self.output_stride,
               self.image_size // self.output_stride,
           )					, )






     def 				_A       (				self       :	Tuple      ):
       SCREAMING_SNAKE_CASE       :      int							=    self.prepare_config_and_inputs()
       SCREAMING_SNAKE_CASE						,  SCREAMING_SNAKE_CASE						,  SCREAMING_SNAKE_CASE						,  SCREAMING_SNAKE_CASE       :      Dict							=    config_and_inputs
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    {"pixel_values": pixel_values}
       return config, inputs_dict





@require_torch
class  SCREAMING_SNAKE_CASE			(			lowerCAmelCase      ,		lowerCAmelCase      ,		unittest.TestCase					):
     '''simple docstring'''


     UpperCamelCase_							:		int			=	(
         (MobileNetVaModel, MobileNetVaForImageClassification, MobileNetVaForSemanticSegmentation)
         if is_torch_available()
         else ()
     )
     UpperCamelCase_							:		Dict			=	(
         {
             '''feature-extraction''': MobileNetVaModel,
             '''image-classification''': MobileNetVaForImageClassification,
             '''image-segmentation''': MobileNetVaForSemanticSegmentation,
         }
         if is_torch_available()
         else {}
     )

     UpperCamelCase_							:		int			=	False
     UpperCamelCase_							:		str			=	False
     UpperCamelCase_							:		Union[str, Any]			=	False
     UpperCamelCase_							:		Tuple			=	False


     def 				_A       (				self       :	str      ):
       SCREAMING_SNAKE_CASE       :      Optional[int]							=    MobileNetVaModelTester(self      )
       SCREAMING_SNAKE_CASE       :      Dict							=    MobileNetVaConfigTester(self					, config_class=UpperCAmelCase_					, has_text_modality=UpperCAmelCase_      )


     def 				_A       (				self       :	List[str]      ):
       self.config_tester.run_common_tests()


     @unittest.skip(reason="MobileNetV2 does not use inputs_embeds"      )
     def 				_A       (				self       :	Union[str, Any]      ):
       pass


     @unittest.skip(reason="MobileNetV2 does not support input and output embeddings"      )
     def 				_A       (				self       :	Any      ):
       pass


     @unittest.skip(reason="MobileNetV2 does not output attentions"      )
     def 				_A       (				self       :	Dict      ):
       pass


     def 				_A       (				self       :	List[str]      ):
       SCREAMING_SNAKE_CASE						,  SCREAMING_SNAKE_CASE       :      int							=    self.model_tester.prepare_config_and_inputs_for_common()

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

         SCREAMING_SNAKE_CASE       :      Any							=    ["pixel_values"]
         self.assertListEqual(arg_names[:1]					, UpperCAmelCase_      )


     def 				_A       (				self       :	Optional[int]      ):
       SCREAMING_SNAKE_CASE       :      Dict							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*UpperCAmelCase_      )


     def 				_A       (				self       :	Tuple      ):
       def check_hidden_states_output(UpperCAmelCase_       :	str					, UpperCAmelCase_       :	List[Any]					, UpperCAmelCase_       :	Union[str, Any]      ):
         SCREAMING_SNAKE_CASE       :      Tuple							=    model_class(UpperCAmelCase_      )
         model.to(UpperCAmelCase_      )
         model.eval()

         with torch.no_grad():
           SCREAMING_SNAKE_CASE       :      Tuple							=    model(**self._prepare_for_class(UpperCAmelCase_					, UpperCAmelCase_      )      )

         SCREAMING_SNAKE_CASE       :      str							=    outputs.hidden_states

         SCREAMING_SNAKE_CASE       :      str							=    16
         self.assertEqual(len(UpperCAmelCase_      )					, UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE						,  SCREAMING_SNAKE_CASE       :      Tuple							=    self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
         SCREAMING_SNAKE_CASE       :      Optional[Any]							=    True
         check_hidden_states_output(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_      )

         # check that output_hidden_states also work using config
         del inputs_dict["output_hidden_states"]
         SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    True

         check_hidden_states_output(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_      )


     def 				_A       (				self       :	int      ):
       SCREAMING_SNAKE_CASE       :      List[str]							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_image_classification(*UpperCAmelCase_      )


     def 				_A       (				self       :	int      ):
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_for_semantic_segmentation(*UpperCAmelCase_      )






     @slow
     def 				_A       (				self       :	Union[str, Any]      ):
       for model_name in MOBILENET_V2_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
         SCREAMING_SNAKE_CASE       :      List[str]							=    MobileNetVaModel.from_pretrained(UpperCAmelCase_      )
         self.assertIsNotNone(UpperCAmelCase_      )





def   lowerCamelCase__			(     ):

  """simple docstring"""






  SCREAMING_SNAKE_CASE       :      Any							=    Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"				)
  return image


@require_torch
@require_vision
class  SCREAMING_SNAKE_CASE			(			unittest.TestCase					):
     '''simple docstring'''


     @cached_property
     def 				_A       (				self       :	List[str]      ):
       return (
           MobileNetVaImageProcessor.from_pretrained("google/mobilenet_v2_1.0_224"      ) if is_vision_available() else None
       )


     @slow
     def 				_A       (				self       :	Optional[int]      ):
       SCREAMING_SNAKE_CASE       :      Dict							=    MobileNetVaForImageClassification.from_pretrained("google/mobilenet_v2_1.0_224"      ).to(UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE       :      str							=    self.default_image_processor
       SCREAMING_SNAKE_CASE       :      List[Any]							=    prepare_img()
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    image_processor(images=UpperCAmelCase_					, return_tensors="pt"      ).to(UpperCAmelCase_      )

       # forward pass
       with torch.no_grad():
         SCREAMING_SNAKE_CASE       :      List[Any]							=    model(**UpperCAmelCase_      )

       # verify the logits
       SCREAMING_SNAKE_CASE       :      Any							=    torch.Size((1, 1001)      )
       self.assertEqual(outputs.logits.shape					, UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    torch.tensor([0.2_445, -1.1_993, 0.1_905]      ).to(UpperCAmelCase_      )

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






     @slow
     def 				_A       (				self       :	Optional[int]      ):
       SCREAMING_SNAKE_CASE       :      Any							=    MobileNetVaForSemanticSegmentation.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513"      )
       SCREAMING_SNAKE_CASE       :      Optional[Any]							=    model.to(UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE       :      List[str]							=    MobileNetVaImageProcessor.from_pretrained("google/deeplabv3_mobilenet_v2_1.0_513"      )

       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    prepare_img()
       SCREAMING_SNAKE_CASE       :      Any							=    image_processor(images=UpperCAmelCase_					, return_tensors="pt"      ).to(UpperCAmelCase_      )

       # forward pass
       with torch.no_grad():
         SCREAMING_SNAKE_CASE       :      str							=    model(**UpperCAmelCase_      )
       SCREAMING_SNAKE_CASE       :      Tuple							=    outputs.logits

       # verify the logits
       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    torch.Size((1, 21, 65, 65)      )
       self.assertEqual(logits.shape					, UpperCAmelCase_      )

       SCREAMING_SNAKE_CASE       :      Union[str, Any]							=    torch.tensor(
           [
               [[17.5_790, 17.7_581, 18.3_355], [18.3_257, 18.4_230, 18.8_973], [18.6_169, 18.8_650, 19.2_187]],
               [[-2.1_595, -2.0_977, -2.3_741], [-2.4_226, -2.3_028, -2.6_835], [-2.7_819, -2.5_991, -2.7_706]],
               [[4.2_058, 4.8_317, 4.7_638], [4.4_136, 5.0_361, 4.9_383], [4.5_028, 4.9_644, 4.8_734]],
           ]					, device=UpperCAmelCase_					, )

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














# Copyright 2022 The HuggingFace Team and The OpenBMB 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

# rely on isort to merge the imports
from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tokenizers_available, is_torch_available


snake_case    				=		{
    """configuration_cpmant""": ["""CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP""", """CpmAntConfig"""],
    """tokenization_cpmant""": ["""CpmAntTokenizer"""],
}

try:
   if not is_torch_available():
      raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
   pass
else:
   snake_case    				=		[
       """CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST""",
       """CpmAntForCausalLM""",
       """CpmAntModel""",
       """CpmAntPreTrainedModel""",
   ]


if TYPE_CHECKING:
   from .configuration_cpmant import CPMANT_PRETRAINED_CONFIG_ARCHIVE_MAP, CpmAntConfig
   from .tokenization_cpmant import CpmAntTokenizer

   try:
      if not is_torch_available():
         raise OptionalDependencyNotAvailable()
   except OptionalDependencyNotAvailable:
      pass
   else:
      from .modeling_cpmant import (
          CPMANT_PRETRAINED_MODEL_ARCHIVE_LIST,
          CpmAntForCausalLM,
          CpmAntModel,
          CpmAntPreTrainedModel,
      )


else:
   import sys

   snake_case    				=		_LazyModule(__name__, globals()["""__file__"""], _import_structure, module_spec=__spec__)






