
    Ph              	       J    d Z ddlmZmZmZ dgZd	dee   dedeee      fdZy)
up   Implements "Block Partitions of Sequences" by Imre Bárány et al.

Paper: https://arxiv.org/pdf/1308.2452.pdf

    )IteratorListTuplesolvesequence
partitionsreturnc                    dk  rt        d d      t        |       }|k  rt        d| d d      t        |       }t        |       |z
  }|dk(  r| D cg c]  }d c}n| D cg c]
  }||z
  |z   c}t	        dz
        D cg c]  }|z  |dz   z   c}|gz   dt
        d	t        ffd
d	t        t        t        t
        f      ffd}	 	 t         |             \  }}		 	 t         |             \  }
}||
dz   k  r)t        dgdd z         D cg c]
  \  }}| ||  c}}S 	 |	|k  r	 |dz
  }|xx   dz  cc<   n	 |dz   }|xx   dz  cc<   	 |	|k(  rnzc c}w c c}w c c}w c c}}w )u   Splits a sequence into several partitions to minimize variance for each
    partition.

    The result might not be optimal. However, it can be done only in O(kn³),
    where k is the number of partitions and n is the length of the sequence.

       z'partitions must be a positive integer (z < 1)z.sequence is shorter than intended partitions (z < )r   ir	   c                 H    | dkD  r| dz
     nd}|    }t        ||       S )Nr   r   )sum)r   startstopnormal_sequencesplitss      C:\Users\daisl\Desktop\realtime-object-detection\venv\Lib\site-packages\torch/distributed/pipeline/sync/_balance/blockpartition.py
block_sizezsolve.<locals>.block_size,   s3    !"Qq1uAay?5.//    c                  ,     fdt              D        S )Nc              3   2   K   | ]  } |      |f  y w)N ).0r   r   s     r   	<genexpr>z-solve.<locals>.leaderboard.<locals>.<genexpr>2   s     >,=qA",=s   )range)r   r   s   r   leaderboardzsolve.<locals>.leaderboard1   s    >E*,=>>r   N)

ValueErrorlenminmaxr   intfloatr   r   zip)r   r   nminimummaximum_xr   max_sizepmin_sizeqr   jhr   r   r   s    `             @@@r   r   r      s    A~B:,eTUUHA:~I!CPZ|[\]^^ (mG(mg%G !|&./h1h/<DEHqAK72HE16zA~1FG1FAa:oQ'1FG1#MF0c 0e 0
?%s
"34 ? 	 +-(! km,KHa8a<'25qcF3BK6G2PQ2P$!Q12PQQ 1u Eq	Q	 Eq	Q	 AvI   0EG2 Rs   	E..E3E8%E=N)r   )__doc__typingr   r   r   __all__r#   r   r   r   r   <module>r4      sC   
 ) ()NDI N3 NtDI Nr   