a
    yf6	                     @   sj   d dl 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 e	dddZ
dd	 Zd
d Zdd ZdS )    N   )USER_CONFIG_DIR)	TORCH_1_9)returnc                  C   sJ   t  t jt j&} | d |  d W  d   S 1 s<0    Y  dS )z
    Finds 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.
    )z	127.0.0.1r   r   N)socketAF_INETSOCK_STREAMbindgetsockname)s r   R/var/www/html/django/DPS/env/lib/python3.9/site-packages/ultralytics/utils/dist.pyfind_free_network_port   s    
r   c                 C   s   | j j d| j j dd\}}dt| j d| d| d| dt| jd| jj d	}t	d
 j
dd tjdt|  dddt	d
 dd}|| W d   n1 s0    Y  |jS )z/Generates a DDP file and returns its file name..r   zd
# Ultralytics Multi-GPU training temp file (should be automatically deleted after use)
overrides = z&

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

    cfg = DEFAULT_CFG_DICT.copy()
    cfg.update(save_dir='')   # handle the extra key 'save_dir'
    trainer = z9(cfg=cfg, overrides=overrides)
    trainer.args.model = "Z	model_urlz "
    results = trainer.train()
ZDDPT)exist_okZ_temp_.pyzw+zutf-8F)prefixsuffixmodeencodingdirdeleteN)	__class__
__module____name__rsplitvarsargsgetattrZhub_sessionmodelr   mkdirtempfileNamedTemporaryFileidwritename)trainermoduler%   contentfiler   r   r   generate_ddp_file   s0    "(r*   c                 C   sV   ddl }|jst|j t|}tr*dnd}t }tj	d|d|  d| |g}||fS )z7Generates and returns command for distributed 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)Z
world_sizer&   r+   r)   Zdist_cmdportcmdr   r   r   generate_ddp_command8   s    r4   c                 C   s    t |  d|v rt| dS )zDelete temp file if created.r   N)r#   osremove)r&   r)   r   r   r   ddp_cleanupE   s    r7   )r5   r-   r   r0   r!    r   Ztorch_utilsr   intr   r*   r4   r7   r   r   r   r   <module>   s   