





'''simple docstring'''



import logging
import os
from dataclasses import dataclass
from typing import List, Optional, Union

import tqdm
from filelock import FileLock

from transformers import (
    BartTokenizer,
    BartTokenizerFast,
    DataProcessor,
    PreTrainedTokenizer,
    RobertaTokenizer,
    RobertaTokenizerFast,
    XLMRobertaTokenizer,
    is_tf_available,
    is_torch_available,
)


__magic_name__   :						Optional[Any]    =			logging.getLogger(__name__)


@dataclass(frozen=__snake_case			)
class       __SCREAMING_SNAKE_CASE	:



       '''simple docstring'''






       UpperCAmelCase__       :						str
       UpperCAmelCase__       :						str
       UpperCAmelCase__       :						Optional[str]      =   None
       UpperCAmelCase__       :						Optional[str]      =   None
       UpperCAmelCase__       :						Optional[str]      =   None







@dataclass(frozen=__snake_case			)
class       __SCREAMING_SNAKE_CASE	:



       '''simple docstring'''






       UpperCAmelCase__       :						List[int]
       UpperCAmelCase__       :						Optional[List[int]]      =   None
       UpperCAmelCase__       :						Optional[List[int]]      =   None
       UpperCAmelCase__       :						Optional[Union[int, float]]      =   None
       UpperCAmelCase__       :						Optional[int]      =   None


if is_torch_available():
 import torch
 from torch.utils.data import Dataset


 class       __SCREAMING_SNAKE_CASE	(						__snake_case			):



        '''simple docstring'''






        UpperCAmelCase__       :						List[InputFeatures]

        def __init__(					self						,   lowerCamelCase						,   lowerCamelCase						,   lowerCamelCase						,   lowerCamelCase = None						,   lowerCamelCase=False						,   lowerCamelCase = False						,   ):
          _snake_case     		=				hans_processors[task]()

          _snake_case     		=				os.path.join(
              A_						,   "cached_{}_{}_{}_{}".format(
                  "dev" if evaluate else "train"						,   tokenizer.__class__.__name__						,   str(A_	)						,   A_						,   )						,   )
          _snake_case     		=				processor.get_labels()
          if tokenizer.__class__ in (
              RobertaTokenizer,
              RobertaTokenizerFast,
              XLMRobertaTokenizer,
              BartTokenizer,
              BartTokenizerFast,
          ):
            # HACK(label indices are swapped in RoBERTa pretrained model)
            _snake_case     		=				label_list[2], label_list[1]
          _snake_case     		=				label_list

          # Make sure only the first process in distributed training processes the dataset,
          # and the others will use the cache.
          _snake_case     		=				cached_features_file + ".lock"
          with FileLock(A_	):
            if os.path.exists(A_	) and not overwrite_cache:
              logger.info(F'''Loading features from cached file {cached_features_file}'''	)
              _snake_case     		=				torch.load(A_	)
            else:
              logger.info(F'''Creating features from dataset file at {data_dir}'''	)

              _snake_case     		=				(
                  processor.get_dev_examples(A_	) if evaluate else processor.get_train_examples(A_	)
              )

              logger.info("Training examples: %s"						,   len(A_	)	)
              _snake_case     		=				hans_convert_examples_to_features(A_						,   A_						,   A_						,   A_	)
              logger.info("Saving features into cached file %s"						,   A_	)
              torch.save(self.features						,   A_	)

        def __len__(					self	):
          return len(self.features	)

        def __getitem__(					self						,   lowerCamelCase	):
          return self.features[i]






        def 	UpperCamelCase(					self	):
          return self.label_list


