
    ]i&                        S r SSKrSSKJr  SSKrSSKrSSKJr  SSKr	SSK
r
SSKJrJr  SSKJr  SSKJrJr  \	R&                  rS	 rS
 r " S S\5      r " S S\5      rS/ SSSS.S\R2                  S\	R4                  \\4   S\	R8                  \   S\	R:                  \   S\	R:                  \	R4                  \\4      S\4S jjrg)a*  
DecoratedMain is the main abstraction used inside Dora.
This defines the core interface that needs to be fullfilled
for Dora to be able to handle an experiment.

This is also where the end user will specify how to launch job, default
Slurm configuration, storage location, naming conventions etc.
    N)OrderedDict)Path   )
DoraConfigSlurmConfig)
NamesMixin)XP_contextc                 l    U R                  SS5      u  p[        R                  " U5      n[        X25      $ )N.r   )rsplit	importlibimport_modulegetattr)	full_namemodule_namefun_namemodules       F/mnt/rpi/tmp/demucs-venv-sys/lib/python3.13/site-packages/dora/main.py
_load_mainr       s2    %,,S!4K$$[1F6$$    c                 l    U S:X  a-  [         R                  U    R                  nUc  g UR                  $ U $ )N__main__)sysmodules__spec__name)r   specs     r   get_module_namer   &   s5    {{6"++<99r   c                      \ rS rSrSrSrS\S\4S jrS r	S\
R                  \   S	\4S
 jrS rS rS\
R"                  \   S	\4S jrS	\
R                  \   4S jrS\4S jrS\S	\
R"                  \   4S jrS\S	\4S jrS rS\
R2                  S	\
R                  \   4S jrS\S	\
R                  \   4S jrS	\4S jrSrg)DecoratedMain1   z
Main function that will actually run the XP, wrapped with tons of meta-informations.

Args:
    main (callable, taking no argument): main function for the experiment as provided
        by the user.
    dora (DoraConfig): configuration for Dora.
Fmaindorac                 4   Xl         X l        [        UR                  5      nUc  SU l        SU l        O3SU;   a   UR                  SS5      u  U l        U l        OSU l        X0l        U R                  U l        UR                  S-   UR                  -   U l	        g )Nunknowntrainr   r   )
r#   r$   r   
__module__packagemain_moduler   r   __name__
_full_name)selfr#   r$   r   s       r   __init__DecoratedMain.__init__<   s    		%doo6 %DL&Dk!1<1C1CC1K.d.(#. LL	//C/$--?r   c                 4   U R                  5       nU R                  U5      (       d  U R                  5       $ U R                  U5      nU R	                  U5        [
        R                  " U5         U R                  5       sS S S 5        $ ! , (       d  f       g = fN)	_get_argv
