Ë
    èÿæi’$  ã                   ó8   — d Z ddlmZ dZdZd„ Z G d„ de«      Zy)	zC
A library written in CUDA Python for generating reduction kernels
é    )Ú
from_dtypeé    é   c                 óX  ‡‡‡‡‡‡‡‡	— ddl mŠ  ‰j                  d¬«      | «      Š	t        dz   Št        t        z  Š‰j                  d¬«      ˆˆ	fd„«       Š‰j                  d¬«      ˆˆˆ	fd„«       Š‰j                  d¬«      ˆˆˆ	fd„«       Šˆˆˆˆˆˆˆ	fd	„}‰j                  |«      S )
Nr   ©ÚcudaT)Údeviceé   c                 ó  •— ‰j                   j                  }|t        z  }|t        z  }| |dd…f   }|||<   ‰j                  «        t        dz  }|r5||k  r||   } ‰	||||z      «      ||<   ‰j                  «        |dz  }|rŒ4yy)z8
        Compute reduction within a single warp
        Né   )Ú	threadIdxÚxÚ	_WARPSIZEÚsyncwarp)
Úsm_partialsÚinitÚtidÚwarpidÚlaneidÚsm_thisÚwidthÚoldr   Ú	reduce_ops
           €€úq/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/numba/cuda/kernels/reduction.pyÚinner_warp_reductionz1_gpu_reduce_factory.<locals>.inner_warp_reduction   s“   ø€ ð
 �n‰n×ÑˆØœ	Ñ!ˆØ”y‘ˆà˜f¢a˜iÑ(ˆØˆ�‰Ø�‰Œä˜Q‘ˆÙØ˜Š~Ø˜f‘o�Ù"+¨C°¸À%¹Ñ1HÓ"I�˜‘Ø�M‰MŒOØ�a‰KˆEô ó    c                 ó  •— ‰j                   j                  }‰j                  j                  }‰j                  j                  }‰j                  j                  }|||z  z   }| j
                  }||z  }	| |   }
t        ||	z   ||	«      D ]  } ‰|
| |   «      }
Œ ‰j                  «         ‰||
«       ‰j                  «        |dk  r+ ‰||df   ||dz   df   «      ||df<   ‰j                  «        |dk(  r ‰|d   |d   «      ||<   yy)aœ  
        Partially reduce `arr` into `partials` using `sm_partials` as working
        space.  The algorithm goes like:

            array chunks of 128:  |   0 | 128 | 256 | 384 | 512 |
                        block-0:  |   x |     |     |   x |     |
                        block-1:  |     |   x |     |     |   x |
                        block-2:  |     |     |   x |     |     |

        The array is divided into chunks of 128 (size of a threadblock).
        The threadblocks consumes the chunks in roundrobin scheduling.
        First, a threadblock loads a chunk into temp memory.  Then, all
        subsequent chunks are combined into the temp memory.

        Once all chunks are processed.  Inner-block reduction is performed
        on the temp memory.  So that, there will just be one scalar result
        per block.  The result from each block is stored to `partials` at
        the dedicated slot.
        r   r   ©r   r   )r
   r   N)	r   r   ÚblockIdxÚblockDimÚgridDimÚsizeÚrangeÚsyncthreadsr   )ÚarrÚpartialsr   r   ÚblkidÚblkszÚgridszÚstartÚstopÚstepÚtmpÚir   r   r   s               €€€r   Údevice_reduce_full_blockz5_gpu_reduce_factory.<locals>.device_reduce_full_block(   s  ø€ ð* �n‰n×ÑˆØ—‘—‘ˆØ—‘—‘ˆØ—‘—‘ˆð �e˜e‘mÑ#ˆØ�x‰xˆØ�v‰~ˆð �%‰jˆä�u˜t‘| T¨4Ö0ˆAÙ˜C  Q¡Ó(‰Cð 1ð 	×ÑÔá˜[¨#Ô.à×ÑÔð �Š7Ù"+¨K¸¸Q¸Ñ,?Ø,7¸¸a¹À¸
Ñ,Có#EˆK˜˜Q˜Ñà�M‰MŒOØ�!Š8Ù'¨°DÑ(9¸;ÀtÑ;LÓMˆH�UŠOð r   c                 óz  •— ‰j                   j                  }‰j                  j                  }‰j                  j                  }|t        z  }|t        z  }| j
                  }‰j                   j                  }| |   }	|	|||f<   ‰j                  «        |dz   t        z  |k  r
 ‰||	«       n=|dk(  r8||dd…f   }
