§
     ’Þj¼  ã                   óL  — d Z ddlZddlZddlmZ ddlmZ ddlmZ  e¦   «         Z	ej
        dej        dej        diZd	Zej        d
„ ¦   «         Zdddœd„Zej        d„ ¦   «         Zej        d„ ¦   «         Zd„ Zddddddœd„Zej
        fddddœd„Z ej        d„ ¦  «         dS )a{  Common eligibility and family ordering for activation kernels.

Dispatch never caches tensors, modules, bound methods or shape-specific choices.
An operation supplies only the implementations that satisfy its arithmetic and
layout contract; the existing user policy orders those implementations.

Device eligibility lives here, once: a WanGP-owned kernel runs on every NVIDIA GPU
whose instructions its arithmetic needs (DTYPE_FLOORS), not only where it was first
timed. Triton compiles each kernel for the running GPU; the kernels carry no
architecture-specific code, and their precision against PyTorch was measured per
layer and end to end on compute capabilities 8.9 and 12.0 (specs:
H3_FL2VA_ACCELERATION_AUDIT_20261021*.md, H3_VAE_SPEED_20261009.md). An operation
whose kernel needs more than its dtype floor says so (`capability=`); one validated
on HIP admits it (`hip=True`); PACKED_FMA_CAPABILITY marks the one architecture whose
compiler changes a bit-exact kernel's rounding. Performance-only restrictions (a kernel
that runs everywhere but only beats cuDNN on RTX 50) stay with the kernel that measured
them, built on `capability()`.
é    N)Útqdm)Úcuda_graphsé   )Úkernel_policy©é   r   )é   é   )é   r   c                 ó¦   — t           j        j        �dS t           j                             | €t           j                             ¦   «         n| ¦  «        S )z{(major, minor) of an NVIDIA CUDA device, None on HIP. Device capabilities are immutable; cache metadata, never activations.N)ÚtorchÚversionÚhipÚcudaÚget_device_capabilityÚcurrent_device)Údevice_indexs    ú?C:\Users\johnn\OneDrive\Documents\AI\shared\kernels\dispatch.pyÚ
capabilityr   %   sC   € õ „}ÔÐ$ØˆtÝŒ:×+Ò+È<ÐK_­E¬J×,EÒ,EÑ,GÔ,GÐ,GÐeqÑrÔrÐró    F©Úcapability_floorr   c                ó–   — | j         dk    rdS t          | j        ¦  «        }|€|S |€t                               |d¦  «        n|}||k    S )a  Whether a WanGP kernel computing in dtype runs on device: NVIDIA GPUs from the dtype's floor, or from the kernel's own
    `capability_floor` when it states one (its arithmetic, e.g. a BF16 kernel reading bits on Turing); HIP devices only when the
    operation admits them.r   FNr   )Útyper   ÚindexÚDTYPE_FLOORSÚget)ÚdeviceÚdtyper   r   ÚfoundÚfloors         r   Údevice_supportedr"   -   sY   € ð „{�fÒÐØˆuÝ�v”|Ñ$Ô$€EØ€}Øˆ
Ø/?Ð/G�L×Ò˜U FÑ+Ô+Ð+ÐM]€EØ�EŠ>Ðr   c                 ó¸   — |t           j        k    pJt           j        j        dup7t           j                             | ¦  «        t          t           j                 k    S )zðBF16 arithmetic needs native conversion instructions on NVIDIA GPUs (HIP: Triton's own conversions). Self-contained, so that
    clearing its cache alone re-reads the device (the LM engine and FlashVSR tests patch the capability around it).N)r   Úbfloat16r   r   r   r   r   )r   r   s     r   Útriton_dtype_supportedr%   :   s^   € ð •E”NÒ"ð  P¥e¤mÔ&7¸tÐ&Cð  PÅuÄz×GgÒGgÐhnÑGoÔGoÕsõ  AFô  AOô  tPò  HPð  Pr   c                 óV   — t          | ¦  «        }|duo|d         t          d         k    S )zøWhether the compiler of this device fuses a packed multiply and add despite enable_fp_fusion=False (PACKED_FMA_CAPABILITY): a
    kernel bit-identical to PyTorch elsewhere is one rounding apart here, so it counts as approximate on this device only.Nr   )r   ÚPACKED_FMA_CAPABILITY)r   r    s     r   Ú
