
    /j                        d dl mZ d dlZ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rd dlmZ dd
ZddZddZddZdS )    )annotationsN)TYPE_CHECKING   )USER_CONFIG_DIR)	TORCH_1_9)BaseTrainerreturnintc                     ddl } |                      | j        | j                  5 }|                    d           |                                d         cddd           S # 1 swxY w Y   dS )zFind a free port on localhost.

    It is useful in single-node training when we don't want to connect to a real main node but have to set the
    `MASTER_PORT` environment variable.

    Returns:
        (int): The available network port number.
    r   N)z	127.0.0.1r   r   )socketAF_INETSOCK_STREAMbindgetsockname)r   ss     [/home/longshao/multi-rider-rag/.venv/lib/python3.11/site-packages/ultralytics/utils/dist.pyfind_free_network_portr      s     MMM	v~v'9	:	: "a	   }}q!" " " " " " " " " " " " " " " " " "s   /A!!A%(A%trainerr   strc                |   | j         j         d| j         j                             dd          \  }}t	          | j                                                  }|                    d          ddlfd|d         D             |d<   d| d| d	| d
| dt          | j
        d| j        j                   d}t          dz                      d           t          j        dt!          |            dddt          dz  d          5 }|                    |           ddd           n# 1 swxY w Y   |j        S )a1  Generate a DDP (Distributed Data Parallel) file for multi-GPU training.

    This function creates a temporary Python file that enables distributed training across multiple GPUs. The file
    contains the necessary configuration to initialize the trainer in a distributed environment.

    Args:
        trainer (ultralytics.engine.trainer.BaseTrainer): The trainer containing training configuration and arguments.
            Must have args attribute and be a class instance.

    Returns:
        (str): Path to the generated temporary DDP file.

    Notes:
        The generated file is saved in the USER_CONFIG_DIR/DDP directory and includes:
        - Trainer class import
        - Configuration overrides from the trainer arguments
        - Model path configuration
        - Training initialization code
    .r   augmentationsNr   c                :    g | ]}                     |          S  )to_dict).0tAs     r   
<listcomp>z%generate_ddp_file.<locals>.<listcomp>=   s#    %W%W%Wqaiill%W%W%W    z
# Ultralytics Multi-GPU training temp file (should be automatically deleted after use)
from pathlib import Path, PosixPath  # For model arguments stored as Path instead of str
overrides = z&

if __name__ == "__main__":
    from z import a  
    from ultralytics.utils import DEFAULT_CFG_DICT

    # Deserialize augmentations from dicts back to Albumentations transform objects
    if overrides.get("augmentations") is not None:
        import albumentations as A
        overrides["augmentations"] = [A.from_dict(t) for t in overrides["augmentations"]]

    cfg = DEFAULT_CFG_DICT.copy()
    cfg.update(save_dir='')   # handle the extra key 'save_dir'
    trainer = z9(cfg=cfg, overrides=overrides)
    trainer.args.model = "	model_urlz "
    results = trainer.train()
DDPT)exist_ok_temp_.pyzw+zutf-8F)prefixsuffixmodeencodingdirdelete)	__class__
__module____name__rsplitvarsargscopygetalbumentationsgetattrhub_sessionmodelr   mkdirtempfileNamedTemporaryFileidwritename)r   moduler=   	overridescontentfiler   s         @r   generate_ddp_filerB   "   s   ( '2QQW5F5OQQXXY\^_``LFD W\""''))I}}_%%1""""%W%W%W%WIo<V%W%W%W	/"        ! " #7#6W\EWXX#  G( u##T#222		$W++"""e#
 
 
  


7               9s   
D,,D03D0tuple[list[str], str]c                    ddl }| j        st          j        | j                   t          |           }t          rdnd}t                      }t          j	        d|d| j
         d| |g}||fS )aT  Generate command for distributed training.

    Args:
        trainer (ultralytics.engine.trainer.BaseTrainer): The trainer containing configuration for distributed training.

    Returns:
        cmd (list[str]): The command to execute for distributed training.
        file (str): Path to the temporary file created for DDP training.
    r   Nztorch.distributed.runztorch.distributed.launchz-mz--nproc_per_nodez--master_port)__main__resumeshutilrmtreesave_dirrB   r   r   sys
executable
world_size)r   rE   rA   dist_cmdportcmds         r   generate_ddp_commandrP   `   s     OOO> (g&'''W%%D*3S&&9SH!##D		C 9r    rA   Nonec                Z    t          |            d|v rt          j        |           dS dS )aD  Delete temporary file if created during distributed data parallel (DDP) training.

    This function checks if the provided file contains the trainer's ID in its name, indicating it was created as a
    temporary file for DDP training, and deletes it if so.

    Args:
        trainer (ultralytics.engine.trainer.BaseTrainer): The trainer used for distributed training.
        file (str): Path to the file that might need to be deleted.

    Examples:
        >>> trainer = YOLOTrainer()
        >>> file = "/tmp/ddp_temp_123456789.py"
        >>> ddp_cleanup(trainer, file)
    r%   N)r;   osremove)r   rA   s     r   ddp_cleanuprU   ~   s7     W++d""
	$ #"r    )r	   r
   )r   r   r	   r   )r   r   r	   rC   )r   r   rA   r   r	   rQ   )
__future__r   rS   rG   rJ   r9   typingr    r   torch_utilsr   ultralytics.engine.trainerr   r   rB   rP   rU   r   r    r   <module>r[      s    # " " " " " 				  



                    " " " " " " 7666666" " " " ; ; ; ;|   <     r    