
    &`i+                        d dl Z d dlZd dlmZmZmZmZ d dlZd dlm	Z	 d dl
mZ d dlmZmZmZmZmZ d dlmZ  ej        e          Zdedee         fd	Zd
ee         dee         dee         fdZd
ee         dee         deee                  fdZdedededee         deeeeee         f         ef         df         f
dZdee         dedee         fdZdeeee         ef                  deee                  dedeeee         ef                  fdZdeeee         ef                  dee         deeeee                           eee                  f         fdZ	 	 d!deeee         ef                  dee         dedee         deeeee                           eee                  f         f
dZ dedefd Z!dS )"    N)IterableListTupleUnion)trace_deallocation)cached_remote_fn)BlockBlockAccessorBlockExecStatsBlockMetadataBlockPartition)	ObjectRefblocks_with_metadatareturnc                     t          t                    }g }| D ]W\  }}|j        /t          j        |                    |                    }||_        n|j        }|                    |           X|S )z@Calculate the number of rows for a list of blocks with metadata.)r   _get_num_rowsnum_rowsraygetremoteappend)r   get_num_rows
block_rowsblockmetadatar   s         l/home/jaya/work/projects/VOICE-AGENT/VIET/agent-env/lib/python3.11/site-packages/ray/data/_internal/split.py_calculate_blocks_rowsr      s     $M22LJ/ $ $x$w|22599::H (H(H(####    num_rows_per_blocksplit_indicesc                 >    t          |           fd|D             S )zTGenerate valid split indices by apply min(index, total_num_rows)
    to every index.c                 0    g | ]}t          |          S  )min).0index
total_rowss     r   
<listcomp>z+_generate_valid_indices.<locals>.<listcomp>,   s#    >>>uCz"">>>r   )sum)r   r    r'   s     @r   _generate_valid_indicesr*   %   s-     '((J>>>>>>>>r   c                    g }d}g }d}d}|t          |          k     rq||         }| |         }||z
  |k    r|                    ||z
             |dz  }J|                    |           g }|| |         z  }|dz  }|t          |          k     qt          |          t          |           k     r7|                    |           g }t          |          t          |           k     7|S )a:  Given num rows per block and valid split indices, generate per block split indices.

    Args:
        num_rows_per_block: num of rows per block.
        split_indices: The (global) indices at which to split the blocks.
    Returns:
        Per block split indices indicates each input block's split point(s).
    r      )lenr   )	r   r    per_block_split_indicescurrent_input_block_idcurrent_block_split_indicescurrent_block_global_offsetcurrent_index_idsplit_indexcurrent_block_rows	            r   !_generate_per_block_split_indicesr5   /   s=     !"$"#
S//
/
/#$45./EF448III'..99   !&&'BCCC&(##'9:P'QQ#!# S//
/
/  %
&
&-?)@)@
@
@&&'BCCC&(# %
&
&-?)@)@
@
@ #"r   block_idr   meta.c                 t   g }g }t          j        |          }d}|                    |j                   |D ]}t                              d| d|            t          j                    }	|                    ||          }
t          j        |
          }t          |                                |
                                |j        |	                                          }|                    |           |                    |
           |}| |fg}|                    |           t          |          S )aP  Split the provided block at the given indices.

    Args:
        block_id: the id of this block in the block list.
        block: block to be split.
        meta: metadata of the block, we expect meta.num is valid.
        split_indices: the indices where the block should be split.
    Returns:
        returns block_id, split blocks metadata, and a list of blocks
        in the following form. We return blocks in this way
        so that the owner of blocks could be the caller(driver)
        instead of worker itself.
        Tuple(block_id, split_blocks_meta), block0, block1 ...
    r   zslicing block :)r   
size_bytesinput_files
exec_stats)r
   	for_blockr   r   loggerdebugr   builderslicer   r:   r;   buildextendtuple)r6   r   r7   r    
split_metasplit_blocksblock_accessor
prev_indexr&   statssplit_blockaccessor_metaresultss                 r   _split_single_blockrN   [   s?   ( JL",U33NJ '''  :j::5::;;;&(($**:u== *;77&&((**,,({{}}	
 
 
 	%   K(((

*%&GNN<   >>r   block_split_indicesr   c                 n    d}g }| D ]-}|dk    s||k    r||k    r|                     |           |}.|S )zdrop split indices that creates empty block split. This could happen when there
    are duplicated indices, or index equal to 0 (start of the block) or num_block_rows
    (end of the block).
    r   )r   )rO   r   rH   optimized_indicesr&   s        r   _drop_empty_block_splitrS      sc    
 J$  A::(**J  '''

r   r.   owned_by_consumerc                 z   t          t                    }dgt          |           z  }g }g }g }t          |          D ]\  }}	| |         \  }
}|j        }t          |	|          }	t          |	          dk    r	|
|fg||<   C|                    ddt          |	          z                                 ||
||	          }|                    |d                    |                    |dd                    |                    |
           |rbt          j
        |          }t          ||          D ]=\  \  }}}t          |          t          |          k    sJ t          ||          ||<   >|r|D ]}t          |d           n|D ]}t          |dd	           t          j                            |          S )
z5Split all the input blocks based on the split indicesNr   SPREAD   )scheduling_strategynum_returnsr,   zsplit._split_all_blocksF)free)r   rN   r-   	enumerater   rS   optionsr   r   r   r   zipr   	itertoolschainfrom_iterable)r   r.   rT   split_single_blockall_blocks_split_results per_block_split_metadata_futuresper_block_split_block_refsblocks_splittedr6   rO   	block_refr7   	block_rowobject_refsper_block_split_metadata
block_refsbs                    r   _split_all_blocksrl      s9    **=>>6:VcBV>W>W5W')$!# O)23J)K)K . .%%0:DM	56I9UU"##q(( 4=d2C1D$X.. -44$,!cBU>V>V:V 5  f#	   -33KNCCC&--k!""o>>>""9----' G#&7+K#L#L ,/$&@-
 -
 	G 	G(Xtj t99J////14Z1F1F$X..
  I  	= 	=Aq";<<<<	= ! 	I 	IAq";%HHHHH?(()ABBBr   rb   global_split_sizesc                    g }g }g }g }d}d}|t          |          k     r|||         k    rD|||         k    sJ |                    |           |                    |           g }g }d}|dz  }nFt          |           \  }}	|                    |           |                    |	           ||	j        z  }|t          |          k     ||fS )z<Reassemble per block's split result into final split result.r   r,   )r-   r   nextr   )
rb   rm   result_blocksresult_metascurrent_blockscurrent_metacurrent_split_sizecurrent_split_idrf   r7   s
             r   _generate_global_split_resultsrv      s   
 MLNL
S!344
4
4!34D!EEE%);<L)MMMMM  000---NL!"! $%= > >Y!!),,,%%%$-/ S!344
4
4  ,&&r   Tindicesr   c                    t          |           } t          |           dk    r,g gt          |          dz   z  g gt          |          dz   z  fS |t          |           }t          ||          }t	          ||          }t          | ||          }dg|z   t          |          gz   fdt          dt                              D             }t          ||          S )a!  Split blocks at the provided indices.

    Args:
        blocks_with_metadata: Block futures to split, including the associated metadata.
        indices: The (global) indices at which to split the blocks.
        owned_by_consumer: Whether the provided blocks are owned by the consumer.
        block_rows: The number of rows for each block, in case it has already been
            computed.

    Returns:
        The block split futures and their metadata. If an index split is empty, the
        corresponding block split will be empty .
    r   r,   Nc                 8    g | ]}|         |d z
           z
  S )r,   r#   )r%   ihelpers     r   r(   z%_split_at_indices.<locals>.<listcomp>!  s*    LLL6!9va!e},LLLr   )	listr-   r   r*   r5   rl   r)   rangerv   )	r   rw   rT   r   valid_indicesr.   rb   split_sizesr{   s	           @r   _split_at_indicesr      s   ,   455
  A%%Gq()B43w<<!3C+DEE+,@AA
+J@@M/PM0 0 	57H	 	  S= C
OO#44FLLLLeAs6{{6K6KLLLK)*BKPPPr   c                 N    t          j        |                                           S )z7Get the number of rows contained in the provided block.)r
   r=   r   )r   s    r   r   r   &  s    "5))22444r   )TN)"r^   loggingtypingr   r   r   r   r   !ray.data._internal.memory_tracingr   ray.data._internal.remote_fnr   ray.data.blockr	   r
   r   r   r   	ray.typesr   	getLogger__name__r>   intr   r*   r5   rN   rS   boolrl   rv   r   r   r#   r   r   <module>r      s        / / / / / / / / / / / / 



 @ @ @ @ @ @ 9 9 9 9 9 9                    		8	$	$(	#Y   "?S	?9? 
#Y? ? ? ?)#S	)#9)# 
$s)_)# )# )# )#X+++ + 9	+
 5sD//0%78#=>+ + + +\c c dSVi    "9CuYu%5}%DEF9C!$s)_9C 9C eIe$m345	9C 9C 9C 9Cx'&uYu-=}-L'MN'S	' 4Yu%&'d=.A)BBC' ' ' 'F # 	-Q -QuYu%5}%DEF-Q#Y-Q -Q S		-Q
 4Yu%&'d=.A)BBC-Q -Q -Q -Q`5 53 5 5 5 5 5 5r   