+
    "Ý³jœ  ã                   óF   € R t ^ RIt]P                  ! ]4      tRsR R ltR# )aq  
Workaround for a Triton miscompile that produces spurious NaN/Inf in compiled
float8 backward kernels (e.g. FSDP2 float8 training with torch.compile).

Root cause: PyTorch inductor PR pytorch/pytorch#186933 changed the Triton codegen
for ``minimum``/``maximum`` from the ``tl.where``-based ``triton_helpers.{minimum,
maximum}`` to ``tl.{minimum,maximum}(a, b, tl.PropagateNan.ALL)``. The two forms
are numerically identical (both propagate NaN), but the ``PropagateNan.ALL`` form
lowers to PTX ``min.NaN``/``max.NaN`` instructions that make Triton mis-lower a
neighboring transposed, vectorized 1-byte (fp8) store. The stored value is
correct, but the transposed store writes garbage bytes that decode as NaN/Inf in
fp8. See https://github.com/triton-lang/triton/issues/11111.

This monkeypatch reverts the inductor min/max codegen to the numerically-identical
``tl.where`` form, which does not emit the ``min.NaN``/``max.NaN`` instructions and
so avoids tripping the Triton store bug. It is applied from
``convert_to_float8_training`` so it only affects callers who actually use the
float8 training product, not every ``import torchao``. The ``tl.where`` form is
always correct, so this is a no-op on numerics even once Triton fixes the
underlying bug.

Note: we must emit the ``tl.where`` expression *inline* rather than delegate to
``triton_helpers.{minimum,maximum}``. #186933's follow-up also rewrote those helper
functions to ``tl.{minimum,maximum}(..., propagate_nan=tl.PropagateNan.ALL)``, so
routing through them would still emit ``min.NaN``/``max.NaN`` and fail to work
around the bug (confirmed against torch 2.14.0.dev / triton 3.8.0).
NFc                ó   € V ^8„  d   QhRR/# )é   ÚreturnN© )Úformats   "Ú_/home/luca/screen-checking/.venv/lib/python3.14/site-packages/torchao/float8/_inductor_patch.pyÚ__annotate__r   *   s   € ÷ %ñ %¨ñ %ó    c                 ó  € \         '       d   R#  ^ RIHp  \        R 4       p\        R 4       pY n        Y0n        Rs \        P                  R4       R#   \         d"   p\        P                  RT4        Rp?R# Rp?ii ; i)zÆRevert inductor's Triton min/max codegen to the ``tl.where`` form.

Idempotent and defensive: if inductor internals have moved, this logs and
returns rather than breaking ``import torchao.float8``.
N)ÚTritonOverridesz5float8: could not import TritonOverrides to patch: %sc                 ó,   € R V  RV RV  RV  RV  RV R2# )ú
tl.where((z < ú) | (ú != ú), ú, Ú)r   ©ÚaÚbs   &&r   ÚminimumÚ0_patch_inductor_min_max_codegen.<locals>.minimumA   ó/   € à˜A˜3˜c !  E¨!¨¨D°°°3°q°c¸¸A¸3¸aÐ@Ð@r	   c                 ó,   € R V  RV RV  RV  RV  RV R2# )r   z > r   r   r   r   r   r   r   s   &&r   ÚmaximumÚ0_patch_inductor_min_max_codegen.<locals>.maximumE   r   r	   Tzqfloat8: patched inductor Triton min/max codegen to work around https://github.com/triton-lang/triton/issues/11111)	Ú_PATCHEDÚtorch._inductor.codegen.tritonr   Ú	ExceptionÚloggerÚdebugÚstaticmethodr   r   )r   Úer   r   s       r   Ú_patch_inductor_min_max_codegenr#   *   sˆ   € ÷ ƒxÙðÝBô ñAó ðAô ñAó ðAð &ÔØ%ÔØ€HÜ
‡L�Lð	=öøô- ô Ü�‰ÐLÈaÔPÝûðús   �A ÁA?ÁA:Á:A?)Ú__doc__ÚloggingÚ	getLoggerÚ__name__r   r   r#   r   r	   r   Ú<module>r(      s)   ðñó8 à	×	Ò	˜8Ó	$€à€÷%r	   