



import math
from dataclasses import dataclass
from typing import List, Optional, Union

import numpy as np
import PIL
import torch
from transformers import CLIPTextModelWithProjection, CLIPTokenizer

from ...models import PriorTransformer
from ...pipelines import DiffusionPipeline
from ...schedulers import HeunDiscreteScheduler
from ...utils import (
    BaseOutput,
    is_accelerate_available,
    is_accelerate_version,
    logging,
    randn_tensor,
    replace_example_docstring,
)
from .renderer import ShapERenderer


a			:     Dict     = logging.get_logger(__name__)  # pylint: disable=invalid-name

a			:     List[str]     = '\n    Examples:\n        ```py\n        >>> import torch\n        >>> from diffusers import DiffusionPipeline\n        >>> from diffusers.utils import export_to_gif\n\n        >>> device = torch.device("cuda" if torch.cuda.is_available() else "cpu")\n\n        >>> repo = "openai/shap-e"\n        >>> pipe = DiffusionPipeline.from_pretrained(repo, torch_dtype=torch.float16)\n        >>> pipe = pipe.to(device)\n\n        >>> guidance_scale = 15.0\n        >>> prompt = "a shark"\n\n        >>> images = pipe(\n        ...     prompt,\n        ...     guidance_scale=guidance_scale,\n        ...     num_inference_steps=64,\n        ...     frame_size=256,\n        ... ).images\n\n        >>> gif_path = export_to_gif(images[0], "shark_3d.gif")\n        ```\n'







@dataclass
class 							_a							(      _lowerCAmelCase				):
			A					=  42







