



"""simple docstring"""




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        __lowercase     :

  def __init__(					self						:     Any   ,A						:     Any   ,A						:     Any=2   ,A						:     List[str]=8   ,A						:     List[str]=True   ,A						:     int=False   ,A						:     Tuple=False   ,A						:     Dict=False   ,A						:     List[str]=False   ,A						:     Optional[Any]=99   ,A						:     List[str]=32   ,A						:     Any=3   ,A						:     Dict=4   ,A						:     List[str]=37   ,A						:     List[str]=32   ,A						:     Union[str, Any]=128   ,A						:     Any=8   ,A						:     Union[str, Any]=8   ,A						:     Optional[int]=8   ,A						:     Optional[Any]=1.0   ,A						:     Any=True   ,):






   '''simple docstring'''
   UpperCAmelCase__   :					Union[str, Any]   				=   parent
   UpperCAmelCase__   :					Tuple   				=   batch_size
   UpperCAmelCase__   :					Dict   				=   seq_length
   UpperCAmelCase__   :					Optional[Any]   				=   is_training
   UpperCAmelCase__   :					Optional[int]   				=   use_token_type_ids
   UpperCAmelCase__   :					List[str]   				=   use_input_mask
   UpperCAmelCase__   :					str   				=   use_labels
   UpperCAmelCase__   :					Tuple   				=   use_mc_token_ids
   UpperCAmelCase__   :					Dict   				=   vocab_size
   UpperCAmelCase__   :					Any   				=   hidden_size
   UpperCAmelCase__   :					Union[str, Any]   				=   num_hidden_layers
   UpperCAmelCase__   :					List[Any]   				=   num_attention_heads
   UpperCAmelCase__   :					Union[str, Any]   				=   intermediate_size
   UpperCAmelCase__   :					Tuple   				=   num_buckets
   UpperCAmelCase__   :					Optional[Any]   				=   max_distance
   UpperCAmelCase__   :					List[Any]   				=   prompt_length
   UpperCAmelCase__   :					Optional[int]   				=   prompt_types
   UpperCAmelCase__   :					List[Any]   				=   segment_types
   UpperCAmelCase__   :					List[str]   				=   init_std
   UpperCAmelCase__   :					int   				=   return_dict

  def 				__lowercase    (					self						:     Optional[Any]      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Optional[Any]   				=   {}
   UpperCAmelCase__   :					Optional[int]   				=   ids_tensor([self.batch_size, self.seq_length]   ,self.vocab_size      ).type(torch.intaa      )
   UpperCAmelCase__   :					Any   				=   False

   UpperCAmelCase__   :					Any   				=   self.get_config()

   return (config, input_ids)

  def 				__lowercase    (					self						:     int      ):






   '''simple docstring'''
   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=A   ,init_std=self.init_std   ,return_dict=self.return_dict   ,)

  def 				__lowercase    (					self						:     Tuple   ,A						:     Dict   ,A						:     str   ,*A						:     Union[str, Any]      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Union[str, Any]   				=   CpmAntModel(config=A      )
   model.to(A      )
   model.eval()

   UpperCAmelCase__   :					Any   				=   model(**A      ).last_hidden_state

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

  def 				__lowercase    (					self						:     Tuple   ,A						:     Union[str, Any]   ,A						:     Any   ,*A						:     Tuple      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Dict   				=   CpmAntForCausalLM(A      )
   model.to(A      )
   UpperCAmelCase__   :					List[Any]   				=   input_ids['input_ids'].to(A      )
   model.eval()

   UpperCAmelCase__   :					Any   				=   model(**A      )
   self.parent.assertEqual(
       model_output.logits.shape   ,(self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)   ,)




  def 				__lowercase    (					self						:     Dict      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Optional[Any]   				=   self.prepare_config_and_inputs()
   return config, inputs_dict







@require_torch
class        __lowercase     (      snake_case__		,     snake_case__		,     unittest.TestCase   ):
  snake_case_			    =    (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
  snake_case_			    =    (
      {'feature-extraction': CpmAntModel, 'text-generation': CpmAntForCausalLM} if is_torch_available() else {}
  )

  snake_case_			    =    False
  snake_case_			    =    False
  snake_case_			    =    False
  snake_case_			    =    False
  snake_case_			    =    False

  def 				__lowercase    (					self						:     Dict      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Dict   				=   CpmAntModelTester(self      )
   UpperCAmelCase__   :					List[str]   				=   ConfigTester(self   ,config_class=A      )

  def 				__lowercase    (					self						:     int      ):






   '''simple docstring'''
   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 				__lowercase    (					self						:     Dict      ):






   '''simple docstring'''
   unittest.skip("""CPMAnt doesn\'t support input_embeds."""      )(self.test_inputs_embeds      )

  def 				__lowercase    (					self						:     int      ):






   '''simple docstring'''
   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 				__lowercase    (					self						:     List[Any]      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Union[str, Any]   				=   self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_cpmant_model(A   ,A      )




  def 				__lowercase    (					self						:     str      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Any   				=   self.model_tester.prepare_config_and_inputs()
   self.model_tester.create_and_check_lm_head_model(A   ,A      )







@require_torch
class        __lowercase     (      unittest.TestCase   ):

  @tooslow
  def 				__lowercase    (					self						:     List[str]      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Optional[int]   				=   '今天天气真好！'
   UpperCAmelCase__   :					List[str]   				=   'openbmb/cpm-ant-10b'
   UpperCAmelCase__   :					Optional[Any]   				=   CpmAntModel.from_pretrained(A      )
   UpperCAmelCase__   :					Optional[Any]   				=   CpmAntTokenizer.from_pretrained(A      )
   UpperCAmelCase__   :					List[Any]   				=   tokenizer(A   ,return_tensors="""pt"""      )
   UpperCAmelCase__   :					str   				=   model(**A      ).last_hidden_state

   UpperCAmelCase__   :					Union[str, Any]   				=   torch.tensor(
       [[[6.1_7_0_8, 5.9_2_4_4, 1.0_8_3_5], [6.5_2_0_7, 6.2_8_9_3, -1_1.3_3_2_4], [-1.0_1_0_7, -0.0_5_7_6, -5.9_5_7_7]]]   ,)
   self.assertTrue(torch.allclose(hidden_states[:, :3, :3]   ,A   ,atol=1e-2      )      )







@require_torch
class        __lowercase     (      unittest.TestCase   ):

  @tooslow
  def 				__lowercase    (					self						:     Tuple      ):






   '''simple docstring'''
   UpperCAmelCase__   :					Optional[int]   				=   '今天天气真好！'
   UpperCAmelCase__   :					Any   				=   'openbmb/cpm-ant-10b'
   UpperCAmelCase__   :					int   				=   CpmAntForCausalLM.from_pretrained(A      )
   UpperCAmelCase__   :					Optional[Any]   				=   CpmAntTokenizer.from_pretrained(A      )
   UpperCAmelCase__   :					Dict   				=   tokenizer(A   ,return_tensors="""pt"""      )
   UpperCAmelCase__   :					Dict   				=   model(**A      ).logits

   UpperCAmelCase__   :					List[Any]   				=   torch.tensor(
       [[[-6.4_2_6_7, -6.4_0_8_3, -6.3_9_5_8], [-5.8_8_0_2, -5.9_4_4_7, -5.7_8_1_1], [-5.3_8_9_6, -5.4_8_2_0, -5.4_2_9_5]]]   ,)
   self.assertTrue(torch.allclose(hidden_states[:, :3, :3]   ,A   ,atol=1e-2      )      )

  @tooslow
  def 				__lowercase    (					self						:     Tuple      ):






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




  @tooslow
  def 				__lowercase    (					self						:     Any      ):






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








"""simple docstring"""




from __future__ import annotations
def 					lowerCAmelCase  (   __UpperCamelCase							,      __UpperCamelCase							,      __UpperCamelCase							,      __UpperCamelCase ):
 '''simple docstring'''





 UpperCAmelCase__				, UpperCAmelCase__   :					Optional[Any]   				=   m - 1, 0
 while i >= 0 and j < n:
  if key == mat[i][j]:
   print(F"Key {key} found at row- {i + 1} column- {j + 1}" )
   return
  if key < mat[i][j]:
   i -= 1
  else:
   j += 1
 print(F"Key {key} not found" )
def 					lowerCAmelCase  (   ):
 '''simple docstring'''





 UpperCAmelCase__   :					Union[str, Any]   				=   [[2, 5, 7], [4, 8, 13], [9, 11, 15], [12, 17, 20]]
 UpperCAmelCase__   :					int   				=   int(input("""Enter the element to be searched:""" ) )
 print(__UpperCamelCase )
 search_in_a_sorted_matrix(__UpperCamelCase							,      len(__UpperCamelCase )							,      len(mat[0] )							,      __UpperCamelCase )


if __name__ == "__main__":
      import doctest

      doctest.testmod()
      main()





