


'''simple docstring'''







import unittest
from queue import Empty
from threading import Thread

from transformers import AutoTokenizer, TextIteratorStreamer, TextStreamer, is_torch_available
from transformers.testing_utils import CaptureStdout, require_torch, torch_device

from ..test_modeling_common import ids_tensor


if is_torch_available():
   import torch

   from transformers import AutoModelForCausalLM
@require_torch
class 			_lowercase						(       unittest.TestCase					):







     def     UpperCamelCase			( self    ) ->	int:
      snake_case				=			AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''    )
      snake_case				=			AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''    ).to(lowercase__    )
      snake_case				=			-1

      snake_case				=			ids_tensor((1, 5)    ,   vocab_size=model.config.vocab_size    ).to(lowercase__    )
      snake_case				=			model.generate(lowercase__    ,   max_new_tokens=10    ,   do_sample=lowercase__    )
      snake_case				=			tokenizer.decode(greedy_ids[0]    )

      with CaptureStdout() as cs:
       snake_case				=			TextStreamer(lowercase__    )
       model.generate(lowercase__    ,   max_new_tokens=10    ,   do_sample=lowercase__    ,   streamer=lowercase__    )
      # The greedy text should be printed to stdout, except for the final "\n" in the streamer
      snake_case				=			cs.out[:-1]

      self.assertEqual(lowercase__    ,   lowercase__    )







     def     UpperCamelCase			( self    ) ->	Union[str, Any]:
      snake_case				=			AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''    )
      snake_case				=			AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''    ).to(lowercase__    )
      snake_case				=			-1

      snake_case				=			ids_tensor((1, 5)    ,   vocab_size=model.config.vocab_size    ).to(lowercase__    )
      snake_case				=			model.generate(lowercase__    ,   max_new_tokens=10    ,   do_sample=lowercase__    )
      snake_case				=			tokenizer.decode(greedy_ids[0]    )

      snake_case				=			TextIteratorStreamer(lowercase__    )
      snake_case				=			{"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer}
      snake_case				=			Thread(target=model.generate    ,   kwargs=lowercase__    )
      thread.start()
      snake_case				=			""
      for new_text in streamer:
       streamer_text += new_text

      self.assertEqual(lowercase__    ,   lowercase__    )







     def     UpperCamelCase			( self    ) ->	Any:
      snake_case				=			AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''    )
      snake_case				=			AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''    ).to(lowercase__    )
      snake_case				=			-1

      snake_case				=			ids_tensor((1, 5)    ,   vocab_size=model.config.vocab_size    ).to(lowercase__    )
      snake_case				=			model.generate(lowercase__    ,   max_new_tokens=10    ,   do_sample=lowercase__    )
      snake_case				=			greedy_ids[:, input_ids.shape[1] :]
      snake_case				=			tokenizer.decode(new_greedy_ids[0]    )

      with CaptureStdout() as cs:
       snake_case				=			TextStreamer(lowercase__    ,   skip_prompt=lowercase__    )
       model.generate(lowercase__    ,   max_new_tokens=10    ,   do_sample=lowercase__    ,   streamer=lowercase__    )
      # The greedy text should be printed to stdout, except for the final "\n" in the streamer
      snake_case				=			cs.out[:-1]

      self.assertEqual(lowercase__    ,   lowercase__    )







     def     UpperCamelCase			( self    ) ->	str:
      snake_case				=			AutoTokenizer.from_pretrained('''distilgpt2'''    )
      snake_case				=			AutoModelForCausalLM.from_pretrained('''distilgpt2'''    ).to(lowercase__    )
      snake_case				=			-1

      snake_case				=			torch.ones((1, 5)    ,   device=lowercase__    ).long() * model.config.bos_token_id
      with CaptureStdout() as cs:
       snake_case				=			TextStreamer(lowercase__    ,   skip_special_tokens=lowercase__    )
       model.generate(lowercase__    ,   max_new_tokens=1    ,   do_sample=lowercase__    ,   streamer=lowercase__    )

      # The prompt contains a special token, so the streamer should not print it. As such, the output text, when
      # re-tokenized, must only contain one token
      snake_case				=			cs.out[:-1]  # Remove the final "\n"
      snake_case				=			tokenizer(lowercase__    ,   return_tensors='''pt'''    )
      self.assertEqual(streamer_text_tokenized.input_ids.shape    ,   (1, 1)    )







     def     UpperCamelCase			( self    ) ->	str:
      snake_case				=			AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''    )
      snake_case				=			AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''    ).to(lowercase__    )
      snake_case				=			-1

      snake_case				=			ids_tensor((1, 5)    ,   vocab_size=model.config.vocab_size    ).to(lowercase__    )
      snake_case				=			TextIteratorStreamer(lowercase__    ,   timeout=0.0_0_1    )
      snake_case				=			{"input_ids": input_ids, "max_new_tokens": 10, "do_sample": False, "streamer": streamer}
      snake_case				=			Thread(target=model.generate    ,   kwargs=lowercase__    )
      thread.start()

      # The streamer will timeout after 0.001 seconds, so an exception will be raised
      with self.assertRaises(lowercase__    ):
       snake_case				=			""
       for new_text in streamer:
        streamer_text += new_text