|t        z  }t        d||z
  «      D ]  } ‰|
d   |
|   «      |
d<   Œ ‰j                  «        |dk(  r=|t        z   dz
  t        z  }|d   }t        d|«      D ]  } ‰|||df   «      }Œ |||<   yy)zÂ
        This computes reduction on `arr`.
        This device function must be used by 1 threadblock only.
        The blocksize must match `arr.size` and must not be greater than 128.
        r
   r   Nr   )r   r   r   r    r   r"   r$   r#   )r%   r&   r   r   r'   r(   r   r   r"   Úvaluer   Úbaser.   Únum_active_warpsÚresultr   r   r   s                  €€€r   Údevice_reduce_partial_blockz8_gpu_reduce_factory.<locals>.device_reduce_partial_block_   sP  ø€ ð �n‰n×ÑˆØ—‘—‘ˆØ—‘—‘ˆØœ	Ñ!ˆØ”y‘ˆà�x‰xˆà�n‰n×ÑˆØ�C‘ˆØ&+ˆ�F˜F�NÑ#à×ÑÔà�Q‰Jœ)Ñ# dÒ*á  ¨eÕ4ð ˜Š{Ø% fªa iÑ0�Ø¤	Ñ)�Ü˜q $¨¡+Ö.�AÙ!*¨7°1©:°w¸q±zÓ!B�G˜A’Jð /ð 	×ÑÔà�!Š8Ø %¬	Ñ 1°AÑ 5¼)ÑCÐà  Ñ&ˆFÜ˜1Ð.Ö/�Ù" 6¨;°q¸!°tÑ+<Ó=‘ð 0ð %ˆH�UŠOð r   c                 ó8  •— ‰j                   j                  }‰j                  j                  t        ‰	f‰¬«      }‰j
                  j                  ‰
