
    VpfJ                    f   U d Z ddlmZ ddlmZmZ ddlmZ  G d d          Zee         Z	 G d de          Z
 G d	 d
          Z e            Zded<    e            Zd
ed<    e            Zd
ed<    e            Zd
ed<    e            Zd
ed<    e            Zd
ed<    e            Zd
ed<   dS )aq  JAX effects.

JAX uses effects to describe computations that may have side-effects. Effects
are associated with JAX primitive instances and Jaxprs.

A primitive instance with an effect will be protected from dead-code elimination
even if its result is unused.

A special class of effects are the **ordered** effects
(members of `effects.ordered_effects`).
The lowering of a computation with ordered effects will have one additional
input and one additional output for each ordered effect. These appear before
the regular inputs/outputs, and are of type `i1[0]`. These tokens
are threaded through the instructions with ordered effects to ensure that the
compiler will not eliminate, replicate, or reordered the corresponding
instructions.

To ensure the ordering across multiple computations we maintain a
per-thread set of the tokens returned by the last dispatched computation. There
is one token per ordered effect, and it may be sharded over the devices
used by the last dispatched computation. Upon dispatching a
new computation with ordered effects we take the current token, we shard it
on the devices for the computation to be dispatched and we pass it as an input.
Then we update the current token to refer to the token output of
the dispatched computation.

When we have ordered effects, we also use the current token to implement
`jax.barrier` which waits until the current tokens are ready.

The implementation of `jax.barrier` for unordered effects is a bit different,
because for these effects we do not thread tokens in and out of dispatched
computation. Instead, we use a `RuntimeToken`, which is an object returned when
dispatching a computation and on which we can block until is ready. We store
for each thread the `RuntimeToken` returned by the last dispatched computation.

For more details, see the design note:
https://jax.readthedocs.io/en/latest/jep/10657-sequencing-effects.html.
    )annotations)IterableSet)Anyc                      e Zd ZdZdS )EffectzA generic side-effect.N)__name__
__module____qualname____doc__     P/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/effects.pyr   r   ;   s        r   r   c                  :    e Zd ZdZddZddddZd	 Zd
 Zd ZdS )JaxprInputEffectziA side-effect associated with the input of a jaxpr.

  Note that the `input_index` includes constvars.
  input_indexr   c                    || _         d S Nr   selfr   s     r   __init__zJaxprInputEffect.__init__F   s    "Dr   Nr   
Any | Nonec               >    || j         }|                     |          S r   )r   	__class__r   s     r   replacezJaxprInputEffect.replaceI   s"    $k>>+&&&r   c                Z    t          |t                    st          S | j        |j        k    S r   )
isinstancer   NotImplementedr   )r   others     r   __eq__zJaxprInputEffect.__eq__N   s,    e-.. u000r   c                8    t          | j        | j        f          S r   )hashr   r   r   s    r   __hash__zJaxprInputEffect.__hash__S   s    !12333r   c                0    | j         j         d| j         dS )N())r   r	   r   r$   s    r   __repr__zJaxprInputEffect.__repr__V   s!    n%;;(8;;;;r   )r   r   )r   r   )	r	   r
   r   r   r   r   r!   r%   r)   r   r   r   r   r   @   s         
# # # # 26 ' ' ' ' ' '
1 1 1
4 4 4< < < < <r   r   c                  4    e Zd Zd ZddZdd	ZddZddZdS )EffectTypeSetc                ,    t                      | _        d S r   )set_effect_typesr$   s    r   r   zEffectTypeSet.__init__[   s    ,/EEDr   effect_typetype[Effect]c                :    | j                             |           d S r   )r.   add)r   r/   s     r   add_typezEffectTypeSet.add_type^   s    ;'''''r   effr   returnboolc                D    t          fd| j        D                       S )Nc              3  8   K   | ]}t          |          V  d S r   )r   ).0eff_typer4   s     r   	<genexpr>z)EffectTypeSet.contains.<locals>.<genexpr>b   s-      LLXz#x((LLLLLLr   )anyr.   )r   r4   s    `r   containszEffectTypeSet.containsa   s)    LLLL9KLLLLLLr   effectsIterable[Effect]list[Effect]c                       fd|D             S )Nc                >    g | ]}                     |          |S r   r=   r9   r4   r   s     r   
<listcomp>z+EffectTypeSet.filter_in.<locals>.<listcomp>e   s*    999CdmmC&8&89C999r   r   r   r>   s   ` r   	filter_inzEffectTypeSet.filter_ind   s    999979999r   c                       fd|D             S )Nc                >    g | ]}                     |          |S r   rC   rD   s     r   rE   z/EffectTypeSet.filter_not_in.<locals>.<listcomp>h   s*    ===C$--*<*<=C===r   r   rF   s   ` r   filter_not_inzEffectTypeSet.filter_not_ing   s    ====7====r   N)r/   r0   )r4   r   r5   r6   )r>   r?   r5   r@   )r	   r
   r   r   r3   r=   rG   rJ   r   r   r   r+   r+   Y   sw        2 2 2( ( ( (M M M M: : : :> > > > > >r   r+   Effects
no_effectsordered_effectsshardable_ordered_effectslowerable_effectscontrol_flow_allowed_effects"custom_derivatives_allowed_effectsremat_allowed_effectsN)r   
__future__r   collections.abcr   r   typingr   r   rK   r   r+   	frozensetrL   __annotations__rM   rN   rO   rP   rQ   rR   r   r   r   <module>rX      s  % % %N # " " " " " ) ) ) ) ) ) ) )              f+< < < < <v < < <2> > > > > > > >$  ikk
 ! ! ! !!. 0 0 0 0 ,9=??  : : : :#0=??  2 2 2 2.;moo  = = = =4AMOO " C C C C'4}  6 6 6 6 6 6r   