Ë
    @çiÖ   ã            
       óT  — d Z ddlmZ ddlmZ ddlmZmZ ddlm	Z	 ddl
mZmZ ddlZddlmZ ddlZd	d
lmZ d	dlmZmZ  G d„ de«      Z	 ddedej4                  e   dedej8                  e   fd„Ze G d„ d«      «       Z G d„ d«      Z ejB                  e gdf   Z" G d„ d«      Z#y)a  
Classes used to define a grid search.

`Launcher`: a launcher is passed to each grid search explore function,
and can be called repeatidly to schedule XPs.

`Explorer`: defines some metadata, in particular the metrics to display
with the `dora grid` command.
é    )ÚOrderedDict)Údeepcopy)ÚProcessPoolExecutorÚFuture)Úcontextmanager)Ú	dataclassÚfieldN)Ú_Nodeé   )ÚSlurmConfig)ÚShepherdÚSheepc                   ó   — e Zd Zy)ÚProcessExceptionN)Ú__name__Ú
__module__Ú__qualname__© ó    úa/Volumes/fast/ai/experiments/voice-extract-mac/.venv/lib/python3.12/site-packages/dora/explore.pyr   r      s   „ Ør   r   ÚshepherdÚargvÚslurmÚjob_array_indexc                 óv   — 	 | j                  |«      ||fS # t        $ r}t        t        |«      «      ‚d }~ww xY w©N)Úget_sheep_from_argvÚ	Exceptionr   Úrepr)r   r   r   r   Úexcs        r   Ú_processr!   "   s>   € ð*Ø×,Ñ,¨TÓ2°E¸?ÐKÐKøÜò *Üœt C›yÓ)Ð)ûð*ús   ‚ –	8Ÿ3³8c            
       ó¼  — e Zd ZU dZ ee¬«      Zej                  e	e
f   ed<    ee¬«      Zej                  e	ef   ed<    ee¬«      Zej"                  ej"                  e	      ed<    ee¬«      Zej"                  e   ed<   dZej*                  d   ed	<   d
„ Z	 ddedej"                  e	   dedej*                  e   fd„Z	 dde
dedej*                  e   fd„Zy)ÚHerdz7Represents a herd of sheeps ready to be scheduled.
    )Údefault_factoryÚsheepsÚslurm_configsÚ
job_arraysÚ	_pendingsNÚLauncherÚ_job_array_launcherc                 ó¼   — | j                   rP| j                   j                  d«      }|j                  «       \  }}}| j                  |||«       | j                   rŒOyy)z@Complete all pending sheep evaluations and add them to the herd.r   N)r(   ÚpopÚresultÚ
_add_sheep)ÚselfÚfutureÚsheepr   r   s        r   ÚcompletezHerd.complete7   sH   € à�nŠnØ—^‘^×'Ñ'¨Ó*ˆFØ,2¯M©M«OÑ)ˆE�5˜/Ø�O‰O˜E 5¨/Ô:ð �n�nr   r   r   r   Úpoolc           	      ó0  — | j                   €| j                  j                  g «       t        | j                  «      dz
  }|€#| j	                  |j                  |«      ||«       y | j                  j                  |j                  t        ||||«      «       y )Nr   )	r*   r'   ÚappendÚlenr.   r   r(   Úsubmitr!   )r/   r   r   r   r3   r   s         r   Ú	add_sheepzHerd.add_sheep>   sv   € à×#Ñ#Ð+Ø�O‰O×"Ñ" 2Ô&Ü˜dŸo™oÓ.°Ñ2ˆØˆ<Ø�O‰O˜H×8Ñ8¸Ó>ÀÀÕWà�N‰N×!Ñ! $§+¡+¬h¸À$ÈÈÓ"_Õ`r   r1   r   c                 ó@  — |j                   j                  | j                  v ry || j                  |j                   j                  <   || j                  |j                   j                  <   |�3| j                  |   j                  |j                   j                  «       y y r   )ÚxpÚsigr%   r&   r'   r5   )r/   r1   r   r   s       r   r.   zHerd._add_sheepH   sq   € à�8‰8�<‰<˜4Ÿ;™;Ñ&ØØ$)ˆ�‰�E—H‘H—L‘LÑ!Ø+0ˆ×Ñ˜5Ÿ8™8Ÿ<™<Ñ(ØÐ&Ø�O‰O˜OÑ,×3Ñ3°E·H±H·L±LÕAð 'r   r   )r   r   r   Ú__doc__r	   r   r%   ÚtpÚDictÚstrr   Ú__annotations__Údictr&   r   Úlistr'   ÚListr(   r   r*   ÚOptionalr2   r   r   r8   Úintr.   r   r   r   r#   r#   *   sþ   … ñá"'¸Ô"D€FˆB�G‰G�C˜�JÑÓDÙ/4ÀTÔ/J€M�2—7‘7˜3 Ð+Ñ,ÓJÙ(-¸dÔ(C€J�—‘˜Ÿ™ ™Ñ%ÓCñ "'°tÔ!<€Iˆr�w‰w�v‰Ó<à37Ð˜Ÿ™ ZÑ0Ó7ò;ð <@ña (ð a°"·'±'¸#±,ð aÀ{ð aØŸ™Ð$7Ñ8óað 8<ñB ð B¨kð BØ$&§K¡K°Ñ$4ôBr   r#   c                   óš   — e Zd ZdZg dfdedededej                  e	   dej                  e   f
