import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import (
    is_pt_flax_cross_test,
    require_flax,
    require_torch,
    require_vision,
    slow,
    torch_device,
)
from transformers.utils import is_flax_available, is_torch_available, is_vision_available

from ...test_modeling_flax_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_flax_bert import FlaxBertModelTester
from ..clip.test_modeling_flax_clip import FlaxCLIPVisionModelTester
from ..vit.test_modeling_flax_vit import FlaxViTModelTester


if is_flax_available():
      from transformers import (
          FlaxBertModel,
          FlaxCLIPVisionModel,
          FlaxVisionTextDualEncoderModel,
          FlaxViTModel,
          VisionTextDualEncoderConfig,
          VisionTextDualEncoderProcessor,
      )
      from transformers.modeling_flax_pytorch_utils import (
          convert_pytorch_state_dict_to_flax,
          load_flax_weights_in_pytorch_model,
      )


if is_torch_available():
      import torch

      from transformers import VisionTextDualEncoderModel

if is_vision_available():
      from PIL import Image







def 			A__      (	snake_case_ :		int      ):
 if isinstance(snake_case_       ,				collections.abc.Iterable      ):
  return x
 return (x, x)






@require_flax
class 		_lowerCamelCase		:
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase   )       ->					Union[str, Any]:
     pass
    def 						UpperCamelCase_				(  self   )       ->					Dict:
     pass
    def 						UpperCamelCase_				(  self   )       ->					Tuple:
     pass
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					Optional[int]:
     SCREAMING_SNAKE_CASE__:   Dict=      np.abs((a - b)   ).max()
     self.assertLessEqual(lowerCAmelCase , lowerCAmelCase , f'Difference between torch and flax is {diff} (>= {tol}).'   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=None , **lowerCAmelCase   )       ->					Dict:
     SCREAMING_SNAKE_CASE__:   Optional[int]=      VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase , lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   Tuple=      FlaxVisionTextDualEncoderModel(lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   Optional[int]=      model(input_ids=lowerCAmelCase , pixel_values=lowerCAmelCase , attention_mask=lowerCAmelCase   )

     self.assertEqual(output['''text_embeds'''].shape , (input_ids.shape[0], config.projection_dim)   )
     self.assertEqual(output['''image_embeds'''].shape , (pixel_values.shape[0], config.projection_dim)   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=None , **lowerCAmelCase   )       ->					Tuple:
     SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Dict=      self.get_vision_text_model(lowerCAmelCase , lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      {'''vision_model''': vision_model, '''text_model''': text_model}
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   Optional[int]=      model(input_ids=lowerCAmelCase , pixel_values=lowerCAmelCase , attention_mask=lowerCAmelCase   )

     self.assertEqual(output['''text_embeds'''].shape , (input_ids.shape[0], model.config.projection_dim)   )
     self.assertEqual(output['''image_embeds'''].shape , (pixel_values.shape[0], model.config.projection_dim)   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=None , **lowerCAmelCase   )       ->					Tuple:
     SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   List[str]=      self.get_vision_text_model(lowerCAmelCase , lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   str=      {'''vision_model''': vision_model, '''text_model''': text_model}
     SCREAMING_SNAKE_CASE__:   Dict=      FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   Dict=      model(input_ids=lowerCAmelCase , pixel_values=lowerCAmelCase , attention_mask=lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   List[Any]=      output[0]

     with tempfile.TemporaryDirectory() as tmpdirname:
      model.save_pretrained(lowerCAmelCase   )
      SCREAMING_SNAKE_CASE__:   Tuple=      FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase   )

      SCREAMING_SNAKE_CASE__:   Tuple=      model(input_ids=lowerCAmelCase , pixel_values=lowerCAmelCase , attention_mask=lowerCAmelCase   )
      SCREAMING_SNAKE_CASE__:   List[str]=      after_output[0]
      SCREAMING_SNAKE_CASE__:   str=      np.amax(np.abs(out_a - out_a   )   )
      self.assertLessEqual(lowerCAmelCase , 1e-3   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase=None , **lowerCAmelCase   )       ->					Tuple:
     SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   str=      self.get_vision_text_model(lowerCAmelCase , lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   int=      {'''vision_model''': vision_model, '''text_model''': text_model}
     SCREAMING_SNAKE_CASE__:   Tuple=      FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(**lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   Dict=      model(
         input_ids=lowerCAmelCase , pixel_values=lowerCAmelCase , attention_mask=lowerCAmelCase , output_attentions=lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   List[str]=      output.vision_model_output.attentions
     self.assertEqual(len(lowerCAmelCase   ) , vision_config.num_hidden_layers   )

     # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
     SCREAMING_SNAKE_CASE__:   int=      to_atuple(vision_model.config.image_size   )
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      to_atuple(vision_model.config.patch_size   )
     SCREAMING_SNAKE_CASE__:   str=      (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
     SCREAMING_SNAKE_CASE__:   str=      num_patches + 1
     self.assertEqual(vision_attentions[0].shape[-3:] , (vision_config.num_attention_heads, seq_len, seq_len)   )

     SCREAMING_SNAKE_CASE__:   List[str]=      output.text_model_output.attentions
     self.assertEqual(len(lowerCAmelCase   ) , text_config.num_hidden_layers   )

     self.assertEqual(
         text_attentions[0].shape[-3:] , (text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1]) , )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					Optional[int]:
     pt_model.to(lowerCAmelCase   )
     pt_model.eval()

     # prepare inputs
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      inputs_dict
     SCREAMING_SNAKE_CASE__:   List[str]=      {k: torch.tensor(v.tolist()   ) for k, v in flax_inputs.items()}

     with torch.no_grad():
      SCREAMING_SNAKE_CASE__:   Tuple=      pt_model(**lowerCAmelCase   ).to_tuple()

     SCREAMING_SNAKE_CASE__:   int=      fx_model(**lowerCAmelCase   ).to_tuple()
     self.assertEqual(len(lowerCAmelCase   ) , len(lowerCAmelCase   ) , '''Output lengths differ between Flax and PyTorch'''   )
     for fx_output, pt_output in zip(fx_outputs[:4] , pt_outputs[:4]   ):
      self.assert_almost_equals(lowerCAmelCase , pt_output.numpy() , 4e-2   )

     # PT -> Flax
     with tempfile.TemporaryDirectory() as tmpdirname:
      pt_model.save_pretrained(lowerCAmelCase   )
      SCREAMING_SNAKE_CASE__:   List[str]=      FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase , from_pt=lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   Tuple=      fx_model_loaded(**lowerCAmelCase   ).to_tuple()
     self.assertEqual(len(lowerCAmelCase   ) , len(lowerCAmelCase   ) , '''Output lengths differ between Flax and PyTorch'''   )
     for fx_output_loaded, pt_output in zip(fx_outputs_loaded[:4] , pt_outputs[:4]   ):
      self.assert_almost_equals(lowerCAmelCase , pt_output.numpy() , 4e-2   )

     # Flax -> PT
     with tempfile.TemporaryDirectory() as tmpdirname:
      fx_model.save_pretrained(lowerCAmelCase   )
      SCREAMING_SNAKE_CASE__:   Tuple=      VisionTextDualEncoderModel.from_pretrained(lowerCAmelCase , from_flax=lowerCAmelCase   )

     pt_model_loaded.to(lowerCAmelCase   )
     pt_model_loaded.eval()

     with torch.no_grad():
      SCREAMING_SNAKE_CASE__:   Optional[Any]=      pt_model_loaded(**lowerCAmelCase   ).to_tuple()

     self.assertEqual(len(lowerCAmelCase   ) , len(lowerCAmelCase   ) , '''Output lengths differ between Flax and PyTorch'''   )
     for fx_output, pt_output_loaded in zip(fx_outputs[:4] , pt_outputs_loaded[:4]   ):
      self.assert_almost_equals(lowerCAmelCase , pt_output_loaded.numpy() , 4e-2   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					str:
     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase , lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   int=      VisionTextDualEncoderModel(lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   List[Any]=      FlaxVisionTextDualEncoderModel(lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   Union[str, Any]=      convert_pytorch_state_dict_to_flax(pt_model.state_dict() , lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   Tuple=      fx_state

     self.check_pt_flax_equivalence(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )       ->					Union[str, Any]:
     SCREAMING_SNAKE_CASE__:   Dict=      VisionTextDualEncoderConfig.from_vision_text_configs(lowerCAmelCase , lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   str=      VisionTextDualEncoderModel(lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   List[Any]=      FlaxVisionTextDualEncoderModel(lowerCAmelCase   )

     SCREAMING_SNAKE_CASE__:   int=      load_flax_weights_in_pytorch_model(lowerCAmelCase , fx_model.params   )

     self.check_pt_flax_equivalence(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					Optional[Any]:
     SCREAMING_SNAKE_CASE__:   List[str]=      self.prepare_config_and_inputs()
     self.check_model_from_pretrained_configs(**lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					Optional[int]:
     SCREAMING_SNAKE_CASE__:   Dict=      self.prepare_config_and_inputs()
     self.check_vision_text_dual_encoder_from_pretrained(**lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					Union[str, Any]:
     SCREAMING_SNAKE_CASE__:   Dict=      self.prepare_config_and_inputs()
     self.check_save_load(**lowerCAmelCase   )
    def 						UpperCamelCase_				(  self   )       ->					Optional[int]:
     SCREAMING_SNAKE_CASE__:   Tuple=      self.prepare_config_and_inputs()
     self.check_vision_text_output_attention(**lowerCAmelCase   )
    @is_pt_flax_cross_test
    def 						UpperCamelCase_				(  self   )       ->					Dict:
     SCREAMING_SNAKE_CASE__:   Dict=      self.prepare_config_and_inputs()
     SCREAMING_SNAKE_CASE__:   int=      config_inputs_dict.pop('''vision_config'''   )
     SCREAMING_SNAKE_CASE__:   int=      config_inputs_dict.pop('''text_config'''   )

     SCREAMING_SNAKE_CASE__:   Optional[Any]=      config_inputs_dict

     self.check_equivalence_pt_to_flax(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )
     self.check_equivalence_flax_to_pt(lowerCAmelCase , lowerCAmelCase , lowerCAmelCase   )



    @slow
    def 						UpperCamelCase_				(  self   )       ->					str:
     SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Union[str, Any]=      self.get_pretrained_model_and_inputs()

     SCREAMING_SNAKE_CASE__:   Tuple=      model_a(**lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   Optional[int]=      outputs[0]

     with tempfile.TemporaryDirectory() as tmp_dirname:
      model_a.save_pretrained(lowerCAmelCase   )
      SCREAMING_SNAKE_CASE__:   List[str]=      FlaxVisionTextDualEncoderModel.from_pretrained(lowerCAmelCase   )

      SCREAMING_SNAKE_CASE__:   Optional[Any]=      model_a(**lowerCAmelCase   )
      SCREAMING_SNAKE_CASE__:   List[str]=      after_outputs[0]
      SCREAMING_SNAKE_CASE__:   Optional[Any]=      np.amax(np.abs(out_a - out_a   )   )
      self.assertLessEqual(lowerCAmelCase , 1e-5   )






@require_flax
class 		_lowerCamelCase		(   UpperCamelCase_    ,     unittest.TestCase    ):
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   Any=      FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
         '''hf-internal-testing/tiny-random-vit''' , '''hf-internal-testing/tiny-bert''' , vision_from_pt=lowerCAmelCase , text_from_pt=lowerCAmelCase , )
     SCREAMING_SNAKE_CASE__:   Optional[int]=      13
     SCREAMING_SNAKE_CASE__:   List[Any]=      floats_tensor(
         [
             batch_size,
             model.config.vision_config.num_channels,
             model.config.vision_config.image_size,
             model.config.vision_config.image_size,
         ]   )
     SCREAMING_SNAKE_CASE__:   List[str]=      ids_tensor([batch_size, 4] , model.config.text_config.vocab_size   )
     SCREAMING_SNAKE_CASE__:   List[str]=      random_attention_mask([batch_size, 4]   )
     SCREAMING_SNAKE_CASE__:   str=      {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask}

     return model, inputs
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase   )       ->					str:
     SCREAMING_SNAKE_CASE__:   str=      FlaxViTModel(lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   Any=      FlaxBertModel(lowerCAmelCase   )
     return vision_model, text_model



    def 						UpperCamelCase_				(  self   )       ->					Optional[Any]:
     SCREAMING_SNAKE_CASE__:   Dict=      FlaxViTModelTester(self   )
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      FlaxBertModelTester(self   )
     SCREAMING_SNAKE_CASE__:   Any=      vit_model_tester.prepare_config_and_inputs()
     SCREAMING_SNAKE_CASE__:   int=      bert_model_tester.prepare_config_and_inputs()

     SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Optional[int]=      vision_config_and_inputs

     SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Tuple=      text_config_and_inputs

     # make sure that cross attention layers are added
     return {
         "text_config": text_config,
         "vision_config": vision_config,
         "pixel_values": pixel_values,
         "attention_mask": attention_mask,
         "input_ids": input_ids,
         "token_type_ids": token_type_ids,
     }






@require_torch
class 		_lowerCamelCase		(   UpperCamelCase_    ,     unittest.TestCase    ):
    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   Dict=      FlaxVisionTextDualEncoderModel.from_vision_text_pretrained(
         '''hf-internal-testing/tiny-random-clip''' , '''hf-internal-testing/tiny-bert''' , vision_from_pt=lowerCAmelCase , text_from_pt=lowerCAmelCase , )
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      13
     SCREAMING_SNAKE_CASE__:   Optional[int]=      floats_tensor(
         [
             batch_size,
             model.config.vision_config.num_channels,
             model.config.vision_config.image_size,
             model.config.vision_config.image_size,
         ]   )
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      ids_tensor([batch_size, 4] , model.config.text_config.vocab_size   )
     SCREAMING_SNAKE_CASE__:   Optional[int]=      random_attention_mask([batch_size, 4]   )
     SCREAMING_SNAKE_CASE__:   Tuple=      {'''pixel_values''': pixel_values, '''input_ids''': input_ids, '''attention_mask''': attention_mask}

     return model, inputs
    def 						UpperCamelCase_				(  self , lowerCAmelCase , lowerCAmelCase   )       ->					Union[str, Any]:
     SCREAMING_SNAKE_CASE__:   List[Any]=      FlaxCLIPVisionModel(lowerCAmelCase   )
     SCREAMING_SNAKE_CASE__:   Dict=      FlaxBertModel(lowerCAmelCase   )
     return vision_model, text_model



    def 						UpperCamelCase_				(  self   )       ->					List[str]:
     SCREAMING_SNAKE_CASE__:   List[Any]=      FlaxCLIPVisionModelTester(self   )
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      FlaxBertModelTester(self   )
     SCREAMING_SNAKE_CASE__:   Optional[Any]=      clip_model_tester.prepare_config_and_inputs()
     SCREAMING_SNAKE_CASE__:   int=      bert_model_tester.prepare_config_and_inputs()

     SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Optional[Any]=      vision_config_and_inputs

     SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Any=      text_config_and_inputs

     # make sure that cross attention layers are added
     return {
         "text_config": text_config,
         "vision_config": vision_config,
         "pixel_values": pixel_values,
         "attention_mask": attention_mask,
         "input_ids": input_ids,
         "token_type_ids": token_type_ids,
     }






@require_flax
@require_vision
class 		_lowerCamelCase		(   unittest.TestCase    ):
    @slow
    def 						UpperCamelCase_				(  self   )       ->					str:
     SCREAMING_SNAKE_CASE__:   Any=      FlaxVisionTextDualEncoderModel.from_pretrained('''clip-italian/clip-italian''' , logit_scale_init_value=1.0   )
     SCREAMING_SNAKE_CASE__:   Tuple=      VisionTextDualEncoderProcessor.from_pretrained('''clip-italian/clip-italian'''   )

     SCREAMING_SNAKE_CASE__:   Dict=      Image.open('''./tests/fixtures/tests_samples/COCO/000000039769.png'''   )
     SCREAMING_SNAKE_CASE__:   Optional[int]=      processor(
         text=['''una foto di un gatto''', '''una foto di un cane'''] , images=lowerCAmelCase , padding=lowerCAmelCase , return_tensors='''np'''   )

     SCREAMING_SNAKE_CASE__:   Any=      model(**lowerCAmelCase   )

     # verify the logits
     self.assertEqual(outputs.logits_per_image.shape , (inputs.pixel_values.shape[0], inputs.input_ids.shape[0])   )
     self.assertEqual(
         outputs.logits_per_text.shape , (inputs.input_ids.shape[0], inputs.pixel_values.shape[0]) , )

     SCREAMING_SNAKE_CASE__:   Optional[int]=      np.array([[1.2284727, 0.3104122]]   )

     self.assertTrue(np.allclose(outputs.logits_per_image , lowerCAmelCase , atol=1e-3   )   )


def 			A__      (	snake_case_ :		str      ):
 if not head:
  return True
 # split the list to two parts
 SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Optional[int]=      head.next, head
 while fast and fast.next:
  SCREAMING_SNAKE_CASE__:   Dict=      fast.next.next
  SCREAMING_SNAKE_CASE__:   Union[str, Any]=      slow.next
 SCREAMING_SNAKE_CASE__:   Union[str, Any]=      slow.next
 SCREAMING_SNAKE_CASE__:   Union[str, Any]=      None  # Don't forget here! But forget still works!
 # reverse the second part
 SCREAMING_SNAKE_CASE__:   Optional[int]=      None
 while second:
  SCREAMING_SNAKE_CASE__:   Any=      second.next
  SCREAMING_SNAKE_CASE__:   int=      node
  SCREAMING_SNAKE_CASE__:   Optional[Any]=      second
  SCREAMING_SNAKE_CASE__:   Any=      nxt
 # compare two parts
 # second part has the same or one less node
 while node:
  if node.val != head.val:
   return False
  SCREAMING_SNAKE_CASE__:   Tuple=      node.next
  SCREAMING_SNAKE_CASE__:   Optional[int]=      head.next
 return True







def 			A__      (	snake_case_ :		Optional[Any]      ):
 if not head or not head.next:
  return True

 # 1. Get the midpoint (slow)
 SCREAMING_SNAKE_CASE__:   List[Any]=      head
 while fast and fast.next:
  SCREAMING_SNAKE_CASE__,	SCREAMING_SNAKE_CASE__:   Any=      fast.next.next, slow.next

 # 2. Push the second half into the stack
 SCREAMING_SNAKE_CASE__:   Optional[Any]=      [slow.val]
 while slow.next:
  SCREAMING_SNAKE_CASE__:   Optional[int]=      slow.next
  stack.append(slow.val      )

 # 3. Comparison
 while stack:
  if stack.pop() != cur.val:
   return False
  SCREAMING_SNAKE_CASE__:   Tuple=      cur.next

 return True







def 			A__      (	snake_case_ :		Any      ):
 if not head or not head.next:
  return True
 SCREAMING_SNAKE_CASE__:   Optional[int]=      {}
 SCREAMING_SNAKE_CASE__:   Union[str, Any]=      0
 while head:
  if head.val in d:
   d[head.val].append(snake_case_      )
  else:
   SCREAMING_SNAKE_CASE__:   Optional[int]=      [pos]
  SCREAMING_SNAKE_CASE__:   Dict=      head.next
  pos += 1
 SCREAMING_SNAKE_CASE__:   Dict=      pos - 1
 SCREAMING_SNAKE_CASE__:   str=      0
 for v in d.values():
  if len(snake_case_      ) % 2 != 0:
   middle += 1
  else:
   SCREAMING_SNAKE_CASE__:   List[Any]=      0
   for i in range(0       ,				len(snake_case_      )      ):
    if v[i] + v[len(snake_case_      ) - 1 - step] != checksum:
     return False
    step += 1
  if middle > 1:
   return False
 return True

