
    Vpf\                     4   d dl mZ d dlZd dlZd dlmZ d dlmZ d dlm	Z	 d dlm
Z
 d dlmZ d dlmZ d d	lmZ d d
lmZ ej        Ze	j        Zedej        f         ZeZdZdej        dej        fdZdej        fdZde
j        dedefdZdej        de
j        fdZde
j        defdZ  ej!        dee eedd          Z" ej#        e"           dej        fdZ$de
j        dedefdZ%de
j        de
j        fdZ&de
j        defdZ' ej!        d e$e'e%e&d!d"          Z( ej#        e(           d#edefd$Z) e)ej*                  Z* e)ej+                  Z+ e)ej,                  Z,	 d,d%ed&ej        d'ed(ed)ed*e-e
j.        df         dz  dej        fd+Z/dS )-    )CallableN)numpy)random)blocked_sampler)typing)	prng_seedprng_random_bits)
primitives)prng.   keyreturnc                    | j         }t          j        |           }t          j        t
          j                  }t          j        |j         t          |          d                   |k     rt          d| d          t          j
        ||dz             dd|f         }t          j
        ||t
          j        z             }t          j        |d          S )zEHelper function for converting non-Pallas PRNG keys into Pallas keys.NzKey data must be at least z bytes.).
pallas_tpuimpl)shapejax_api_randomkey_datanpprodtpu_key_impl	key_shapelen
ValueErrorjnpreshapewrap_key_data)r   
batch_dimsr   pallas_key_sizepallas_key_datas        ]/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/pallas/mosaic/random.pyto_pallas_keyr%   &   s    y*$S))(GL233/WX^C
OO,,-..@@
J/JJJ
K
KKK
U"$ $$')9/)9$9;/K *\-C CE E/		%oL	I	I	II    seedc                     t          j        t          j        t           j                  }|| z                       t           j                  S )Ndtype)r   zerosr   r   int32astypeuint32)r'   	seed_datas     r$   
_seed_funcr0   3   s6    i.ci@@@)
d
	"	"3:	.	..r&   	bit_widthr   c                 h    |dk    rt          d          t          |            t          |          S N    zBit width must be 32)r   r   r
   r   r1   r   s      r$   _random_bitsr6   7   s3    "__
+
,
,,C...	%	 	  r&   datac                    t           j        }t          |           t          |t          fz                                 t          j                  }t          |            t          |t          fz                                 t          j                  }|dt          dz
  f         |dt          dz
  f         z  }|j        |k    sJ t          j
                            |d          S )N.   r   r   )r   r   r   r
   FOLD_IN_ROUNDSr-   r   r.   r   jaxr   r    )r   r7   r   	data_bitskey_bitsmixeds         r$   _fold_inr?   =   s     $)D///>##% %%+VCJ%7%7 C...>##% %%+VCJ%7%7  3q((
)Ic>!;K6K,L
L%			!	!	!	!		!	!%l	!	;	;;r&   c                 "    ~ ~t                      NNotImplementedErrorr   r   s     r$   _splitrE   P       	5r&   )r9   r9   r   pl)r   r'   splitrandom_bitsfold_innametagc                 D    ~ t          j        dt           j                  S )N r)   )r   emptyr,   )r'   s    r$   _pl_stateful_seed_funcrP   i   s    
	2SY	'	'	''r&   c                 B    ~ |dk    s
