o
    i                     @   sN   d dl Z d dl mZ d dlmZmZmZmZmZ d dlZ	G dd dej
ZdS )    N)nn)DoubleStreamBlock	LastLayerMLPEmbedderSingleStreamBlocktimestep_embeddingc                       sT   e Zd Z																	d fd
d	Zd	i fddZd	i fddZ  ZS )Hunyuan3Dv2@               @       TFNc              	      s   t    | _ dkrtd d d| _j|d d| _td d| _|	r;td dnd | _	j| d	| _
t fd
dt|D | _t fddt|D | _td| d| _d S )Nr   zHidden size z  must be divisible by num_heads i  T)biasdtypedevice   )in_dim
hidden_dimr   r   
operations)r   r   c                    s"   g | ]}t  d qS ))	mlp_ratioqkv_biasr   r   r   )r   .0_r   r   hidden_sizer   	num_headsr   r    7/mnt/c/Users/fbmor/ComfyUI/comfy/ldm/hunyuan3d/model.py
<listcomp>.   s    z(Hunyuan3Dv2.__init__.<locals>.<listcomp>c              
      s    g | ]}t  d qS ))r   r   r   r   )r   r   )r   r   r   r   r   r   r   r   r    :   s       )r   r   r   )super__init__r   
ValueError
max_periodLinear	latent_inr   time_inguidance_incond_inr   
ModuleListrangedouble_blockssingle_blocksr   final_layer)selfin_channelscontext_in_dimr   r   r   depthdepth_single_blocksr   guidance_embedimage_modelr   r   r   	__class__r   r   r#      s.   
zHunyuan3Dv2.__init__c                 K   s:   t jj| j| t jt jjj|j|||||fi |S )N)	comfypatcher_extensionWrapperExecutornew_class_executor_forwardget_all_wrappers
WrappersMPDIFFUSION_MODELexecute)r0   xtimestepcontextguidancetransformer_optionskwargsr   r   r   forwardF   s   zHunyuan3Dv2.forwardc              	      s  | dd}d| }|}| |}| t|d| jj|jd}	| jd ur9|d ur9|	| t|d| j|j }	| |}d }
d }|	di }|	di }t
| jD ]7\} d|f|v r~ fd	d
}|d|f |||	|
||dd|i}|d }|d }qS |||	|
||d\}}qSt||fd}t
| jD ]/\} d|f|v r fdd
}|d|f ||	|
||dd|i}|d }q ||	|
||d}q|d d |jd d df }| ||	}| ddd S )Ng      ?r   )r   patches_replaceditdouble_blockc                    sB   i } | d | d | d | d |  d| d d\|d< |d< |S )Nimgtxtvecpe	attn_maskrF   rN   rO   rP   rQ   rR   rF   getargsoutblockr   r   
block_wrap`   s   z(Hunyuan3Dv2._forward.<locals>.block_wraprS   original_blockrO   rN   r!   single_blockc                    s4   i } | d | d | d |  d| d d|d< |S )NrN   rP   rQ   rR   rF   rP   rQ   rR   rF   rT   rV   rY   r   r   r[      s   
)rN   rP   rQ   rR   rF   r^   .g      )movedimr'   r(   r   r%   tor   r)   r*   rU   	enumerater-   torchcatr.   shaper/   )r0   rB   rC   rD   rE   rF   rG   rO   rN   rP   rQ   rR   rK   blocks_replaceir[   rX   r   rY   r   r=   M   sf   

 


	
zHunyuan3Dv2._forward)r	   r
   r   r   r   r   r   TFNNNN)__name__
__module____qualname__r#   rH   r=   __classcell__r   r   r7   r   r      s"    8r   )rb   r   comfy.ldm.flux.layersr   r   r   r   r   comfy.patcher_extensionr9   Moduler   r   r   r   r   <module>   s
    