if is_tf_available():
 import tensorflow as tf


 class       __SCREAMING_SNAKE_CASE	:



        '''simple docstring'''






        UpperCAmelCase__       :						List[InputFeatures]

        def __init__(					self						,   lowerCamelCase						,   lowerCamelCase						,   lowerCamelCase						,   lowerCamelCase = 128						,   lowerCamelCase=False						,   lowerCamelCase = False						,   ):
          _snake_case     		=				hans_processors[task]()
          _snake_case     		=				processor.get_labels()
          if tokenizer.__class__ in (
              RobertaTokenizer,
              RobertaTokenizerFast,
              XLMRobertaTokenizer,
              BartTokenizer,
              BartTokenizerFast,
          ):
            # HACK(label indices are swapped in RoBERTa pretrained model)
            _snake_case     		=				label_list[2], label_list[1]
          _snake_case     		=				label_list

          _snake_case     		=				processor.get_dev_examples(A_	) if evaluate else processor.get_train_examples(A_	)
          _snake_case     		=				hans_convert_examples_to_features(A_						,   A_						,   A_						,   A_	)

          def gen():
            for ex_index, ex in tqdm.tqdm(enumerate(self.features	)						,   desc="convert examples to features"	):
              if ex_index % 10_000 == 0:
                logger.info("Writing example %d of %d" % (ex_index, len(A_	))	)

              yield (
                  {
                      "example_id": 0,
                      "input_ids": ex.input_ids,
                      "attention_mask": ex.attention_mask,
                      "token_type_ids": ex.token_type_ids,
                  },
                  ex.label,
              )

          _snake_case     		=				tf.data.Dataset.from_generator(
              A_						,   (
                  {
                      "example_id": tf.intaa,
                      "input_ids": tf.intaa,
                      "attention_mask": tf.intaa,
                      "token_type_ids": tf.intaa,
                  },
                  tf.intaa,
              )						,   (
                  {
                      "example_id": tf.TensorShape([]	),
                      "input_ids": tf.TensorShape([None, None]	),
                      "attention_mask": tf.TensorShape([None, None]	),
                      "token_type_ids": tf.TensorShape([None, None]	),
                  },
                  tf.TensorShape([]	),
              )						,   )

        def 	UpperCamelCase(					self	):
          return self.dataset

        def __len__(					self	):
          return len(self.features	)

        def __getitem__(					self						,   lowerCamelCase	):
          return self.features[i]






        def 	UpperCamelCase(					self	):
          return self.label_list


class       __SCREAMING_SNAKE_CASE	(						__snake_case			):



       '''simple docstring'''






       def 	UpperCamelCase(					self						,   lowerCamelCase	):
         return self._create_examples(self._read_tsv(os.path.join(A_						,   "heuristics_train_set.txt"	)	)						,   "train"	)

       def 	UpperCamelCase(					self						,   lowerCamelCase	):
         return self._create_examples(self._read_tsv(os.path.join(A_						,   "heuristics_evaluation_set.txt"	)	)						,   "dev"	)

       def 	UpperCamelCase(					self	):
         return ["contradiction", "entailment", "neutral"]






       def 	UpperCamelCase(					self						,   lowerCamelCase						,   lowerCamelCase	):
         _snake_case     		=				[]
         for i, line in enumerate(A_	):
           if i == 0:
             continue
           _snake_case     		=				"%s-%s" % (set_type, line[0])
           _snake_case     		=				line[5]
           _snake_case     		=				line[6]
           _snake_case     		=				line[7][2:] if line[7].startswith("ex"	) else line[7]
           _snake_case     		=				line[0]
           examples.append(InputExample(guid=A_						,   text_a=A_						,   text_b=A_						,   label=A_						,   pairID=A_	)	)
         return examples







def 				snake_case_      (		SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__					,	):







  '''simple docstring'''



  _snake_case     		=				{label: i for i, label in enumerate(_lowerCAmelCase       )}

  _snake_case     		=				[]
  for ex_index, example in tqdm.tqdm(enumerate(_lowerCAmelCase       )					,	desc="convert examples to features"       ):
    if ex_index % 1_00_00 == 0:
      logger.info("Writing example %d" % (ex_index)       )

    _snake_case     		=				tokenizer(
        example.text_a					,	example.text_b					,	add_special_tokens=_lowerCAmelCase					,	max_length=_lowerCAmelCase					,	padding="max_length"					,	truncation=_lowerCAmelCase					,	return_overflowing_tokens=_lowerCAmelCase					,	)

    _snake_case     		=				label_map[example.label] if example.label in label_map else 0

    _snake_case     		=				int(example.pairID       )

    features.append(InputFeatures(**_lowerCAmelCase					,	label=_lowerCAmelCase					,	pairID=_lowerCAmelCase       )       )

  for i, example in enumerate(examples[:5]       ):
    logger.info("*** Example ***"       )
    logger.info(f'''guid: {example}'''       )
    logger.info(f'''features: {features[i]}'''       )

  return features