_is_active_mainget_xpinit_xpr
   enter_xp)r-   argvxps      r   __call__DecoratedMain.__call__O   sf    ~~t$$::<[[Rr"::< #""s   /B		
Br8   returnc                     gNT r-   r8   s     r   r3   DecoratedMain._is_activeX   s    r   c                 "    U R                  5       $ r1   r#   r-   s    r   r4   DecoratedMain._main[   s    yy{r   c                 (    [         U R                  44$ r1   )r   r,   rD   s    r   
__reduce__DecoratedMain.__reduce__^   s    DOO---r   c                     [        5       e)z0Return an XP given a list of arguments.
        NotImplementedErrorr@   s     r   r5   DecoratedMain.get_xpa   s     "##r   c                 (    [         R                  SS  $ )Nr   )r   r8   rD   s    r   r2   DecoratedMain._get_argvf   s     xx|r   r9   c                    UR                   R                  SSS9  [        R                  " UR                  [        UR                  S5      5        UR                  b  UR                  R                  R                  R                  SSSS9  UR                  R                  R                  SSSS9   UR                  R                  R                  R                  S5        UR                  R                  R                  S5        [        R                  " UR                  [        UR                  S5      5         UR                  R                  S5        U$ U$ ! [         a     Naf = f! [         a     U$ f = f)a7  
Initialize the XP folder. Once this is done, the XP can be retrieved
from its signature. This is done automatically before running,
or when using the `--init` flag of the `dora grid` command.

This will also initialize the shared XP folder so that the XP hyper-params
can be easily shared using its signature.
T)exist_okparentswi  )rP   rQ   mode)foldermkdirjsondumpr8   open_argv_cache_shared_argv_cacheparentchmodPermissionErrorr-   r9   s     r   r6   DecoratedMain.init_xpl   s;    			t4		"''445  ,!!((//55tTX]5^!!((..dQV.W%%,,3399%@%%,,2259 IIbggtB$9$93?@%%++E2 	r	 # 
 # 	s%   2AE <E+ 
E('E(+
E98E9sigc                    [        XR                  S/ S9nUR                  R                  5       (       a)  [        R
                  " [        UR                  5      5      $ UR                  bH  UR                  R                  5       (       a)  [        R
                  " [        UR                  5      5      $ [        SU 35      e)zwReturns the argv used to obtain a given signature.
This can only work if an XP was previously ran with this signature.
N)r`   r$   cfgr8   z)Could not find experiment with signature )	r	   r$   rY   existsrV   loadrX   rZ   RuntimeError)r-   r`   r9   s      r   get_argv_from_sigDecoratedMain.get_argv_from_sig   s     CiiT;>>  ""99T"..122"".23H3H3O3O3Q3Q99T""7"7899!J3%PQQr   c                 B    U R                  U R                  U5      5      $ )zSReturns the XP from the signature. Can only work if such an XP
has previously ran.
)r5   rf   )r-   r`   s     r   get_xp_from_sigDecoratedMain.get_xp_from_sig   s     {{411#677r   c                 "    SU R                    S3$ )NzDecoratedMain()rC   rD   s    r   __repr__DecoratedMain.__repr__   s    		{!,,r   argc                     [        5       e)zConvert a Python value to argv. arg can be either:
- a list, then each entry will be converted and all argv are concatenated.
- a str, then it is directly an argv entry.
- a dict, then each key, value pair is mapped to an argv entry.
rJ   )r-   ro   s     r   value_to_argvDecoratedMain.value_to_argv   s     "##r   c                 b    UR                   R                  5         UR                   R                  $ )zReturn the metrics for a given XP. By default this will look into
the `history.json` file, that can be populated with the Link class.

Can be overriden, but metrics should still be returned as a list
of dicts, possibly with nested dicts.
)linkrd   historyr^   s     r   get_xp_historyDecoratedMain.get_xp_history   s     	wwr   c                     [        5       $ zEReturn default Slurm config for the launch and grid actions.
        )r   rD   s    r   get_slurm_configDecoratedMain.get_slurm_config   s     }r   )r,   r$   r#   r*   r   r)   N) r+   r(   __qualname____firstlineno____doc___slowMainFunr   r.   r:   tpListstrboolr3   r4   rG   Sequencer	   r5   r2   r6   rf   ri   rm   Anyrq   dictrv   r   rz   __static_attributes__r?   r   r   r!   r!   1   s    E@W @J @& rwws|  .$2;;s+ $ $
2773< " 6
RS 
RR[[-= 
R83 82 8-$ $BGGCL $  + r   r!   c                   
  ^  \ rS rSrSr SS\S\S\R                  S\	R                  \   S\4
U 4S jjjrS	\	R                  \   S
\4S jrS\	R$                  S
\	R&                  \   4S jrS\S
\4S jrS
\4U 4S jjrSrU =r$ )ArgparseMain   a  Implementation of `DecoratedMain` for XP that uses argparse.

Args:
    main : main function to wrap.
    dora : Dora config, containing the exclude and dir fields.
    parser : parser to use, and to derive default values from.
    slurm : default slurm config for scheduling jobs.
    use_underscore : if False, scheduling a job as `launcher(batch_size=32)`
        will translate to the command-line `--batch-size=32`,
        otherwise, it will stay as `--batch_size=32`.
r#   r$   parserslurmuse_underscorec                 H   > [         TU ]  X5        X0l        XPl        X@l        g r1   )superr.   r   r   r   )r-   r#   r$   r   r   r   	__class__s         r   r.   ArgparseMain.__init__   s     $,
r   r8   r<   c                 ,   [        U5      nU R                  R                  U5      n/ nUR                  R	                  5        H8  u  pEU R                  R                  U5      U:w  d  M&  UR                  XE45        M:     [        U R                  X!US9nU$ )N)r$   rb   r8   delta)	listr   
parse_args__dict__itemsget_defaultappendr	   r$   )r-   r8   argsr   keyvaluer9   s          r   r5   ArgparseMain.get_xp   s{    Dz{{%%d+----/JC{{&&s+u4c\* 0 TYYD5A	r   ro   c                    / n[        U[        5      (       a  UR                  U5        U$ [        U[        5      (       ap  UR	                  5        HZ  u  p4U R
                  (       d  UR                  SS5      nUSL a  UR                  SU 35        MC  UR                  SU SU 35        M\     U$ [        U[        [        45      (       a  U H  nX R                  U5      -  nM     U$ [        SU 35      e)N_-Tz--=z5Can only process dict, tuple, lists and str, but got )
isinstancer   r   r   r   r   replacer   tuplerq   
ValueError)r-   ro   r8   r   r   parts         r   rq   ArgparseMain.value_to_argv   s    c3KK  T""!iik
**++c3/CD=KK"SE
+KK"SE5' 23 *  dE]++**400   TUXTYZ[[r   r9   c                 j    [        5       nUR                  c   eUR                   H	  u  p4XBU'   M     U$ r1   )r   r   )r-   r9   partsr   r   s        r   get_name_partsArgparseMain.get_name_parts   s5    xx###88KD$K $r   c                 R   > U R                   b  U R                   $ [        TU ]	  5       $ ry   )r   r   rz   )r-   r   s    r   rz   ArgparseMain.get_slurm_config   s'     ::!::w'))r   )r   r   r   r>   )r+   r(   r|   r}   r~   r   r   argparseArgumentParserr   Optionalr   r   r.   r   r   r	   r5   r   r   rq   r   r   rz   r   __classcell__)r   s   @r   r   r      s    
 QUW J @W@W K0IM 2;;s+   BGGCL &  *+ * *r   r   z	./outputsT)direxcluder   sharedr   r   r   r   r   r   r   c                <   ^ ^^^^^^ S[         4UUUU UUU4S jjnU$ )a  Nicer version of `ArgparseMain` that acts like a decorator, and directly
exposes the most useful configs to override.

Args:
    parser: parser to use, and to derive default values from.
    exclude: list of patterns of arguments to exclude from the computation
        of the XP signature.
    dir: path to store logs, checkpoints, etc. to.
    slurm: default slurm config for scheduling jobs.
    shared: path to the shared XP repository.
    use_underscore: if False, scheduling a job as `launcher(batch_size=32)`
        will translate to the command-line `--batch-size=32`,
        otherwise, it will stay as `--batch_size=32`.
    **kwargs: extra args are passed to `DoraConfig`.
r#   c                 z   > [        S[        T5      Tc  S O
[        T5      [        T5      S.TD6n[        XTTTS9$ )N)r   r   r   )r   r   r?   )r   r   r   r   )	r#   r$   r   r   kwargsr   r   r   r   s	     r   
_decorator!argparse_main.<locals>._decorator	  sK     S	!>4tF|M 	
 D~UZ[[r   )r   )r   r   r   r   r   r   r   r   s   ``````` r   argparse_mainr      s    ,\ \ \ r   ) r~   r   collectionsr   r   rV   pathlibr   typingr   r   confr   r   namesr   r9   r	   r
   Callabler   r   r   r!   r   r   Unionr   r   r   r   r   r?   r   r   <module>r      s     #     
 )   ++%@J @F<*= <*@ .9.048=A)-(11 xxT	*;;s+ [1 ++bhhsDy&9:	
 #'r   