o
    2wi                     @   s   d Z ddlmZ ddlmZmZ ddlmZ ddlmZ ddl	Z
ddlmZ ddlmZ d	e
jd
efddZd	e
jdefddZeG dd dZeG dd dZeG dd dZeG dd dZdS )z'
Basic configuration for Dora is here.
    )	Namespace)	dataclassfield)fnmatch)PathN)
DictConfig)	OmegaConfdataargsc                 C   sF   | j D ]}t|tsJ t||r t||}|dur t| || qdS )z>Update the given dataclass from the argument parser args.
    N)__dict__
isinstancestrhasattrgetattrsetattr)r	   r
   keyvalue r   F/home/ubuntu/sommelier/.venv/lib/python3.10/site-packages/dora/conf.pyupdate_from_args   s   


r   cfgc                 C   sl   t j|dd}t|tsJ | D ]!\}}t|tsJ t| |r)t| || qtd| j	 d| dS )z6Update the given dataclass from the hydra config.
    T)resolvezObject of type z does not have an attribute N)
r   to_containerr   dictitemsr   r   r   AttributeError	__class__)r	   r   dctr   r   r   r   r   update_from_hydra   s   
r   c                   @   s   e Zd ZU dZdZeed< dZeed< dZ	eed< dZ
eed	< d
Zeje ed< dZeed< d
Zeje ed< eedZeje ed< dZeed< dZeed< dZeed< dZeed< d
Zeje ed< d
Zeje ed< d
Zeje ed< d
S )SlurmConfiga+  
    Configuration when scheduling a job.
    This differs slightly from Slurm/Submitit params because this will
    automatically scale some values based on the number of GPUs.

    Args:
        gpus (int): number of total GPUs to schedule. Number of nodes
            and tasks per nodes will be automatically inferred.
        mem_per_gpu (float): amount of memory in GB to schedule
            per gpus.
        time (int): maximum duration for the job in minutes.
        cpus_per_gpu (int): number of cpus per gpu, this will set
            the `cpus_per_task` automatically, based on the
            number of gpus and `one_task_per_node`, unless `cpus_per_task`
            is explicitely provided.
        cpus_per_task (int or None): number of cpus per task.
        partition (str): partition name
        comment (str): comment for the job.
        setup (List[str]): list of shell commands to execute
            before the actual command. Use it for `module load`.
        max_num_timeout (int): maximum number of requeue.
        one_task_per_node (bool): if True, schedules a single task
            per node, otherwise, will schedule one task per gpu (default is False).
        array_parallelism (int): when using job arrays, how many tasks can run
            in parallel.
        qos: (str or None): qos param for slurm.
        account: (str or None): account param for slurm.

    ..warning:: this assumes one task per GPU.
        Set `one_task_per_node` if you do not want that.
        Tasks without any gpus are not really supported at the moment.
       gpus(   mem_per_gpui  time
   cpus_per_gpuNcpus_per_tasklearnlab	partitioncommentdefault_factorysetup   max_num_timeout 
constraintFone_task_per_node   array_parallelismexcludeqosaccount)__name__
__module____qualname____doc__r!   int__annotations__r#   floatr$   r&   r'   tpOptionalr)   r   r*   r   listr-   Listr/   r1   r2   boolr4   r5   r6   r7   r   r   r   r   r   .   s"   
  r   c                   @   sB   e Zd ZU dZdZeed< dZeed< dZeed< dZ	eed< dS )SubmitRulesa:  
    Submit rules describe in which case Shepherd will schedule new jobs.

    Args:
        retry (bool): if true, all failed or canceled jobs will be rescheduled.
        update_pending (bool): if true, all pending jobs whose Slurm parameters
            have changed will be replaced.
        update (bool): if true, all pending or running jobs whose Slurm parameters
            have changed will be replaced.
        replace (bool): if true, all running jobs will be replaced by new jobs.
        replace_done (bool): if true, all done jobs will be relaunched.
    Fretryupdatereplacereplace_doneN)
r8   r9   r:   r;   rE   rC   r=   rF   rG   rH   r   r   r   r   rD   a   s   
 rD   c                   @   sZ   e Zd ZU dZdZeed< dZeed< dZeed< dZ	eed< dZ
eed	< d
Zeed
< dS )
ShepConfigz
    Configuration for Shepherd. Mostly naming conventions for folders and files.
    There should be little reasons to change that.
    zjob.pkljob_fileby_idorphanssubmititsubmitit_folderlatestlatest_submititarraysN)r8   r9   r:   r;   rJ   r   r=   rK   rL   rN   rP   rQ   r   r   r   r   rI   v   s   
 rI   c                       s   e Zd ZU dZedZeed< eedZ	e
je ed< dZeed< dZe
je ed	< dZe
je ed
< dZeed< dZeed< eedZeed< dZeed< dZeed< dZeed< dZeed< dedefddZ fddZ  ZS )
DoraConfiga<  
    Main Dora configuration. The main parameters to change are the following.

    Args:
        dir (Path or str): path where Dora will save all useful informations, logs.
            This is also where you should store your checkpoints (see `dora.xp.XP`).
        exclude (List[str]): list of patterns of argument names to ignore
            when computing the XP signature and doing deduplication.
            For instance 'num_workers', etc.
        git_save (bool): when True, experiments can only be scheduled from a clean repo.
            A shallow clone of the repo will be made and execution will happen from there.
            This does not impact `dora run` unless you pass the `--git_save` flag.
        shared (Path or None): if provided, the path to a central repository of XPs.
            For the moment, this only supports sharing hyper-params, logs etc. will stay
            in the per user folder.
        grid_package (str or None): if provided, package to look for grids. Default
            to the package with the `train.py` module followed by `.grids`.
    z	./outputsdirr+   r5   Fgit_saveNsharedgrid_packagezhistory.jsonhistoryxpsshepzrendezvous.txtrendezvous_fileuse_rendezvousgrids_gridscodes_codesarg_namereturnc                 C   s    | j D ]
}t||r dS qdS )zUReturn True if the given argument name should be excluded from
        the signature.TF)r5   r   )selfr`   patternr   r   r   is_excluded   s
   

zDoraConfig.is_excludedc                    s:   |dv rddl m} |d urt||}t || d S )N)rS   rU   r    )to_absolute_path)rT   re   r   super__setattr__)rb   namer   re   r   r   r   rg      s
   zDoraConfig.__setattr__)r8   r9   r:   r;   r   rS   r=   r   rA   r5   r?   rB   r   rT   rC   rU   r@   rV   rW   rX   rI   rY   rZ   r[   r]   r_   rd   rg   __classcell__r   r   ri   r   rR      s    
 rR   )r;   argparser   dataclassesr   r   r   pathlibr   typingr?   omegaconf.dictconfigr   	omegaconfr   Anyr   r   r   rD   rI   rR   r   r   r   r   <module>   s$   2