






from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


a						:			int     	=					logging.get_logger(__name__)

a						:			Union[str, Any]     	=					{
    "google/mobilenet_v2_1.4_224": "https://huggingface.co/google/mobilenet_v2_1.4_224/resolve/main/config.json",
    "google/mobilenet_v2_1.0_224": "https://huggingface.co/google/mobilenet_v2_1.0_224/resolve/main/config.json",
    "google/mobilenet_v2_0.75_160": "https://huggingface.co/google/mobilenet_v2_0.75_160/resolve/main/config.json",
    "google/mobilenet_v2_0.35_96": "https://huggingface.co/google/mobilenet_v2_0.35_96/resolve/main/config.json",
    # See all MobileNetV2 models at https://huggingface.co/models?filter=mobilenet_v2
}
class        a       (					lowercase__  ):



      """simple docstring"""


      a    :					Union[str, Any]							=      'mobilenet_v2'
      def __init__( self   :     Tuple						, __lowercase   :     List[Any]=3						, __lowercase   :     List[str]=224						, __lowercase   :     Tuple=1.0						, __lowercase   :     Union[str, Any]=8						, __lowercase   :     Tuple=8						, __lowercase   :     List[Any]=6						, __lowercase   :     Optional[int]=32						, __lowercase   :     List[Any]=True						, __lowercase   :     Any=True						, __lowercase   :     List[Any]="relu6"						, __lowercase   :     Tuple=True						, __lowercase   :     str=0.8						, __lowercase   :     List[str]=0.02						, __lowercase   :     Union[str, Any]=0.001						, __lowercase   :     Union[str, Any]=255						, **__lowercase   :     str						, )				-> Optional[int]:
        super().__init__(**__lowercase   )

        if depth_multiplier <= 0:
          raise ValueError("""depth_multiplier must be greater than zero."""   )

        __UpperCAmelCase				:      List[Any]	 =    num_channels
        __UpperCAmelCase				:      int	 =    image_size
        __UpperCAmelCase				:      Tuple	 =    depth_multiplier
        __UpperCAmelCase				:      Dict	 =    depth_divisible_by
        __UpperCAmelCase				:      Union[str, Any]	 =    min_depth
        __UpperCAmelCase				:      Optional[int]	 =    expand_ratio
        __UpperCAmelCase				:      List[Any]	 =    output_stride
        __UpperCAmelCase				:      Any	 =    first_layer_is_expansion
        __UpperCAmelCase				:      str	 =    finegrained_output
        __UpperCAmelCase				:      Optional[Any]	 =    hidden_act
        __UpperCAmelCase				:      Optional[int]	 =    tf_padding
        __UpperCAmelCase				:      Optional[int]	 =    classifier_dropout_prob
        __UpperCAmelCase				:      List[str]	 =    initializer_range
        __UpperCAmelCase				:      Any	 =    layer_norm_eps
        __UpperCAmelCase				:      str	 =    semantic_loss_ignore_index


class        a       (					lowercase__  ):



      """simple docstring"""


      a    :					Tuple							=      version.parse('1.11'  )
      @property
      def       UpperCAmelCase ( self   :     Tuple   )				-> Mapping[str, Mapping[int, str]]:
        return OrderedDict([("""pixel_values""", {0: """batch"""})]   )
      @property
      def       UpperCAmelCase ( self   :     Union[str, Any]   )				-> Mapping[str, Mapping[int, str]]:
        if self.task == "image-classification":
          return OrderedDict([("""logits""", {0: """batch"""})]   )
        else:
          return OrderedDict([("""last_hidden_state""", {0: """batch"""}), ("""pooler_output""", {0: """batch"""})]   )




      @property
      def       UpperCAmelCase ( self   :     List[str]   )				-> float:
        return 1e-4








import unittest

import numpy as np

from transformers import is_flax_available
from transformers.testing_utils import require_flax

from ..test_modeling_flax_common import ids_tensor


