
    +h                     ,   S SK r S SKrS SKrS SKrS SKJr  S SKJrJ	r	J
r
JrJrJr  S SKrS SKJrJrJr  SSKJrJr  SSKJr  SSKJr  SSKJr  SS	KJr  SS
KJr  SSK J!r!J"r"J#r#J$r$J%r%J&r&J'r'  SSK(J)r)  SSK*J+r+J,r,  \%" 5       (       a  S SK-J.s  J/r0  Sr1OSr1\&Rd                  " \35      r4\#" 5       (       a  S SK5J6r6  \$" 5       (       a  S SK7r7Sr8    SS\\9   S\\\:\Rv                  4      S\\
\9      S\\
\<      4S jjr= " S S\+5      r> " S S\>5      r?g)    N)CallableDictListOptionalTupleUnion)GemmaPreTrainedModelGemmaTokenizerGemmaTokenizerFast   )MultiPipelineCallbacksPipelineCallback)VaeImageProcessor)AutoencoderKL)get_2d_rotary_pos_embed_lumina)LuminaNextDiT2DModel)FlowMatchEulerDiscreteScheduler)BACKENDS_MAPPING	deprecateis_bs4_availableis_ftfy_availableis_torch_xla_availableloggingreplace_example_docstring)randn_tensor   )DiffusionPipelineImagePipelineOutputTF)BeautifulSoupaA  
    Examples:
        ```py
        >>> import torch
        >>> from diffusers import LuminaPipeline

        >>> pipe = LuminaPipeline.from_pretrained("Alpha-VLLM/Lumina-Next-SFT-diffusers", torch_dtype=torch.bfloat16)
        >>> # Enable memory optimizations.
        >>> pipe.enable_model_cpu_offload()

        >>> prompt = "Upper body of a young woman in a Victorian-era outfit with brass goggles and leather straps. Background shows an industrial revolution cityscape with smoky skies and tall, metal structures"
        >>> image = pipe(prompt).images[0]
        ```
