




import inspect
import unittest

from transformers import ResNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


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

 from transformers.models.resnet.modeling_flax_resnet import FlaxResNetForImageClassification, FlaxResNetModel

if is_vision_available():
 from PIL import Image

 from transformers import AutoImageProcessor

class 	_lowerCamelCase  (     unittest.TestCase				):

      """simple docstring"""

      def __init__(       self   :	Tuple	,    snake_case   :	Dict	,    snake_case   :	Any=3	,    snake_case   :	List[Any]=32	,    snake_case   :	Any=3	,    snake_case   :	Optional[Any]=10	,    snake_case   :	int=[10, 20, 30, 40]	,    snake_case   :	Tuple=[1, 1, 2, 1]	,    snake_case   :	Dict=True	,    snake_case   :	Optional[int]=True	,    snake_case   :	Tuple="relu"	,    snake_case   :	List[Any]=3	,    snake_case   :	Tuple=None	,    ):
          __UpperCamelCase											=  parent
          __UpperCamelCase											=  batch_size
          __UpperCamelCase											=  image_size
          __UpperCamelCase											=  num_channels
          __UpperCamelCase											=  embeddings_size
          __UpperCamelCase											=  hidden_sizes
          __UpperCamelCase											=  depths
          __UpperCamelCase											=  is_training
          __UpperCamelCase											=  use_labels
          __UpperCamelCase											=  hidden_act
          __UpperCamelCase											=  num_labels
          __UpperCamelCase											=  scope
          __UpperCamelCase											=  len(snake_case			)







      def  snake_case		(       self   :	List[Any]			):
          __UpperCamelCase											=  floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]			)

          __UpperCamelCase											=  self.get_config()

          return config, pixel_values







      def  snake_case		(       self   :	Any			):
          return ResNetConfig(
              num_channels=self.num_channels	,    embeddings_size=self.embeddings_size	,    hidden_sizes=self.hidden_sizes	,    depths=self.depths	,    hidden_act=self.hidden_act	,    num_labels=self.num_labels	,    image_size=self.image_size	,    )







      def  snake_case		(       self   :	Any	,    snake_case   :	Any	,    snake_case   :	Dict			):
          __UpperCamelCase											=  FlaxResNetModel(config=snake_case			)
          __UpperCamelCase											=  model(snake_case			)

          # Output shape (b, c, h, w)
          self.parent.assertEqual(
              result.last_hidden_state.shape	,    (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)	,    )







      def  snake_case		(       self   :	List[str]	,    snake_case   :	Optional[int]	,    snake_case   :	str			):
          __UpperCamelCase											=  self.num_labels
          __UpperCamelCase											=  FlaxResNetForImageClassification(config=snake_case			)
          __UpperCamelCase											=  model(snake_case			)
          self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.num_labels)			)







      def  snake_case		(       self   :	Any			):
          __UpperCamelCase											=  self.prepare_config_and_inputs()
          __UpperCamelCase						, __UpperCamelCase											=  config_and_inputs
          __UpperCamelCase											=  {'''pixel_values''': pixel_values}
          return config, inputs_dict