class 							_a							(      _lowerCAmelCase				):

			def __init__(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      )					->      int:
						super().__init__()

						self.register_modules(
						    prior=SCREAMING_SNAKE_CASE_,      text_encoder=SCREAMING_SNAKE_CASE_,      tokenizer=SCREAMING_SNAKE_CASE_,      scheduler=SCREAMING_SNAKE_CASE_,      renderer=SCREAMING_SNAKE_CASE_,      )

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)					->      Dict:
						if latents is None:
									UpperCAmelCase_:		Union[str, Any]       			=    randn_tensor(SCREAMING_SNAKE_CASE_,      generator=SCREAMING_SNAKE_CASE_,      device=SCREAMING_SNAKE_CASE_,      dtype=SCREAMING_SNAKE_CASE_				)
						else:
									if latents.shape != shape:
												raise ValueError(f'Unexpected latents shape, got {latents.shape}, expected {shape}'				)
									UpperCAmelCase_:		Union[str, Any]       			=    latents.to(SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		int       			=    latents * scheduler.init_noise_sigma
						return latents

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_=0				)					->      List[str]:
						if is_accelerate_available():
									from accelerate import cpu_offload
						else:
									raise ImportError("""Please install accelerate via `pip install accelerate`"""				)

						UpperCAmelCase_:		str       			=    torch.device(f'cuda:{gpu_id}'				)

						UpperCAmelCase_:		Optional[Any]       			=    [self.text_encoder, self.prior]
						for cpu_offloaded_model in models:
									if cpu_offloaded_model is not None:
												cpu_offload(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_=0				)					->      int:
						if is_accelerate_available() and is_accelerate_version(""">=""",      """0.17.0.dev0"""				):
									from accelerate import cpu_offload_with_hook
						else:
									raise ImportError("""`enable_model_cpu_offload` requires `accelerate v0.17.0` or higher."""				)

						UpperCAmelCase_:		Tuple       			=    torch.device(f'cuda:{gpu_id}'				)

						if self.device.type != "cpu":
									self.to("""cpu""",      silence_dtype_warnings=SCREAMING_SNAKE_CASE_				)
									torch.cuda.empty_cache()  # otherwise we don't see the memory savings (but they probably exist)

						UpperCAmelCase_:		str       			=    None
						for cpu_offloaded_model in [self.text_encoder, self.prior, self.renderer]:
									UpperCAmelCase_:		Tuple       			=    cpu_offload_with_hook(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      prev_module_hook=SCREAMING_SNAKE_CASE_				)

						if self.safety_checker is not None:
									UpperCAmelCase_:		Optional[int]       			=    cpu_offload_with_hook(self.safety_checker,      SCREAMING_SNAKE_CASE_,      prev_module_hook=SCREAMING_SNAKE_CASE_				)

						# We'll offload the last model manually.
						UpperCAmelCase_:		Optional[Any]       			=    hook

			@property
			def     __snake_case					(self				)					->      Optional[int]:
						if self.device != torch.device("""meta"""				) or not hasattr(self.text_encoder,      """_hf_hook"""				):
									return self.device
						for module in self.text_encoder.modules():
									if (
									    hasattr(SCREAMING_SNAKE_CASE_,      """_hf_hook"""				)
									    and hasattr(module._hf_hook,      """execution_device"""				)
									    and module._hf_hook.execution_device is not None
									):
												return torch.device(module._hf_hook.execution_device				)
						return self.device

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      )					->      str:
						len(SCREAMING_SNAKE_CASE_				) if isinstance(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				) else 1

						# YiYi Notes: set pad_token_id to be 0, not sure why I can't set in the config file
						UpperCAmelCase_:		str       			=    0
						# get prompt text embeddings
						UpperCAmelCase_:		Dict       			=    self.tokenizer(
						    SCREAMING_SNAKE_CASE_,      padding="""max_length""",      max_length=self.tokenizer.model_max_length,      truncation=SCREAMING_SNAKE_CASE_,      return_tensors="""pt""",      )
						UpperCAmelCase_:		Union[str, Any]       			=    text_inputs.input_ids
						UpperCAmelCase_:		Any       			=    self.tokenizer(SCREAMING_SNAKE_CASE_,      padding="""longest""",      return_tensors="""pt"""				).input_ids

						if untruncated_ids.shape[-1] >= text_input_ids.shape[-1] and not torch.equal(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				):
									UpperCAmelCase_:		int       			=    self.tokenizer.batch_decode(untruncated_ids[:, self.tokenizer.model_max_length - 1 : -1]				)
									logger.warning(
									    """The following part of your input was truncated because CLIP can only handle sequences up to"""
									    f' {self.tokenizer.model_max_length} tokens: {removed_text}'				)

						UpperCAmelCase_:		Optional[int]       			=    self.text_encoder(text_input_ids.to(SCREAMING_SNAKE_CASE_				)				)
						UpperCAmelCase_:		Optional[Any]       			=    text_encoder_output.text_embeds

						UpperCAmelCase_:		Optional[int]       			=    prompt_embeds.repeat_interleave(SCREAMING_SNAKE_CASE_,      dim=0				)
						# in Shap-E it normalize the prompt_embeds and then later rescale it
						UpperCAmelCase_:		List[Any]       			=    prompt_embeds / torch.linalg.norm(SCREAMING_SNAKE_CASE_,      dim=-1,      keepdim=SCREAMING_SNAKE_CASE_				)

						if do_classifier_free_guidance:
									UpperCAmelCase_:		str       			=    torch.zeros_like(SCREAMING_SNAKE_CASE_				)

									# For classifier free guidance, we need to do two forward passes.
									# Here we concatenate the unconditional and text embeddings into a single batch
									# to avoid doing two forward passes
									UpperCAmelCase_:		List[str]       			=    torch.cat([negative_prompt_embeds, prompt_embeds]				)

						# Rescale the features to have unit variance
						UpperCAmelCase_:		List[Any]       			=    math.sqrt(prompt_embeds.shape[1]				) * prompt_embeds

						return prompt_embeds

			@torch.no_grad()
			@replace_example_docstring(SCREAMING_SNAKE_CASE_				)
			def __call__(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_ = 1,      SCREAMING_SNAKE_CASE_ = 25,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = None,      SCREAMING_SNAKE_CASE_ = 4.0,      SCREAMING_SNAKE_CASE_ = 64,      SCREAMING_SNAKE_CASE_ = "pil",      SCREAMING_SNAKE_CASE_ = True,      )					->      List[Any]:

						if isinstance(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				):
									UpperCAmelCase_:		int       			=    1
						elif isinstance(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				):
									UpperCAmelCase_:		Dict       			=    len(SCREAMING_SNAKE_CASE_				)
						else:
									raise ValueError(f'`prompt` has to be of type `str` or `list` but is {type(SCREAMING_SNAKE_CASE_				)}'				)

						UpperCAmelCase_:		List[Any]       			=    self._execution_device

						UpperCAmelCase_:		Optional[Any]       			=    batch_size * num_images_per_prompt

						UpperCAmelCase_:		List[str]       			=    guidance_scale > 1.0
						UpperCAmelCase_:		Any       			=    self._encode_prompt(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)

						# prior

						self.scheduler.set_timesteps(SCREAMING_SNAKE_CASE_,      device=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		int       			=    self.scheduler.timesteps

						UpperCAmelCase_:		List[str]       			=    self.prior.config.num_embeddings
						UpperCAmelCase_:		Dict       			=    self.prior.config.embedding_dim

						UpperCAmelCase_:		List[Any]       			=    self.prepare_latents(
						    (batch_size, num_embeddings * embedding_dim),      prompt_embeds.dtype,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      self.scheduler,      )

						# YiYi notes: for testing only to match ldm, we can directly create a latents with desired shape: batch_size, num_embeddings, embedding_dim
						UpperCAmelCase_:		Dict       			=    latents.reshape(latents.shape[0],      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)

						for i, t in enumerate(self.progress_bar(SCREAMING_SNAKE_CASE_				)				):
									# expand the latents if we are doing classifier free guidance
									UpperCAmelCase_:		List[Any]       			=    torch.cat([latents] * 2				) if do_classifier_free_guidance else latents
									UpperCAmelCase_:		List[Any]       			=    self.scheduler.scale_model_input(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)

									UpperCAmelCase_:		str       			=    self.prior(
									    SCREAMING_SNAKE_CASE_,      timestep=SCREAMING_SNAKE_CASE_,      proj_embedding=SCREAMING_SNAKE_CASE_,      ).predicted_image_embedding

									# remove the variance
									UpperCAmelCase_:		Any       			=    noise_pred.split(
									    scaled_model_input.shape[2],      dim=2				)  # batch_size, num_embeddings, embedding_dim

									if do_classifier_free_guidance is not None:
												UpperCAmelCase_:		Dict       			=    noise_pred.chunk(2				)
												UpperCAmelCase_:		List[str]       			=    noise_pred_uncond + guidance_scale * (noise_pred - noise_pred_uncond)

									UpperCAmelCase_:		Any       			=    self.scheduler.step(
									    SCREAMING_SNAKE_CASE_,      timestep=SCREAMING_SNAKE_CASE_,      sample=SCREAMING_SNAKE_CASE_,      ).prev_sample

						if output_type == "latent":
									return ShapEPipelineOutput(images=SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		Tuple       			=    []
						for i, latent in enumerate(SCREAMING_SNAKE_CASE_				):
									UpperCAmelCase_:		Tuple       			=    self.renderer.decode(
									    latent[None, :],      SCREAMING_SNAKE_CASE_,      size=SCREAMING_SNAKE_CASE_,      ray_batch_size=4096,      n_coarse_samples=64,      n_fine_samples=128,      )
									images.append(SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		str       			=    torch.stack(SCREAMING_SNAKE_CASE_				)

						if output_type not in ["np", "pil"]:
									raise ValueError(f'Only the output types `pil` and `np` are supported not output_type={output_type}'				)

						UpperCAmelCase_:		int       			=    images.cpu().numpy()

						if output_type == "pil":
									UpperCAmelCase_:		List[Any]       			=    [self.numpy_to_pil(SCREAMING_SNAKE_CASE_				) for image in images]

						# Offload last model to CPU
						if hasattr(self,      """final_offload_hook"""				) and self.final_offload_hook is not None:
									self.final_offload_hook.offload()

						if not return_dict:
									return (images,)

						return ShapEPipelineOutput(images=SCREAMING_SNAKE_CASE_				)











from __future__ import annotations

import unittest

from transformers import AutoTokenizer, GPTJConfig, is_tf_available
from transformers.testing_utils import require_tf, slow, tooslow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin
from ...utils.test_modeling_tf_core import TFCoreModelTesterMixin


if is_tf_available():
	import tensorflow as tf

	from transformers.models.gptj.modeling_tf_gptj import (
	    TFGPTJForCausalLM,
	    TFGPTJForQuestionAnswering,
	    TFGPTJForSequenceClassification,
	    TFGPTJModel,
	    shape_list,
	)







class 							_a							:

			def __init__(self,      SCREAMING_SNAKE_CASE_				)					->      Any:
						UpperCAmelCase_:		List[str]       			=    parent
						UpperCAmelCase_:		Tuple       			=    13
						UpperCAmelCase_:		Optional[Any]       			=    7
						UpperCAmelCase_:		List[Any]       			=    True
						UpperCAmelCase_:		List[str]       			=    True
						UpperCAmelCase_:		int       			=    True
						UpperCAmelCase_:		Optional[int]       			=    True
						UpperCAmelCase_:		str       			=    True
						UpperCAmelCase_:		Union[str, Any]       			=    99
						UpperCAmelCase_:		str       			=    32
						UpperCAmelCase_:		str       			=    4
						UpperCAmelCase_:		str       			=    2
						UpperCAmelCase_:		Dict       			=    4
						UpperCAmelCase_:		List[Any]       			=    37
						UpperCAmelCase_:		str       			=    """gelu"""
						UpperCAmelCase_:		int       			=    0.1
						UpperCAmelCase_:		Optional[int]       			=    0.1
						UpperCAmelCase_:		int       			=    512
						UpperCAmelCase_:		List[str]       			=    16
						UpperCAmelCase_:		Optional[int]       			=    2
						UpperCAmelCase_:		List[Any]       			=    0.0_2
						UpperCAmelCase_:		Tuple       			=    3
						UpperCAmelCase_:		Tuple       			=    4
						UpperCAmelCase_:		Union[str, Any]       			=    None
						UpperCAmelCase_:		Dict       			=    self.vocab_size - 1
						UpperCAmelCase_:		Union[str, Any]       			=    self.vocab_size - 1
						UpperCAmelCase_:		Any       			=    self.vocab_size - 1

			def     __snake_case					(self				)					->      Tuple:
						UpperCAmelCase_:		str       			=    ids_tensor([self.batch_size, self.seq_length],      self.vocab_size				)

						UpperCAmelCase_:		Tuple       			=    None
						if self.use_input_mask:
									UpperCAmelCase_:		int       			=    random_attention_mask([self.batch_size, self.seq_length]				)

						UpperCAmelCase_:		Tuple       			=    None
						if self.use_token_type_ids:
									UpperCAmelCase_:		List[Any]       			=    ids_tensor([self.batch_size, self.seq_length],      self.type_vocab_size				)

						UpperCAmelCase_:		Optional[Any]       			=    None
						if self.use_mc_token_ids:
									UpperCAmelCase_:		str       			=    ids_tensor([self.batch_size, self.num_choices],      self.seq_length				)

						UpperCAmelCase_:		int       			=    None
						UpperCAmelCase_:		Optional[int]       			=    None
						UpperCAmelCase_:		Union[str, Any]       			=    None
						if self.use_labels:
									UpperCAmelCase_:		List[str]       			=    ids_tensor([self.batch_size],      self.type_sequence_label_size				)
									UpperCAmelCase_:		str       			=    ids_tensor([self.batch_size, self.seq_length],      self.num_labels				)
									UpperCAmelCase_:		int       			=    ids_tensor([self.batch_size],      self.num_choices				)

						UpperCAmelCase_:		List[str]       			=    GPTJConfig(
						    vocab_size=self.vocab_size,      n_embd=self.hidden_size,      n_layer=self.num_hidden_layers,      n_head=self.num_attention_heads,      intermediate_size=self.intermediate_size,      hidden_act=self.hidden_act,      hidden_dropout_prob=self.hidden_dropout_prob,      attention_probs_dropout_prob=self.attention_probs_dropout_prob,      n_positions=self.max_position_embeddings,      type_vocab_size=self.type_vocab_size,      initializer_range=self.initializer_range,      bos_token_id=self.bos_token_id,      eos_token_id=self.eos_token_id,      pad_token_id=self.pad_token_id,      rotary_dim=self.rotary_dim,      return_dict=SCREAMING_SNAKE_CASE_,      )

						UpperCAmelCase_:		int       			=    ids_tensor([self.num_hidden_layers, self.num_attention_heads],      2				)

						return (
						    config,
						    input_ids,
						    input_mask,
						    head_mask,
						    token_type_ids,
						    mc_token_ids,
						    sequence_labels,
						    token_labels,
						    choice_labels,
						)

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      *SCREAMING_SNAKE_CASE_				)					->      Dict:
						UpperCAmelCase_:		Any       			=    TFGPTJModel(config=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		str       			=    {
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}
						UpperCAmelCase_:		str       			=    model(SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		Optional[int]       			=    [input_ids, None, input_mask]  # None is the input for 'past'
						UpperCAmelCase_:		List[str]       			=    model(SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		Tuple       			=    model(SCREAMING_SNAKE_CASE_				)

						self.parent.assertEqual(result.last_hidden_state.shape,      (self.batch_size, self.seq_length, self.hidden_size)				)

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      *SCREAMING_SNAKE_CASE_				)					->      Union[str, Any]:
						UpperCAmelCase_:		List[Any]       			=    TFGPTJModel(config=SCREAMING_SNAKE_CASE_				)

						# first forward pass
						UpperCAmelCase_:		Dict       			=    model(SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      use_cache=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		Optional[Any]       			=    model(SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		Optional[Any]       			=    model(SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      use_cache=SCREAMING_SNAKE_CASE_				)

						self.parent.assertTrue(len(SCREAMING_SNAKE_CASE_				) == len(SCREAMING_SNAKE_CASE_				)				)
						self.parent.assertTrue(len(SCREAMING_SNAKE_CASE_				) == len(SCREAMING_SNAKE_CASE_				) + 1				)

						UpperCAmelCase_			,       UpperCAmelCase_:		List[Any]       			=    outputs.to_tuple()

						# create hypothetical next token and extent to next_input_ids
						UpperCAmelCase_:		Tuple       			=    ids_tensor((self.batch_size, 1),      config.vocab_size				)
						UpperCAmelCase_:		Any       			=    ids_tensor([self.batch_size, 1],      self.type_vocab_size				)

						# append to next input_ids and token_type_ids
						UpperCAmelCase_:		Dict       			=    tf.concat([input_ids, next_tokens],      axis=-1				)
						UpperCAmelCase_:		Dict       			=    tf.concat([token_type_ids, next_token_types],      axis=-1				)

						UpperCAmelCase_:		Tuple       			=    model(SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_				)["""last_hidden_state"""]
						UpperCAmelCase_:		List[str]       			=    model(SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      past_key_values=SCREAMING_SNAKE_CASE_				)[
						    """last_hidden_state"""
						]

						# select random slice
						UpperCAmelCase_:		List[Any]       			=    int(ids_tensor((1,),      shape_list(SCREAMING_SNAKE_CASE_				)[-1]				)				)
						UpperCAmelCase_:		Any       			=    output_from_no_past[:, -1, random_slice_idx]
						UpperCAmelCase_:		str       			=    output_from_past[:, 0, random_slice_idx]

						# test that outputs are equal for slice
						tf.debugging.assert_near(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      rtol=1E-6				)

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      *SCREAMING_SNAKE_CASE_				)					->      int:
						UpperCAmelCase_:		List[str]       			=    TFGPTJModel(config=SCREAMING_SNAKE_CASE_				)

						# create attention mask
						UpperCAmelCase_:		Tuple       			=    self.seq_length // 2
						UpperCAmelCase_:		str       			=    tf.ones((self.batch_size, half_seq_length),      dtype=tf.intaa				)
						UpperCAmelCase_:		Dict       			=    tf.zeros((self.batch_size, self.seq_length - half_seq_length),      dtype=tf.intaa				)
						UpperCAmelCase_:		str       			=    tf.concat([attn_mask_begin, attn_mask_end],      axis=1				)

						# first forward pass
						UpperCAmelCase_			,       UpperCAmelCase_:		Optional[Any]       			=    model(SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_				).to_tuple()

						# create hypothetical next token and extent to next_input_ids
						UpperCAmelCase_:		Dict       			=    ids_tensor((self.batch_size, 1),      config.vocab_size				)

						# change a random masked slice from input_ids
						UpperCAmelCase_:		Any       			=    ids_tensor((1,),      SCREAMING_SNAKE_CASE_				).numpy() + 1
						UpperCAmelCase_:		Any       			=    ids_tensor((self.batch_size, self.seq_length),      config.vocab_size				)
						UpperCAmelCase_:		List[Any]       			=    tf.range(self.seq_length				) == (self.seq_length - random_seq_idx_to_change)
						UpperCAmelCase_:		Optional[int]       			=    tf.transpose(
						    tf.broadcast_to(tf.expand_dims(SCREAMING_SNAKE_CASE_,      -1				),      (self.seq_length, self.batch_size)				)				)
						UpperCAmelCase_:		Any       			=    tf.where(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)

						# append to next input_ids and attn_mask
						UpperCAmelCase_:		Any       			=    tf.concat([input_ids, next_tokens],      axis=-1				)
						UpperCAmelCase_:		List[str]       			=    tf.concat([attn_mask, tf.ones((shape_list(SCREAMING_SNAKE_CASE_				)[0], 1),      dtype=tf.intaa				)],      axis=1				)

						# get two different outputs
						UpperCAmelCase_:		List[Any]       			=    model(SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_				)["""last_hidden_state"""]
						UpperCAmelCase_:		Optional[Any]       			=    model(SCREAMING_SNAKE_CASE_,      past_key_values=SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_				)[
						    """last_hidden_state"""
						]

						# select random slice
						UpperCAmelCase_:		Union[str, Any]       			=    int(ids_tensor((1,),      shape_list(SCREAMING_SNAKE_CASE_				)[-1]				)				)
						UpperCAmelCase_:		Tuple       			=    output_from_no_past[:, -1, random_slice_idx]
						UpperCAmelCase_:		int       			=    output_from_past[:, 0, random_slice_idx]

						# test that outputs are equal for slice
						tf.debugging.assert_near(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      rtol=1E-12				)

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      *SCREAMING_SNAKE_CASE_				)					->      Union[str, Any]:
						UpperCAmelCase_:		int       			=    TFGPTJModel(config=SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		Optional[int]       			=    input_ids[:1, :]
						UpperCAmelCase_:		Tuple       			=    input_mask[:1, :]
						UpperCAmelCase_:		Union[str, Any]       			=    token_type_ids[:1, :]
						UpperCAmelCase_:		Any       			=    1

						# first forward pass
						UpperCAmelCase_:		Optional[Any]       			=    model(SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      use_cache=SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_			,       UpperCAmelCase_:		Optional[int]       			=    outputs.to_tuple()

						# create hypothetical next token and extent to next_input_ids
						UpperCAmelCase_:		Dict       			=    ids_tensor((self.batch_size, 3),      config.vocab_size				)
						UpperCAmelCase_:		List[str]       			=    ids_tensor((self.batch_size, 3),      2				)
						UpperCAmelCase_:		Optional[int]       			=    ids_tensor((self.batch_size, 3),      self.type_vocab_size				)

						# append to next input_ids and token_type_ids
						UpperCAmelCase_:		Any       			=    tf.concat([input_ids, next_tokens],      axis=-1				)
						UpperCAmelCase_:		List[Any]       			=    tf.concat([input_mask, next_attn_mask],      axis=-1				)
						UpperCAmelCase_:		Tuple       			=    tf.concat([token_type_ids, next_token_types],      axis=-1				)

						UpperCAmelCase_:		str       			=    model(
						    SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_				)["""last_hidden_state"""]
						UpperCAmelCase_:		Optional[Any]       			=    model(
						    SCREAMING_SNAKE_CASE_,      token_type_ids=SCREAMING_SNAKE_CASE_,      attention_mask=SCREAMING_SNAKE_CASE_,      past_key_values=SCREAMING_SNAKE_CASE_,      )["""last_hidden_state"""]
						self.parent.assertTrue(output_from_past.shape[1] == next_tokens.shape[1]				)

						# select random slice
						UpperCAmelCase_:		List[str]       			=    int(ids_tensor((1,),      shape_list(SCREAMING_SNAKE_CASE_				)[-1]				)				)
						UpperCAmelCase_:		List[str]       			=    output_from_no_past[:, -3:, random_slice_idx]
						UpperCAmelCase_:		List[str]       			=    output_from_past[:, :, random_slice_idx]

						# test that outputs are equal for slice
						tf.debugging.assert_near(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      rtol=1E-3				)

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      *SCREAMING_SNAKE_CASE_				)					->      Optional[int]:
						UpperCAmelCase_:		Tuple       			=    TFGPTJForCausalLM(config=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		Optional[int]       			=    {
						    """input_ids""": input_ids,
						    """attention_mask""": input_mask,
						    """token_type_ids""": token_type_ids,
						}
						UpperCAmelCase_:		Union[str, Any]       			=    model(SCREAMING_SNAKE_CASE_				)
						self.parent.assertEqual(result.logits.shape,      (self.batch_size, self.seq_length, self.vocab_size)				)

			def     __snake_case					(self				)					->      Optional[Any]:
						UpperCAmelCase_:		Optional[int]       			=    self.prepare_config_and_inputs()

						(
						    (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       (
						    UpperCAmelCase_
						)			,       
						):		Optional[int]       			=    config_and_inputs

						UpperCAmelCase_:		int       			=    {
						    """input_ids""": input_ids,
						    """token_type_ids""": token_type_ids,
						    """attention_mask""": input_mask,
						}
						return config, inputs_dict







@require_tf
class 							_a							(      _lowerCAmelCase   ,		_lowerCAmelCase   ,		_lowerCAmelCase   ,		unittest.TestCase				):
			A					=  (
			    (TFGPTJForCausalLM, TFGPTJForSequenceClassification, TFGPTJForQuestionAnswering, TFGPTJModel)
			    if is_tf_available()
			    else ()
			)

			A					=  (TFGPTJForCausalLM,) if is_tf_available() else ()
			A					=  (
			    {
			        '''feature-extraction''': TFGPTJModel,
			        '''question-answering''': TFGPTJForQuestionAnswering,
			        '''text-classification''': TFGPTJForSequenceClassification,
			        '''text-generation''': TFGPTJForCausalLM,
			        '''zero-shot''': TFGPTJForSequenceClassification,
			    }
			    if is_tf_available()
			    else {}
			)
			A					=  False
			A					=  False
			A					=  False
			A					=  False

			def     __snake_case					(self,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)					->      int:
						if (
						    pipeline_test_casse_name == "QAPipelineTests"
						    and tokenizer_name is not None
						    and not tokenizer_name.endswith("""Fast"""				)
						):
									# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
									# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
									# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
									return True

						return False

			def     __snake_case					(self				)					->      Tuple:
						UpperCAmelCase_:		Union[str, Any]       			=    TFGPTJModelTester(self				)
						UpperCAmelCase_:		str       			=    ConfigTester(self,      config_class=SCREAMING_SNAKE_CASE_,      n_embd=37				)

			def     __snake_case					(self				)					->      Optional[Any]:
						self.config_tester.run_common_tests()

			def     __snake_case					(self				)					->      Optional[int]:
						UpperCAmelCase_:		Optional[Any]       			=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_gptj_model(*SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      Tuple:
						UpperCAmelCase_:		Tuple       			=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_gptj_model_past(*SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      Any:
						UpperCAmelCase_:		Dict       			=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_gptj_model_attention_mask_past(*SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      Dict:
						UpperCAmelCase_:		List[Any]       			=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_gptj_model_past_large_inputs(*SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      str:
						UpperCAmelCase_:		Dict       			=    self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_gptj_lm_head_model(*SCREAMING_SNAKE_CASE_				)

			@slow
			@unittest.skipIf(
			    not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""				)				) > 0,      """skip testing on GPU for now to avoid GPU OOM.""",      )
			def     __snake_case					(self				)					->      Any:
						UpperCAmelCase_:		List[str]       			=    TFGPTJModel.from_pretrained("""EleutherAI/gpt-j-6B""",      from_pt=SCREAMING_SNAKE_CASE_				)
						self.assertIsNotNone(SCREAMING_SNAKE_CASE_				)

			@unittest.skip(reason="""Currently, model embeddings are going to undergo a major refactor."""				)
			def     __snake_case					(self				)					->      Tuple:
						super().test_resize_token_embeddings()







@require_tf
@tooslow
# Marked as @tooslow due to GPU OOM -- but still useful to run locally. Requires ~39GB of RAM.
class 							_a							(      unittest.TestCase				):

			def     __snake_case					(self				)					->      str:
						UpperCAmelCase_:		List[Any]       			=    TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B""",      from_pt=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		List[str]       			=    tf.convert_to_tensor([[464, 3290]],      dtype=tf.intaa				)  # The dog
						# fmt: off
						# The dog is a man's best friend. It is a loyal companion, and it is a friend
						UpperCAmelCase_:		str       			=    [464, 3290, 318, 257, 582, 338, 1266, 1545, 13, 632, 318, 257, 9112, 15185, 11, 290, 340, 318, 257, 1545]
						# fmt: on
						UpperCAmelCase_:		Tuple       			=    model.generate(SCREAMING_SNAKE_CASE_,      do_sample=SCREAMING_SNAKE_CASE_				)
						self.assertListEqual(output_ids[0].numpy().tolist(),      SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      Any:
						UpperCAmelCase_:		Optional[int]       			=    AutoTokenizer.from_pretrained("""EleutherAI/gpt-j-6B""",      revision="""float16"""				)
						UpperCAmelCase_:		Dict       			=    TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B""",      revision="""float16""",      from_pt=SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		Tuple       			=    tokenizer("""Today is a nice day and""",      return_tensors="""tf"""				)
						# forces the generation to happen on CPU, to avoid GPU-related quirks
						with tf.device(""":/CPU:0"""				):
									UpperCAmelCase_:		List[Any]       			=    model.generate(**SCREAMING_SNAKE_CASE_,      do_sample=SCREAMING_SNAKE_CASE_,      seed=[42, 0]				)
						UpperCAmelCase_:		Dict       			=    tokenizer.decode(output_ids[0],      skip_special_tokens=SCREAMING_SNAKE_CASE_				)

						UpperCAmelCase_:		Union[str, Any]       			=    """Today is a nice day and I’m going to go for a walk. I’"""
						self.assertEqual(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      List[Any]:
						UpperCAmelCase_:		List[Any]       			=    TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B""",      revision="""float16""",      from_pt=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		int       			=    AutoTokenizer.from_pretrained("""EleutherAI/gpt-j-6B""",      revision="""float16"""				)

						UpperCAmelCase_:		int       			=    """left"""

						# Define PAD Token = EOS Token = 50256
						UpperCAmelCase_:		int       			=    tokenizer.eos_token
						UpperCAmelCase_:		Optional[int]       			=    model.config.eos_token_id

						# use different length sentences to test batching
						UpperCAmelCase_:		Any       			=    [
						    """Hello, my dog is a little""",
						    """Today, I""",
						]
						UpperCAmelCase_:		Any       			=    [
						    """Hello, my dog is a little over a year old and has been diagnosed with hip dysplasia""",
						    """Today, I’m going to be talking about a topic that’""",
						]
						return model, tokenizer, sentences, expected_output_sentences

			def     __snake_case					(self				)					->      Any:
						# Confirms that we get the expected results with left-padded beam search
						UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_:		Optional[Any]       			=    self._get_beam_search_test_objects()

						UpperCAmelCase_:		Optional[int]       			=    tokenizer(SCREAMING_SNAKE_CASE_,      return_tensors="""tf""",      padding=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		Dict       			=    model.generate(**SCREAMING_SNAKE_CASE_,      do_sample=SCREAMING_SNAKE_CASE_,      num_beams=2				)
						UpperCAmelCase_:		Optional[Any]       			=    tokenizer.batch_decode(SCREAMING_SNAKE_CASE_,      skip_special_tokens=SCREAMING_SNAKE_CASE_				)
						self.assertListEqual(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)

			def     __snake_case					(self				)					->      List[Any]:
						# Confirms that left-padding is working properly
						UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_:		List[Any]       			=    self._get_beam_search_test_objects()

						UpperCAmelCase_:		List[Any]       			=    tokenizer(SCREAMING_SNAKE_CASE_,      return_tensors="""tf""",      padding=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		Dict       			=    tokenizer(sentences[0],      return_tensors="""tf"""				)
						UpperCAmelCase_:		Optional[Any]       			=    model.generate(**SCREAMING_SNAKE_CASE_,      do_sample=SCREAMING_SNAKE_CASE_,      num_beams=2				)
						UpperCAmelCase_:		Optional[int]       			=    (
						    shape_list(inputs_non_padded["""input_ids"""]				)[-1]
						    - tf.reduce_sum(tf.cast(inputs["""attention_mask"""][-1],      tf.intaa				)				).numpy()
						)
						UpperCAmelCase_:		List[str]       			=    tokenizer(sentences[1],      return_tensors="""tf"""				)
						UpperCAmelCase_:		Any       			=    model.generate(
						    **SCREAMING_SNAKE_CASE_,      do_sample=SCREAMING_SNAKE_CASE_,      num_beams=2,      max_length=model.config.max_length - num_paddings				)
						UpperCAmelCase_:		Dict       			=    tokenizer.decode(output_non_padded[0],      skip_special_tokens=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		str       			=    tokenizer.decode(output_padded[0],      skip_special_tokens=SCREAMING_SNAKE_CASE_				)
						self.assertListEqual(SCREAMING_SNAKE_CASE_,      [non_padded_sentence, padded_sentence]				)

			def     __snake_case					(self				)					->      str:
						# Confirms that XLA is working properly
						UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_			,       UpperCAmelCase_:		Optional[int]       			=    self._get_beam_search_test_objects()

						UpperCAmelCase_:		Tuple       			=    tokenizer(SCREAMING_SNAKE_CASE_,      return_tensors="""tf""",      padding=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		int       			=    tf.function(model.generate,      jit_compile=SCREAMING_SNAKE_CASE_				)
						UpperCAmelCase_:		Union[str, Any]       			=    xla_generate(**SCREAMING_SNAKE_CASE_,      do_sample=SCREAMING_SNAKE_CASE_,      num_beams=2				)
						UpperCAmelCase_:		Any       			=    tokenizer.batch_decode(SCREAMING_SNAKE_CASE_,      skip_special_tokens=SCREAMING_SNAKE_CASE_				)
						self.assertListEqual(SCREAMING_SNAKE_CASE_,      SCREAMING_SNAKE_CASE_				)