J d            t          |          S r3   r	   r5   s      r$   _pl_stateful_random_bitsrR   n   s'    		b0	%	 	  r&   c                 "    ~ ~t                      rA   rB   )r   r7   s     r$   _pl_stateful_fold_inrT   s   s    	4r&   c                 "    ~ ~t                      rA   rB   rD   s     r$   _pl_stateful_splitrV   w   rF   r&   rN   _pallas_internal_stateful_pl_statefulsamplerc                       fd} j         rCt          d  j                             d                    }d                    |          |_         |S )zConverts a jax.random sampling function to a stateful version.

  Args:
    sampler: A sampling function that consumes a key and returns
      random samples.

  Returns:
    A stateful sampling function with the key argument removed.
  c                  R    t          j        dt                    } |g| R i |S )Nr   r   )r   r   tpu_internal_stateful_impl)argskwargsplaceholder_keyrY   s      r$   new_samplerz+_make_stateful_sampler.<locals>.new_sampler   s;     %(1KLLLO7?4T444V444r&   c                 
    d| vS )Nzkey:rN   )lines    r$   <lambda>z(_make_stateful_sampler.<locals>.<lambda>   s    V4' r&   
)__doc__filterrH   join)rY   r`   	doc_liness   `  r$   _make_stateful_samplerri      sk    5 5 5 5 5 _ /'')>)>t)D)DF FI))I..K	r&   
sampler_fn
global_key
block_size	tile_size
total_sizeblock_indexc                 j   t          |          t          |          k    r0t          dt          |           dt          |           d          |5t          |          }t          d t          |          D                       }t	          j        |||||          }t	          j        | |||fi |S )a]  Samples a block of random values with invariance guarantees.

  `sample_block` allows the sampling of identical blocks of random values
  across kernels with different block shapes and iteration orders. Each call
  to `sample_block` returns a `block_size`-shaped array of random samples
  corresponding to the `block_index`.

  `tile_size` should be chosen such that it is a divisor to all block sizes
  one needs to be invariant to. The larger the `tile_size`, the more
  efficient the sampling process wil be and therefore the best choice is
  typically the greatest common divisor between all possible block sizes.

  Args:
    sampler_fn: A sampling function that consumes a key and returns
      random samples.
    global_key: The global key to use for sampling.
    block_size: The shape of an individual block.
    tile_size: The shape of a `tile`, which is the smallest unit at
      which samples are generated. This should be selected to be a divisor
      of all block sizes one needs to be invariant to.
    total_size: The total size of the array to sample.
    block_index: The index denoting which block to generate keys for. Defaults
      to the program_id for each block axis.
    **kwargs: Additional arguments to pass to the sampler_fn.

  Returns:
    A `block_size` shaped array of samples for the current block corresponding
    to `block_index`.
  zblock_size (z) and tile_size (z) must have the same length.Nc              3   >   K   | ]}t          j        |          V  d S rA   )r   
program_id).0axiss     r$   	<genexpr>zsample_block.<locals>.<genexpr>   s>       ? ?&*jD!!? ? ? ? ? ?r&   )r   r   tupleranger   blocked_fold_insample_block)	rj   rk   rl   rm   rn   ro   r^   num_axeskeyss	            r$   ry   ry      s    H 	__I&&
 FC
OO F FYF F F G G G :H ? ?.3Hoo? ? ? ? ?K 
	(*j)[
B 
B$		%$
I
9 
917
9 
9 9r&   rA   )0collections.abcr   r;   r   r   r   r   r   jax._srcr   r   !jax._src.pallas.mosaic.primitivesr   r
   jax._src.pallasr   r   jax_prngShapeSampleFnSampleFnTypeArrayKeylessSampleFnTypeset_seedr:   PRNGKeyArrayr%   r,   r0   intr6   r?   rE   PRNGImplr   register_prngrP   rR   rT   rV   r\   ri   bitsuniform	bernoullirv   	ArrayLikery   rN   r&   r$   <module>r      s   % $ $ $ $ $ 



           ( ( ( ( ( ( $ $ $ $ $ $       7 7 7 7 7 7 > > > > > > & & & & & & % % % % % % 	'sCI~. Jx, J1F J J J J/SY / / / /!fl !s !5 ! ! ! !<(' <v| < < < <& U     !x  	
 
 
  | $ $ $( ( ( ( (
!&, !3 !u ! ! ! !
fl &,    FL     
 /X.	'#     1 2 2 2L 5H    . n122
 
 !7
8
8"">#;<<	 EI09 09\ 09%209"09 "09 #	09
 $F$4c$9:TA09 "i09 09 09 09 09 09r&   