§
    ÎÐjE'  ã                   óR  — d Z ddlZddlZddlZddlZddlZdZdZdZdZ	dZ
dxaad	ad
Zd\  ZZZdZej                             ej                             ej                             e¦  «        ¦  «        ¦  «        Zd„ Zd„ Zd„ Zd„ Zdd„Zd„ Zd„ Zd„ Zd„ Z d„ Z!d„ Z"d„ Z#d„ Z$dd„Z%dS )aµ  CUDA memory settings applied when WanGP starts, before anything initializes CUDA: the VRAM allocator (Configuration > RAM/VRAM
Management or --vram-allocator), its debug mode (--vram-debug) and the per-thread stack reserve of the CUDA context; the RAM debug
mode (--ram-debug) and the MMGP RAM allocator (Configuration > RAM/VRAM Management or --ram-allocator); and the folder of the driver's
compute cache (Configuration > Performance).é    NÚvram_allocator)ÚdefaultÚvmmÚ	vmm_spillÚram_allocator)r   Úmmgpr   Fr   é   )Úcompute_cacheÚcompute_cache_pathÚComputeCachel        c                 ó~   — t           j                             t          |                      ¦   «         pt
          ¦  «        S )zHThe folder of the compute cache: a relative path is in the WanGP folder.)ÚosÚpathÚjoinÚ
WANGP_ROOTÚstripÚCOMPUTE_CACHE_PATH_DEFAULT)r   s    ú:C:\Users\johnn\OneDrive\Documents\AI\shared\cuda_memory.pyÚcompute_cache_folderr      s&   € åŒ7�<Š<�
 D§J¢J¡L¤LÐ$NÕ4NÑOÔOÐOó    c                  ó   — t           dv S )zòThe MMGP VRAM allocator runs: without the fragmentation the VAE tiling presets were made against, VAEs offer two levels,
    Default (fast, about 2-3 GB of VRAM) and Low VRAM (Configuration > RAM/VRAM Management > VAE Tiling, values 0 and 3).©r   r   )ÚVRAM_ALLOCATOR© r   r   Úmmgp_vram_allocatorr   !   s   € õ Ð1Ð1Ð1r   c                  óž   — ddl } | j        j        du o;| j                             ¦   «         o"| j                             ¦   «         d         dk    S )a
  NVIDIA Blackwell GPUs (RTX 50xx, B200...): libraries ship their code in a form that the driver optimizes for the exact GPU when it
    loads it, about 15 s per launch, and caches; older GPUs run the shipped code as is. ROCm reports AMD architectures as capabilities.r   Né
   )ÚtorchÚversionÚhipÚcudaÚis_availableÚget_device_capability)r   s    r   Úcompute_cache_usefulr$   '   sO   € ð €L€L€LØŒ=Ô Ð$Ðr¨¬×)@Ò)@Ñ)BÔ)BÐrÀuÄz×GgÒGgÑGiÔGiÐjkÔGlÐprÒGrÐrr   c                 óî   — t          | ¦  «        D ]d\  }}||k    r#|dz   t          | ¦  «        k     r| |dz            c S |                     |dz   ¦  «        r|                     dd¦  «        d         c S Œed S )Né   ú=)Ú	enumerateÚlenÚ
startswithÚsplit)ÚargvÚoptionÚindexÚargs       r   Ú_argv_valuer0   .   s�   € Ý ‘o”oð (ð (‰
ˆˆsØ�&Š=ˆ=˜U Q™Y­¨T©¬Ò2Ð2Ø˜ ™	”?Ð"Ð"Ð"Ø�>Š>˜& 3™,Ñ'Ô'ð 	(Ø—9’9˜S !Ñ$Ô$ QÔ'Ð'Ð'Ð'ð	(àˆ4r   c                 óœ  — t          | d¦  «        }|r>t          j                             t          j                             |¦  «        |¦  «        gng |gz   D ]t}t          j                             |¦  «        rSt          |d¬¦  «        5 }t          j        |¦  «         	                    ||¦  «        cd d d ¦  «         c S # 1 swxY w Y   Œu|S )Nz--configzutf-8)Úencoding)
r0   r   r   r   ÚabspathÚisfileÚopenÚjsonÚloadÚget)r,   Úconfig_filenameÚkeyr   Ú
config_dirr   Úreaders          r   Ú_config_valuer=   7   s  € å˜T :Ñ.Ô.€JØQ[Ðc•"”'—,’,�rœwŸš¨zÑ:Ô:¸OÑLÔLÐMÐMÐacÐhwÐgxÑxð ;ð ;ˆÝŒ7�>Š>˜$ÑÔð 	;Ý�d WÐ-Ñ-Ô-ð ;°Ý”y Ñ(Ô(×,Ò,¨S°'Ñ:Ô:ð;ð ;ð ;ð ;ñ ;ô ;ð ;ð ;ð ;ð ;ð ;ð ;ð ;ð ;øøøð ;ð ;ð ;ð ;øà€Ns   Â	(C Ã C	ÃC	c                 óL   — t          | |¦  «        }|€t          | ||¦  «        n|S ©N)r0   r=   )r,   r9   r-   r:   Úvalues        r   Ú
_requestedrA   A   s,   € Ý˜˜fÑ%Ô%€EØ8=¸�=˜˜°Ñ4Ô4Ð4È5ÐPr   c                 óœ  — t          | |t          d¦  «        sd S t          t          | |t          t          ¦  «        ¦  «        }	 t          j        |d¬¦  «         n.# t          $ r!}t          d|› d|› d�¦  «         Y d }~d S d }~ww xY w|t          t          ¦  «        ct
          j        d<   t
          j        d<   t          d	|› �¦  «         d S )
