o
    i                     @   sf   d dl mZ d dlmZ d dlmZ d dlZd dlmZ edej	j
dZeded	ee fd
dZdS )    )Iterator)contextmanager)TypeVarN)cleanup_memory_M)boundmodelreturnc              	   c   sF    z| V  W t j  | d t  dS t j  | d t  w )u  Context manager that yields a model and releases its memory on exit.
    Moves all parameters and buffers to ``meta`` device on exit, which
    immediately releases the underlying storage on **both** GPU and CPU,
    then runs ``cleanup_memory()`` to reclaim fragmented CUDA memory.
    Usage::
        with gpu_model(build_encoder()) as encoder:
            ...  # use encoder — typed as the concrete class
        # GPU + CPU memory freed automatically
    metaN)torchcudasynchronizetor   )r    r   N/home/ubuntu/LTX-2/packages/ltx-pipelines/src/ltx_pipelines/utils/gpu_model.py	gpu_model   s   




r   )collections.abcr   
contextlibr   typingr   r   ltx_pipelines.utils.helpersr   nnModuler   r   r   r   r   r   <module>   s    