
    +je,                         d dl mZ d dlZd dlZd dlZd dlZd dlmZm	Z	  G d de	j
                  Z G d de	j
                  Z G d d	e	j
                  Z G d
 de          ZdefdZdS )    )overrideN)ComfyExtensionioc            
       l    e Zd Zedej        fd            Zedddedededej        fd            Z	d	S )
EmptyHiDreamO1LatentImagereturnc                 N   t          j        ddddt           j                            ddddd	
          t           j                            ddddd	
          t           j                            dddd          gt          j                                                    g          S )Nr   zEmpty HiDream-O1 Latent Imagezmodel/latent/imagea,  Empty pixel-space latent for HiDream-O1-Image. The model was trained at ~4 megapixels; lower resolutions go off-distribution and quality regresses noticeably. Trained resolutions: 2048x2048, 2304x1728, 1728x2304, 2560x1440, 1440x2560, 2496x1664, 1664x2496, 3104x1312, 1312x3104, 2304x1792, 1792x2304.widthi   @   i       )iddefaultminmaxstepheight
batch_size   )r   r   r   r   node_iddisplay_namecategorydescriptioninputsoutputs)r   SchemaIntInputLatentOutputclss    =/home/wildlama/comfy/ComfyUI/comfy_extras/nodes_hidream_o1.pydefine_schemaz'EmptyHiDreamO1LatentImage.define_schema   s    y/8)T 24bQQ$BDrRRaQBGG
 Y[[''))*!
 
 
 	
    r   )r   r
   r   r   c                    t          j        |d||ft          j                                                  }t          j        d|i          S )N   )devicesamples)torchzeroscomfymodel_managementintermediate_devicer   
NodeOutput)r"   r
   r   r   latents        r#   executez!EmptyHiDreamO1LatentImage.execute!   sM    FE*)==??
 
 
 }i0111r%   N)
__name__
__module____qualname__classmethodr   r   r$   intr/   r1    r%   r#   r   r      s        
bi 
 
 
 [
( CD 2 2 2s 2C 2S 2 2 2 2 [2 2 2r%   r   c                   v    e Zd ZdZedej        fd            Zedej        j	        dej
        fd            ZdS )HiDreamO1ReferenceImageszCAttach reference images to both positive and negative conditioning.r   c                    t          j        ddddt           j                            d          t           j                            d          t           j                            dt           j                            t           j                            d	          d
 t          dd          D             d          d          gt           j                            d          t           j                            d          g          S )Nr9   zHiDream-O1 Reference Imageszmodel/conditioning/imagezuAttach 1-10 reference images to conditioning, one for edit instructionor multiple for subject-driven personalization.positiver   negativeimagesimagec                     g | ]}d | S image_r7   ).0is     r#   
<listcomp>z:HiDreamO1ReferenceImages.define_schema.<locals>.<listcomp>>   s    BBB|||BBBr%   r      )namesr   zLReference images. 1 image = instruction edit; 2-10 images = multi reference.)templatetooltip)r   r   )	r   r   Conditioningr   AutogrowTemplateNamesImageranger    r!   s    r#   r$   z&HiDreamO1ReferenceImages.define_schema-   s    y.6/B %%%44%%%44!![66w//BBU1b\\BBB 7  
 l " 	 	 &&J&??&&J&??-
 
 
 	
r%   r>   c                    fdt          dd          D             }t          j        |d|id          }t          j        |d|id          }t          j        ||          S )Nc                 4    g | ]}d | v 	d |          S rA   r7   )rC   rD   r>   s     r#   rE   z4HiDreamO1ReferenceImages.execute.<locals>.<listcomp>M   s4    UUUlqllf>T>T|||$>T>T>Tr%   r   rF   reference_latentsT)append)rN   node_helpersconditioning_set_valuesr   r/   )r"   r;   r=   r>   refss      ` r#   r1   z HiDreamO1ReferenceImages.executeK   st    UUUUeArllUUU7CVX\B]fjkkk7CVX\B]fjkkk}Xx000r%   N)r2   r3   r4   __doc__r5   r   r   r$   rK   Typer/   r1   r7   r%   r#   r9   r9   *   sv        MM
bi 
 
 
 [
: 1BK4D 1 1 1 1 [1 1 1r%   r9   c                       e Zd ZdZdZddgg dg dddgg d	g d
dZdgdgddgg ddZeddededede	j
        fd            Zedej        fd            Zedededededededej        fd            ZdS )HiDreamO1PatchSeamSmoothingr      r   r      r]   )r[   r]   r   r   r]   r\   )r[   r^   r_   r\      ra   )   ra   )ra   rb   )rb   rb   rd   r`   )rc   )ra   rd   )rd   ra   r`   ))re   )rZ   re   )re   rZ   )rZ   rZ   )rf   )   rf   )rf   rg   )rg   rg   ))single_shift   )rh   rZ   )rh   ra   )	symmetricri   )rj   rZ   )rj   ra   ri   )ri   rZ   ra   24ramp_2_4