packed_fmar(   A   s0   € õ �|Ñ$Ô$€EØ˜ÐÐE  q¤Õ-BÀ1Ô-EÒ!EÐEr   c                 ó˜   — t          | t          ¦  «        rt          | ¦  «        dk    rt          d¦  «        ‚|                      ¦   «         S )zëConsume an owned activation. Callers must drop their other references.

    Residuals, parameters, rotary tables and caches are borrowed explicitly, not
    put in this holder. In-place APIs ending in '_' borrow their destination.
    r   z(Expected a one-element activation holder)Ú
isinstanceÚlistÚlenÚ
ValueErrorÚpop)Úx_lists    r   Útaker0   I   sD   € õ �f�dÑ#Ô#ð E¥s¨6¡{¤{°aÒ'7Ð'7ÝÐCÑDÔDÐDØ�:Š:‰<Œ<Ðr   )Ú
wgpkernelsÚkitchenÚtritonÚowned_tritonc          	      óN  — t          j        |p|||p|dœ¦  «        }| �†t          j                             ¦   «         sh|dk    r|sdn|pd}| |t           j        f}|t          vrBt                               |¦  «         t          j	        d| › d|› dt           j        › d�¦  «         |S )	N)r1   r2   r3   r1   zWanGP TritonÚpytorchz
[Kernels] z: z (z).)
r   Úfirstr   ÚcompilerÚis_compilingÚ	precisionÚ
_announcedÚaddr   Úwrite)Ú	operationr1   r2   r3   r4   ÚfamilyÚimplementationÚkeys           r   ÚchooserB   T   sÍ   € õ Ô °
Ð0J¸lØ-4ÀÐ@VÈ,ð"Xð "Xñ Yô Y€FàÐ¥U¤^×%@Ò%@Ñ%BÔ%BÐØ+1°\Ò+AÐ+AÈ*Ð+A˜˜ÐZ`ÐZmÐdmˆØ˜­Ô)@Ð@ˆØ•jÐ Ð Ý�NŠN˜3ÑÔÐÝŒJÐ^ IÐ^Ð^°Ð^Ð^Å=ÔCZÐ^Ð^Ð^Ñ_Ô_Ð_Ø€Mr   )ÚdtypesÚapproximater   r   c                ó"  — t           j                             ¦   «         sSt          j        ¦   «         s@t	          | ¦  «        t           j        us%| j        r| j        |vs|rt          j	        ¦   «         sdS t          | j        | j        ||¬¦  «        S )a7  Whether a WanGP kernel may replace the PyTorch path for x: no autograd (inference mode, or grad disabled as wgp.py runs every
    generation: the kernels never touch autograd, and pipelines under no_grad alone, such as Flux, Qwen Image and Wan, were running
    the PyTorch paths while their blocks measured the kernels under inference mode), no compilation, a CUDA tensor of one of dtypes
    on a device the kernel supports (device_supported), and the user's precision policy when the kernel is approximate (a bool,
    possibly decided per device: see packed_fma).Fr   )r   r8   r9   Úis_grad_enabledr   ÚTensorÚis_cudar   r   Úallow_approximater"   r   )ÚxrC   rD   r   r   s        r   Úcuda_inferencerK   b   s�   € õ 	Œ×#Ò#Ñ%Ô%ð ­Ô)>Ñ)@Ô)@ð Ý�A‰wŒw�eœlÐ*Ð*°!´)Ð*¸q¼wÈfÐ?TÐ?TØð @UÝ$1Ô$CÑ$EÔ$Eð @UàˆuÝ˜AœH a¤gÐ@PÐVYÐZÑZÔZÐZr   c                  ó2   — t           j        t           j        fS ©N)r   r:   Úfamilies© r   r   ú<lambda>rP   q   s   € ¥-Ô"9½=Ô;QÐ!R€ r   rM   )Ú__doc__Ú	functoolsr   r   Úmmgpr   Ú r   Úsetr;   r$   Úfloat16Úfloat32r   r'   Úcacher   r"   r%   r(   r0   rB   rK   Úregister_keyrO   r   r   ú<module>rZ      sŽ  ððð ð$ Ð Ð Ð Ø €€€Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð Ø Ð Ð Ð Ð Ð àˆS‰UŒU€
ð ” ¨¬°v¸u¼}ÈfÐU€ð  Ð ð „ðsð sñ „ðsð 9=À%ð 
ð 
ð 
ð 
ð 
ð „ðPð Pñ „ðPð „ðFð Fñ „ðFðð ð ð¨¸ÀeÐZ_ð ð ð ð ð ð "'¤Ð 1¸uÐW[Ðafð 
[ð 
[ð 
[ð 
[ð 
[ð €Ô ÐRÐRÑ SÔ SÐ SÐ SÐ Sr   