


'''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  UpperCamelCase__(     unittest.TestCase ):




				def   a__(	self    :				Optional[int]   )->  Any:

										"""simple docstring"""




										UpperCAmelCase         =			AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''   )
										UpperCAmelCase         =			AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''   ).to(lowerCAmelCase   )
										UpperCAmelCase         =			-1

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

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

										self.assertEqual(lowerCAmelCase   ,					lowerCAmelCase   )




				def   a__(	self    :				Optional[int]   )->  str:

										"""simple docstring"""




										UpperCAmelCase         =			AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''   )
										UpperCAmelCase         =			AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''   ).to(lowerCAmelCase   )
										UpperCAmelCase         =			-1

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

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

										self.assertEqual(lowerCAmelCase   ,					lowerCAmelCase   )




				def   a__(	self    :				Tuple   )->  Union[str, Any]:

										"""simple docstring"""




										UpperCAmelCase         =			AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''   )
										UpperCAmelCase         =			AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''   ).to(lowerCAmelCase   )
										UpperCAmelCase         =			-1

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

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

										self.assertEqual(lowerCAmelCase   ,					lowerCAmelCase   )




				def   a__(	self    :				Union[str, Any]   )->  List[Any]:

										"""simple docstring"""




										UpperCAmelCase         =			AutoTokenizer.from_pretrained('''distilgpt2'''   )
										UpperCAmelCase         =			AutoModelForCausalLM.from_pretrained('''distilgpt2'''   ).to(lowerCAmelCase   )
										UpperCAmelCase         =			-1

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

										# 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
										UpperCAmelCase         =			cs.out[:-1]  # Remove the final "\n"
										UpperCAmelCase         =			tokenizer(lowerCAmelCase   ,					return_tensors='''pt'''   )
										self.assertEqual(streamer_text_tokenized.input_ids.shape   ,					(1, 1)   )





				def   a__(	self    :				Union[str, Any]   )->  Dict:

										"""simple docstring"""




										UpperCAmelCase         =			AutoTokenizer.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''   )
										UpperCAmelCase         =			AutoModelForCausalLM.from_pretrained('''hf-internal-testing/tiny-random-gpt2'''   ).to(lowerCAmelCase   )
										UpperCAmelCase         =			-1

										UpperCAmelCase         =			ids_tensor((1, 5)   ,					vocab_size=model.config.vocab_size   ).to(lowerCAmelCase   )
										UpperCAmelCase         =			TextIteratorStreamer(lowerCAmelCase   ,					timeout=0.001   )
										UpperCAmelCase         =			{'''input_ids''': input_ids, '''max_new_tokens''': 10, '''do_sample''': False, '''streamer''': streamer}
										UpperCAmelCase         =			Thread(target=model.generate   ,					kwargs=lowerCAmelCase   )
										thread.start()

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









'''simple docstring'''



# coding=utf-8
# Copyright 2020 The HuggingFace Inc. team.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# this script dumps information about the environment

import os
import sys

import transformers


_lowercase    :    Union[str, Any]			     =  """3"""

print("""Python version:""", sys.version)
print("""transformers version:""", transformers.__version__)

try:
	import torch

	print("""Torch version:""", torch.__version__)
	print("""Cuda available:""", torch.cuda.is_available())
	print("""Cuda version:""", torch.version.cuda)
	print("""CuDNN version:""", torch.backends.cudnn.version())
	print("""Number of GPUs available:""", torch.cuda.device_count())
	print("""NCCL version:""", torch.cuda.nccl.version())
except ImportError:
	print("""Torch version:""", None)

try:
	import deepspeed

	print("""DeepSpeed version:""", deepspeed.__version__)
except ImportError:
	print("""DeepSpeed version:""", None)

try:
	import tensorflow as tf

	print("""TensorFlow version:""", tf.__version__)
	print("""TF GPUs available:""", bool(tf.config.list_physical_devices("""GPU""")))
	print("""Number of TF GPUs available:""", len(tf.config.list_physical_devices("""GPU""")))
except ImportError:
	print("""TensorFlow version:""", None)