NFT)Úexist_okz[CUDA] Compute Cache folder z not available (z,): the NVIDIA driver's default cache is usedÚCUDA_CACHE_PATHÚCUDA_CACHE_MAXSIZEz[CUDA] Compute Cache: )r=   ÚCOMPUTE_CACHE_KEYr   ÚCOMPUTE_CACHE_PATH_KEYr   r   ÚmakedirsÚOSErrorÚprintÚstrÚCOMPUTE_CACHE_MAX_BYTESÚenviron)r,   r9   r   Úerrors       r   Ú_apply_compute_cacherO   F   sî   € õ ˜˜Õ0AÀ5ÑIÔIð ØˆÝ¥¨d°OÕE[Õ]wÑ xÔ xÑyÔy€DðÝ
Œ�D 4Ð(Ñ(Ô(Ð(Ð(øÝð ð ð ÝÐv¨TÐvÐvÀ5ÐvÐvÐvÑwÔwÐwØˆˆˆˆˆøøøøðøøøð GKÍCÕPgÑLhÔLhÐC…B„JÐ Ñ!¥2¤:Ð.BÑ#CÝ	Ð
) 4Ð
)Ð
)Ñ*Ô*Ð*Ð*Ð*s   ÁA Á
BÁ%BÂBc                 ó4   — t          | |dt          ¦  «        pdS )Nz--vram-allocatorr   )rA   ÚVRAM_ALLOCATOR_KEY)r,   r9   s     r   Úrequested_vram_allocatorrR   V   s   € Ý�d˜OÐ-?ÕASÑTÔTÐcÐXcÐcr   c                 óÚ  — dd l }t          | |dt          ¦  «        pt          }|t          vrt          d|›dt          › �¦  «        ‚|dk    rUddlm} 	 |                     ¦   «          t          d¦  «         n*# t          $ r}t          d|› d	�¦  «         Y d }~nd }~ww xY w|j        j        €t          | |¦  «         |j        j        €*|j                             ¦   «         rt!          | ||¦  «         t#          | d
¦  «        }|r$|                     d¦  «        st'          |¦  «        nd
| v rdnd}|dk    r4ddlm} |                     |¬¦  «         dat          d|d›d�¦  «         d S d S )Nr   z--ram-allocatorzUnknown RAM allocator ú: expected one of r   ©ÚramzN[RAM] MMGP RAM Allocator: the RAM of freed CPU tensors goes back to the systemz([RAM] MMGP RAM Allocator not available (z"): PyTorch's CPU allocator is usedz--ram-debugú-é   ©Ú	ram_debug©Úmin_mbTz–[RAM] Debug mode: a report of where the RAM goes is written after each generation and after each model release in <outputs>/ram_debug (allocations of Úgz$ MB and more traced to their source))r   rA   ÚRAM_ALLOCATOR_KEYÚRAM_ALLOCATOR_DEFAULTÚRAM_ALLOCATOR_CHOICESÚ
ValueErrorÚmmgp.allocatorrV   ÚinstallrJ   ÚRuntimeErrorr   r    rO   r!   r"   Ú_apply_cuda_settingsr0   r*   ÚfloatrZ   ÚstartÚ
_ram_debug)	r,   r9   r   r   rV   rN   Údebug_valueÚdebug_mbrZ   s	            r   Úapply_startup_settingsrk   Z   sø  € Ø€L€L€LÝ˜t _Ð6GÕIZÑ[Ô[ÐtÕ_t€MØÕ1Ð1Ð1ÝÐl°-ÐlÐlÕUjÐlÐlÑmÔmÐmØ˜ÒÐØ&Ð&Ð&Ð&Ð&Ð&ð	dØ�KŠK‰MŒMˆMõ ÐbÑcÔcÐcÐcøõ ð 	hð 	hð 	hÝÐf¸UÐfÐfÐfÑgÔgÐgÐgÐgÐgÐgÐgøøøøð	høøøð „}ÔÐ Ý˜T ?Ñ3Ô3Ð3Ø„}ÔÐ  U¤Z×%<Ò%<Ñ%>Ô%>Ð Ý˜T ?°EÑ:Ô:Ð:Ý˜d MÑ2Ô2€KØ%0Ð|¸×9OÒ9OÐPSÑ9TÔ9TÐ|�u�[Ñ!Ô!Ð!Ð`mÐquÐ`uÐ`uÐZ\ÐZ\Ð{|€HØ�!‚|€|Ø,Ð,Ð,Ð,Ð,Ð,Ø�Š˜xˆÑ(Ô(Ð(àˆ
Ýð RØ!)ÐQðRð Rð Rñ 	Sô 	Sð 	Sð 	Sð 	Sð €|s   ÁA7 Á7
BÂBÂBc                 ó´  — t          | |¦  «        }|t          vrt          d|›dt          › �¦  «        ‚|dv rhddlm} 	 |                     |dk    ¬¦  «         t          d|dk    rd	nd
z   ¦  «         n,# t          $ r}t          d|› d�¦  «         d}Y d }~nd }~ww xY w|at          t          | d¦  «        pd¦  «        }|dk    rE|dvrt          d¦  «        ‚ddlm} |                     |¬¦  «         dat          d|d›d�¦  «         t          t           j                             dt&          ¦  «        ¦  «        }|dk    r’|                     dd¬¦  «         t*          j        dk    rt/          j        d¦  «        nt/          j        d¦  «        }	|	                     dt/          j        |¦  «        ¦  «        }
|
dk    rt          d|› d|
› �¦  «        ‚d S d S )NzUnknown VRAM allocator rT   r   r   )Ú	allocatorr   )Úspillz$[VRAM] MMGP Optimized VRAM Allocatorz%, spilling into RAM when VRAM is fullÚ z4[VRAM] MMGP Optimized VRAM Allocator not available (z): PyTorch's allocator is usedr   z--vram-debugzH--vram-debug needs the MMGP allocator: --vram-allocator vmm or vmm_spill©Údebugr[   Tz"[VRAM] Debug mode: allocations of r]   zX MB and more recorded, a report is written after each generation in <outputs>/vram_debugÚWANGP_CUDA_STACK_BYTESr&   r!   )ÚdeviceÚwin32z
nvcuda.dllzlibcuda.so.1z#cuCtxSetLimit(CU_LIMIT_STACK_SIZE, z) failed with CUDA error )rR   ÚVRAM_ALLOCATOR_CHOICESra   r   rm   rc   rJ   rd   r   rf   r0   rb   rq   rg   Ú_vram_debugÚintr   rM   r8   ÚCUDA_STACK_BYTESÚzerosÚsysÚplatformÚctypesÚWinDLLÚCDLLÚcuCtxSetLimitÚc_size_t)r,   r9   r   rm   r   rN   rj   rq   Ústack_bytesÚdriverÚresults              r   re   re   v   sl  € Ý(¨¨Ñ?Ô?€IØÕ.Ð.Ð.ÝÐj°9ÐjÐjÕRhÐjÐjÑkÔkÐkØÐ(Ð(Ð(Ø4Ð4Ð4Ð4Ð4Ð4ð	JØ×"Ò"¨°kÒ)AÐ"ÑBÔBÐBõ
 Ð8ÐgpÐtÒgÐgÐ<cÐ<cð  FHñ  Iñ  Jô  Jð  Jð  Jøõ	 ð 	"ð 	"ð 	"ÝÐnÈÐnÐnÐnÑoÔoÐoØ!ˆIˆIˆIˆIˆIˆIøøøøð	"øøøð €NÝ•[  ~Ñ6Ô6Ð;¸!Ñ<Ô<€HØ�!‚|€|ØÐ0Ð0Ð0ÝÐgÑhÔhÐhØ(Ð(Ð(Ð(Ð(Ð(Ø�Š˜8ˆÑ$Ô$Ð$àˆÝð  X°8ð  Xð  Xð  Xð  Xñ  	Yô  	Yð  	YÝ•b”j—n’nÐ%=Õ?OÑPÔPÑQÔQ€KØ�Q‚€Ø�Š�A˜fˆÑ%Ô%Ð%Ý03´ÀÒ0GÐ0G•”˜|Ñ,Ô,Ð,ÍVÌ[ÐYgÑMhÔMhˆØ×%Ò% a­¬¸Ñ)EÔ)EÑFÔFˆØ�QŠ;ˆ;ÝÐsÀ[ÐsÐsÐkqÐsÐsÑtÔtÐtð €ð ˆ;s   ¿A4 Á4
BÁ>BÂBc                 ó^  — t           sdS ddlm} t          j                             | dt          j        d¦  «        › d|pd› d�¦  «        }|                     |¦  «         | 	                    ¦   «          t          d	|› d
t          j                             |¦  «        d         › d�¦  «         dS )z{With --vram-debug: the report of the generation that just ended, in output_dir/vram_debug; then new peaks for the next one.Nr   rp   Ú
vram_debugú%Y-%m-%d-%Hh%Mm%SsÚ_Ú
generationú.jsonz[VRAM debug] Report: ú (summary: z.md))rv   rb   rq   r   r   r   ÚtimeÚstrftimeÚreportÚresetrJ   Úsplitext)Ú
output_dirÚlabelrq   r   s       r   Úwrite_vram_debug_reportr’   —   s·   € åð ØˆØ$Ð$Ð$Ð$Ð$Ð$ÝŒ7�<Š<˜
 LµT´]ÐCWÑ5XÔ5XÐ2wÐ2wÐ[`Ð[pÐdpÐ2wÐ2wÐ2wÑxÔx€DØ	‡L‚L�ÑÔÐØ	‡K‚K�M„M€MÝ	Ð
R $Ð
RÐ
Rµ2´7×3CÒ3CÀDÑ3IÔ3IÈ!Ô3LÐ
RÐ
RÐ
RÑSÔSÐSÐSÐSr   c                  ó:   — ddl m}  |                      ¦   «          dS )zµThe end of a queue: the freed blocks the MMGP RAM allocator kept for reuse go back to the system (between generations of the same model
    they are kept: the next one reuses them).r   rU   N)rb   rV   ÚreleaserU   s    r   Úrelease_ram_cacher•   ¢   s'   € ð #Ð"Ð"Ð"Ð"Ð"Ø‡K‚K�M„M€M€M€Mr   c                 óN   — t           rddlm} |                     | ¦  «         dS dS )zIWith --ram-debug: the RAM that follows counts for the peak of phase name.r   rY   N)rh   rb   rZ   Úmark)ÚnamerZ   s     r   Úram_debug_markr™   ©   s>   € åð Ø,Ð,Ð,Ð,Ð,Ð,Ø�Š�tÑÔÐÐÐðð r   úafter generationc                 ód  — t           sdS ddlm} t          j                             | dt          j        d¦  «        › d|pd› d�¦  «        }|                     |||¬	¦  «         | 	                    ¦   «          t          d