@require_flax
class 	_lowerCamelCase  (     _SCREAMING_SNAKE_CASE							,     unittest.TestCase				):

      """simple docstring"""

      lowerCAmelCase__     :	Optional[Any]      =   (FlaxResNetModel, FlaxResNetForImageClassification) if is_flax_available() else ()

      lowerCAmelCase__     :	List[Any]      =   False
      lowerCAmelCase__     :	Any      =   False
      lowerCAmelCase__     :	Tuple      =   False







      def  snake_case		(       self   :	str			):
          __UpperCamelCase											=  FlaxResNetModelTester(self			)
          __UpperCamelCase											=  ConfigTester(self	,    config_class=snake_case	,    has_text_modality=snake_case			)







      def  snake_case		(       self   :	Union[str, Any]			):
          self.create_and_test_config_common_properties()
          self.config_tester.create_and_test_config_to_json_string()
          self.config_tester.create_and_test_config_to_json_file()
          self.config_tester.create_and_test_config_from_and_save_pretrained()
          self.config_tester.create_and_test_config_with_num_labels()
          self.config_tester.check_config_can_be_init_without_params()
          self.config_tester.check_config_arguments_init()







      def  snake_case		(       self   :	List[Any]			):
          return







      def  snake_case		(       self   :	Union[str, Any]			):
          __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*snake_case			)







      def  snake_case		(       self   :	Union[str, Any]			):
          __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_for_image_classification(*snake_case			)







      @unittest.skip(reason='''ResNet does not use inputs_embeds'''			)
      def  snake_case		(       self   :	Tuple			):
          pass







      @unittest.skip(reason='''ResNet does not support input and output embeddings'''			)
      def  snake_case		(       self   :	List[str]			):
          pass







      def  snake_case		(       self   :	int			):
          __UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              __UpperCamelCase											=  model_class(snake_case			)
              __UpperCamelCase											=  inspect.signature(model.__call__			)
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              __UpperCamelCase											=  [*signature.parameters.keys()]

              __UpperCamelCase											=  ['''pixel_values''']
              self.assertListEqual(arg_names[:1]	,    snake_case			)







      def  snake_case		(       self   :	Any			):
          def check_hidden_states_output(snake_case   :	Dict	,    snake_case   :	Any	,    snake_case   :	Dict			):
              __UpperCamelCase											=  model_class(snake_case			)

              __UpperCamelCase											=  model(**self._prepare_for_class(snake_case	,    snake_case			)			)

              __UpperCamelCase											=  outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

              __UpperCamelCase											=  self.model_tester.num_stages
              self.assertEqual(len(snake_case			)	,    expected_num_stages + 1			)







      @unittest.skip(reason='''ResNet does not use feedforward chunking'''			)
      def  snake_case		(       self   :	Optional[int]			):
          pass







      def  snake_case		(       self   :	int			):
          __UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
              with self.subTest(model_class.__name__			):
                  __UpperCamelCase											=  self._prepare_for_class(snake_case	,    snake_case			)
                  __UpperCamelCase											=  model_class(snake_case			)

                  @jax.jit
                  def model_jitted(snake_case   :	int	,    **snake_case   :	Dict			):
                      return model(pixel_values=snake_case	,    **snake_case			)

                  with self.subTest('''JIT Enabled'''			):
                      __UpperCamelCase											=  model_jitted(**snake_case			).to_tuple()

                  with self.subTest('''JIT Disabled'''			):
                      with jax.disable_jit():
                          __UpperCamelCase											=  model_jitted(**snake_case			).to_tuple()

                  self.assertEqual(len(snake_case			)	,    len(snake_case			)			)
                  for jitted_output, output in zip(snake_case	,    snake_case			):
                      self.assertEqual(jitted_output.shape	,    output.shape			)







