
    Vpf6                        d dl mZ d dlmZmZ d dlZd dlmZ d dlm	Z	m
Z
 d dlmZ eZej        Z G d de          Zd	ee         d
ededee         def
dZdej        d
edededee
         defdZdededededej	        f
dZdS )    )Sequence)AnyProtocolN)random)Array	ArrayLike)numpyc                   ,    e Zd Zdej        dedefdZdS )SampleFnkeyshapereturnc                    d S N )selfr   r   argskwargss        X/var/www/html/nettyfy-visnx/env/lib/python3.11/site-packages/jax/_src/blocked_sampler.py__call__zSampleFn.__call__   s    C    N)__name__
__module____qualname__r   KeyArrayLikeShaper   r   r   r   r   r   r      sA        &- U !     r   r   iteration_index
total_size
block_sizeblock_indexr   c                     t          |           }d}d}t          |dz
  dd          D ]/}||         | |         ||         z  z   }|||z  z  }|||         z  }0|S )N   r   )lenrange)	r   r   r   r    ndimsdim_size	total_idxidim_idxs	            r   _compute_scalar_indexr+      s{     o

%()q"b!!  a!nq1JqMAAG8##I
1HH	r   
global_key	tile_sizec                      t          d t          ||          D                        fd dt                                S )aN  Computes a grid of keys for block-invariant sampling.

  Suppose we wished to construct a 16x512 array of random numbers, using
  block sizes of 16x128 and 16x256. We could select an tile size of 8x128
  (which divides both 16x128 and 16x256) and divide the total array in tiles as:
  ---------------------------------
  | 8x128 | 8x128 | 8x128 | 8x128 |
  ---------------------------------
  | 8x128 | 8x128 | 8x128 | 8x128 |
  ---------------------------------

  We generate a key for each tile as:
    tile_key = fold_in(global_key, tile_idx)

  Where the tile_idx is the row-major raveled index of each element:
  -----------------
  | 0 | 1 | 2 | 3 |
  -----------------
  | 4 | 5 | 6 | 7 |
  -----------------

  We then compute and return the keys required to sample the tiles that make
  up the current block (specified via `block_index`).
  With a 16x256 block size, each block requires 4 (2x2) tile keys:
  ---------------
  | 0, 1 | 2, 3 |
  | 4, 5 | 6, 7 |
  ---------------
  Therefore, we return a grid of 2x2 keys for each block (2 blocks total).

  With a 16x128 block size, each block requires 2 (2x1) tile keys:
  -----------------
  | 0 | 1 | 2 | 3 |
  | 4 | 5 | 6 | 7 |
  -----------------
  Therefore, we return a grid of 2x1 keys for each block (4 blocks total).

  Args:
    global_key: The global key shared between all blocks.
    total_size: The shape of the array being generated.
    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.
    block_index: The index denoting which block to generate keys for.

  Returns:
    An N-dimensional nested list of keys required to sample the tiles
    corresponding to the block specified by `block_index`.
  c              3   &   K   | ]\  }}||z  V  d S r   r   .0_shape_elements      r   	<genexpr>z"blocked_fold_in.<locals>.<genexpr>f   sD       M M-VXfM M M M M Mr   c           	         | t                    k    r2t          j                            t	          	|                    }|S g }t          |                    D ](}|                     | dz   ||fz                        )|S )Nr"   )r$   jaxr   fold_inr+   r%   append)
axisprefixsubtile_keykeysr)   _keygen_loopr    r,   size_in_blocksr   s
        r   r=   z%blocked_fold_in.<locals>._keygen_loopi   s    s>""""J&&
+:~v? ?@ @k d^D)** 7 7!LLa556666kr   r   )tuplezip)r,   r   r   r-   r    r=   r>   s   ``  `@@r   blocked_fold_inrA   -   s    r  M M14Z1K1KM M M M M.
 
 
 
 
 
 
 
 
 
a	!	!!r   
sampler_fnr<   c           	      X    t          d t          |          D                       dt          j        dt          t
                   dt          j        ffddt
          dt           t
          df         dt          j        f fd	 d
t                                S )aQ  Draws random samples for a single block.

  This function is intended to be used in conjunction with `blocked_fold_in`:
  ```
  key_list = blocked_fold_in(global_key, total_size, block_size, tile_size,
                             block_index)
  samples = sample_block(jax.random.uniform, key_list, block_size, tile_size)
  ```

  Args:
    sampler_fn: A random sampling function, e.g. jax.random.uniform.
    keys: A grid of keys generated by `blocked_fold_in`.
    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.
    args: varargs for sampler_fn.
    kwargs: kwargs for sampler_fn.

  Returns:
    An array of random samples drawn using sampler_fn.
  c              3   &   K   | ]\  }}||z  V  d S r   r   r0   s      r   r4   zsample_block.<locals>.<genexpr>   sD       M M-VXfM M M M M Mr   arridxr   c                     t          |          dk    r| |d                  S  | |d                  |dd                    S )Nr"   r   )r$   )rE   rF   _nested_indexs     r   rH   z#sample_block.<locals>._nested_index   sB    
3xx1}}Q[=SVc!""g...r   r9   r:   .c                    | t          
          k    r 	 |          gR diS g }t          
|                    D ](}|                     | dz   ||fz                        )t          j        ||           S )Nr   r"   )r9   )r$   r%   r8   jnpconcatenate)r9   r:   samplesr)   rH   _sample_loopr   r<   r   rB   size_in_tilesr-   s       r   rM   z"sample_block.<locals>._sample_loop   s    s=!!!!ZdF33 3d 3 3 3'3+13 3 3 g]4()) : :!||DFFA4K889999_W40000r   r   )r?   r@   r6   r   r   int)	rB   r<   r   r-   r   r   rH   rM   rN   s	   `` ```@@@r   sample_blockrP   w   s    <  M M14Z1K1KM M M M M-/ /# /39 / / / / / /
1 1eCHo 1#) 1 1 1 1 1 1 1 1 1 1 1 1 1 
a	!	!!r   )collections.abcr   typingr   r   r6   jax._srcr   jax._src.typingr   r   r	   rJ   	NdKeyListr   r   rO   r+   r   rA   rP   r   r   r   <module>rV      s   % $ $ $ $ $                 



       , , , , , , , ,      	    x   8C= &+&+ (0} :=   G"!G"G" G" 	G"
 	"G" G" G" G" G"T."."
." ." 	." y." ." ." ." ." ."r   