num_inference_stepsdevice	timestepssigmasc                    Ub  Ub  [        S5      eUb  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
X2S.UD6  U R                  n[        U5      nX14$ Ub  S[        [        R                  " U R                  5      R
                  R                  5       5      ;   nU(       d  [        SU R                   S35      eU R                  " S
XBS.UD6  U R                  n[        U5      nX14$ U R                  " U4S	U0UD6  U R                  nX14$ )a  
Calls the scheduler's `set_timesteps` method and retrieves timesteps from the scheduler after the call. Handles
custom timesteps. Any kwargs will be supplied to `scheduler.set_timesteps`.

Args:
    scheduler (`SchedulerMixin`):
        The scheduler to get timesteps from.
    num_inference_steps (`int`):
        The number of diffusion steps used when generating samples with a pre-trained model. If used, `timesteps`
        must be `None`.
    device (`str` or `torch.device`, *optional*):
        The device to which the timesteps should be moved to. If `None`, the timesteps are not moved.
    timesteps (`List[int]`, *optional*):
        Custom timesteps used to override the timestep spacing strategy of the scheduler. If `timesteps` is passed,
        `num_inference_steps` and `sigmas` must be `None`.
    sigmas (`List[float]`, *optional*):
        Custom sigmas used to override the timestep spacing strategy of the scheduler. If `sigmas` is passed,
        `num_inference_steps` and `timesteps` must be `None`.

Returns:
    `Tuple[torch.Tensor, int]`: A tuple where the first element is the timestep schedule from the scheduler and the
    second element is the number of inference steps.
zYOnly one of `timesteps` or `sigmas` can be passed. Please choose one to set custom valuesr"   zThe current scheduler class zx's `set_timesteps` does not support custom timestep schedules. Please check whether you are using the correct scheduler.)r"   r!   r#   zv's `set_timesteps` does not support custom sigmas schedules. Please check whether you are using the correct scheduler.)r#   r!   r!    )

ValueErrorsetinspect	signatureset_timesteps
parameterskeys	__class__r"   len)	schedulerr    r!   r"   r#   kwargsaccepts_timestepsaccept_sigmass           d/home/james-whalen/.local/lib/python3.13/site-packages/diffusers/pipelines/lumina/pipeline_lumina.pyretrieve_timestepsr4   M   s}   > !3tuu'3w/@/@AXAX/Y/d/d/i/i/k+ll .y/B/B.C Da b  	M)MfM''	!)n )) 
	 C(9(9):Q:Q(R(](](b(b(d$ee.y/B/B.C D_ `  	GvGG''	!)n )) 	 3MFMfM''	))    c            3       B  ^  \ rS rSrSr\R                  " S5      r/ rSr	SS/r
S\S\S	\S
\S\\\4   4
U 4S jjr    S9S\\\\   4   S\S\\R0                     S\\   S\\   4
S jjr         S:S\\\\   4   S\S\\\\   4   S\S\\R0                     S\\R6                     S\\R6                     S\\R6                     S\\R6                     S\4S jjrS r     S;S jrS<S jrS r S=S  jr!\"S! 5       r#\"S" 5       r$\"S# 5       r%\RL                  " 5       \'" \(5      SSSS$S%SSSSSSSSSS&SSS'S(SSS/4S\\\\   4   S)\\   S*\\   S+\S,\)S\\\\   4   S-\\)   S\\   S.\\\RT                  \\RT                     4      S\\R6                     S\\R6                     S\\R6                     S\\R6                     S\\R6                     S/\\   S0\S\S1\S2\\)   S3\\   S4\\\+\\\,/S4   \-\.4      S5\\   S6\\/\04   4.S7 jj5       5       r1S8r2U =r3$ )>LuminaPipeline   aW  
Pipeline for text-to-image generation using Lumina-T2I.

This model inherits from [`DiffusionPipeline`]. Check the superclass documentation for the generic methods the
library implements for all the pipelines (such as downloading or saving, running on a particular device, etc.)

Args:
    vae ([`AutoencoderKL`]):
        Variational Auto-Encoder (VAE) Model to encode and decode images to and from latent representations.
    text_encoder ([`GemmaPreTrainedModel`]):
        Frozen Gemma text-encoder.
    tokenizer (`GemmaTokenizer` or `GemmaTokenizerFast`):
        Gemma tokenizer.
    transformer ([`Transformer2DModel`]):
        A text conditioned `Transformer2DModel` to denoise the encoded image latents.
    scheduler ([`SchedulerMixin`]):
        A scheduler to be used in combination with `transformer` to denoise the encoded image latents.
u5   [#®•©™&@·º½¾¿¡§~\)\(\]\[\}\{\|\\/\*]{1,}ztext_encoder->transformer->vaelatentsprompt_embedstransformerr/   vaetext_encoder	tokenizerc                 Z  > [         TU ]  5         U R                  UUUUUS9  SU l        [	        U R                  S9U l        SU l        [        U S5      (       a-  U R                  b   U R                  R                  R                  OSU l        U R                  U R                  -  U l        g )N)r<   r=   r>   r;   r/      )vae_scale_factor   r;      )super__init__register_modulesrA   r   image_processormax_sequence_lengthhasattrr;   configsample_sizedefault_sample_sizedefault_image_size)selfr;   r/   r<   r=   r>   r-   s         r3   rE   LuminaPipeline.__init__   s     	%# 	 	
 !"0$BWBWX#&  t]++0@0@0L ##// 	 
 #'":":T=R=R"Rr5      Npromptnum_images_per_promptr!   clean_caption
max_lengthc           	      b   U=(       d    U R                   n[        U[        5      (       a  U/OUn[        U5      nU R	                  XS9nU R                  USU R                  SSSS9nUR                  R                  U5      nU R                  USSS9R                  R                  U5      n	U	R                  S   UR                  S   :  aq  [        R                  " X5      (       dV  U R
                  R                  U	S S 2U R                  S	-
  S24   5      n
[        R                  S
U R                   SU
 35        UR                  R                  U5      nU R!                  XSS9nUR"                  S   nU R                   b  U R                   R$                  nO&U R&                  b  U R&                  R$                  nOS nUR                  XS9nUR                  u  pnUR)                  S	US	5      nUR+                  Xb-  US5      nUR)                  US	5      nUR+                  Xb-  S5      nX4$ )N)rS   r@   Tpt)pad_to_multiple_ofrT   
truncationpaddingreturn_tensorslongest)rY   rZ   rP   z]The following part of your input was truncated because Gemma can only handle sequences up to z	 tokens: attention_maskoutput_hidden_statesdtyper!   )_execution_device
isinstancestrr.   _text_preprocessingr>   rH   	input_idstoshapetorchequalbatch_decodeloggerwarningr^   r=   hidden_statesrb   r;   repeatview)rN   rQ   rR   r!   rS   rT   
batch_sizetext_inputstext_input_idsuntruncated_idsremoved_textprompt_attention_maskr:   rb   _seq_lens                   r3   _get_gemma_prompt_embeds'LuminaPipeline._get_gemma_prompt_embeds   s>    1411'44&&[
))&)Nnn // % 
 %..11&9..SW.Xbbeeflm  $(<(<R(@@UcIuIu>>66q$JbJbefJfikJkGk7lmLNN,,-Y|nF
 !, : : = =f E))W[ * 
 &33B7(%%++E)$$**EE%((u(D%++A%,,Q0EqI%**:+MwXZ[ 5 < <=RTU V 5 : ::;]_a b33r5   Tdo_classifier_free_guidancenegative_promptnegative_prompt_embedsrw   negative_prompt_attention_maskc                 *   Uc  U R                   n[        U[        5      (       a  U/OUnUb  [        U5      nOUR                  S   nUc  U R                  UUUU
S9u  phU(       Ga  UGc  Ub  UOSn[        U[        5      (       a  X/-  OUnUb;  [        U5      [        U5      La$  [        S[        U5       S[        U5       S35      e[        U[        5      (       a  U/nO0U[        U5      :w  a!  [        SU S[        U5       S	U SU S
3	5      eUR                  S   nU R                  USUSSS9nUR                  R                  U5      nUR                  R                  U5      n	U R                  UU	SS9nU R                  R                  nUR                  S   nUR                  u  nnnUR                  UUS9nUR!                  SUS5      nUR#                  X-  US5      nU	R!                  US5      n	U	R#                  X-  S5      n	XhXy4$ )a  
Encodes the prompt into text encoder hidden states.

Args:
    prompt (`str` or `List[str]`, *optional*):
        prompt to be encoded
    negative_prompt (`str` or `List[str]`, *optional*):
        The prompt not to guide the image generation. If not defined, one has to pass `negative_prompt_embeds`
        instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is less than `1`). For
        Lumina-T2I, this should be "".
    do_classifier_free_guidance (`bool`, *optional*, defaults to `True`):
        whether to use classifier free guidance or not
    num_images_per_prompt (`int`, *optional*, defaults to 1):
        number of images that should be generated per prompt
    device: (`torch.device`, *optional*):
        torch device to place the resulting embeddings on
    prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
        provided, text embeddings will be generated from `prompt` input argument.
    negative_prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated negative text embeddings. For Lumina-T2I, it's should be the embeddings of the "" string.
    clean_caption (`bool`, defaults to `False`):
        If `True`, the function will preprocess and clean the provided caption before encoding.
    max_sequence_length (`int`, defaults to 256): Maximum sequence length to use for the prompt.
r   )rQ   rR   r!   rS    z?`negative_prompt` should be the same type to `prompt`, but got z != .z`negative_prompt`: z has batch size z, but `prompt`: zT. Please make sure that passed `negative_prompt` matches the batch size of `prompt`.rP   rT   TrV   )rY   rT   rX   rZ   r]   r`   ra   r\   )rc   rd   re   r.   ri   rz   type	TypeErrorr&   r>   rg   rh   r^   r=   rb   ro   rp   rq   )rN   rQ   r|   r}   rR   r!   r:   r~   rw   r   rS   r0   rr   prompt_max_lengthnegative_text_inputsnegative_text_input_idsnegative_dtyperx   ry   s                      r3   encode_promptLuminaPipeline.encode_prompt  s   N >++F'44&&VJ&,,Q/J 373P3P&;+	 4Q 40M '+A+I1@1LoRTO AK?\_@`@`j+<<fuO!d6l$:O&OUVZ[jVkUl mV~Q(  OS11#2"3s?33 )/)::J3K_J` ax/
| <33  !. 3 3A 6#'>>$,# $2 $  ';&D&D&G&G&O#-A-P-P-S-STZ-[*%)%6%6'=%) &7 &" "..44N%;%I%I"%M"288MAw%;%>%>^\b%>%c"%;%B%B1F[]^%_"%;%@%@Acelnp%q"-K-R-RShjk-l*-K-P-P2B.* 5Kkkr5   c                 n   S[        [        R                  " U R                  R                  5      R
                  R                  5       5      ;   n0 nU(       a  X$S'   S[        [        R                  " U R                  R                  5      R
                  R                  5       5      ;   nU(       a  XS'   U$ )Neta	generator)r'   r(   r)   r/   stepr+   r,   )rN   r   r   accepts_etaextra_step_kwargsaccepts_generators         r3   prepare_extra_step_kwargs(LuminaPipeline.prepare_extra_step_kwargst  s     s7#4#4T^^5H5H#I#T#T#Y#Y#[\\'*e$ (3w/@/@ATAT/U/`/`/e/e/g+hh-6k*  r5   c
           
        ^  UT R                   S-  -  S:w  d  UT R                   S-  -  S:w  a"  [        ST R                   S-   SU SU S35      eU	bW  [        U 4S jU	 5       5      (       d=  [        ST R                   S	U	 V
s/ s H  oT R                  ;  d  M  U
PM     sn
 35      eUb  Ub  [        S
U SU S35      eUc  Uc  [        S5      eUbA  [	        U[
        5      (       d,  [	        U[        5      (       d  [        S[        U5       35      eUb  Ub  [        S
U SU S35      eUb  Ub  [        SU SU S35      eUb  Uc  [        S5      eUb  Uc  [        S5      eUb  Ub  UR                  UR                  :w  a&  [        SUR                   SUR                   S35      eUR                  UR                  :w  a&  [        SUR                   SUR                   S35      eg g g s  sn
f )Nr   r   z-`height` and `width` have to be divisible by z	 but are z and r   c              3   @   >#    U  H  oTR                   ;   v   M     g 7fN)_callback_tensor_inputs).0krN   s     r3   	<genexpr>.LuminaPipeline.check_inputs.<locals>.<genexpr>  s      F
7Y!---7Ys   z2`callback_on_step_end_tensor_inputs` has to be in z, but found zCannot forward both `prompt`: z and `prompt_embeds`: z2. Please make sure to only forward one of the two.zeProvide either `prompt` or `prompt_embeds`. Cannot leave both `prompt` and `prompt_embeds` undefined.z2`prompt` has to be of type `str` or `list` but is z and `negative_prompt_embeds`: z'Cannot forward both `negative_prompt`: zEMust provide `prompt_attention_mask` when specifying `prompt_embeds`.zWMust provide `negative_prompt_attention_mask` when specifying `negative_prompt_embeds`.zu`prompt_embeds` and `negative_prompt_embeds` must have the same shape when passed directly, but got: `prompt_embeds` z != `negative_prompt_embeds` z`prompt_attention_mask` and `negative_prompt_attention_mask` must have the same shape when passed directly, but got: `prompt_attention_mask` z% != `negative_prompt_attention_mask` )	rA   r&   allr   rd   re   listr   ri   )rN   rQ   heightwidthr}   r:   r~   rw   r   "callback_on_step_end_tensor_inputsr   s   `          r3   check_inputsLuminaPipeline.check_inputs  s    T**Q./14AVAVYZAZ8[_`8`?@U@UXY@Y?ZZcdjckkpqvpwwxy  .9# F
7YF
 C
 C
 DTEaEaDbbn  |^  pH  |^vw  ko  kG  kG  bGpq  |^  pH  oI  J  -";08N}o ^0 0  ^ 5w  FC)@)@TZ\`IaIaQRVW]R^Q_`aa"8"D0 9*++]_ 
 &+A+M9/9J K*++]_ 
 $)>)Fdee!-2P2Xvww$)?)K""&<&B&BB --:-@-@,A B.445Q8 
 %**.L.R.RR 55J5P5P4Q R6<<=Q@  S *L$C pHs   G2G2c                   ^ ^ T(       aT  [        5       (       dE  [        R                  [        S   S   R	                  S5      5        [        R                  S5        SmT(       aT  [        5       (       dE  [        R                  [        S   S   R	                  S5      5        [        R                  S5        Sm[        U[        [        45      (       d  U/nS[        4UU 4S jjnU Vs/ s H
  oC" U5      PM     sn$ s  snf )	Nbs4r\   zSetting `clean_caption=True`z#Setting `clean_caption` to False...Fftfytextc                    > T(       a$  TR                  U 5      n TR                  U 5      n U $ U R                  5       R                  5       n U $ r   )_clean_captionlowerstrip)r   rS   rN   s    r3   process3LuminaPipeline._text_preprocessing.<locals>.process  sH    **40**40 K zz|))+Kr5   )
r   rm   rn   r   formatr   rd   tupler   re   )rN   r   rS   r   ts   ` `  r3   rf   "LuminaPipeline._text_preprocessing  s    !1!3!3NN+E226==>\]^NN@A!M!2!4!4NN+F3B7>>?]^_NN@A!M$..6D	# 	 	 %))Dq
D)))s   *C>c                 ^
   [        U5      n[        R                  " U5      nUR                  5       R	                  5       n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[        USS9R                  n[
        R                  " SSU5      n[
        R                  " S	SU5      n[
        R                  " S
SU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " SSU5      n[
        R                  " S SU5      n[
        R                  " S!SU5      n[
        R                  " U R                  SU5      n[
        R                  " S"SU5      n[
        R                  " S#5      n[        [
        R                  " X!5      5      S$:  a  [
        R                  " USU5      n[        R                  " U5      n[        R                   " [        R                   " U5      5      n[
        R                  " S%SU5      n[
        R                  " S&SU5      n[
        R                  " S'SU5      n[
        R                  " S(SU5      n[
        R                  " S)SU5      n[
        R                  " S*SU5      n[
        R                  " S+SU5      n[
        R                  " S,SU5      n[
        R                  " S-SU5      n[
        R                  " S.SU5      n[
        R                  " S/S0U5      n[
        R                  " S1S2U5      n[
        R                  " S3SU5      nUR                  5         [
        R                  " S4S5U5      n[
        R                  " S6SU5      n[
        R                  " S7SU5      n[
        R                  " S8SU5      nUR                  5       $ )9Nz<person>personzk\b((?:https?:(?:\/{1,3}|[a-zA-Z0-9%])|[a-zA-Z0-9.\-]+[.](?:com|co|ru|net|org|edu|gov|it)[\w/-]*\b\/?(?!@)))r   zh\b((?:www:(?:\/{1,3}|[a-zA-Z0-9%])|[a-zA-Z0-9.\-]+[.](?:com|co|ru|net|org|edu|gov|it)[\w/-]*\b\/?(?!@)))zhtml.parser)featuresz
@[\w\d]+\bz[\u31c0-\u31ef]+z[\u31f0-\u31ff]+z[\u3200-\u32ff]+z[\u3300-\u33ff]+z[\u3400-\u4dbf]+z[\u4dc0-\u4dff]+z[\u4e00-\u9fff]+z|[\u002D\u058A\u05BE\u1400\u1806\u2010-\u2015\u2E17\u2E1A\u2E3A\u2E3B\u2E40\u301C\u3030\u30A0\uFE31\uFE32\uFE58\uFE63\uFF0D]+-u   [`´«»“”¨]"u   [‘’]'z&quot;?z&ampz"\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3} z\d:\d\d\s+$z\\nz
#\d{1,3}\bz	#\d{5,}\bz
\b\d{6,}\bz0[\S]+\.(?:png|jpg|jpeg|bmp|webp|eps|pdf|apk|mp4)z
[\"\']{2,}z[\.]{2,}z\s+\.\s+z	(?:\-|\_)r   z\b[a-zA-Z]{1,3}\d{3,15}\bz\b[a-zA-Z]+\d+[a-zA-Z]+\bz\b\d+[a-zA-Z]+\d+\bz!(worldwide\s+)?(free\s+)?shippingz(free\s)?download(\sfree)?z\bclick\b\s(?:for|on)\s\w+z9\b(?:png|jpg|jpeg|bmp|webp|eps|pdf|apk|mp4)(\simage[s]?)?z\bpage\s+\d+\bz*\b\d*[a-zA-Z]+\d+[a-zA-Z]+\d+[a-zA-Z\d]*\bu   \b\d+\.?\d*[xх×]\d+\.?\d*\bz
\b\s+\:\s+z: z(\D[,\./])\bz\1 z\s+z^[\"\']([\w\W]+)[\"\']$z\1z^[\'\_,\-\:;]z[\'\_,\-\:\-\+]$z^\.\S+$)re   ulunquote_plusr   r   resubr   r   bad_punct_regexcompiler.   findallr   fix_texthtmlunescape)rN   captionregex2s      r3   r   LuminaPipeline._clean_caption  s   g,//'*--/'')&&Xw7&&z

 &&w
  -@EE &&G4 &&,b':&&,b':&&,b':&&,b':&&,b':&&,b':&&,b': && L
 &&-sG<&&c73 &&R1&&"g. &&>WM &&W5 &&g. &&G4&&r73&&G4&&LbRYZ &&g6&&dG4&&--tW=&&dG4 L)rzz&*+a/ffVS'2G--(--g 67&&5r7C&&5r7C&&/W=&&=r7K&&6GD&&6GD&&UWY[bc&&*B8&&FgV&&92wG&&w7&&&':&&g.&&3UGD&&)38&&,c7;&&R1}}r5   c	                 (   UU[        U5      U R                  -  [        U5      U R                  -  4n	[        U[        5      (       a*  [	        U5      U:w  a  [        S[	        U5       SU S35      eUc  [        XXeS9nU$ UR                  U5      nU$ )Nz/You have passed a list of generators of length z+, but requested an effective batch size of z@. Make sure the batch size matches the length of the generators.)r   r!   rb   )intrA   rd   r   r.   r&   r   rh   )
rN   rr   num_channels_latentsr   r   rb   r!   r   r9   ri   s
             r3   prepare_latentsLuminaPipeline.prepare_latentsU  s     K4000J$///	
 i&&3y>Z+GA#i.AQ R&<'gi 
 ?"5fZG  jj(Gr5   c                     U R                   $ r   _guidance_scalerN   s    r3   guidance_scaleLuminaPipeline.guidance_scalei  s    ###r5   c                      U R                   S:  $ )NrP   r   r   s    r3   r|   *LuminaPipeline.do_classifier_free_guidancep  s    ##a''r5   c                     U R                   $ r   )_num_timestepsr   s    r3   num_timestepsLuminaPipeline.num_timestepst  s    """r5      g      @pilrB         ?r   r   r    r   r#   r   output_typereturn_dictrH   scaling_watershedproportional_attncallback_on_step_endr   returnc                    U=(       d    U R                   U R                  -  nU=(       d    U R                   U R                  -  nU R                  UUUUUUUUUS9	  XPl        0 nUb  [	        U[
        5      (       a  SnO3Ub!  [	        U[        5      (       a  [        U5      nOUR                  S   nU(       a  U R                  S-  S-  US'   [        R                  " X#-  U R                  S-  -  5      nU R                  nUS:  nU R                  UUUUUUUUUUUS	9u  nnnnU(       a,  [        R                  " X/SS
9n[        R                  " X/SS
9n[!        U R"                  UUUS9u  nnU R$                  R&                  R(                  nU R+                  UU-  UUUUR,                  UU	U
5      n
[        U5      U l        U R1                  US9 n[3        U5       GHp  u  nn U(       a  [        R                  " U
/S-  5      OU
n!U n"[        R4                  " U"5      (       d  U!R6                  R8                  S:H  n#U!R6                  R8                  S:H  n$[	        U"[:        5      (       a/  U#(       d  U$(       a  [        R<                  O[        R>                  n%O.U#(       d  U$(       a  [        R@                  O[        RB                  n%[        RD                  " U"/U%U!R6                  S9n"O7[        U"R                  5      S:X  a  U"S   RG                  U!R6                  5      n"U"RI                  U!R                  S   5      n"SU"U R"                  R&                  RJ                  -  -
  n"U"S   U:  a  Un&Sn'OSn&Un'[M        U R$                  RN                  SSU&U'S9n(U R%                  U!U"UUU(USS9S   n)U)RQ                  SSS
9S   n)U(       a  U)SS2SS24   U)SS2SS24   n+n*[        RR                  " U*[        U*5      S-  SS
9u  n,n-U-UU,U--
  -  -   n.[        R                  " U.U./SS
9n*[        R                  " U*U+/SS
9n)U)RQ                  SSS
9u  n)n/U
R,                  n0U)* n)U R"                  RU                  U)U U
SS9S   n
U
R,                  U0:w  a>  [        RV                  RX                  R[                  5       (       a  U
RG                  U05      n
UR]                  5         UbJ  0 n1U H  n2[_        5       U2   U1U2'   M     U" U UU U15      n3U3Ra                  SU
5      n
U3Ra                  SU5      n[b        (       d  GM[  [d        Rf                  " 5         GMs     SSS5        US:X  dZ  XRh                  R&                  Rj                  -  n
U Rh                  Rm                  U
SS9S   n4U Rn                  Rq                  U4US9n4OU
n4U Rs                  5         U(       d  U44$ [u        U4S9$ ! , (       d  f       N= f)u  
Function invoked when calling the pipeline for generation.

Args:
    prompt (`str` or `List[str]`, *optional*):
        The prompt or prompts to guide the image generation. If not defined, one has to pass `prompt_embeds`.
        instead.
    negative_prompt (`str` or `List[str]`, *optional*):
        The prompt or prompts not to guide the image generation. If not defined, one has to pass
        `negative_prompt_embeds` instead. Ignored when not using guidance (i.e., ignored if `guidance_scale` is
        less than `1`).
    num_inference_steps (`int`, *optional*, defaults to 30):
        The number of denoising steps. More denoising steps usually lead to a higher quality image at the
        expense of slower inference.
    sigmas (`List[float]`, *optional*):
        Custom sigmas to use for the denoising process with schedulers which support a `sigmas` argument in
        their `set_timesteps` method. If not defined, the default behavior when `num_inference_steps` is passed
        will be used.
    guidance_scale (`float`, *optional*, defaults to 4.0):
        Guidance scale as defined in [Classifier-Free Diffusion
        Guidance](https://huggingface.co/papers/2207.12598). `guidance_scale` is defined as `w` of equation 2.
        of [Imagen Paper](https://huggingface.co/papers/2205.11487). Guidance scale is enabled by setting
        `guidance_scale > 1`. Higher guidance scale encourages to generate images that are closely linked to
        the text `prompt`, usually at the expense of lower image quality.
    num_images_per_prompt (`int`, *optional*, defaults to 1):
        The number of images to generate per prompt.
    height (`int`, *optional*, defaults to self.unet.config.sample_size):
        The height in pixels of the generated image.
    width (`int`, *optional*, defaults to self.unet.config.sample_size):
        The width in pixels of the generated image.
    eta (`float`, *optional*, defaults to 0.0):
        Corresponds to parameter eta (η) in the DDIM paper: https://huggingface.co/papers/2010.02502. Only
        applies to [`schedulers.DDIMScheduler`], will be ignored for others.
    generator (`torch.Generator` or `List[torch.Generator]`, *optional*):
        One or a list of [torch generator(s)](https://pytorch.org/docs/stable/generated/torch.Generator.html)
        to make generation deterministic.
    latents (`torch.Tensor`, *optional*):
        Pre-generated noisy latents, sampled from a Gaussian distribution, to be used as inputs for image
        generation. Can be used to tweak the same generation with different prompts. If not provided, a latents
        tensor will ge generated by sampling using the supplied random `generator`.
    prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated text embeddings. Can be used to easily tweak text inputs, *e.g.* prompt weighting. If not
        provided, text embeddings will be generated from `prompt` input argument.
    prompt_attention_mask (`torch.Tensor`, *optional*): Pre-generated attention mask for text embeddings.
    negative_prompt_embeds (`torch.Tensor`, *optional*):
        Pre-generated negative text embeddings. For Lumina-T2I this negative prompt should be "". If not
        provided, negative_prompt_embeds will be generated from `negative_prompt` input argument.
    negative_prompt_attention_mask (`torch.Tensor`, *optional*):
        Pre-generated attention mask for negative text embeddings.
    output_type (`str`, *optional*, defaults to `"pil"`):
        The output format of the generate image. Choose between
        [PIL](https://pillow.readthedocs.io/en/stable/): `PIL.Image.Image` or `np.array`.
    return_dict (`bool`, *optional*, defaults to `True`):
        Whether or not to return a [`~pipelines.stable_diffusion.IFPipelineOutput`] instead of a plain tuple.
    clean_caption (`bool`, *optional*, defaults to `True`):
        Whether or not to clean the caption before creating embeddings. Requires `beautifulsoup4` and `ftfy` to
        be installed. If the dependencies are not installed, the embeddings will be created from the raw
        prompt.
    max_sequence_length (`int` defaults to 120):
        Maximum sequence length to use with the `prompt`.
    callback_on_step_end (`Callable`, *optional*):
        A function that calls at the end of each denoising steps during the inference. The function is called
        with the following arguments: `callback_on_step_end(self: DiffusionPipeline, step: int, timestep: int,
        callback_kwargs: Dict)`. `callback_kwargs` will include a list of all tensors as specified by
        `callback_on_step_end_tensor_inputs`.
    callback_on_step_end_tensor_inputs (`List`, *optional*):
        The list of tensor inputs for the `callback_on_step_end` function. The tensors specified in the list
        will be passed as `callback_kwargs` argument. You will only be able to include variables listed in the
        `._callback_tensor_inputs` attribute of your pipeline class.

Examples:

Returns:
    [`~pipelines.ImagePipelineOutput`] or `tuple`:
        If `return_dict` is `True`, [`~pipelines.ImagePipelineOutput`] is returned, otherwise a `tuple` is
        returned where the first element is a list with the generated images
)r:   r~   rw   r   r   NrP   r      r   base_sequence_lengthr   )	r}   rR   r!   r:   r~   rw   r   rS   rH   )dim)r#   )totalmpsnpura   i  )linear_factor
ntk_factorF)ro   timestepencoder_hidden_statesencoder_maskimage_rotary_embcross_attention_kwargsr   r   )r   r9   r:   latent)r   )images);rL   rA   r   r   rd   re   r   r.   ri   rM   mathsqrtrc   r   rj   catr4   r/   r;   rJ   in_channelsr   rb   r   progress_bar	enumerate	is_tensorr!   r   floatfloat32float64int32int64tensorrh   expandnum_train_timestepsr   head_dimchunksplitr   backendsr   is_availableupdatelocalspopXLA_AVAILABLExm	mark_stepr<   scaling_factordecoderG   postprocessmaybe_free_model_hooksr   )5rN   rQ   r   r   r    r   r}   r#   rR   r   r9   r:   r~   rw   r   r   r   rS   rH   r   r   r   r   r   rr   r	  r!   r|   r"   latent_channelsr   ir   latent_model_inputcurrent_timestepis_mpsis_npurb   r   r   r   
noise_prednoise_pred_epsnoise_pred_restnoise_pred_cond_epsnoise_pred_uncond_epsnoise_pred_halfrx   latents_dtypecallback_kwargsr   callback_outputsimages5                                                        r3   __call__LuminaPipeline.__call__x  s   T K433d6K6KKI11D4I4II 	'#9"7+I/Q 	 
	
  .!# *VS"9"9JJvt$<$<VJ&,,Q/J>B>U>UY[>[`a=a"#9:5>D4K4KQ4N#NO''
 '5s&:# '+"7'#9"7+I' 3  
	
!"* '!II}&MSTUM$)II/D.ekl$m! *<DNNL_agpv)w&	& **11==&&..	
 ")n %89\!),1A\UYYy1}%=bi"#$ '788 066;;uDF/66;;uDF!"2E::28F06&u{{',||)*#188($
 )//0A5'7'='@'@ASAZAZ'[$#3#:#:;M;S;STU;V#W  $%'7$..:O:O:c:c'c#c  $A&)::$2M!$J$'M!/J#A$$--"/)$  "--"4-*7!6%5+A % .  
 (--aQ-7:
 /6@BQB6GTUWXWYTYIZONAF&N(;q(@aB>')> '<n+.CC? 'O &+YY/QWX%YN!&NO+LRS!TJ$.$4$4QA$4$>MJ !(([
..--j!WRW-XYZ[==M1~~))6688")**]";##%'3&(O?-3Xa[* @';D!Q'X$.229gFG$4$8$8-$XM =LLNC - :H h& > >>GHHOOGO?BE((44U4TEE 	##%8O"%00c :9s   M"W#<W##
W1)r   r   rM   rL   rG   rH   rA   )rP   NFN)	TNrP   NNNNNF)NNNNN)Fr   )4__name__
__module____qualname____firstlineno____doc__r   r   r   _optional_componentsmodel_cpu_offload_seqr   r   r   r   r	   r   r
   r   rE   re   r   r   r   rj   r!   boolrz   Tensorr   r   r   rf   r   r   propertyr   r|   r   no_gradr   EXAMPLE_DOC_STRINGr   	Generatorr   r   r   r   r   r   r  __static_attributes____classcell__r-   s   @r3   r7   r7      sO   & jj	O  <
S)S 3S 	S
 +S );;<S< &')-(-$(54c49n%54  #54 &	54
  ~54 SM54v -115%&)-049=8<AE#klc49n%kl &*kl sDI~.	kl
  #kl &kl  -kl !) 6kl  (5kl )1(>kl kl\!. #"'++/BJ*2pd( $ $ ( ( # # ]]_12 )-# $#% #15"/0MQ*.049=8<AE%* "#&-0,0 9B3k1c49n%k1 }k1 	k1
 !k1 k1 sDI~.k1 Uk1  (}k1 E%//43H"HIJk1 %,,'k1  -k1 !) 6k1  (5k1 )1(>k1  c]!k1" #k1$ %k1& !'k1( $E?)k1* $D>+k1, '(Cd+T124DF\\]
-k12 -1I3k14 
"E)	*5k1 3 k1r5   r7   c                   J   ^  \ rS rSrS\S\S\S\S\\	\
4   4
U 4S jjrSrU =r$ )	LuminaText2ImgPipelinei  r;   r/   r<   r=   r>   c                 F   > Sn[        SSU5        [        TU ]	  UUUUUS9  g )Nz`LuminaText2ImgPipeline` has been renamed to `LuminaPipeline` and will be removed in a future version. Please use `LuminaPipeline` instead.zAdiffusers.pipelines.lumina.pipeline_lumina.LuminaText2ImgPipelinez0.34)r;   r/   r<   r=   r>   )r   rD   rE   )rN   r;   r/   r<   r=   r>   deprecation_messager-   s          r3   rE   LuminaText2ImgPipeline.__init__  s<     lUW]_rs#% 	 	
r5   r%   )r  r   r!  r"  r   r   r   r	   r   r
   r   rE   r,  r-  r.  s   @r3   r0  r0    sH    
)
 3
 	

 +
 );;<
 
r5   r0  )NNNN)@r   r(   r   r   urllib.parseparser   typingr   r   r   r   r   r   rj   transformersr	   r
   r   	callbacksr   r   rG   r   modelsr   models.embeddingsr   $models.transformers.lumina_nextdit2dr   
schedulersr   utilsr   r   r   r   r   r   r   utils.torch_utilsr   pipeline_utilsr   r   torch_xla.core.xla_modelcore	xla_modelr  r  
get_loggerr  rm   r   r   r   r*  r   re   r!   r   r4   r7   r0  r%   r5   r3   <module>rD     s      	  ? ?  Q Q A 0 # ? H 9   . C ))MM			H	% ! & *.15%)$(8*!#8* U3,-.8* S	"	8*
 T%[!8*v]1& ]1@
^ 
r5   