"""simple docstring"""





import collections
import inspect
import unittest

from transformers import FocalNetConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import cached_property, is_torch_available, is_vision_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, _config_zero_init, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
			import torch
			from torch import nn

			from transformers import (
			    FocalNetBackbone,
			    FocalNetForImageClassification,
			    FocalNetForMaskedImageModeling,
			    FocalNetModel,
			)
			from transformers.models.focalnet.modeling_focalnet import FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST

if is_vision_available():
			from PIL import Image

			from transformers import AutoImageProcessor




class  _snake_case		:







				"""simple docstring"""






				def __init__(      self							:							Any      ,    _A							:							int      ,    _A							:							Optional[Any]=1_3      ,    _A							:							Dict=3_2      ,    _A							:							int=2      ,    _A							:							Optional[Any]=3      ,    _A							:							Tuple=1_6      ,    _A							:							Tuple=[3_2, 6_4, 1_2_8]      ,    _A							:							Optional[int]=[1, 2, 1]      ,    _A							:							List[str]=[2, 2, 4]      ,    _A							:							Optional[Any]=2      ,    _A							:							int=2.0      ,    _A							:							Optional[int]=True      ,    _A							:							Optional[Any]=0.0      ,    _A							:							Tuple=0.0      ,    _A							:							Union[str, Any]=0.1      ,    _A							:							Optional[Any]="gelu"      ,    _A							:							List[str]=False      ,    _A							:							int=True      ,    _A							:							List[str]=0.02      ,    _A							:							Tuple=1e-5      ,    _A							:							List[str]=True      ,    _A							:							Optional[int]=None      ,    _A							:							Dict=True      ,    _A							:							Any=1_0      ,    _A							:							int=8      ,    _A							:							Tuple=["stage1", "stage2"]      ,    _A							:							str=[1, 2]      ,    ):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    parent
											_SCREAMING_SNAKE_CASE		:						Any			  =    batch_size
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    image_size
											_SCREAMING_SNAKE_CASE		:						int			  =    patch_size
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    num_channels
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    embed_dim
											_SCREAMING_SNAKE_CASE		:						Dict			  =    hidden_sizes
											_SCREAMING_SNAKE_CASE		:						Dict			  =    depths
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    num_heads
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    window_size
											_SCREAMING_SNAKE_CASE		:						Any			  =    mlp_ratio
											_SCREAMING_SNAKE_CASE		:						str			  =    qkv_bias
											_SCREAMING_SNAKE_CASE		:						Any			  =    hidden_dropout_prob
											_SCREAMING_SNAKE_CASE		:						int			  =    attention_probs_dropout_prob
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    drop_path_rate
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    hidden_act
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    use_absolute_embeddings
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    patch_norm
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    layer_norm_eps
											_SCREAMING_SNAKE_CASE		:						str			  =    initializer_range
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    is_training
											_SCREAMING_SNAKE_CASE		:						str			  =    scope
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    use_labels
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    type_sequence_label_size
											_SCREAMING_SNAKE_CASE		:						int			  =    encoder_stride
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    out_features
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    out_indices





				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

											_SCREAMING_SNAKE_CASE		:						Dict			  =    None
											if self.use_labels:
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    ids_tensor([self.batch_size]      ,    self.type_sequence_label_size)

											_SCREAMING_SNAKE_CASE		:						Tuple			  =    self.get_config()

											return config, pixel_values, labels





				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											return FocalNetConfig(
											    image_size=self.image_size      ,    patch_size=self.patch_size      ,    num_channels=self.num_channels      ,    embed_dim=self.embed_dim      ,    hidden_sizes=self.hidden_sizes      ,    depths=self.depths      ,    num_heads=self.num_heads      ,    window_size=self.window_size      ,    mlp_ratio=self.mlp_ratio      ,    qkv_bias=self.qkv_bias      ,    hidden_dropout_prob=self.hidden_dropout_prob      ,    attention_probs_dropout_prob=self.attention_probs_dropout_prob      ,    drop_path_rate=self.drop_path_rate      ,    hidden_act=self.hidden_act      ,    use_absolute_embeddings=self.use_absolute_embeddings      ,    path_norm=self.patch_norm      ,    layer_norm_eps=self.layer_norm_eps      ,    initializer_range=self.initializer_range      ,    encoder_stride=self.encoder_stride      ,    out_features=self.out_features      ,    out_indices=self.out_indices      ,    )





				def 		_lowerCAmelCase		(      self							:							List[Any]      ,    _A							:							List[Any]      ,    _A							:							Tuple      ,    _A							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[str]			  =    FocalNetModel(config=_A)
											model.to(_A)
											model.eval()
											_SCREAMING_SNAKE_CASE		:						Dict			  =    model(_A)

											_SCREAMING_SNAKE_CASE		:						Dict			  =    ((config.image_size // config.patch_size) ** 2) // (4 ** (len(config.depths) - 1))
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    int(config.embed_dim * 2 ** (len(config.depths) - 1))

											self.parent.assertEqual(result.last_hidden_state.shape      ,    (self.batch_size, expected_seq_len, expected_dim))





				def 		_lowerCAmelCase		(      self							:							str      ,    _A							:							Optional[Any]      ,    _A							:							Any      ,    _A							:							Any):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    FocalNetBackbone(config=_A)
											model.to(_A)
											model.eval()
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    model(_A)

											# verify feature maps
											self.parent.assertEqual(len(result.feature_maps)      ,    len(config.out_features))
											self.parent.assertListEqual(list(result.feature_maps[0].shape)      ,    [self.batch_size, self.image_size, 8, 8])

											# verify channels
											self.parent.assertEqual(len(model.channels)      ,    len(config.out_features))
											self.parent.assertListEqual(model.channels      ,    config.hidden_sizes[:-1])

											# verify backbone works with out_features=None
											_SCREAMING_SNAKE_CASE		:						Any			  =    None
											_SCREAMING_SNAKE_CASE		:						str			  =    FocalNetBackbone(config=_A)
											model.to(_A)
											model.eval()
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    model(_A)

											# verify feature maps
											self.parent.assertEqual(len(result.feature_maps)      ,    1)
											self.parent.assertListEqual(list(result.feature_maps[0].shape)      ,    [self.batch_size, self.image_size * 2, 4, 4])

											# verify channels
											self.parent.assertEqual(len(model.channels)      ,    1)
											self.parent.assertListEqual(model.channels      ,    [config.hidden_sizes[-1]])





				def 		_lowerCAmelCase		(      self							:							Optional[int]      ,    _A							:							Any      ,    _A							:							Union[str, Any]      ,    _A							:							Dict):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Dict			  =    FocalNetForMaskedImageModeling(config=_A)
											model.to(_A)
											model.eval()
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    model(_A)
											self.parent.assertEqual(
											    result.reconstruction.shape      ,    (self.batch_size, self.num_channels, self.image_size, self.image_size))

											# test greyscale images
											_SCREAMING_SNAKE_CASE		:						Dict			  =    1
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    FocalNetForMaskedImageModeling(_A)
											model.to(_A)
											model.eval()

											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    model(_A)
											self.parent.assertEqual(result.reconstruction.shape      ,    (self.batch_size, 1, self.image_size, self.image_size))





				def 		_lowerCAmelCase		(      self							:							int      ,    _A							:							Dict      ,    _A							:							Tuple      ,    _A							:							Any):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						List[str]			  =    self.type_sequence_label_size
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    FocalNetForImageClassification(_A)
											model.to(_A)
											model.eval()
											_SCREAMING_SNAKE_CASE		:						str			  =    model(_A      ,    labels=_A)
											self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.type_sequence_label_size))

											# test greyscale images
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    1
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    FocalNetForImageClassification(_A)
											model.to(_A)
											model.eval()

											_SCREAMING_SNAKE_CASE		:						Dict			  =    floats_tensor([self.batch_size, 1, self.image_size, self.image_size])
											_SCREAMING_SNAKE_CASE		:						Dict			  =    model(_A)
											self.parent.assertEqual(result.logits.shape      ,    (self.batch_size, self.type_sequence_label_size))





				def 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Dict			  =    self.prepare_config_and_inputs()

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    config_and_inputs
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    {"""pixel_values""": pixel_values}
											return config, inputs_dict




@require_torch
class  _snake_case		(    __snake_case   ,       __snake_case   ,       unittest.TestCase ):







				"""simple docstring"""






				a			=						(
				    (
				        FocalNetModel,
				        FocalNetForImageClassification,
				        FocalNetForMaskedImageModeling,
				        FocalNetBackbone,
				    )
				    if is_torch_available()
				    else ()
				)
				a			=						(
				    {"feature-extraction": FocalNetModel, "image-classification": FocalNetForImageClassification}
				    if is_torch_available()
				    else {}
				)
				a			=						False

				a			=						False
				a			=						False
				a			=						False
				a			=						False





				def 		_lowerCAmelCase		(      self							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Dict			  =    FocalNetModelTester(self)
											_SCREAMING_SNAKE_CASE		:						str			  =    ConfigTester(self      ,    config_class=_A      ,    embed_dim=3_7      ,    has_text_modality=_A)





				def 		_lowerCAmelCase		(      self							:							Dict):



											"""simple docstring"""







											self.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.create_and_test_config_with_num_labels()
											self.config_tester.check_config_can_be_init_without_params()
											self.config_tester.check_config_arguments_init()





				def 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







											return





				def 		_lowerCAmelCase		(      self							:							List[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_model(*_A)





				def 		_lowerCAmelCase		(      self							:							List[str]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_backbone(*_A)





				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_masked_image_modeling(*_A)





				def 		_lowerCAmelCase		(      self							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    self.model_tester.prepare_config_and_inputs()
											self.model_tester.create_and_check_for_image_classification(*_A)





				@unittest.skip(reason="""FocalNet does not use inputs_embeds""")
				def 		_lowerCAmelCase		(      self							:							Optional[Any]):



											"""simple docstring"""







											pass





				@unittest.skip(reason="""FocalNet does not use feedforward chunking""")
				def 		_lowerCAmelCase		(      self							:							Optional[int]):



											"""simple docstring"""







											pass





				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						List[str]			  =    self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes[:-1]:
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    model_class(_A)
																		self.assertIsInstance(model.get_input_embeddings()      ,    (nn.Module))
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    model.get_output_embeddings()
																		self.assertTrue(x is None or isinstance(_A      ,    nn.Linear))





				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Any			  =    self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes[:-1]:
																		_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    model_class(_A)
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    inspect.signature(model.forward)
																		# signature.parameters is an OrderedDict => so arg_names order is deterministic
																		_SCREAMING_SNAKE_CASE		:						str			  =    [*signature.parameters.keys()]

																		_SCREAMING_SNAKE_CASE		:						int			  =    ["""pixel_values"""]
																		self.assertListEqual(arg_names[:1]      ,    _A)





				def 		_lowerCAmelCase		(      self							:							int      ,    _A							:							Any      ,    _A							:							Any      ,    _A							:							Dict      ,    _A							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    model_class(_A)
											model.to(_A)
											model.eval()

											with torch.no_grad():
																		_SCREAMING_SNAKE_CASE		:						str			  =    model(**self._prepare_for_class(_A      ,    _A))

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    outputs.hidden_states

											_SCREAMING_SNAKE_CASE		:						int			  =    getattr(
											    self.model_tester      ,    """expected_num_hidden_layers"""      ,    len(self.model_tester.depths) + 1)
											self.assertEqual(len(_A)      ,    _A)

											# FocalNet has a different seq_length
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    (
											    config.patch_size
											    if isinstance(config.patch_size      ,    collections.abc.Iterable)
											    else (config.patch_size, config.patch_size)
											)

											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])

											self.assertListEqual(
											    list(hidden_states[0].shape[-2:])      ,    [num_patches, self.model_tester.embed_dim]      ,    )

											_SCREAMING_SNAKE_CASE		:						str			  =    outputs.reshaped_hidden_states
											self.assertEqual(len(_A)      ,    _A)

											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						str			  =    reshaped_hidden_states[0].shape
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    (
											    reshaped_hidden_states[0].view(_A      ,    _A      ,    height * width).permute(0      ,    2      ,    1)
											)
											self.assertListEqual(
											    list(reshaped_hidden_states.shape[-2:])      ,    [num_patches, self.model_tester.embed_dim]      ,    )





				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Dict			  =    self.model_tester.prepare_config_and_inputs_for_common()

											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    (
											    self.model_tester.image_size
											    if isinstance(self.model_tester.image_size      ,    collections.abc.Iterable)
											    else (self.model_tester.image_size, self.model_tester.image_size)
											)

											for model_class in self.all_model_classes[:-1]:
																		_SCREAMING_SNAKE_CASE		:						List[str]			  =    True
																		self.check_hidden_states_output(_A      ,    _A      ,    _A      ,    _A)

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    True

																		self.check_hidden_states_output(_A      ,    _A      ,    _A      ,    _A)





				def 		_lowerCAmelCase		(      self							:							int):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						List[str]			  =    self.model_tester.prepare_config_and_inputs_for_common()
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    3

											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    (
											    self.model_tester.image_size
											    if isinstance(self.model_tester.image_size      ,    collections.abc.Iterable)
											    else (self.model_tester.image_size, self.model_tester.image_size)
											)
											_SCREAMING_SNAKE_CASE		:						str			  =    (
											    config.patch_size
											    if isinstance(config.patch_size      ,    collections.abc.Iterable)
											    else (config.patch_size, config.patch_size)
											)

											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    image_size[0] + patch_size[0] - (image_size[0] % patch_size[0])
											_SCREAMING_SNAKE_CASE		:						Dict			  =    image_size[1] + patch_size[1] - (image_size[1] % patch_size[1])

											for model_class in self.all_model_classes[:-1]:
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    True
																		self.check_hidden_states_output(_A      ,    _A      ,    _A      ,    (padded_height, padded_width))

																		# check that output_hidden_states also work using config
																		del inputs_dict["output_hidden_states"]
																		_SCREAMING_SNAKE_CASE		:						Tuple			  =    True
																		self.check_hidden_states_output(_A      ,    _A      ,    _A      ,    (padded_height, padded_width))





				@slow
				def 		_lowerCAmelCase		(      self							:							Dict):



											"""simple docstring"""







											for model_name in FOCALNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																		_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    FocalNetModel.from_pretrained(_A)
																		self.assertIsNotNone(_A)





				def 		_lowerCAmelCase		(      self							:							Optional[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE  ,				_SCREAMING_SNAKE_CASE		:						Any			  =    self.model_tester.prepare_config_and_inputs_for_common()

											_SCREAMING_SNAKE_CASE		:						str			  =    _config_zero_init(_A)
											for model_class in self.all_model_classes:
																		_SCREAMING_SNAKE_CASE		:						int			  =    model_class(config=_A)
																		for name, param in model.named_parameters():
																									if "embeddings" not in name and param.requires_grad:
																																self.assertIn(
																																    ((param.data.mean() * 1e9).round() / 1e9).item()      ,    [0.0, 1.0]      ,    msg=f"""Parameter {name} of model {model_class} seems not properly initialized"""      ,    )




@require_vision
@require_torch
class  _snake_case		(    unittest.TestCase ):







				"""simple docstring"""






				@cached_property
				def 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







											return AutoImageProcessor.from_pretrained("""microsoft/focalnet-tiny""") if is_vision_available() else None





				@slow
				def 		_lowerCAmelCase		(      self							:							Tuple):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    FocalNetForImageClassification.from_pretrained("""microsoft/focalnet-tiny""").to(_A)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    self.default_image_processor

											_SCREAMING_SNAKE_CASE		:						int			  =    Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    image_processor(images=_A      ,    return_tensors="""pt""").to(_A)

											# forward pass
											with torch.no_grad():
																		_SCREAMING_SNAKE_CASE		:						List[Any]			  =    model(**_A)

											# verify the logits
											_SCREAMING_SNAKE_CASE		:						int			  =    torch.Size((1, 1_0_0_0))
											self.assertEqual(outputs.logits.shape      ,    _A)
											_SCREAMING_SNAKE_CASE		:						Dict			  =    torch.tensor([0.2_166, -0.4_368, 0.2_191]).to(_A)
											self.assertTrue(torch.allclose(outputs.logits[0, :3]      ,    _A      ,    atol=1e-4))
											self.assertTrue(outputs.logits.argmax(dim=-1).item()      ,    2_8_1)




@require_torch
class  _snake_case		(    __snake_case   ,       unittest.TestCase ):







				"""simple docstring"""






				a			=						(FocalNetBackbone,) if is_torch_available() else ()
				a			=						FocalNetConfig

				a			=						False





				def 		_lowerCAmelCase		(      self							:							List[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						int			  =    FocalNetModelTester(self)



"""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  _snake_case		(    unittest.TestCase ):







				"""simple docstring"""






				def 		_lowerCAmelCase		(      self							:							List[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2""")
											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    AutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2""").to(_A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    -1

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    ids_tensor((1, 5)      ,    vocab_size=model.config.vocab_size).to(_A)
											_SCREAMING_SNAKE_CASE		:						Dict			  =    model.generate(_A      ,    max_new_tokens=1_0      ,    do_sample=_A)
											_SCREAMING_SNAKE_CASE		:						Dict			  =    tokenizer.decode(greedy_ids[0])

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

											self.assertEqual(_A      ,    _A)





				def 		_lowerCAmelCase		(      self							:							Any):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2""")
											_SCREAMING_SNAKE_CASE		:						Any			  =    AutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2""").to(_A)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    -1

											_SCREAMING_SNAKE_CASE		:						str			  =    ids_tensor((1, 5)      ,    vocab_size=model.config.vocab_size).to(_A)
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    model.generate(_A      ,    max_new_tokens=1_0      ,    do_sample=_A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    tokenizer.decode(greedy_ids[0])

											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    TextIteratorStreamer(_A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    {"""input_ids""": input_ids, """max_new_tokens""": 1_0, """do_sample""": False, """streamer""": streamer}
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    Thread(target=model.generate      ,    kwargs=_A)
											thread.start()
											_SCREAMING_SNAKE_CASE		:						Any			  =    """"""
											for new_text in streamer:
																		streamer_text += new_text

											self.assertEqual(_A      ,    _A)





				def 		_lowerCAmelCase		(      self							:							List[Any]):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2""")
											_SCREAMING_SNAKE_CASE		:						Dict			  =    AutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2""").to(_A)
											_SCREAMING_SNAKE_CASE		:						Any			  =    -1

											_SCREAMING_SNAKE_CASE		:						Any			  =    ids_tensor((1, 5)      ,    vocab_size=model.config.vocab_size).to(_A)
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    model.generate(_A      ,    max_new_tokens=1_0      ,    do_sample=_A)
											_SCREAMING_SNAKE_CASE		:						str			  =    greedy_ids[:, input_ids.shape[1] :]
											_SCREAMING_SNAKE_CASE		:						Dict			  =    tokenizer.decode(new_greedy_ids[0])

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

											self.assertEqual(_A      ,    _A)





				def 		_lowerCAmelCase		(      self							:							Dict):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						int			  =    AutoTokenizer.from_pretrained("""distilgpt2""")
											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    AutoModelForCausalLM.from_pretrained("""distilgpt2""").to(_A)
											_SCREAMING_SNAKE_CASE		:						int			  =    -1

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    torch.ones((1, 5)      ,    device=_A).long() * model.config.bos_token_id
											with CaptureStdout() as cs:
																		_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    TextStreamer(_A      ,    skip_special_tokens=_A)
																		model.generate(_A      ,    max_new_tokens=1      ,    do_sample=_A      ,    streamer=_A)

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





				def 		_lowerCAmelCase		(      self							:							str):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    AutoTokenizer.from_pretrained("""hf-internal-testing/tiny-random-gpt2""")
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    AutoModelForCausalLM.from_pretrained("""hf-internal-testing/tiny-random-gpt2""").to(_A)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    -1

											_SCREAMING_SNAKE_CASE		:						Dict			  =    ids_tensor((1, 5)      ,    vocab_size=model.config.vocab_size).to(_A)
											_SCREAMING_SNAKE_CASE		:						int			  =    TextIteratorStreamer(_A      ,    timeout=0.001)
											_SCREAMING_SNAKE_CASE		:						List[Any]			  =    {"""input_ids""": input_ids, """max_new_tokens""": 1_0, """do_sample""": False, """streamer""": streamer}
											_SCREAMING_SNAKE_CASE		:						List[str]			  =    Thread(target=model.generate      ,    kwargs=_A)
											thread.start()

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