'''simple docstring'''







import inspect
import re
from hashlib import shaaaa
from typing import Dict, List

from .arrow import arrow
from .audiofolder import audiofolder
from .csv import csv
from .imagefolder import imagefolder
from .json import json
from .pandas import pandas
from .parquet import parquet
from .sql import sql  # noqa F401
from .text import text

def    __UpperCamelCase	(	a    : List[str]     ) ->str:
 snake_case				=			[]
 for line in lines:
  snake_case				=			re.sub(R'''#.*'''					,   ''''''					,   a     )  # remove comments
  if line:
   filtered_lines.append(a     )
 snake_case				=			'''\n'''.join(a     )

 # Make a hash from all this code
 snake_case				=			full_str.encode('''utf-8'''     )
 return shaaaa(a     ).hexdigest()


# get importable module names and hash for caching
_lowercase       						=			{
    'csv': (csv.__name__, _hash_python_lines(inspect.getsource(csv).splitlines())),
    'json': (json.__name__, _hash_python_lines(inspect.getsource(json).splitlines())),
    'pandas': (pandas.__name__, _hash_python_lines(inspect.getsource(pandas).splitlines())),
    'parquet': (parquet.__name__, _hash_python_lines(inspect.getsource(parquet).splitlines())),
    'arrow': (arrow.__name__, _hash_python_lines(inspect.getsource(arrow).splitlines())),
    'text': (text.__name__, _hash_python_lines(inspect.getsource(text).splitlines())),
    'imagefolder': (imagefolder.__name__, _hash_python_lines(inspect.getsource(imagefolder).splitlines())),
    'audiofolder': (audiofolder.__name__, _hash_python_lines(inspect.getsource(audiofolder).splitlines())),
}

# Used to infer the module to use based on the data files extensions
_lowercase       						=			{
    '.csv': ('csv', {}),
    '.tsv': ('csv', {'sep': '\t'}),
    '.json': ('json', {}),
    '.jsonl': ('json', {}),
    '.parquet': ('parquet', {}),
    '.arrow': ('arrow', {}),
    '.txt': ('text', {}),
}
_EXTENSION_TO_MODULE.update({ext: ('imagefolder', {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ('imagefolder', {}) for ext in imagefolder.ImageFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext: ('audiofolder', {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
_EXTENSION_TO_MODULE.update({ext.upper(): ('audiofolder', {}) for ext in audiofolder.AudioFolder.EXTENSIONS})
_lowercase       						=			{'imagefolder', 'audiofolder'}

# Used to filter data files based on extensions given a module name
_lowercase       						=			{}
for _ext, (_module, _) in _EXTENSION_TO_MODULE.items():
   _MODULE_TO_EXTENSIONS.setdefault(_module, []).append(_ext)

_MODULE_TO_EXTENSIONS["imagefolder"].append('.zip')






_MODULE_TO_EXTENSIONS["audiofolder"].append('.zip')