ramp_2_4_8cycxsizer   c                    |dz  }t          j        |                              |d          }t          j        |                              d|          }|| z
  |z   |z  |z
  }||z
  |z   |z  |z
  }ddt          j        t           j        |z  |z            z   z  dt          j        t           j        |z  |z            z   z  S )z9size x size Hann tile peaking at (cy, cx) within a patch.ri   r   g      ?)r*   arangeviewcospi)rp   rq   rr   halfyyxxdydxs           r#   
_hann_tilez&HiDreamO1PatchSeamSmoothing._hann_tilei   s     qy\$$$T1--\$$$Q--Bw~%-Bw~%-q59UX]T%9:::;q59UXXZ]]aMaCbCb?bccr%   c                 `   t          j        dddddt           j                            d          t           j                            dd	d
ddd          t           j                            ddd
ddd          t           j                            dddgdd          t           j                            dg ddd          t           j                            dg ddd          t           j                            ddd
ddd          gt           j                                        g           S )!NrY   zHiDream-O1 Patch Seam Smoothingzadvanced/modelTzyAverage the model output across multiple shifted patch-grid positions during the late portion of sampling. Cancels seams.modelr<   start_percentg?              ?g{Gz?z?Sampling progress (0=start, 1=end) at which the blend turns ON.)r   r   r   r   r   rI   end_percentz/Sampling progress at which the blend turns OFF.patternrh   rj   zShift layout. single_shift: one pass at the natural patch grid + others offset. symmetric: all passes off-grid, shifts split around origin.)r   optionsr   rI   passesrk   rl   zNumber of passes per gated step. 2/4 = fixed. ramp_*: pass count increases as sampling approaches end (more smoothing where seams are most visible).blend)averagewindowmedianr   zaverage: equal-weight mean. window: Hann-windowed weighting favoring each pass away from its patch boundaries. median: per-pixel median, rejects wraparound-outlier passes.strengthzLInterpolation between the natural-grid pred (0) and the averaged result (1).)r   r   r   is_experimentalr   r   r   )r   r   Modelr   FloatCombor    r!   s    r#   r$   z)HiDreamO1PatchSeamSmoothing.define_schemas   s^   y1:% P '**/3CSW[]    -#3UYM     +[9* j	    @@@ s	    ;;;% J	    *csRVj   5< X__&&'O(
 (
 (
 (	
r%   r   r   r   r   r   r   c                    dk    s||k    rt          j        |          S  j        dz   fd j        |         D             dk    r fdD             nd gt	                    z  |                                }|                    d          }	t          |	j                  }
t          |		                    |                    |
z  t          |		                    |                    |
z  i dt          dt          d	t          j        f fd
fd}|                    t          j        j        j        d|           t          j        |          S )Nr   ri   c                 .    g | ]}j         |f         S r7   )SHIFTS_BY_PATTERN)rC   nr"   r   s     r#   rE   z7HiDreamO1PatchSeamSmoothing.execute.<locals>.<listcomp>   s%    ]]]-wl;]]]r%   r   c                 X    g | ]&}t          j        fd |D             d          'S )c                 \    g | ](\  }}                     |z
  z  |z
  z            )S r7   )r}   )rC   sysxPr"   rx   s      r#   rE   zBHiDreamO1PatchSeamSmoothing.execute.<locals>.<listcomp>.<listcomp>   s@    cccU[UWY[S^^TBY!OdRi1_aPPcccr%   r   dim)r*   stack)rC   lstr   r"   rx   s     r#   rE   z7HiDreamO1PatchSeamSmoothing.execute.<locals>.<listcomp>   sU     " " " cccccc_bcccijkkk" " "r%   model_samplingHWr   c           	         | |||f}                     |          }||S j        }t          j        t          j        | |t          j                  | dz
  t          j        | |t          j                  z
            }t          j        t          j        ||t          j                  |dz
  t          j        ||t          j                  z
            }|z
  |z                      dd          }	|z
  |z                      dd          }
|	d d d f         |
d d d f         z                      |          }||<   |S )Nr(   dtyper   r   )getEDGE_FEATHERr*   minimumrt   float32clampto)r   r   r(   r   keycachedfeatherysxsy_maskx_maskrampr   r"   edge_ramp_caches               r#   get_edge_rampz:HiDreamO1PatchSeamSmoothing.execute.<locals>.get_edge_ramp   s@   a'C$((--F!&Gu|AfEMRRR !Aaem)\)\)\\^ ^Bu|AfEMRRR !Aaem)\)\)\\^ ^BAv(//155FAv(//155F111d7OfT111Wo599%@@D#'OC Kr%   c                 b   |d         }t          |d         d                   } | |i |}|cxk    rk    sn |S t                    dk    rd}nV|z
  t          z
  d          z  }t          t	          |t                    z            t                    dz
            }|         } |         }	g }
|D ]\  }}|dk    r|dk    r|
                    |           't          j        |||fd          } | |g|dd          R i |}|
                    t          j        || | fd                     t          j        |
d          }|j	        \  }}}}}dk    r|j	        d         }|	
                    |j        |j                  }|                    d|z  |z            d d d |d |f         }|                    dd	
          }t          j        |dk     t          j        |d|z            ||                    d          z            }||d d d d d d d d f         z                      d          }n8dk    rt          j        |d          j        }n|                    d          } |||j        |j                  }|d|z  z
  z  ||z  z  z   S )Nr   r   g:0yE>))shiftsdimsr   r   r   T)r   keepdimgMbP?r   )r   r   )floatlenr   r   r6   rR   r*   rollr   shaper   r(   r   repeatsumwhere	full_liker   r   valuesmean)!executorargskwargsxtpred	level_idxphaser   window_tilespredsr   r   x_rolledpred_rolledstacked_r   r   Ntileswsum_wavgmaskr   r   end_tr   shift_levelsstart_tr   window_tile_levelss!                            r#   smoothing_wrapperz>HiDreamO1PatchSeamSmoothing.execute.<locals>.smoothing_wrapper   s
   QAd1gaj!!A8T,V,,DQ))))'))))<  A%%		 1GeOT(B(BBEC,=,=$= > >L@Q@QTU@UVV	!),F-i8LE  X XB77rQwwLL&&& :aRxHHH&hxE$qrr(EEEfEEUZbS2#JXVVVWWWWk%Q///G#MMAq!Q  M!$$w~W]SSLLAFAF33AAArr2A2I>!T22Keoaq.I.I1u{{_c{OdOdKdee111dD!!!QQQ#6!77<<<CC(""l72229llql)) !=At{DJ??D301C4(?4KKKr%   hidream_o1_patch_seam_smoothing)r   r/   
PATCH_SIZERAMP_LEVELSr   cloneget_model_objectr   
multiplierpercent_to_sigmar6   r*   Tensoradd_wrapper_with_keyr,   patcher_extension
WrappersMPDIFFUSION_MODEL)r"   r   r   r   r   r   r   r   mr   r   r   r   r   r   r   rx   r   r   r   s   `   ` ``    @@@@@@@@r#   r1   z#HiDreamO1PatchSeamSmoothing.execute   s   s??k]::='''NAv]]]]]S_U[E\]]]H" " " " " "'" " "
 #'#l*;*;!;KKMM++,<==>455
77FFGG*Tn55kBBCCjP "	S 	S 	EL 	 	 	 	 	 	 	 	 '	L '	L '	L '	L '	L '	L '	L '	L '	L '	L '	L '	LR 	
u6AQSt  wH  	I  	I  	I}Qr%   N)r   )r2   r3   r4   r   r   r   r   staticmethodr6   r*   r   r}   r5   r   r   r$   r   strr/   r1   r7   r%   r#   rY   rY   S   s}       JL %h/AAAB B B&/AAAF F F	 	 cc!fii	 K d ds d d3 d d d d \d )
bi )
 )
 )
 [)
V R e R % R RU R _b R kn R z R   EG  ER R  R  R  [R  R  R r%   rY   c                   L    e Zd Zedeeej                          fd            ZdS )HiDreamO1Extensionr   c                 .   K   t           t          t          gS N)r   r9   rY   )selfs    r#   get_node_listz HiDreamO1Extension.get_node_list   s       &$'
 	
r%   N)	r2   r3   r4   r   listtyper   	ComfyNoder   r7   r%   r#   r   r      sE        
T$r|*<%= 
 
 
 X
 
 
r%   r   r   c                  "   K   t                      S r   )r   r7   r%   r#   comfy_entrypointr      s      r%   )typing_extensionsr   r*   comfy.model_managementr,   comfy.patcher_extensionrS   comfy_api.latestr   r   r   r   r9   rY   r   r   r7   r%   r#   <module>r      s:   & & & & & &              / / / / / / / /2 2 2 2 2 2 2 2>&1 &1 &1 &1 &1r| &1 &1 &1R_  _  _  _  _ ", _  _  _ D
 
 
 
 
 
 
 
  2            r%   