+
    "Ý³j  ã                  ó®   € ^ RI Ht ^ RIt^ RIt^ RIt^ RIt^ RIHtHtH	t	H
t
 ^ RItRR.t]P                  ! RR7       ! R R4      4       t ! R	 R4      tR# )
é    )ÚannotationsN)ÚAnyÚCallableÚListÚOptionalÚActivationPacketÚCudaEventActivationBufferT)Úslotsc                  ó‚   € ] tR t^t$ RtR]R&   R]R&   RtR]R&   ]P                  ! ]	P                  R	7      tR
]R&   RtR# )r   zÝ
Tracks a single async device->host transfer triggered from a forward hook.

The event is recorded on the dedicated copy stream so the consumer can
decide when to block. The `host_tensor` already points at pinned memory.
ztorch.cuda.EventÚeventútorch.TensorÚhost_tensorNzOptional[Any]Úmeta)Údefault_factoryÚfloatÚ
created_at© )Ú__name__Ú
__module__Ú__qualname__Ú__firstlineno__Ú__doc__Ú__annotations__r   ÚdataclassesÚfieldÚtimeÚperf_counterr   Ú__static_attributes__r   ó    Úg/home/luca/screen-checking/.venv/lib/python3.14/site-packages/gptqmodel/utils/cuda_activation_buffer.pyr   r      s:   ‡ ñð ÓØÓØ€Dˆ-ÓØ#×)Ò)¸$×:KÑ:KÔL€J�×Lr   c                  óº   € ] tR t^!tRtRR R lltRRRR/R R	 lltR
RRR/R R lltR R ltR R lt	R R lt
R R ltR R ltR R ltR R ltR R ltRtR# ) r	   a˜  
Schedules non-blocking GPU->CPU copies using a dedicated CUDA stream + event.

Typical usage inside a forward hook::

    buffer = CudaEventActivationBuffer(device="cuda:6")

    def hook(module, inputs, output):
        tensor = output[0] if isinstance(output, (tuple, list)) else output
        buffer.capture_async(tensor, meta=module.__class__.__name__)

    # elsewhere in consumer thread
    for packet in buffer.drain():
        packet.event.synchronize()
        process(packet.host_tensor, packet.meta)

The hook thread returns immediately after enqueuing the async copy which
allows the caller to release activation VRAM without waiting on D2H traffic.
NTc               ó0   € V ^8„  d   QhRRRRRRRRR	R
RR/# )é   Údeviceztorch.device | str | intÚstreamzOptional[torch.cuda.Stream]Ú
pin_memoryÚboolÚhost_allocatorzIOptional[Callable[[torch.Size, torch.dtype, torch.layout], torch.Tensor]]Úhost_reclaimerz(Optional[Callable[[torch.Tensor], None]]ÚreturnÚNoner   )Úformats   "r    Ú__annotate__Ú&CudaEventActivationBuffer.__annotate__6   sH   € ÷ !ñ !à(ð!ð ,ð!ð ð	!ð
 bð!ð Að!ð 