def    __SCREAMING_SNAKE_CASE				(					)    ->		Any:

    """simple docstring"""


    __UpperCamelCase											=  Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png''' )
    return image


@require_flax
class 	_lowerCamelCase  (     unittest.TestCase				):

      """simple docstring"""

      @cached_property
      def  snake_case		(       self   :	Any			):
          return AutoImageProcessor.from_pretrained('''microsoft/resnet-50'''			) if is_vision_available() else None







      @slow
      def  snake_case		(       self   :	Any			):
          __UpperCamelCase											=  FlaxResNetForImageClassification.from_pretrained('''microsoft/resnet-50'''			)

          __UpperCamelCase											=  self.default_image_processor
          __UpperCamelCase											=  prepare_img()
          __UpperCamelCase											=  image_processor(images=snake_case	,    return_tensors='''np'''			)

          __UpperCamelCase											=  model(**snake_case			)

          # verify the logits
          __UpperCamelCase											=  (1, 1000)
          self.assertEqual(outputs.logits.shape	,    snake_case			)

          __UpperCamelCase											=  jnp.array([-11.10_69, -9.78_77, -8.37_77]			)

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












import unittest

from transformers.testing_utils import is_torch_available, require_torch, tooslow

from ...generation.test_utils import torch_device
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     CpmAntConfig,
     CpmAntForCausalLM,
     CpmAntModel,
     CpmAntTokenizer,
 )

@require_torch
class 	_lowerCamelCase  :

      """simple docstring"""

      def __init__(       self   :	str	,    snake_case   :	str	,    snake_case   :	List[str]=2	,    snake_case   :	Optional[Any]=8	,    snake_case   :	List[Any]=True	,    snake_case   :	Optional[Any]=False	,    snake_case   :	str=False	,    snake_case   :	Tuple=False	,    snake_case   :	str=False	,    snake_case   :	int=99	,    snake_case   :	Tuple=32	,    snake_case   :	Union[str, Any]=3	,    snake_case   :	str=4	,    snake_case   :	List[Any]=37	,    snake_case   :	Tuple=32	,    snake_case   :	Tuple=128	,    snake_case   :	Optional[int]=8	,    snake_case   :	Optional[Any]=8	,    snake_case   :	List[Any]=8	,    snake_case   :	List[str]=1.0	,    snake_case   :	List[Any]=True	,    ):
          __UpperCamelCase											=  parent
          __UpperCamelCase											=  batch_size
          __UpperCamelCase											=  seq_length
          __UpperCamelCase											=  is_training
          __UpperCamelCase											=  use_token_type_ids
          __UpperCamelCase											=  use_input_mask
          __UpperCamelCase											=  use_labels
          __UpperCamelCase											=  use_mc_token_ids
          __UpperCamelCase											=  vocab_size
          __UpperCamelCase											=  hidden_size
          __UpperCamelCase											=  num_hidden_layers
          __UpperCamelCase											=  num_attention_heads
          __UpperCamelCase											=  intermediate_size
          __UpperCamelCase											=  num_buckets
          __UpperCamelCase											=  max_distance
          __UpperCamelCase											=  prompt_length
          __UpperCamelCase											=  prompt_types
          __UpperCamelCase											=  segment_types
          __UpperCamelCase											=  init_std
          __UpperCamelCase											=  return_dict







      def  snake_case		(       self   :	Tuple			):
          __UpperCamelCase											=  {}
          __UpperCamelCase											=  ids_tensor([self.batch_size, self.seq_length]	,    self.vocab_size			).type(torch.intaa			)
          __UpperCamelCase											=  False

          __UpperCamelCase											=  self.get_config()

          return (config, input_ids)







      def  snake_case		(       self   :	List[str]			):
          return CpmAntConfig(
              vocab_size=self.vocab_size	,    hidden_size=self.hidden_size	,    num_hidden_layers=self.num_hidden_layers	,    num_attention_heads=self.num_attention_heads	,    dim_ff=self.intermediate_size	,    position_bias_num_buckets=self.num_buckets	,    position_bias_max_distance=self.max_distance	,    prompt_types=self.prompt_types	,    prompt_length=self.prompt_length	,    segment_types=self.segment_types	,    use_cache=snake_case	,    init_std=self.init_std	,    return_dict=self.return_dict	,    )







      def  snake_case		(       self   :	Union[str, Any]	,    snake_case   :	Tuple	,    snake_case   :	List[str]	,    *snake_case   :	str			):
          __UpperCamelCase											=  CpmAntModel(config=snake_case			)
          model.to(snake_case			)
          model.eval()

          __UpperCamelCase											=  model(**snake_case			).last_hidden_state

          self.parent.assertEqual(hidden_states.shape	,    (self.batch_size, self.seq_length, config.hidden_size)			)







      def  snake_case		(       self   :	List[str]	,    snake_case   :	Optional[int]	,    snake_case   :	List[str]	,    *snake_case   :	Any			):
          __UpperCamelCase											=  CpmAntForCausalLM(snake_case			)
          model.to(snake_case			)
          __UpperCamelCase											=  input_ids['''input_ids'''].to(snake_case			)
          model.eval()

          __UpperCamelCase											=  model(**snake_case			)
          self.parent.assertEqual(
              model_output.logits.shape	,    (self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)	,    )







      def  snake_case		(       self   :	List[str]			):
          __UpperCamelCase						, __UpperCamelCase											=  self.prepare_config_and_inputs()
          return config, inputs_dict







@require_torch
class 	_lowerCamelCase  (     _SCREAMING_SNAKE_CASE							,     _SCREAMING_SNAKE_CASE							,     unittest.TestCase				):

      """simple docstring"""

      lowerCAmelCase__     :	List[Any]      =   (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
      lowerCAmelCase__     :	Union[str, Any]      =   (
          {"feature-extraction": CpmAntModel, "text-generation": CpmAntForCausalLM} if is_torch_available() else {}
      )

      lowerCAmelCase__     :	Optional[int]      =   False
      lowerCAmelCase__     :	Union[str, Any]      =   False
      lowerCAmelCase__     :	Optional[int]      =   False
      lowerCAmelCase__     :	Tuple      =   False
      lowerCAmelCase__     :	Union[str, Any]      =   False







      def  snake_case		(       self   :	Any			):
          __UpperCamelCase											=  CpmAntModelTester(self			)
          __UpperCamelCase											=  ConfigTester(self	,    config_class=snake_case			)







      def  snake_case		(       self   :	Optional[Any]			):
          self.config_tester.create_and_test_config_common_properties()
          self.config_tester.create_and_test_config_to_json_string()
          self.config_tester.create_and_test_config_to_json_file()
          self.config_tester.create_and_test_config_from_and_save_pretrained()
          self.config_tester.check_config_can_be_init_without_params()
          self.config_tester.check_config_arguments_init()







      def  snake_case		(       self   :	List[str]			):
          unittest.skip('''CPMAnt doesn\'t support input_embeds.'''			)(self.test_inputs_embeds			)







      def  snake_case		(       self   :	int			):
          unittest.skip(
              '''CPMAnt doesn\'t support retain grad in hidden_states or attentions, because prompt management will peel off the output.hidden_states from graph.\
                 So is attentions. We strongly recommand you use loss to tune model.'''			)(self.test_retain_grad_hidden_states_attentions			)







      def  snake_case		(       self   :	Union[str, Any]			):
          __UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_cpmant_model(snake_case	,    snake_case			)







      def  snake_case		(       self   :	Any			):
          __UpperCamelCase						, __UpperCamelCase											=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(snake_case	,    snake_case			)







