o
    "Õj%  ã                   @   s\   d dl Z d dlZd dlZd dlmZ d dlmZ d dlmZ edd„ ƒZ	dd„ Z
dd	„ ZdS )
é    N)Údygraph_only)Úfleetc                 K   sž   t  ¡ dkrtj| fi |¤ŽS t ¡ }| ¡ dkr| ¡ dks#J dƒ‚d|vr+d|d< |d }t|t	ƒs=J dt
|ƒ› ƒ‚t d|¡sGJ dƒ‚t| fi |¤ŽS )aV  
    Load an object can be used in paddle from specified path.
    The file is saved by distributed.save

    Note:
        The file to load must be saved bu the API paddle.incubate.distributed.utils.io.save

    Args:
        path(str|BytesIO) : The path/buffer to load the target object. Generally, the path is the target
            file path. When loading state_dict from the saved result of the API used to save
            the inference model, the path may be a file prefix or directory.
        **configs (dict, optional): other load configuration options for compatibility. We do not
            recommend using these configurations, they may be removed in the future. If not necessary,
            DO NOT use them. Default None.
            The following options are currently supported:
                (1) place: where to place the loaded state dict.
                     If the state dict is too large, the palce should be set 'cpu'.
            Note:
                Other config value may cause some error.Please don't use any more config options.
    Returns:
        Object(Object): a target object can be used in paddle

    Examples:
        import paddle
        paddle.distributed.init_process_group(backend='nccl')
        paddle.distributed.fleet.init(is_collective=True)

        model = build_model()
        optimizer = build_optimizer(model)

        dist_model = paddle.distributed_optimizer(model)
        dist_optimizer = paddle.distributed_optimizer(optimizer)


        # load model state dict
        model_state_dict = paddle.incubate.distributed.utils.io.load(path="path/to/load.pdparams")
        dist_model.set_state_dict(model_state_dict)

        # load optimizer satte dict
        optimizer_state_dict = paddle.incubate.distributed.utils.io.load(path="path/to/load.pdopt")
        dist_optimizer.set_state_dict(optimizer_state_dict)

    é   z&Sharding and DP are supported only nowÚplaceÚcpuz,configs[place] must be a str, but this is a z^(cpu|gpu:[0-9]*)$z,configs[place] must be cpu, gpu:0, gpu:1 ...)ÚdistZget_world_sizeÚpaddleÚloadr   Zget_hybrid_communicate_groupZget_model_parallel_world_sizeZget_pipe_parallel_world_sizeÚ
isinstanceÚstrÚtypeÚreÚsearchÚload_with_place)ÚpathÚconfigsZhcgr   © r   úo/var/www/html/Deteccion_Ine/venv/lib/python3.10/site-packages/paddle/incubate/distributed/utils/io/dist_load.pyr	      s*   -ÿÿþÿþr	   c                 K   sT   |d }|d u rt  | ¡S t  ¡ }t  |¡ t|ƒ}t j| fi |¤Ž}t  |¡ |S )Nr   )r   r	   Z
get_deviceZ
set_deviceÚ_remove_not_supported_itmes)r   r   r   Zorigin_placeZ
state_dictr   r   r   r   _   s   


r   c                 C   s8   g d¢}t   | ¡}|  ¡ D ]}||vr| |d ¡ q|S )N)Zmodel_filenameZparams_filenameZreturn_numpy)ÚcopyÚkeysÚpop)r   Z__supported_by_load__Z_configsÚkr   r   r   r   o   s   
€r   )r   r   r   Zpaddle.distributedÚdistributedr   Zpaddle.base.frameworkr   r   r	   r   r   r   r   r   r   Ú<module>   s   
F