ñ!r   c                	óì  € \         P                  P                  4       '       g   \        R 4      h\         P                  ! V4      pVP
                  R8w  d   \        RV R24      hVP                  f4   \         P                  ! R\         P                  P                  4       4      pW`n	        W0n
        W@n        WPn        \         P                  P	                  V P                  4      ;_uu_ 4        T;'       g    \         P                  P                  4       V n        RRR4       \        P                   ! 4       V n        \$        P&                  ! 4       V n        ^ V n        R#   + '       g   i     LM; i)z4CUDA is not available for CudaEventActivationBuffer.Úcudaz6CudaEventActivationBuffer requires a CUDA device, got Ú.N)Útorchr0   Úis_availableÚRuntimeErrorr$   ÚtypeÚ
ValueErrorÚindexÚcurrent_deviceÚ_deviceÚ_pin_memoryÚ_host_allocatorÚ_host_reclaimerÚStreamÚ_copy_streamÚqueueÚSimpleQueueÚ_pendingÚ	threadingÚLockÚ_lockÚ_approx_pending)Úselfr$   r%   r&   r(   r)   Údevs   &&&&&& r    Ú__init__Ú"CudaEventActivationBuffer.__init__6   s  € ô �z‰z×&Ñ&×(Ò(ÜÐUÓVÐVä�lŠl˜6Ó"ˆØ�8‰8�vÔÜÐUÐVYÐUZÐZ[Ð\Ó]Ð]à�9‰9ÒÜ—,’,˜v¤u§z¡z×'@Ñ'@Ó'BÓCˆCàŒØ%ÔØ-ÔØ-Ôä�Z‰Z×Ñ˜tŸ|™|×,Õ,Ø &× =Ð =¬%¯*©*×*;Ñ*;Ó*=ˆDÔ÷ -ô @E×?PÒ?PÓ?RˆŒÜ—^’^Ó%ˆŒ
Ø ˆÖ÷ -×,ús   Ã0.E#Å#E3	r   Úenqueuec               ó(   € V ^8„  d   QhRRRRRRRR/# )	r#   Ú
activationr   r   r   rJ   r'   r*   r   r   )r,   s   "r    r-   r.   T   s2   € ÷ $ñ $à ð$ð ð	$ð
 ð$ð 
ñ$r   c               óþ  € VP                   V P                  8w  d'   \        RVP                    RV P                   R24      hVP                  4       pVP	                  4       '       g   VP                  4       pV P                  V4      p\        P                  P                  RRR7      p\        P                  P                  V P                  4      pV P                  pVP                  V4       \        P                  P                  V4      ;_uu_ 4        VP                  VRR7       VP                  V4       RRR4       \!        WTVR	7      pV'       d   V P#                  V4       V#   + '       g   i     L8; i)
z�
Enqueue an async D2H copy of ``activation`` onto the buffer stream.

Returns an ActivationPacket which is also available later via drain().
zActivation tensor is on z, expected r1   F)ÚblockingÚinterprocessT)Únon_blockingN)r   r   r   )r$   r9   r6   ÚdetachÚis_contiguousÚ
contiguousÚ_allocate_hostr2   r0   ÚEventÚcurrent_streamr>   Úwait_streamr%   Úcopy_Úrecordr   Ú_pending_put)	rF   rL   r   rJ   Úhostr   ÚcurrentÚcopy_streamÚpackets	   &&$$     r    Úcapture_asyncÚ'CudaEventActivationBuffer.capture_asyncT   s+  € ð ×Ñ §¡Ô,ÜØ*¨:×+<Ñ+<Ð*=¸[ÈÏÉÈÐVWÐXóð ð  ×&Ñ&Ó(ˆ
Ø×'Ñ'×)Ò)Ø#×.Ñ.Ó0ˆJà×"Ñ" :Ó.ˆä—
‘
× Ñ ¨%¸eÐ ÓDˆä—*‘*×+Ñ+¨D¯L©LÓ9ˆØ×'Ñ'ˆØ×Ñ Ô(ä�Z‰Z×Ñ˜{×+Õ+Ø�J‰J�z°ˆJÔ5Ø�L‰L˜Ô%÷ ,ô "¨ÀdÔKˆßØ×Ñ˜fÔ%Øˆ÷ ,×+ús   Ä%E,Å,E<	ÚwaitÚ	max_itemsc               ó$   € V ^8„  d   QhRRRRRR/# )r#   ra   r'   rb   zOptional[int]r*   zList[ActivationPacket]r   )r,   s   "r    r-   r.   z   s"   € ÷ ñ ˜Tð °]ð ÐNdñ r   c               óü   € . p^ p Ve
   WB8¼  d    V#  V P                  4       pT^,          pT'       d   TP                  P	                  4        TP                  T4       K]    \        P                   d     T# i ; i)zŒ
Collect all queued packets (or up to max_items) in FIFO order.

When ``wait`` is True we synchronize each packet's event before returning.
)Ú_pending_getr?   ÚEmptyr   ÚsynchronizeÚappend)rF   ra   rb   ÚpacketsÚpulledr^   s   &$$   r    ÚdrainÚCudaEventActivationBuffer.drainz   s‚   € ð +-ˆØˆàØÒ$¨Ô)<Øð ˆðØ×*Ñ*Ó,�ð �a�KˆFßØ—‘×(Ñ(Ô*Ø�N‰N˜6Ö"øô —;‘;ô Øð ˆðús   ”A" Á"A;Á:A;c               ó    € V ^8„  d   QhRRRR/# ©r#   r^   r   r*   r+   r   )r,   s   "r    r-   r.   “   s   € ÷ 5ñ 5Ð.ð 5°4ñ 5r   c                ó\   € V P                   e   V P                  VP                  4       R# R# )zD
Return a packet's host buffer to the allocator pool (if provided).
N)r<   r   ©rF   r^   s   &&r    ÚrecycleÚ!CudaEventActivationBuffer.recycle“   s)   € ð ×ÑÒ+Ø× Ñ  ×!3Ñ!3Ö4ñ ,r   c               ó   € V ^8„  d   QhRR/# ©r#   r*   Úintr   )r,   s   "r    r-   r.   š   s   € ÷ ñ ˜sñ r   c                ó‚   € V P                   ;_uu_ 4        \        V R^ 4      pRRR4       V#   + '       g   i     X# ; i)zŠ
Non-blocking length check. The SimpleQueue does not expose qsize()
reliably on all platforms, so we track with a lock-protected counter.
rE   N)rD   Úgetattr)rF   Úcounts   & r    Úpending_countÚ'CudaEventActivationBuffer.pending_countš   s3   € ð
 �Z�Z‹ZÜ˜DÐ"3°QÓ7ˆE÷ àˆ÷ ŽZàˆús   •-­>	c               ó   € V ^8„  d   QhRR/# rt   r   )r,   s   "r    r-   r.   £   s   € ÷ $ñ $˜ñ $r   c                	ó"   € V P                  4       # ©N)ry   ©rF   s   &r    Ú__len__Ú!CudaEventActivationBuffer.__len__£   s   € Ø×!Ñ!Ó#Ð#r   c               ó   € V ^8„  d   QhRR/# )r#   r*   z'CudaEventActivationBuffer'r   )r,   s   "r    r-   r.   ¦   s   € ÷ ñ Ð6ñ r   c                	ó   € V # r}   r   r~   s   &r    Ú	__enter__Ú#CudaEventActivationBuffer.__enter__¦   s   € Øˆr   c               ó   € V ^8„  d   QhRR/# )r#   r*   r+   r   )r,   s   "r    r-   r.   ©   s   € ÷ ñ ¨Tñ r   c                	ó*   € V P                  R R7       R# )T)ra   N)rk   )rF   Úexc_typeÚexcÚtbs   &&&&r    Ú__exit__Ú"CudaEventActivationBuffer.__exit__©   s   € Ø�
‰
˜ˆ
Ör   c               ó    € V ^8„  d   QhRRRR/# rn   r   )r,   s   "r    r-   r.   ¬   s   € ÷ "ñ "Ð#3ð "¸ñ "r   c                	óÎ   € V P                   ;_uu_ 4        \        V R ^ 4      ^,           V n        RRR4       V P                  P	                  V4       R#   + '       g   i     L-; i©rE   N)rD   rw   rE   rA   Úputrp   s   &&r    rZ   Ú&CudaEventActivationBuffer._pending_put¬   sA   € Ø�Z�Z‹ZÜ#*¨4Ð1BÀAÓ#FÈÕ#JˆDÔ ÷ à�‰×Ñ˜&Ö!÷ �Zús   •AÁA$	c               ó   € V ^8„  d   QhRR/# )r#   r*   r   r   )r,   s   "r    r-   r.   ±   s   € ÷ ñ Ð.ñ r   c           	     	óâ   € V P                   P                  4       pV P                  ;_uu_ 4        \        \	        V R ^ 4      ^,
          ^ 4      V n        RRR4       V#   + '       g   i     T# ; irŽ   )rA   Ú
get_nowaitrD   Úmaxrw   rE   rp   s   & r    re   Ú&CudaEventActivationBuffer._pending_get±   sS   € Ø—‘×)Ñ)Ó+ˆØ�Z�Z‹ZÜ#&¤w¨tÐ5FÈÓ'JÈQÕ'NÐPQÓ#RˆDÔ ÷ àˆ÷ ŽZàˆús   ¯$AÁA.	c               ó    € V ^8„  d   QhRRRR/# )r#   rL   r   r*   r   )r,   s   "r    r-   r.   ·   s   € ÷ 
ñ 
¨ð 
¸,ñ 
r   c                	óL  € V P                   eU   V P                  VP                  VP                  VP                  4      pVP	                  4       '       g   \        R4      hV# \        P                  ! VP                  VP                  VP                  RV P                  R7      # )Nz5Custom host allocator must return pinned CPU tensors.Úcpu)ÚdtypeÚlayoutr$   r&   )	r;   Úshaper™   rš   Ú	is_pinnedr6   r2   Úemptyr:   )rF   rL   r[   s   && r    rT   Ú(CudaEventActivationBuffer._allocate_host·   sŠ   € Ø×ÑÒ+Ø×'Ñ'¨
×(8Ñ(8¸*×:JÑ:JÈJ×L]ÑL]Ó^ˆDØ—>‘>×#Ò#Ü Ð!XÓYÐYØˆKÜ�{Š{Ø×ÑØ×"Ñ"Ø×$Ñ$ØØ×'Ñ'ô
ð 	
r   )rE   r>   r9   r;   r<   rD   rA   r:   )NTNN)r   r   r   r   r   rH   r_   rk   rq   ry   r   rƒ   rŠ   rZ   re   rT   r   r   r   r    r	   r	   !   sf   † ñ÷(!ð<$ð ð	$ð
 ÷$ðL Dð ÀT÷ õ25õõ$õõõ"õ
÷
ñ 
r   )Ú
__future__r   r   r?   rB   r   Útypingr   r   r   r   r2   Ú__all__Ú	dataclassr   r	   r   r   r    Ú<module>r£      s_   ðõ #ã Û Û Û ß 0Ó 0ã ð Ð:Ð
;€ð ×Ò˜TÔ"÷Mð Mó #ðM÷b
ó b
r   