o
    * i#                     @  s.  d dl mZ d dlZd dlmZ ddlmZ erd dlmZ G dd dZ	G d	d
 d
e	Z
G dd de	ZG dd de	ZG dd de	ZG dd de	ZG dd de	ZG dd de	ZG dd de	ZG dd de	ZG dd de	ZG dd de	ZG dd  d e	ZG d!d" d"e	ZG d#d$ d$e	ZdS )%    )annotationsN)TYPE_CHECKING   )	constants)_DTypeLiteralc                   @  s@   e Zd ZdddZdd Zdd Zdd	 Zd
d ZdddZdS )
BaseConfigNc                 C  sx   || _ d | _|d urt|tr|| _ntd| t| j }| D ]
\}}t| || q$| jr:| 	| j d S d S Nz%Expected a dictionary. But received: )
	_category_config_dict
isinstancedict
ValueErrorr   get_category_default_configitemssetattr	from_dict)selfcategoryconfig_dictconfigfielddefault_value r   u/home/app/PaddleOCR-VL-test/.venv_paddleocr/lib/python3.10/site-packages/paddle/distributed/auto_parallel/strategy.py__init__   s   
zBaseConfig.__init__c                 C  sB   t | j}| D ]}||t j}|t jkrt| || q
d S N)r   r   r	   keysget	NOT_FOUNDr   )r   r   r   r   valuer   r   r   r   -   s   
zBaseConfig.from_dictc                 C  s^   i }t | j}| D ]}t| |}|||< q| j D ]\}}t|tr,|	 ||< q|S r   )
r   r   r	   r   getattr__dict__r   r   r   to_dict)r   result_dictr   r   r   r   r   r   r"   5   s   


zBaseConfig.to_dictc                 C  s<   |   }d}| D ]\}}|d| d| d7 }q
|d S )N{"z":"z",})r"   r   )r   r#   stringkvr   r   r   __repr__@   s
   zBaseConfig.__repr__c                 C  sH   | j }||}||t| < | j D ]\}}t||t|| q|S r   )	__class____new__idr!   r   r   copydeepcopy)r   memoclsresultr(   r)   r   r   r   __deepcopy__G   s   
