



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






   '''simple docstring'''






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


   def       A__    (    self:     Any							)	->      str:
       UpperCAmelCase_     :       str  		=					{}
       UpperCAmelCase_     :       Union[str, Any]  		=					ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size							).type(torch.intaa							)
       UpperCAmelCase_     :       Optional[int]  		=					False

       UpperCAmelCase_     :       Dict  		=					self.get_config()

       return (config, input_ids)


   def       A__    (    self:     int							)	->      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=lowerCamelCase_  ,init_std=self.init_std  ,return_dict=self.return_dict  ,)


   def       A__    (    self:     Union[str, Any]  ,lowerCamelCase_:     Dict  ,lowerCamelCase_:     Any  ,*lowerCamelCase_:     Optional[int]							)	->      List[str]:
       UpperCAmelCase_     :       Optional[Any]  		=					CpmAntModel(config=lowerCamelCase_							)
       model.to(lowerCamelCase_							)
       model.eval()

       UpperCAmelCase_     :       int  		=					model(**lowerCamelCase_							).last_hidden_state

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


   def       A__    (    self:     Optional[Any]  ,lowerCamelCase_:     List[Any]  ,lowerCamelCase_:     Optional[int]  ,*lowerCamelCase_:     List[Any]							)	->      str:
       UpperCAmelCase_     :       str  		=					CpmAntForCausalLM(lowerCamelCase_							)
       model.to(lowerCamelCase_							)
       UpperCAmelCase_     :       List[Any]  		=					input_ids["""input_ids"""].to(lowerCamelCase_							)
       model.eval()

       UpperCAmelCase_     :       str  		=					model(**lowerCamelCase_							)
       self.parent.assertEqual(
           model_output.logits.shape  ,(self.batch_size, self.seq_length, config.vocab_size + config.prompt_types * config.prompt_length)  ,)






   def       A__    (    self:     Dict							)	->      Union[str, Any]:
       UpperCAmelCase_		, UpperCAmelCase_     :       int  		=					self.prepare_config_and_inputs()
       return config, inputs_dict







@require_torch
class       _snake_case				(     __snake_case ,							__snake_case ,							unittest.TestCase ):






   '''simple docstring'''






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

   A__      : int       =						False
   A__      : Any       =						False
   A__      : Tuple       =						False
   A__      : Tuple       =						False
   A__      : List[Any]       =						False


   def       A__    (    self:     int							)	->      Union[str, Any]:
       UpperCAmelCase_     :       Union[str, Any]  		=					CpmAntModelTester(self							)
       UpperCAmelCase_     :       List[str]  		=					ConfigTester(self  ,config_class=lowerCamelCase_							)


   def       A__    (    self:     Dict							)	->      Dict:
       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       A__    (    self:     str							)	->      int:
       unittest.skip("""CPMAnt doesn't support input_embeds."""							)(self.test_inputs_embeds							)


   def       A__    (    self:     Optional[int]							)	->      List[str]:
       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       A__    (    self:     Union[str, Any]							)	->      List[Any]:
       UpperCAmelCase_		, UpperCAmelCase_     :       List[str]  		=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_cpmant_model(lowerCamelCase_  ,lowerCamelCase_							)






   def       A__    (    self:     int							)	->      int:
       UpperCAmelCase_		, UpperCAmelCase_     :       Optional[Any]  		=					self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_lm_head_model(lowerCamelCase_  ,lowerCamelCase_							)







@require_torch
class       _snake_case				(     unittest.TestCase ):






   '''simple docstring'''






   @tooslow
   def       A__    (    self:     List[str]							)	->      Tuple:
       UpperCAmelCase_     :       str  		=					"""今天天气真好！"""
       UpperCAmelCase_     :       Dict  		=					"""openbmb/cpm-ant-10b"""
       UpperCAmelCase_     :       Dict  		=					CpmAntModel.from_pretrained(lowerCamelCase_							)
       UpperCAmelCase_     :       int  		=					CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
       UpperCAmelCase_     :       str  		=					tokenizer(lowerCamelCase_  ,return_tensors="""pt"""							)
       UpperCAmelCase_     :       Tuple  		=					model(**lowerCamelCase_							).last_hidden_state

       UpperCAmelCase_     :       List[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]  ,lowerCamelCase_  ,atol=1e-2							)							)







@require_torch
class       _snake_case				(     unittest.TestCase ):






   '''simple docstring'''






   @tooslow
   def       A__    (    self:     List[str]							)	->      Any:
       UpperCAmelCase_     :       Optional[Any]  		=					"""今天天气真好！"""
       UpperCAmelCase_     :       Optional[int]  		=					"""openbmb/cpm-ant-10b"""
       UpperCAmelCase_     :       Union[str, Any]  		=					CpmAntForCausalLM.from_pretrained(lowerCamelCase_							)
       UpperCAmelCase_     :       Any  		=					CpmAntTokenizer.from_pretrained(lowerCamelCase_							)
       UpperCAmelCase_     :       str  		=					tokenizer(lowerCamelCase_  ,return_tensors="""pt"""							)
       UpperCAmelCase_     :       Dict  		=					model(**lowerCamelCase_							).logits

       UpperCAmelCase_     :       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]  ,lowerCamelCase_  ,atol=1e-2							)							)


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






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








from __future__ import annotations

from PIL import Image

# Define the first generation of cells
# fmt: off
a 			=							[[0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1,
          0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]]
def    a__    (	A       ):
   return [int(A       ) for c in F'''{ruleset:08}'''[:8]]
def    a__    (	A		,					A		,					A       ):
   lowercase__    :						List[Any]       =			len(cells[0]       )  # 31
   lowercase__    :						int       =			[]
   for i in range(A       ):
      # Get the neighbors of each cell
      # Handle neighbours outside bounds by using 0 as their value
      lowercase__    :						Optional[Any]       =			0 if i == 0 else cells[time][i - 1]
      lowercase__    :						Dict       =			0 if i == population - 1 else cells[time][i + 1]
      # Define a new cell and add it to the new generation
      lowercase__    :						str       =			7 - int(F'''{left_neighbor}{cells[time][i]}{right_neighbor}'''		,					2       )
      next_generation.append(rule[situation]       )
   return next_generation
def    a__    (	A       ):
   lowercase__    :						Tuple       =			Image.new("""RGB"""		,					(len(cells[0]       ), len(A       ))       )
   lowercase__    :						Optional[Any]       =			img.load()
   # Generates image
   for w in range(img.width       ):
      for h in range(img.height       ):
         lowercase__    :						Union[str, Any]       =			255 - int(255 * cells[h][w]       )
         lowercase__    :						int       =			(color, color, color)
   return img


if __name__ == "__main__":
 a 			=							bin(int(input('Rule:\n').strip()))[2:]
 a 			=							format_ruleset(int(rule_num))
 for time in range(16):
  CELLS.append(new_generation(CELLS, rule, time))
 a 			=							generate_image(CELLS)
 # Uncomment to save the image
 # img.save(f"rule_{rule_num}.png")
 img.show()


