Ë
    Fêñi‹  ã                  ó„   — 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y)é    )ÚannotationsN)ÚTYPE_CHECKINGé   )ÚUSER_CONFIG_DIR)Ú	TORCH_1_9)ÚBaseTrainerc                 óÊ   — ddl } | j                  | j                  | j                  «      5 }|j                  d«       |j	                  «       d   cddd«       S # 1 sw Y   yxY w)zûFind 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     úX/var/www/pod-logistic/pod-ai/venv/lib/python3.12/site-packages/ultralytics/utils/dist.pyÚfind_free_network_portr      sQ   € ó à	�‰�v—~‘~ v×'9Ñ'9Ó	:ð "¸aØ	�‰ÐÔ Ø�}‰}‹˜qÑ!÷"÷ "ò "ús   «$AÁA"c                óÈ  — | j                   j                  › d| j                   j                  › �j                  dd«      \  }}t	        | j
                  «      j                  «       }|j                  d«      �)ddl}|d   D �cg c]  }|j                  |«      ‘Œ c}|d<   d|› d|› d|› d	|› d
t        | j                  d| j
                  j                  «      › d�}t        dz  j                  d¬«       t        j                   dt#        | «      › d�ddt        dz  d¬«      5 }|j%                  |«       ddd«       |j&                  S c c}w # 1 sw Y   j&                  S xY w)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   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Úto_dictÚgetattrÚhub_sessionÚmodelr   ÚmkdirÚtempfileÚNamedTemporaryFileÚidÚwriteÚname)ÚtrainerÚmoduler2   Ú	overridesÚAÚtÚcontentÚfiles           r   Úgenerate_ddp_filer:   "   sq  € ð( ×'Ñ'×2Ñ2Ð3°1°W×5FÑ5F×5OÑ5OÐ4PÐQ×XÑXÐY\Ð^_Ó`�L€FˆDô �W—\‘\Ó"×'Ñ'Ó)€IØ‡}�}�_Ó%Ð1Û"à<EÀoÑ<VÖ%W°q a§i¡i°¥lÒ%Wˆ	�/Ñ"ðð ˆKð 
ð ˆ�˜$˜ð 
 ð ˆfð Ü" 7×#6Ñ#6¸ÀWÇ\Á\×EWÑEWÓXÐYð Zð#€Gô( �uÑ×#Ñ#¨TÐ#Ô2Ü	×	$Ñ	$ØÜ�W“+�˜cÐ"ØØÜ˜eÑ#Øô
ð ð 
Ø�
‰
�7Ô÷ð �9‰9ÐùòA &X÷.ð �9‰9Ðús   ÂEÄ"EÅE!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_dirr:   r   r   ÚsysÚ
executableÚ
world_size)r3   r<   r9   Údist_cmdÚportÚcmds         r   Úgenerate_ddp_commandrG   `   sw   € ó à�>Š>Ü�‰�g×&Ñ&Ô'Ü˜WÓ%€DÝ*3Ñ&Ð9S€HÜ!Ó#€Dä�‰ØØØØ×ÑÐ
ØØˆ&Øð	€Cð �ˆ9Ðó    c                óP   — t        | «      › d�|v rt        j                  |«       yy)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)r0   ÚosÚremove)r3   r9   s     r   Úddp_cleanuprL   ~   s'   € ô ˆW‹+ˆ�cÐ˜dÑ"Ü
�	‰	�$�ð #rH   )ÚreturnÚint)r3   r   rM   Ústr)r3   r   rM   ztuple[list[str], str])r3   r   r9   rO   rM   ÚNone)Ú
__future__r   rJ   r>   rA   r.   Útypingr   Ú r   Útorch_utilsr   Úultralytics.engine.trainerr   r   r:   rG   rL   © rH   r   ú<module>rW      s:   ðõ #ã 	Û Û 
Û Ý  å Ý "áÝ6ó"ó ;ó|ô<rH   