zBaseConfig.__deepcopy__c                 C  s   |   }|||S r   )r"   r   )r   r(   dr#   r   r   r   r   O   s   zBaseConfig.getr   )	__name__
__module____qualname__r   r   r"   r*   r3   r   r   r   r   r   r      s    
r   c                         e Zd Zd fdd	Z  ZS )RecomputeConfigNc                      t j}t || d S r   )r   	RECOMPUTEsuperr   r   r   r   r+   r   r   r   U      zRecomputeConfig.__init__r   r5   r6   r7   r   __classcell__r   r   r>   r   r9   T       r9   c                      r8   )FusedLinearPromotionConfigNc                   r:   r   )r   FUSEDLINEARPROMOTIONr<   r   r=   r>   r   r   r   [   r?   z#FusedLinearPromotionConfig.__init__r   r@   r   r   r>   r   rC   Z   rB   rC   c                      s   e Zd ZU ded< ded< ded< ded< d	ed
< d	ed< ded< ded< ded< ded< ded< ded< ded< ded< ded< d fdd	Z  ZS )	AMPConfigboolenabler   ZdtypestrlevelfloatZinit_loss_scalingintZincr_every_n_stepsZdecr_every_n_nan_or_infZ
incr_ratioZ
decr_ratioZuse_dynamic_loss_scalingz	list[str]Zcustom_white_listZcustom_black_listZcustom_black_varnamesZuse_fp16_guardZuse_bf16_guardZuse_master_gradNc                   r:   r   )r   AMPr<   r   r=   r>   r   r   r   q   r?   zAMPConfig.__init__r   r5   r6   r7   __annotations__r   rA   r   r   r>   r   rE   `   s"   
 rE   c                      s8   e Zd ZU ded< ded< ded< d	 fdd	Z  ZS )
ShardingConfigrF   rG   rK   ZstageZdegreeNc                   r:   r   )r   SHARDINGr<   r   r=   r>   r   r   r   {   r?   zShardingConfig.__init__r   rM   r   r   r>   r   rO   v   
   
 rO   c                      s8   e Zd ZU ded< ded< ded< d	 fdd	Z  ZS )
GradientMergeConfigrF   rG   rK   Zk_stepsZavgNc                   r:   r   )r   GRADIENT_MERGEr<   r   r=   r>   r   r   r      r?   zGradientMergeConfig.__init__r   rM   r   r   r>   r   rR      rQ   rR   c                      s@   e Zd ZU ded< ded< ded< ded< d fd	d
	Z  ZS )PipelineConfigrF   rG   rH   Zschedule_moderK   Zmicro_batch_sizeZaccumulate_stepsNc                   r:   r   )r   PIPELINEr<   r   r=   r>   r   r   r      r?   zPipelineConfig.__init__r   rM   r   r   r>   r   rT      s   
 rT   c                      r8   )	QATConfigNc                   r:   r   )r   QATr<   r   r=   r>   r   r   r      r?   zQATConfig.__init__r   r@   r   r   r>   r   rV      rB   rV   c                      r8   )TuningConfigNc                   r:   r   )r   TUNINGr<   r   r=   r>   r   r   r      r?   zTuningConfig.__init__r   r@   r   r   r>   r   rX      rB   rX   c                      r8   )DatasetConfigNc                   r:   r   )r   DATASETr<   r   r=   r>   r   r   r      r?   zDatasetConfig.__init__r   r@   r   r   r>   r   rZ      rB   rZ   c                      r8   )FusedPassesConfigNc                   r:   r   )r   FUSED_PASSESr<   r   r=   r>   r   r   r      r?   zFusedPassesConfig.__init__r   r@   r   r   r>   r   r\      rB   r\   c                      r8   )DPOptimizationConfigNc                   r:   r   )r   DP_OPTIMIZATIONr<   r   r=   r>   r   r   r      r?   zDPOptimizationConfig.__init__r   r@   r   r   r>   r   r^      rB   r^   c                      r8   )MPOptimizationConfigNc                   r:   r   )r   MP_OPTIMIZATIONr<   r   r=   r>   r   r   r      r?   zMPOptimizationConfig.__init__r   r@   r   r   r>   r   r`      rB   r`   c                      r8   )SPOptimizationConfigNc                   r:   r   )r   SP_OPTIMIZATIONr<   r   r=   r>   r   r   r      r?   zSPOptimizationConfig.__init__r   r@   r   r   r>   r   rb      rB   rb   c                      s"   e Zd ZdZd fdd	Z  ZS )Strategya  
    The `Strategy` object is used to configure the parallelization and optimization for static graph.

    Args:
        config (dict|string, optional): If this is None, the default configurations will used.
        If this is a dictionary, the recognized key-value of it will be used to override the default
        configurations while other default configurations are left unchanged. If this is a string,
        it is interpreted as the path to a YAML configuration and will be loaded to override the
        corresponding default configurations.

    Examples:
        .. code-block:: python

            >>> import paddle
            >>> from paddle.distributed.fleet import auto

            >>> strategy = auto.Strategy()
            >>> sharding = strategy.sharding
            >>> assert sharding.enable == False
            >>> assert sharding.stage == 1
            >>> assert sharding.degree == 8

            >>> sharding.enable = True
            >>> sharding.stage = 2
            >>> sharding.degree = 2
            >>> assert sharding.enable == True
            >>> assert sharding.stage == 2
            >>> assert sharding.degree == 2

    Nc                   s  |d urt |trt|| _n
td| i | _tj}t 	|| j | j
tjd }t|| _| j
tjd }t|| _| j
tjd }t|| _| j
tjd }t|| _| j
tjd }t|| _| j
tjd }t|| _| j
tjd }t|| _| j
tj d }t!|| _"| j
tj#d }t$|| _%| j
tj&d }t'|| _(| j
tj)d }t*|| _+| j
tj,d }t-|| _.| j
tj/d }t0|| _1d S r   )2r   r   r.   r/   r
   r   r   ZBASEr<   r   r   r;   r9   Z	recomputerL   rE   amprP   rO   ZshardingrS   rR   Zgradient_mergerU   rT   ZpipelinerW   rV   ZqatrY   rX   Ztuningr[   rZ   Zdatasetr]   r\   Zfused_passesrD   rC   Zfused_linear_promotionr_   r^   Zdp_optimizationra   r`   Zmp_optimizationrc   rb   Zsp_optimization)r   r   r   r   r>   r   r   r      sJ   












zStrategy.__init__r   )r5   r6   r7   __doc__r   rA   r   r   r>   r   rd      s    rd   )
__future__r   r.   typingr    r   Zpaddle._typing.dtype_liker   r   r9   rC   rE   rO   rR   rT   rV   rX   rZ   r\   r^   r`   rb   rd   r   r   r   r   <module>   s*   ;