@require_torch
class 	_lowerCamelCase  (     unittest.TestCase				):

      """simple docstring"""

      @tooslow
      def  snake_case		(       self   :	Dict			):
          __UpperCamelCase											=  '''今天天气真好！'''
          __UpperCamelCase											=  '''openbmb/cpm-ant-10b'''
          __UpperCamelCase											=  CpmAntModel.from_pretrained(snake_case			)
          __UpperCamelCase											=  CpmAntTokenizer.from_pretrained(snake_case			)
          __UpperCamelCase											=  tokenizer(snake_case	,    return_tensors='''pt'''			)
          __UpperCamelCase											=  model(**snake_case			).last_hidden_state

          __UpperCamelCase											=  torch.tensor(
              [[[6.17_08, 5.92_44, 1.08_35], [6.52_07, 6.28_93, -11.33_24], [-1.01_07, -0.05_76, -5.95_77]]]	,    )
          self.assertTrue(torch.allclose(hidden_states[:, :3, :3]	,    snake_case	,    atol=1E-2			)			)







@require_torch
class 	_lowerCamelCase  (     unittest.TestCase				):

      """simple docstring"""

      @tooslow
      def  snake_case		(       self   :	Optional[int]			):
          __UpperCamelCase											=  '''今天天气真好！'''
          __UpperCamelCase											=  '''openbmb/cpm-ant-10b'''
          __UpperCamelCase											=  CpmAntForCausalLM.from_pretrained(snake_case			)
          __UpperCamelCase											=  CpmAntTokenizer.from_pretrained(snake_case			)
          __UpperCamelCase											=  tokenizer(snake_case	,    return_tensors='''pt'''			)
          __UpperCamelCase											=  model(**snake_case			).logits

          __UpperCamelCase											=  torch.tensor(
              [[[-6.42_67, -6.40_83, -6.39_58], [-5.88_02, -5.94_47, -5.78_11], [-5.38_96, -5.48_20, -5.42_95]]]	,    )
          self.assertTrue(torch.allclose(hidden_states[:, :3, :3]	,    snake_case	,    atol=1E-2			)			)







      @tooslow
      def  snake_case		(       self   :	List[str]			):
          __UpperCamelCase											=  '''openbmb/cpm-ant-10b'''
          __UpperCamelCase											=  CpmAntForCausalLM.from_pretrained(snake_case			)
          __UpperCamelCase											=  CpmAntTokenizer.from_pretrained(snake_case			)
          __UpperCamelCase											=  '''今天天气不错，'''
          __UpperCamelCase											=  '''今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的'''
          __UpperCamelCase											=  tokenizer(snake_case	,    return_tensors='''pt'''			)
          __UpperCamelCase											=  model.generate(**snake_case			)
          __UpperCamelCase											=  tokenizer.batch_decode(snake_case			)
          self.assertEqual(snake_case	,    snake_case			)







      @tooslow
      def  snake_case		(       self   :	Any			):
          __UpperCamelCase											=  '''openbmb/cpm-ant-10b'''
          __UpperCamelCase											=  CpmAntForCausalLM.from_pretrained(snake_case			)
          __UpperCamelCase											=  CpmAntTokenizer.from_pretrained(snake_case			)
          __UpperCamelCase											=  ['''今天天气不错，''', '''新年快乐，万事如意！''']
          __UpperCamelCase											=  [
              '''今天天气不错，阳光明媚，我和妈妈一起去超市买东西。\n在超市里，我看到了一个很好玩的玩具，它的名字叫“机器人”。它有一个圆圆的脑袋，两只圆圆的眼睛，还有一个圆圆的''',
              '''新年快乐，万事如意！在这辞旧迎新的美好时刻，我谨代表《农村新技术》杂志社全体同仁，向一直以来关心、支持《农村新技术》杂志发展的各级领导、各界朋友和广大读者致以最诚挚的''',
          ]
          __UpperCamelCase											=  tokenizer(snake_case	,    return_tensors='''pt'''	,    padding=snake_case			)
          __UpperCamelCase											=  model.generate(**snake_case			)
          __UpperCamelCase											=  tokenizer.batch_decode(snake_case			)
          self.assertEqual(snake_case	,    snake_case			)