__magic_name__   :						Tuple    =			{
    """hans""": 3,
}




__magic_name__   :						List[Any]    =			{
    """hans""": HansProcessor,
}









'''simple docstring'''



from __future__ import annotations

from PIL import Image

# Define glider example
__magic_name__   :						str    =			[
    [0, 1, 0, 0, 0, 0, 0, 0],
    [0, 0, 1, 0, 0, 0, 0, 0],
    [1, 1, 1, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
    [0, 0, 0, 0, 0, 0, 0, 0],
]

# Define blinker example
__magic_name__   :						List[Any]    =			[[0, 1, 0], [0, 1, 0], [0, 1, 0]]




def 				snake_case_      (		SCREAMING_SNAKE_CASE__       ):







  '''simple docstring'''



  _snake_case     		=				[]
  for i in range(len(SCREAMING_SNAKE_CASE__       )       ):
    _snake_case     		=				[]
    for j in range(len(cells[i]       )       ):
      # Get the number of live neighbours
      _snake_case     		=				0
      if i > 0 and j > 0:
        neighbour_count += cells[i - 1][j - 1]
      if i > 0:
        neighbour_count += cells[i - 1][j]
      if i > 0 and j < len(cells[i]       ) - 1:
        neighbour_count += cells[i - 1][j + 1]
      if j > 0:
        neighbour_count += cells[i][j - 1]
      if j < len(cells[i]       ) - 1:
        neighbour_count += cells[i][j + 1]
      if i < len(SCREAMING_SNAKE_CASE__       ) - 1 and j > 0:
        neighbour_count += cells[i + 1][j - 1]
      if i < len(SCREAMING_SNAKE_CASE__       ) - 1:
        neighbour_count += cells[i + 1][j]
      if i < len(SCREAMING_SNAKE_CASE__       ) - 1 and j < len(cells[i]       ) - 1:
        neighbour_count += cells[i + 1][j + 1]

      # Rules of the game of life (excerpt from Wikipedia):
      # 1. Any live cell with two or three live neighbours survives.
      # 2. Any dead cell with three live neighbours becomes a live cell.
      # 3. All other live cells die in the next generation.
      #    Similarly, all other dead cells stay dead.
      _snake_case     		=				cells[i][j] == 1
      if (
          (alive and 2 <= neighbour_count <= 3)
          or not alive
          and neighbour_count == 3
      ):
        next_generation_row.append(1       )
      else:
        next_generation_row.append(0       )

    next_generation.append(SCREAMING_SNAKE_CASE__       )
  return next_generation




def 				snake_case_      (		SCREAMING_SNAKE_CASE__					,	SCREAMING_SNAKE_CASE__       ):







  '''simple docstring'''



  _snake_case     		=				[]
  for _ in range(SCREAMING_SNAKE_CASE__       ):
    # Create output image
    _snake_case     		=				Image.new("RGB"					,	(len(cells[0]       ), len(SCREAMING_SNAKE_CASE__       ))       )
    _snake_case     		=				img.load()

    # Save cells to image
    for x in range(len(SCREAMING_SNAKE_CASE__       )       ):
      for y in range(len(cells[0]       )       ):
        _snake_case     		=				2_55 - cells[y][x] * 2_55
        _snake_case     		=				(colour, colour, colour)

        # Save image
    images.append(SCREAMING_SNAKE_CASE__       )
    _snake_case     		=				new_generation(SCREAMING_SNAKE_CASE__       )
  return images


if __name__ == "__main__":
 __magic_name__   :						Optional[Any]    =			generate_images(GLIDER, 16)
 images[0].save("""out.gif""", save_all=True, append_images=images[1:])


