

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


     def __init__(				self     :						str     ,    UpperCAmelCase     :						Union[str, Any]     ,    UpperCAmelCase     :						Optional[Any]=2     ,    UpperCAmelCase     :						Tuple=8     ,    UpperCAmelCase     :						str=True     ,    UpperCAmelCase     :						List[str]=False     ,    UpperCAmelCase     :						Any=False     ,    UpperCAmelCase     :						Tuple=False     ,    UpperCAmelCase     :						List[Any]=False     ,    UpperCAmelCase     :						List[str]=9_9     ,    UpperCAmelCase     :						Union[str, Any]=3_2     ,    UpperCAmelCase     :						Optional[Any]=3     ,    UpperCAmelCase     :						str=4     ,    UpperCAmelCase     :						Optional[int]=3_7     ,    UpperCAmelCase     :						List[str]=3_2     ,    UpperCAmelCase     :						Union[str, Any]=1_2_8     ,    UpperCAmelCase     :						Optional[int]=8     ,    UpperCAmelCase     :						int=8     ,    UpperCAmelCase     :						Union[str, Any]=8     ,    UpperCAmelCase     :						Union[str, Any]=1.0     ,    UpperCAmelCase     :						Dict=True     ,    )				->      Tuple:
      __lowerCAmelCase:  Any        = parent
      __lowerCAmelCase:  List[str]        = batch_size
      __lowerCAmelCase:  Any        = seq_length
      __lowerCAmelCase:  Tuple        = is_training
      __lowerCAmelCase:  str        = use_token_type_ids
      __lowerCAmelCase:  Any        = use_input_mask
      __lowerCAmelCase:  str        = use_labels
      __lowerCAmelCase:  Any        = use_mc_token_ids
      __lowerCAmelCase:  str        = vocab_size
      __lowerCAmelCase:  Tuple        = hidden_size
      __lowerCAmelCase:  Tuple        = num_hidden_layers
      __lowerCAmelCase:  int        = num_attention_heads
      __lowerCAmelCase:  Tuple        = intermediate_size
      __lowerCAmelCase:  Tuple        = num_buckets
      __lowerCAmelCase:  Union[str, Any]        = max_distance
      __lowerCAmelCase:  Dict        = prompt_length
      __lowerCAmelCase:  Optional[int]        = prompt_types
      __lowerCAmelCase:  Any        = segment_types
      __lowerCAmelCase:  Optional[Any]        = init_std
      __lowerCAmelCase:  List[str]        = return_dict


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

      __lowerCAmelCase:  Optional[Any]        = self.get_config()

      return (config, input_ids)


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


     def     UpperCAmelCase    (				self     :						Any     ,    UpperCAmelCase     :						Tuple     ,    UpperCAmelCase     :						Tuple     ,    *UpperCAmelCase     :						List[Any]   )				->      int:
      __lowerCAmelCase:  Any        = CpmAntModel(config=UpperCAmelCase   )
      model.to(UpperCAmelCase   )
      model.eval()

      __lowerCAmelCase:  List[Any]        = model(**UpperCAmelCase   ).last_hidden_state

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


     def     UpperCAmelCase    (				self     :						str     ,    UpperCAmelCase     :						Optional[Any]     ,    UpperCAmelCase     :						Dict     ,    *UpperCAmelCase     :						Optional[int]   )				->      Tuple:
      __lowerCAmelCase:  Tuple        = CpmAntForCausalLM(UpperCAmelCase   )
      model.to(UpperCAmelCase   )
      __lowerCAmelCase:  int        = input_ids['input_ids'].to(UpperCAmelCase   )
      model.eval()

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


     def     UpperCAmelCase    (				self     :						Tuple   )				->      List[str]:
      __lowerCAmelCase      ,				__lowerCAmelCase:  Any        = self.prepare_config_and_inputs()
      return config, inputs_dict







@require_torch
class 				A_					(						snake_case__				,      snake_case__				,      unittest.TestCase   ):
     _lowercase     :	List[str]        =      (CpmAntModel, CpmAntForCausalLM) if is_torch_available() else ()
     _lowercase     :	Optional[int]        =      (
         {'feature-extraction': CpmAntModel, 'text-generation': CpmAntForCausalLM} if is_torch_available() else {}
     )

     _lowercase     :	Optional[int]        =      False
     _lowercase     :	Tuple        =      False
     _lowercase     :	Dict        =      False
     _lowercase     :	str        =      False
     _lowercase     :	str        =      False


     def     UpperCAmelCase    (				self     :						str   )				->      Union[str, Any]:
      __lowerCAmelCase:  str        = CpmAntModelTester(self   )
      __lowerCAmelCase:  str        = ConfigTester(self     ,    config_class=UpperCAmelCase   )


     def     UpperCAmelCase    (				self     :						Any   )				->      List[str]:
      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     UpperCAmelCase    (				self     :						str   )				->      Dict:
      unittest.skip('CPMAnt doesn\'t support input_embeds.'   )(self.test_inputs_embeds   )


     def     UpperCAmelCase    (				self     :						str   )				->      Tuple:
      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     UpperCAmelCase    (				self     :						List[str]   )				->      List[str]:
      __lowerCAmelCase      ,				__lowerCAmelCase:  Optional[Any]        = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_cpmant_model(UpperCAmelCase     ,    UpperCAmelCase   )


     def     UpperCAmelCase    (				self     :						Optional[Any]   )				->      int:
      __lowerCAmelCase      ,				__lowerCAmelCase:  Optional[Any]        = self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_lm_head_model(UpperCAmelCase     ,    UpperCAmelCase   )