d„Zd	„ Zd
„ Zd„ Zd„ Zd„ Zd„ Zed„ «       Zy)r)   a9  
    A launcher is passed to the explore function and can be called repeatidly
    to schedule experiments.

    For instance:

        launcher(epochs=40)
        launcher(bs=64)

    A call to `launcher()` will schedule a new experiments, and all arguments
    have the same effect as in `Launcher.bind()`.
    Nr   r   Úherdr   r3   c                 ó¤   — || _         | j                   j                  | _        || _        t	        |«      | _        t        |«      | _        || _        y r   )	Ú	_shepherdÚmainÚ_mainÚ_herdr   Ú_slurmrB   Ú_argvÚ_pool)r/   r   r   rG   r   r3   s         r   Ú__init__zLauncher.__init__`   s>   € à!ˆŒØ—^‘^×(Ñ(ˆŒ
ØˆŒ
Ü˜u“oˆŒÜ˜$“ZˆŒ
Øˆ�
r   c                 ó„   — t        | j                  | j                  | j                  | j                  | j
                  «      S r   )r)   rI   rM   rL   rN   rO   ©r/   s    r   Ú_copyzLauncher._copyi   s)   € Ü˜Ÿ™¨¯©°T·Z±ZÀÇÁÈTÏZÉZÓXÐXr   c                 óF   — | j                  «       } |j                  |i |¤ŽS )a’  
        Returns a new `Launcher` with different default XP parameters when scheduling experiments.

        Each entry in `*args` can be itself a list of dict or strings,
        or a string or a dict.

        Any string arg is considered directly as something to append to the list
        of *argv*, i.e. the command line arguments passed to the training scripts.

        A dictionary will be converted to a list of `argv`, with the specific syntax
        defined by the `main` function. For an argparse based script, a key
        value pair will be converted to `--key=value`, with some special rules
        (if the value is True, then it is converted to just `--key`).

        A list containing strings or dicts will be the concatenation
        of the argv obtained from each of its entries.

        For instance

            sub_launcher = launcher.bind(["--some_flag=5"], other_flag="test")
        )rS   Úbind_)r/   ÚargsÚkwargsÚnews       r   ÚbindzLauncher.bindl   s%   € ð, �j‰j‹lˆØˆs�y‰y˜$Ð) &Ñ)Ð)r   c                 óÌ   — |D ]0  }| xj                   | j                  j                  |«      z  c_         Œ2 | xj                   | j                  j                  |«      z  c_         | S )z8
        In-place version of `Launcher.bind()`.
        )rN   rK   Úvalue_to_argv)r/   rV   rW   Úargs       r   rU   zLauncher.bind_…   sM   € ó ˆCØ�JŠJ˜$Ÿ*™*×2Ñ2°3Ó7Ñ7ŽJð à�
Š
�d—j‘j×.Ñ.¨vÓ6Ñ6�
Øˆr   c                 óF   — | j                  «       } |j                  di |¤ŽS )z¦
        Return a new `Launcher` with different default Slurm parameters.

        For instance

            sub_launcher = launcher.slurm(cpus_per_task=20)

        r   )rS   Úslurm_)r/   rW   rX   s      r   r   zLauncher.slurmŽ   s#   € ð �j‰j‹lˆØˆs�z‰zÑ#˜FÑ#Ð#r   c                 ó¬   — |j                  «       D ]@  \  }}t        | j                  |«      st        d|› �«      ‚t	        | j                  ||«       ŒB | S )z9
        In-place version of `Launcher.slurm()`.
        zInvalid Slurm config )ÚitemsÚhasattrrM   ÚAttributeErrorÚsetattr)r/   rW   ÚkeyÚvalues       r   r^   zLauncher.slurm_›   sP   € ð !Ÿ,™,ž.‰JˆC�Ü˜4Ÿ;™;¨Ô,Ü$Ð'<¸S¸EÐ%BÓCÐCÜ�D—K‘K  eÕ,ð )ð ˆr   c                 ó$  —  | j                   |i |¤Ž}| j                  j                  }|� |j                  |j                  k(  sJ d«       ‚| j                  j	                  | j
                  |j                  |j                  | j                  «       y)z¤
        Schedule an XP with the current default training hyper-parameters
        and Slurm config. You can also provide extra overrides like in `bind()`.
        Nz,cannot change slurm config inside job array.)rY   rL   r*   rM   r8   rI   rN   rO   )r/   rV   rW   ÚlauncherÚarray_launchers        r   Ú__call__zLauncher.__call__¥   sx   € ð
 �4—9‘9˜dÐ- fÑ-ˆØŸ™×7Ñ7ˆØÐ%Ø!×(Ñ(¨H¯O©OÒ;ð ?Ø>ó?Ð;à�
‰
×Ñ˜TŸ^™^¨X¯^©^¸X¿_¹_ÈdÏjÉjÕYr   c              #   ó&  K  — | j                   j                  �J d«       ‚| j                  «       | j                   _        | j                   j                  j	                  g «       	 d–— d| j                   _        y# d| j                   _        w xY w­w)z•Context manager to indicate that you wish to launch all the included
        XPs using a single job array with the current Slurm parameters.
        NzCannot stack job arrays)rL   r*   rS   r'   r5   rR   s    r   Ú	job_arrayzLauncher.job_array±   sn   è ø€ ð
 �z‰z×-Ñ-Ð5ÐPÐ7PÓPÐ5Ø)-¯©«ˆ�
‰
Ô&Ø�
‰
×Ñ×$Ñ$ RÔ(ð	2Ûà-1ˆD�J‰JÕ*ø¨TˆD�J‰JÕ*üs   ‚A"BÁ%A; Á)BÁ;BÂB)r   r   r   r<   r   r   r#   r=   rC   r?   rD   r   rP   rS   rY   rU   r   r^   ri   r   rk   r   r   r   r)   r)   R   s   „ ñð ')ÐSWñ ð °+ð ÀTð Ø—w‘w˜s‘|ðØ02·±Ð<OÑ0PóòYò*ò2ò$òò
Zð ñ
2ó ñ
2r   r)   c                   óÎ   — e Zd Zdefd„Zdefd„Zdej                  e	   fd„Z
dej                  e	   fd„Zd„ Zd	ed
ej                  e   defd„Zd
ej                  e   defd„Zy)ÚExplorerÚexplorec                 ó   — || _         y r   ©rn   )r/   rn   s     r   rP   zExplorer.__init__Ã   s	   € Øˆ�r   rg   c                 ó&   — | j                  |«       y r   rp   )r/   rg   s     r   ri   zExplorer.__call__Æ   s   € Ø�‰�XÕr   Úreturnc                 ó   — g S )zKReturn the metrics that should be displayed in the tracking table.
        r   rR   s    r   Úget_grid_metricszExplorer.get_grid_metricsÉ   s	   € ð ˆ	r   c                 óÚ   — t        j                  dd¬«      t        j                  d«      t        j                  d«      t        j                  dd¬«      t        j                  dd¬«      gS )zIReturns the list of Meta information to display for each XP/job.
        ÚindexÚ>)ÚalignÚnameÚstater;   Úsid)ÚttÚleafrR   s    r   Úget_grid_metazExplorer.get_grid_metaÎ   sO   € ô �G‰G�G 3Ô'Ü�G‰G�F‹OÜ�G‰G�GÓÜ�G‰G�E Ô%Ü�G‰G�E Ô%ð
ð 	
r   c                 ó
   — ddgS )NÚ0z38;5;245r   rR   s    r   Ú
get_colorszExplorer.get_colorsÙ   s   € Ø�ZÐ Ð r   r1   Úhistoryc                 ó   — t        «       ‚)a   Process a sheep to return a dict (with possibly nested dict inside)
        matching the schema given by `get_grid_metrics`.
        This gives more possiblities than `process_history`, which is kept for compatibility,
        as one has access to the XP config here.
        If this is implemented, it will always be called, otherwise, `process_history` is used.

        One should use the history provided here, rather than the one in `sheep.xp.link.history`,
        as it has possibly been shortened to align multiple experiments.
        )ÚNotImplementedError)r/   r1   r‚   s      r   Úprocess_sheepzExplorer.process_sheepÜ   s   € ô "Ó#Ð#r   c                 óP   — dt        |«      i}|D ]  }|j                  |«       Œ |S )z…Process history to return a dict (with possibly nested dict inside)
        matching the schema given by `get_grid_metrics`.
        Úepoch)r6   Úupdate)r/   r‚   ÚoutÚmetricss       r   Úprocess_historyzExplorer.process_historyè   s1   € ð
 ”S˜“\ð
ˆó ˆGØ�J‰J�wÕð àˆ
r   N)r   r   r   ÚExplorerP   r)   ri   r=   rC   r
   rt   r~   r�   r   rA   r…   r‹   r   r   r   rm   rm   Â   s„   „ ð ó ð ó ð "§'¡'¨%¡.ó ð
	
˜rŸw™w u™~ó 	
ò!ð
$ 5ð 
$°2·7±7¸4±=ð 
$ÀTó 
$ð	 r§w¡w¨t¡}ð 	¸ô 	r   rm   r   )$r<   Úcollectionsr   Úcopyr   Úconcurrent.futuresr   r   Ú
contextlibr   Údataclassesr   r	   Útypingr=   Útreetable.tabler
   Ú	treetabler|   Úconfr   Úshepr   r   ÚRuntimeErrorr   rC   r?   rD   rE   r!   r#   r)   ÚCallablerŒ   rm   r   r   r   Ú<module>r™      s¹   ðñõ $Ý ß :Ý %ß (Û å !Û å ß !ô	�|ô 	ð
 26ñ*�xð * r§w¡w¨s¡|ð *¸Kð *Ø Ÿk™k¨#Ñ.ó*ð ÷$Bð $Bó ð$B÷Nj2ñ j2ðZ �+‰+�x�j $Ð&Ñ
'€÷/ò /r   