k(  r ‰| ||«       n
 ‰| ||«       |r0|dk(  r*‰j                  j                  dk(  r ‰|d   |«      |d<   yyyy)aJ  
        Perform reductions on *arr* and writing out partial reduction result
        into *partials*.  The length of *partials* is determined by the
        number of threadblocks. The initial value is set with *init*.

        Launch config:

        Blocksize must be multiple of warpsize and it is limited to 4 warps.
        )Údtyper   N)r   r   ÚsharedÚarrayÚ	_NUMWARPSr    r   )r%   r&   r   Úuse_initr   r   r   r/   r5   Úinner_sm_sizeÚmax_blocksizeÚnbtyper   s         €€€€€€€r   Úgpu_reduce_block_stridedz5_gpu_reduce_factory.<locals>.gpu_reduce_block_strided‹   s”   ø€ ð �n‰n×Ñˆà—k‘k×'Ñ'¬°MÐ(BØ.4ð (ó 6ˆà�=‰=�?‰?˜mÒ+Ù$ S¨(°KÕ@á'¨¨X°{ÔCá˜˜qš T§]¡]§_¡_¸Ò%9Ù# H¨Q¡K°Ó6ˆH�QŠKð &:˜ˆ8r   )Únumbar   Újitr   r:   )
Úfnr>   r?   r   r/   r5   r<   r   r=   r   s
    ` @@@@@@@r   Ú_gpu_reduce_factoryrC      s«   ÿ€ Ýà%�—‘ �Ó% bÓ)€IÜ ‘M€MÜ¤	Ñ)€Mà	‡X�X�T€XÓôó ðð( 
‡X�X�T€XÓõ4Nó ð4Nðl 
‡X�X�T€XÓõ)%ó ð)%÷V7ò 7ð, �8‰8Ð,Ó-Ð-r   c                   ó(   — e Zd ZdZi Zd„ Zd„ Zdd„Zy)ÚReducezÔCreate a reduction object that reduces values using a given binary
    function. The binary function is compiled once and cached inside this
    object. Keeping this object alive will prevent re-compilation.
    c                 ó   — || _         y)zÚ
        :param functor: A function implementing a binary operation for
                        reduction. It will be compiled as a CUDA device
                        function using ``cuda.jit(device=True)``.
        N)Ú_functor)ÚselfÚfunctors     r   Ú__init__zReduce.__init__¬   s   € ð  ˆ�r   c                 ó¼   — | j                   |f}|| j                  v r| j                  |   }|S t        | j                   t        |«      «      }|| j                  |<   |S )N)rG   Ú_cacherC   r   )rH   r7   ÚkeyÚkernels       r   Ú_compilezReduce._compile´   s[   € Ø�m‰m˜UÐ"ˆØ�$—+‘+ÑØ—[‘[ Ñ%ˆFð ˆô )¨¯©¼
À5Ó8IÓJˆFØ%ˆD�K‰K˜ÑØˆr   Nc                 óš  — ddl m} |j                  dk7  rt        d«      ‚|�|d| }|j                  j                  |«      }|j                  dk  r|S | j                  |j                  «      }t        t        z  }|j                  |z  |z  }	|j                  |	z
  }
t        |	|z  t        dz  «      }|}|
r|dz  }|j                  ||j                  ¬«      }|	r ||||f   |d|	 |d| |d«       |
r |d|
|f   ||	d ||d || «       |j                  dkD  r |d||f   |||d	«       |�|dd j                  |dd |¬
«       y|d   S )a'  Performs a full reduction.

        :param arr: A host or device array.
        :param size: Optional integer specifying the number of elements in
                    ``arr`` to reduce. If this parameter is not specified, the
                    entire array is reduced.
        :param res: Optional device array into which to write the reduction
                    result to. The result is written into the first element of
                    this array. If this parameter is specified, then no
                    communication of the reduction output takes place from the
                    device to the host.
        :param init: Optional initial value for the reduction, the type of which
                    must match ``arr.dtype``.
        :param stream: Optional CUDA stream in which to perform the reduction.
                    If no stream is specified, the default stream of 0 is
                    used.
        :return: If ``res`` is specified, ``None`` is returned. Otherwise, the
                result of the reduction is returned.
        r   r   r
   zonly support 1D arrayNr   )Úshaper7   TF)Ústream)r@   r   ÚndimÚ	TypeErrorr7   Útyper"   rO   r:   r   ÚminÚdevice_arrayÚcopy_to_device)rH   r%   r"   Úresr   rR   r   rN   Ú	blocksizeÚ	size_fullÚsize_partialÚfull_blockctÚpartials_sizer&   s                 r   Ú__call__zReduce.__call__½   sœ  € õ( 	ð �8‰8�qŠ=ÜÐ3Ó4Ð4ð ÐØ�e�t�*ˆCà�y‰y�~‰~˜dÓ#ˆð �8‰8�aŠ<ØˆKà—‘˜sŸy™yÓ)ˆô ¤	Ñ)ˆ	Ø—X‘X Ñ*¨iÑ7ˆ	Ø—x‘x )Ñ+ˆÜ˜9¨	Ñ1´9¸q±=ÓAˆð %ˆÙØ˜QÑˆMØ×$Ñ$¨=ÀÇ	Á	Ð$ÓJˆáà3ˆF�< ¨FÐ2Ñ3°C¸
¸°OØ4<¸]¸lÐ4KØ48Ø48ô:ñ
 à+ˆF�1�l FÐ*Ñ+¨C°	°
¨OØ,4°\°]Ð,CØ,0Ø0<Ð,<ô>ð
 �=‰=˜1Òà,ˆF�1�m VÐ+Ñ,¨X°xÀÀuÔMð ˆ?Ø��ˆG×"Ñ" 8¨B¨Q <¸Ð"Ô?Øà˜A‘;Ðr   )NNr   r   )Ú__name__Ú
__module__Ú__qualname__Ú__doc__rL   rJ   rO   r_   © r   r   rE   rE   ¤   s   „ ñð
 €Fò òôIr   rE   N)rc   Únumba.np.numpy_supportr   r   r:   rC   ÚobjectrE   rd   r   r   Ú<module>rg      s0   ðñõ .ð €	Ø€	òU.ôpbˆVõ br   