@require_torch
class 				A_					(						unittest.TestCase   ):


     @tooslow
     def     UpperCAmelCase    (				self     :						Dict   )				->      Any:
      __lowerCAmelCase:  Optional[Any]        = '今天天气真好！'
      __lowerCAmelCase:  Any        = 'openbmb/cpm-ant-10b'
      __lowerCAmelCase:  Dict        = CpmAntModel.from_pretrained(UpperCAmelCase   )
      __lowerCAmelCase:  Tuple        = CpmAntTokenizer.from_pretrained(UpperCAmelCase   )
      __lowerCAmelCase:  List[Any]        = tokenizer(UpperCAmelCase     ,    return_tensors='pt'   )
      __lowerCAmelCase:  int        = model(**UpperCAmelCase   ).last_hidden_state

      __lowerCAmelCase:  Dict        = torch.tensor(
          [[[6.1708, 5.9244, 1.0835], [6.5207, 6.2893, -11.3324], [-1.0107, -0.0576, -5.9577]]]     ,    )
      self.assertTrue(torch.allclose(hidden_states[:, :3, :3]     ,    UpperCAmelCase     ,    atol=1E-2   )   )







@require_torch
class 				A_					(						unittest.TestCase   ):


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

      __lowerCAmelCase:  int        = torch.tensor(
          [[[-6.4267, -6.4083, -6.3958], [-5.8802, -5.9447, -5.7811], [-5.3896, -5.4820, -5.4295]]]     ,    )
      self.assertTrue(torch.allclose(hidden_states[:, :3, :3]     ,    UpperCAmelCase     ,    atol=1E-2   )   )


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


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








from typing import SupportsIndex

import numpy as np
from scipy.ndimage import convolve







def _a					(			SCREAMING_SNAKE_CASE						:     np.ndarray       ,   SCREAMING_SNAKE_CASE						:     np.ndarray       ,   SCREAMING_SNAKE_CASE						:     np.ndarray						)       ->       np.ndarray:

 """simple docstring"""






 __lowerCAmelCase:  Any        = np.stack((horizontal_flow, vertical_flow)       ,   2						)

 # Create a grid of all pixel coordinates and subtract the flow to get the
 # target pixels coordinates
 __lowerCAmelCase:  List[str]        = np.stack(
     np.meshgrid(np.arange(0       ,   image.shape[1]						)       ,   np.arange(0       ,   image.shape[0]						)						)       ,   2						)
 __lowerCAmelCase:  int        = np.round(grid - flow						).astype(np.intaa						)

 # Find the locations outside of the original image
 __lowerCAmelCase:  Dict        = (grid < 0) | (grid >= np.array([image.shape[1], image.shape[0]]						))
 __lowerCAmelCase:  str        = 0

 __lowerCAmelCase:  List[str]        = image[grid[:, :, 1], grid[:, :, 0]]

 # Set pixels at invalid locations to 0
 __lowerCAmelCase:  List[str]        = 0

 return warped







def _a					(			SCREAMING_SNAKE_CASE						:     np.ndarray       ,   SCREAMING_SNAKE_CASE						:     np.ndarray       ,   SCREAMING_SNAKE_CASE						:     SupportsIndex       ,   SCREAMING_SNAKE_CASE						:     float | None = None       ,   )       ->       tuple[np.ndarray, np.ndarray]:

 """simple docstring"""






 if alpha is None:
  __lowerCAmelCase:  Tuple        = 0.1

 # Initialize flow
 __lowerCAmelCase:  Optional[int]        = np.zeros_like(SCREAMING_SNAKE_CASE						)
 __lowerCAmelCase:  Dict        = np.zeros_like(SCREAMING_SNAKE_CASE						)

 # Prepare kernels for the calculation of the derivatives and the average velocity
 __lowerCAmelCase:  List[Any]        = np.array([[-1, 1], [-1, 1]]						) * 0.2_5
 __lowerCAmelCase:  int        = np.array([[-1, -1], [1, 1]]						) * 0.2_5
 __lowerCAmelCase:  Tuple        = np.array([[1, 1], [1, 1]]						) * 0.2_5
 __lowerCAmelCase:  Dict        = np.array(
     [[1 / 12, 1 / 6, 1 / 12], [1 / 6, 0, 1 / 6], [1 / 12, 1 / 6, 1 / 12]]						)

 # Iteratively refine the flow
 for _ in range(SCREAMING_SNAKE_CASE						):
  __lowerCAmelCase:  Optional[int]        = warp(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						)
  __lowerCAmelCase:  List[str]        = convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						) + convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						)
  __lowerCAmelCase:  int        = convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						) + convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						)
  __lowerCAmelCase:  List[Any]        = convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						) + convolve(SCREAMING_SNAKE_CASE       ,   -kernel_t						)

  __lowerCAmelCase:  Any        = convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						)
  __lowerCAmelCase:  Dict        = convolve(SCREAMING_SNAKE_CASE       ,   SCREAMING_SNAKE_CASE						)

  # This updates the flow as proposed in the paper (Step 12)
  __lowerCAmelCase:  Tuple        = (
      derivative_x * avg_horizontal_velocity
      + derivative_y * avg_vertical_velocity
      + derivative_t
  )
  __lowerCAmelCase:  int        = update / (alpha**2 + derivative_x**2 + derivative_y**2)

  __lowerCAmelCase:  List[Any]        = avg_horizontal_velocity - derivative_x * update
  __lowerCAmelCase:  Union[str, Any]        = avg_vertical_velocity - derivative_y * update

 return horizontal_flow, vertical_flow


if __name__ == "__main__":
  import doctest

  doctest.testmod()





