o
    i                     @   sj   d dl mZ d dlZd dlmZmZ G dd dejZG dd dejZG dd	 d	eZ	d
e	fddZ
dS )    )overrideN)ComfyExtensionioc                   @   sT   e Zd Zedd Zedd Zedd Zedd Zed	d
 Z	edd Z
dS )LatentRebatchc                 C   >   t jddddt jdt jjddddd	gt jjdd
gdS )NRebatchLatentszRebatch Latentszlatent/batchTlatents
batch_size      defaultminmaxis_output_listnode_iddisplay_namecategoryis_input_listinputsoutputs)r   SchemaLatentInputIntOutputcls r    8/mnt/c/Users/fbmor/ComfyUI/comfy_extras/nodes_rebatch.pydefine_schema      
zLatentRebatch.define_schemac                    sB  | | d }|j }d| | v r| | d ntj|d d|d d |d d fdd	}|j d
 |d
 d ks>|j d |d kr_tjjj|d
d|j d |j d
 f|d d |d
 d fdd |j d |j d k r||d d |j d  d dddd|d  }d| | v r| | d }n fddt|d D }|||fS )z*prepare a batch out of the list of latentssamples
noise_maskr   r
            cpu)devicebilinear)sizemodeNbatch_indexc                    s   g | ]}|  qS r    r    .0xoffsetr    r!   
<listcomp>%   s    z+LatentRebatch.get_batch.<locals>.<listcomp>)	shapetorchonesnn
functionalinterpolatereshaperepeatrange)r   list_indr5   r$   r7   mask
batch_indsr    r4   r!   	get_batch   s   B(B2
zLatentRebatch.get_batchc                 C   s\   g }t |D ]}|| || |d |   q|| t| k r*|| || d fS |dfS )zQdivides an indexable object into num slices of length batch_size, and a remainderr
   N)r?   appendlen)	indexablenumr	   slicesir    r    r!   
get_slices(   s    zLatentRebatch.get_slicesc                    s     fdd| D }t t| S )Nc                    s   g | ]	}t | qS r    )r   rJ   r1   r	   rG   r    r!   r6   5   s    z-LatentRebatch.slice_batch.<locals>.<listcomp>)listzip)batchrG   r	   resultr    rK   r!   slice_batch3   s   zLatentRebatch.slice_batchc                 C   s(   | d d u r|S dd t | |D }|S )Nr   c                 S   s0   g | ]\}}t |rt ||fn|| qS r    )r8   	is_tensorcat)r2   b1b2r    r    r!   r6   <   s   0 z+LatentRebatch.cat_batch.<locals>.<listcomp>)rM   )batch1batch2rO   r    r    r!   	cat_batch8   s   zLatentRebatch.cat_batchc                 C   s  |d }g }d}d}t t|D ]}| |||}|t|d 7 }|d d u r*|}nC|d jd |d jd ksF|d jd |d jd krg| |d|\}}	||d d |d d |d d d |}n| ||}|d jd |kr|d jd | }
| ||
|\}}t |
D ]}||d | |d | |d | d q|}q|d d ur| |d|\}}	||d d |d d |d d d |D ]}|d  d	kr|d= qt	|S )
Nr   )NNNr&   r+   r,   r
   )r$   r%   r0   r%   g      ?)
r?   rE   rC   r7   rP   rD   rW   meanr   
NodeOutput)r   r   r	   output_listcurrent_batch	processedrI   
next_batchsliced_rG   	remaindersr    r    r!   execute?   s:   8*,*
zLatentRebatch.executeN)__name__
__module____qualname__classmethodr"   staticmethodrC   rJ   rP   rW   rb   r    r    r    r!   r      s    





r   c                   @   s$   e Zd Zedd Zedd ZdS )ImageRebatchc                 C   r   )NRebatchImageszRebatch Imageszimage/batchTimagesr	   r
   r   r   r   r   )r   r   Imager   r   r   r   r    r    r!   r"   o   r#   zImageRebatch.define_schemac                 C   s   |d }g }g }|D ]}t |jd D ]}||||d   qq
t dt||D ]}|tj||||  dd q*t|S )Nr   r
   )dim)r?   r7   rD   rE   r8   rR   r   rY   )r   rj   r	   rZ   
all_imagesimgrI   r    r    r!   rb      s   "
zImageRebatch.executeN)rc   rd   re   rf   r"   rb   r    r    r    r!   rh   n   s
    
rh   c                   @   s(   e Zd Zedeeej  fddZdS )RebatchExtensionreturnc                    s
   t tgS N)r   rh   )selfr    r    r!   get_node_list   s   zRebatchExtension.get_node_listN)	rc   rd   re   r   rL   typer   	ComfyNoders   r    r    r    r!   ro      s    ro   rp   c                      s   t  S rq   )ro   r    r    r    r!   comfy_entrypoint   s   rv   )typing_extensionsr   r8   comfy_api.latestr   r   ru   r   rh   ro   rv   r    r    r    r!   <module>   s    g!	