─░├žeri─če ge├ž

Referans i├žin ultralytics/utils/dist.py

Not

Bu dosya https://github.com/ultralytics/ultralytics/blob/main/ ultralytics/utils/dist .py adresinde mevcuttur. Bir sorun tespit ederseniz l├╝tfen bir ├çekme ─░ste─či ­čŤá´ŞĆ ile katk─▒da bulunarak d├╝zeltilmesine yard─▒mc─▒ olun. Te┼čekk├╝rler ­čÖĆ!



ultralytics.utils.dist.find_free_network_port()

Localhost ├╝zerinde bo┼č bir ba─člant─▒ noktas─▒ bulur.

Tek d├╝─č├╝ml├╝ e─čitimde, ger├žek bir ana d├╝─č├╝me ba─članmak istemedi─čimizde ancak MASTER_PORT ortam de─či┼čkeni.

Kaynak kodu ultralytics/utils/dist.py
def find_free_network_port() -> int:
    """
    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.
    """
    with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
        s.bind(("127.0.0.1", 0))
        return s.getsockname()[1]  # port



ultralytics.utils.dist.generate_ddp_file(trainer)

Bir DDP dosyas─▒ olu┼čturur ve dosya ad─▒n─▒ d├Ând├╝r├╝r.

Kaynak kodu ultralytics/utils/dist.py
def generate_ddp_file(trainer):
    """Generates a DDP file and returns its file name."""
    module, name = f"{trainer.__class__.__module__}.{trainer.__class__.__name__}".rsplit(".", 1)

    content = f"""
# Ultralytics Multi-GPU training temp file (should be automatically deleted after use)
overrides = {vars(trainer.args)}

if __name__ == "__main__":
    from {module} import {name}
    from ultralytics.utils import DEFAULT_CFG_DICT

    cfg = DEFAULT_CFG_DICT.copy()
    cfg.update(save_dir='')   # handle the extra key 'save_dir'
    trainer = {name}(cfg=cfg, overrides=overrides)
    results = trainer.train()
"""
    (USER_CONFIG_DIR / "DDP").mkdir(exist_ok=True)
    with tempfile.NamedTemporaryFile(
        prefix="_temp_",
        suffix=f"{id(trainer)}.py",
        mode="w+",
        encoding="utf-8",
        dir=USER_CONFIG_DIR / "DDP",
        delete=False,
    ) as file:
        file.write(content)
    return file.name



ultralytics.utils.dist.generate_ddp_command(world_size, trainer)

Da─č─▒t─▒lm─▒┼č e─čitim i├žin komut ├╝retir ve d├Ând├╝r├╝r.

Kaynak kodu ultralytics/utils/dist.py
def generate_ddp_command(world_size, trainer):
    """Generates and returns command for distributed training."""
    import __main__  # noqa local import to avoid https://github.com/Lightning-AI/lightning/issues/15218

    if not trainer.resume:
        shutil.rmtree(trainer.save_dir)  # remove the save_dir
    file = generate_ddp_file(trainer)
    dist_cmd = "torch.distributed.run" if TORCH_1_9 else "torch.distributed.launch"
    port = find_free_network_port()
    cmd = [sys.executable, "-m", dist_cmd, "--nproc_per_node", f"{world_size}", "--master_port", f"{port}", file]
    return cmd, file



ultralytics.utils.dist.ddp_cleanup(trainer, file)

Olu┼čturulmu┼čsa ge├žici dosyay─▒ silin.

Kaynak kodu ultralytics/utils/dist.py
def ddp_cleanup(trainer, file):
    """Delete temp file if created."""
    if f"{id(trainer)}.py" in file:  # if temp_file suffix in file
        os.remove(file)





Created 2023-11-12, Updated 2024-06-02
Authors: glenn-jocher (5), Burhan-Q (1), Laughing-q (1)