o
    €o™iŽ	  ã                   @   sZ   d dl mZmZ d dlmZmZmZmZ 	ddededededee	ef d	efd
d„Z
dS )é    )ÚAnyÚDict)ÚDeviceÚModuleÚTensorÚconcatenateé€   ÚmodelÚdataÚdeviceÚ
batch_sizeÚkwargsÚreturnc                 K   sØ   |   |¡}t|ƒ}|}||krdg }t|| d ƒ}	t|	ƒD ];}
|
| }|
|	d kr;|
d | |kr4|}n|
d | }n|
d | }||krFq| ||||…   |¡fi |¤Ž¡ qt|dƒ}|  |j¡S | |fi |¤ŽS )ad  Run the forward in micro-batches.

    When the just model.forward(data) does not fit into device memory, e.g. on laptop GPU.
    In the end, it transfers the output to the device of the input data tensor.
    E.g. running HardNet on 8000x1x32x32 tensor.

    Args:
        model: Any torch model, which outputs a single tensor as an output.
        data: Input data of Bx(Any) shape.
        device: which device should we run on.
        batch_size: "micro-batch" size.
        **kwargs: any other arguments, which accepts model.

    Returns:
        output of the model.

    Example:
        >>> patches = torch.rand(8000, 1, 32, 32)
        >>> sift = kornia.feature.SIFTDescriptor(32)
        >>> desc_batched = batched_forward(sift, patches, torch.device('cpu'), 128)
        >>> desc = sift(patches)
        >>> assert torch.allclose(desc, desc_batched)

    é   r   )ÚtoÚlenÚintÚrangeÚappendr   r   )r	   r
   r   r   r   Ú	model_devÚBÚbsÚout_listÚ	n_batchesÚ	batch_idxÚstÚendÚout© r   úG/home/ubuntu/.local/lib/python3.10/site-packages/kornia/utils/memory.pyÚbatched_forward   s&   
&
r    N)r   )Útypingr   r   Úkornia.corer   r   r   r   r   Ústrr    r   r   r   r   Ú<module>   s    ÿÿÿÿÿ
ÿþ