|› dt          j                             |¦  «        d         › d�¦  «         dS )zˆWith --ram-debug: a census of the RAM now (census_label) and the peaks of the phases since the previous report, in output_dir/ram_debug.Nr   rY   rZ   r†   r‡   rˆ   r‰   )r‘   Úsnapshot_labelz[RAM debug] Report: rŠ   zJ.md); the latest report includes the RAM after each report since the start)rh   rb   rZ   r   r   r   r‹   rŒ   r�   rŽ   rJ   r�   )r�   r‘   Úcensus_labelrZ   r   s        r   Úwrite_ram_debug_reportrž   °   sä   € åð ØˆØ(Ð(Ð(Ð(Ð(Ð(ÝŒ7�<Š<˜
 KµD´MÐBVÑ4WÔ4WÐ1vÐ1vÐZ_ÐZoÐcoÐ1vÐ1vÐ1vÑwÔw€DØ×Ò�T °|ÐÑDÔDÐDØ‡O‚OÑÔÐÝ	ð  X ð  Xð  Xµ"´'×2BÒ2BÀ4Ñ2HÔ2HÈÔ2Kð  Xð  Xð  Xñ  Yô  Yð  Yð  Yð  Yr   r?   )rš   )&Ú__doc__r|   r6   r   rz   r‹   rQ   ru   r^   r`   r_   rv   rh   r   rx   rF   rG   r   rL   r   Údirnamer3   Ú__file__r   r   r   r$   r0   r=   rA   rO   rR   rk   re   r’   r•   r™   rž   r   r   r   ú<module>r¢      s¹  ðð0ð 0ð €€€Ø €€€Ø 	€	€	€	Ø 
€
€
€
Ø €€€à%Ð Ø8Ð Ø#Ð Ø+Ð ØÐ Ø Ð  €ˆjØ€ð Ð ØH}Ñ EÐ Ð)Ð+EØ!Ð ØŒW�_Š_˜RœWŸ_š_¨R¬W¯_ª_¸XÑ-FÔ-FÑGÔGÑHÔH€
ðPð Pð Pð
2ð 2ð 2ðsð sð sðð ð ðð ð ð ðQð Qð Qð
+ð +ð +ð dð dð dðSð Sð Sð8uð uð uðBTð Tð Tðð ð ðð ð ðYð Yð Yð Yð Yð Yr   