if is_flax_available():
						import jax
						import jax.numpy as jnp

						from transformers.generation import (
						    FlaxForcedBOSTokenLogitsProcessor,
						    FlaxForcedEOSTokenLogitsProcessor,
						    FlaxLogitsProcessorList,
						    FlaxMinLengthLogitsProcessor,
						    FlaxTemperatureLogitsWarper,
						    FlaxTopKLogitsWarper,
						    FlaxTopPLogitsWarper,
						)







@require_flax
class 				lowerCAmelCase_       (					unittest.TestCase							):






				def 		snake_case	(			self      ,snake_case__      ,snake_case__ ):
							SCREAMING_SNAKE_CASE_   : int					  =      jnp.ones((batch_size, length) ) / length
							return scores






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : str					  =      None
							SCREAMING_SNAKE_CASE_   : int					  =      20

							SCREAMING_SNAKE_CASE_   : int					  =      self._get_uniform_logits(batch_size=2      ,length=snake_case__ )

							# tweak scores to not be uniform anymore
							SCREAMING_SNAKE_CASE_   : Any					  =      scores.at[1, 5].set((1 / length) + 0.1 )  # peak, 1st batch
							SCREAMING_SNAKE_CASE_   : Dict					  =      scores.at[1, 10].set((1 / length) - 0.4 )  # valley, 1st batch

							# compute softmax
							SCREAMING_SNAKE_CASE_   : int					  =      jax.nn.softmax(snake_case__      ,axis=-1 )

							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      FlaxTemperatureLogitsWarper(temperature=0.5 )
							SCREAMING_SNAKE_CASE_   : Dict					  =      FlaxTemperatureLogitsWarper(temperature=1.3 )

							SCREAMING_SNAKE_CASE_   : Any					  =      jax.nn.softmax(temp_dist_warper_sharper(snake_case__      ,scores.copy()      ,cur_len=snake_case__ )      ,axis=-1 )
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      jax.nn.softmax(temp_dist_warper_smoother(snake_case__      ,scores.copy()      ,cur_len=snake_case__ )      ,axis=-1 )

							# uniform distribution stays uniform
							self.assertTrue(jnp.allclose(probs[0, :]      ,warped_prob_sharp[0, :]      ,atol=1E-3 ) )
							self.assertTrue(jnp.allclose(probs[0, :]      ,warped_prob_smooth[0, :]      ,atol=1E-3 ) )

							# sharp peaks get higher, valleys get lower
							self.assertLess(probs[1, :].max()      ,warped_prob_sharp[1, :].max() )
							self.assertGreater(probs[1, :].min()      ,warped_prob_sharp[1, :].min() )

							# smooth peaks get lower, valleys get higher
							self.assertGreater(probs[1, :].max()      ,warped_prob_smooth[1, :].max() )
							self.assertLess(probs[1, :].min()      ,warped_prob_smooth[1, :].min() )






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Any					  =      None
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      10
							SCREAMING_SNAKE_CASE_   : int					  =      2

							# create ramp distribution
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      np.broadcast_to(np.arange(snake_case__ )[None, :]      ,(batch_size, vocab_size) ).copy()
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      ramp_logits[1:, : vocab_size // 2] + vocab_size

							SCREAMING_SNAKE_CASE_   : List[str]					  =      FlaxTopKLogitsWarper(3 )

							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      top_k_warp(snake_case__      ,snake_case__      ,cur_len=snake_case__ )

							# check that correct tokens are filtered
							self.assertListEqual(jnp.isinf(scores[0] ).tolist()      ,7 * [True] + 3 * [False] )
							self.assertListEqual(jnp.isinf(scores[1] ).tolist()      ,2 * [True] + 3 * [False] + 5 * [True] )

							# check special case
							SCREAMING_SNAKE_CASE_   : int					  =      5
							SCREAMING_SNAKE_CASE_   : Any					  =      FlaxTopKLogitsWarper(top_k=1      ,filter_value=0.0      ,min_tokens_to_keep=3 )

							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      np.broadcast_to(np.arange(snake_case__ )[None, :]      ,(batch_size, length) ).copy()
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      top_k_warp_safety_check(snake_case__      ,snake_case__      ,cur_len=snake_case__ )

							# min_tokens overwrites k: 3 tokens are kept => 2 tokens are nullified
							self.assertListEqual((scores == 0.0).sum(axis=-1 ).tolist()      ,[2, 2] )






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      None
							SCREAMING_SNAKE_CASE_   : str					  =      10
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      2

							# create distribution and take log (inverse to Softmax as taken in TopPLogitsWarper)
							SCREAMING_SNAKE_CASE_   : Any					  =      np.log(np.array([[0.3, 0.1, 0.1, 0.5], [0.15, 0.3, 0.3, 0.25]] ) )

							SCREAMING_SNAKE_CASE_   : Dict					  =      FlaxTopPLogitsWarper(0.8 )
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      np.exp(top_p_warp(snake_case__      ,snake_case__      ,cur_len=snake_case__ ) )

							# dist should be filtered to keep min num values so that sum is >= top_p
							# exp (-inf) => 0
							SCREAMING_SNAKE_CASE_   : str					  =      np.array([[0.3, 0.0, 0.0, 0.5], [0.0, 0.3, 0.3, 0.25]] )
							self.assertTrue(np.allclose(snake_case__      ,snake_case__      ,atol=1E-3 ) )

							# check edge cases with negative and extreme logits
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      np.broadcast_to(np.arange(snake_case__ )[None, :]      ,(batch_size, vocab_size) ).copy() - (
							    vocab_size // 2
							)

							# make ramp_logits more extreme
							SCREAMING_SNAKE_CASE_   : Any					  =      ramp_logits[1] * 100.0

							# make sure at least 2 tokens are kept
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      FlaxTopPLogitsWarper(0.9      ,min_tokens_to_keep=2      ,filter_value=0.0 )
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      top_p_warp(snake_case__      ,snake_case__      ,cur_len=snake_case__ )

							# first batch should keep three tokens, second batch would keep only 1, but due to `min_tokens_to_keep=2` keeps 2.
							self.assertListEqual((filtered_dist != 0.0).sum(axis=-1 ).tolist()      ,[3, 2] )






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : int					  =      20
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      4
							SCREAMING_SNAKE_CASE_   : str					  =      0

							SCREAMING_SNAKE_CASE_   : str					  =      FlaxMinLengthLogitsProcessor(min_length=10      ,eos_token_id=snake_case__ )

							# check that min length is applied at length 5
							SCREAMING_SNAKE_CASE_   : Dict					  =      ids_tensor((batch_size, 20)      ,vocab_size=20 )
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      5
							SCREAMING_SNAKE_CASE_   : List[str]					  =      self._get_uniform_logits(snake_case__      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : Any					  =      min_dist_processor(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
							self.assertListEqual(scores_before_min_length[:, eos_token_id].tolist()      ,4 * [-float('inf' )] )

							# check that min length is not applied anymore at length 15
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      self._get_uniform_logits(snake_case__      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : int					  =      15
							SCREAMING_SNAKE_CASE_   : Any					  =      min_dist_processor(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
							self.assertFalse(jnp.isinf(snake_case__ ).any() )






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : str					  =      20
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      4
							SCREAMING_SNAKE_CASE_   : Dict					  =      0

							SCREAMING_SNAKE_CASE_   : int					  =      FlaxForcedBOSTokenLogitsProcessor(bos_token_id=snake_case__ )

							# check that all scores are -inf except the bos_token_id score
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      ids_tensor((batch_size, 1)      ,vocab_size=20 )
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      1
							SCREAMING_SNAKE_CASE_   : str					  =      self._get_uniform_logits(snake_case__      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : Dict					  =      logits_processor(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
							self.assertTrue(jnp.isneginf(scores[:, bos_token_id + 1 :] ).all() )
							self.assertListEqual(scores[:, bos_token_id].tolist()      ,4 * [0] )  # score for bos_token_id shold be zero

							# check that bos_token_id is not forced if current length is greater than 1
							SCREAMING_SNAKE_CASE_   : Dict					  =      3
							SCREAMING_SNAKE_CASE_   : Dict					  =      self._get_uniform_logits(snake_case__      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      logits_processor(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
							self.assertFalse(jnp.isinf(snake_case__ ).any() )






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Dict					  =      20
							SCREAMING_SNAKE_CASE_   : Any					  =      4
							SCREAMING_SNAKE_CASE_   : int					  =      0
							SCREAMING_SNAKE_CASE_   : Tuple					  =      5

							SCREAMING_SNAKE_CASE_   : List[Any]					  =      FlaxForcedEOSTokenLogitsProcessor(max_length=snake_case__      ,eos_token_id=snake_case__ )

							# check that all scores are -inf except the eos_token_id when max_length is reached
							SCREAMING_SNAKE_CASE_   : Any					  =      ids_tensor((batch_size, 4)      ,vocab_size=20 )
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      4
							SCREAMING_SNAKE_CASE_   : Dict					  =      self._get_uniform_logits(snake_case__      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : Dict					  =      logits_processor(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
							self.assertTrue(jnp.isneginf(scores[:, eos_token_id + 1 :] ).all() )
							self.assertListEqual(scores[:, eos_token_id].tolist()      ,4 * [0] )  # score for eos_token_id should be zero

							# check that eos_token_id is not forced if max_length is not reached
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      3
							SCREAMING_SNAKE_CASE_   : int					  =      self._get_uniform_logits(snake_case__      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      logits_processor(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
							self.assertFalse(jnp.isinf(snake_case__ ).any() )






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Dict					  =      4
							SCREAMING_SNAKE_CASE_   : Dict					  =      10
							SCREAMING_SNAKE_CASE_   : int					  =      15
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      2
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      1
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      15

							# dummy input_ids and scores
							SCREAMING_SNAKE_CASE_   : Any					  =      ids_tensor((batch_size, sequence_length)      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : int					  =      input_ids.copy()

							SCREAMING_SNAKE_CASE_   : str					  =      self._get_uniform_logits(snake_case__      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : Dict					  =      scores.copy()

							# instantiate all dist processors
							SCREAMING_SNAKE_CASE_   : int					  =      FlaxTemperatureLogitsWarper(temperature=0.5 )
							SCREAMING_SNAKE_CASE_   : int					  =      FlaxTopKLogitsWarper(3 )
							SCREAMING_SNAKE_CASE_   : Dict					  =      FlaxTopPLogitsWarper(0.8 )

							# instantiate all logits processors
							SCREAMING_SNAKE_CASE_   : Any					  =      FlaxMinLengthLogitsProcessor(min_length=10      ,eos_token_id=snake_case__ )
							SCREAMING_SNAKE_CASE_   : Any					  =      FlaxForcedBOSTokenLogitsProcessor(bos_token_id=snake_case__ )
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      FlaxForcedEOSTokenLogitsProcessor(max_length=snake_case__      ,eos_token_id=snake_case__ )

							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      10

							# no processor list
							SCREAMING_SNAKE_CASE_   : Optional[int]					  =      temp_dist_warp(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
							SCREAMING_SNAKE_CASE_   : List[str]					  =      top_k_warp(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      top_p_warp(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
							SCREAMING_SNAKE_CASE_   : str					  =      min_dist_proc(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      bos_dist_proc(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
							SCREAMING_SNAKE_CASE_   : Tuple					  =      eos_dist_proc(snake_case__      ,snake_case__      ,cur_len=snake_case__ )

							# with processor list
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      FlaxLogitsProcessorList(
							    [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] )
							SCREAMING_SNAKE_CASE_   : Any					  =      processor(snake_case__      ,snake_case__      ,cur_len=snake_case__ )

							# scores should be equal
							self.assertTrue(jnp.allclose(snake_case__      ,snake_case__      ,atol=1E-3 ) )

							# input_ids should never be changed
							self.assertListEqual(input_ids.tolist()      ,input_ids_comp.tolist() )






				def 		snake_case	(			self ):
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      4
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      10
							SCREAMING_SNAKE_CASE_   : Dict					  =      15
							SCREAMING_SNAKE_CASE_   : Dict					  =      2
							SCREAMING_SNAKE_CASE_   : Tuple					  =      1
							SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      15

							# dummy input_ids and scores
							SCREAMING_SNAKE_CASE_   : List[Any]					  =      ids_tensor((batch_size, sequence_length)      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : int					  =      input_ids.copy()

							SCREAMING_SNAKE_CASE_   : List[Any]					  =      self._get_uniform_logits(snake_case__      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      scores.copy()

							# instantiate all dist processors
							SCREAMING_SNAKE_CASE_   : str					  =      FlaxTemperatureLogitsWarper(temperature=0.5 )
							SCREAMING_SNAKE_CASE_   : List[str]					  =      FlaxTopKLogitsWarper(3 )
							SCREAMING_SNAKE_CASE_   : int					  =      FlaxTopPLogitsWarper(0.8 )

							# instantiate all logits processors
							SCREAMING_SNAKE_CASE_   : Tuple					  =      FlaxMinLengthLogitsProcessor(min_length=10      ,eos_token_id=snake_case__ )
							SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      FlaxForcedBOSTokenLogitsProcessor(bos_token_id=snake_case__ )
							SCREAMING_SNAKE_CASE_   : str					  =      FlaxForcedEOSTokenLogitsProcessor(max_length=snake_case__      ,eos_token_id=snake_case__ )

							SCREAMING_SNAKE_CASE_   : List[str]					  =      10

							# no processor list
							def run_no_processor_list(snake_case__      ,snake_case__      ,snake_case__ ):
										SCREAMING_SNAKE_CASE_   : int					  =      temp_dist_warp(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
										SCREAMING_SNAKE_CASE_   : int					  =      top_k_warp(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
										SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      top_p_warp(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
										SCREAMING_SNAKE_CASE_   : Optional[Any]					  =      min_dist_proc(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
										SCREAMING_SNAKE_CASE_   : Union[str, Any]					  =      bos_dist_proc(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
										SCREAMING_SNAKE_CASE_   : Optional[int]					  =      eos_dist_proc(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
										return scores

							# with processor list
							def run_processor_list(snake_case__      ,snake_case__      ,snake_case__ ):
										SCREAMING_SNAKE_CASE_   : List[Any]					  =      FlaxLogitsProcessorList(
										    [temp_dist_warp, top_k_warp, top_p_warp, min_dist_proc, bos_dist_proc, eos_dist_proc] )
										SCREAMING_SNAKE_CASE_   : List[str]					  =      processor(snake_case__      ,snake_case__      ,cur_len=snake_case__ )
										return scores

							SCREAMING_SNAKE_CASE_   : Tuple					  =      jax.jit(snake_case__ )
							SCREAMING_SNAKE_CASE_   : Any					  =      jax.jit(snake_case__ )

							SCREAMING_SNAKE_CASE_   : int					  =      jitted_run_no_processor_list(snake_case__      ,snake_case__      ,snake_case__ )
							SCREAMING_SNAKE_CASE_   : List[str]					  =      jitted_run_processor_list(snake_case__      ,snake_case__      ,snake_case__ )

							# scores should be equal
							self.assertTrue(jnp.allclose(snake_case__      ,snake_case__      ,atol=1E-3 ) )

							# input_ids should never be changed
							self.assertListEqual(input_ids.tolist()      ,input_ids_comp.tolist() )



