learning-at-home / hivemind

Decentralized deep learning in PyTorch. Built to train models on thousands of volunteers across the world.
MIT License
2.06k stars 169 forks source link

[BUG] Expert server fails to bootstrap up to 90% of the time #634

Open Vectorrent opened 1 month ago

Vectorrent commented 1 month ago

Describe the bug When using hivemind.moe.Server to host experts in a background thread, bootstrapping will fail over and over again, repeatedly - leading to a complete deadlock. I am forced to restart my application repeatedly, sometimes upwards of 10 times, before a single bootstrap will work.

As you can imagine, this is very frustrating - especially during development, when I need to iterate quickly.

If there is a better way to host experts, or if you could tell me how to resolve this problem - I would GREATLY appreciate it!

To Reproduce Run this script a few times in a row. It will ALWAYS fail, eventually:

import time

import torch
import torch.nn as nn
import torch.nn.functional as F
from hivemind import DHT
from hivemind.moe import ModuleBackend, Server, register_expert_class
from hivemind.moe.server.layers import name_to_block
from hivemind.utils import BatchTensorDescriptor

sample_input = lambda batch_size, hidden_dim: torch.empty((batch_size, hidden_dim))

@register_expert_class("perceptron", sample_input)
class MultilayerPerceptron(nn.Module):
    def __init__(self, hidden_dim, num_classes=10):
        super().__init__()
        self.layer1 = nn.Linear(hidden_dim, 2 * hidden_dim)
        self.layer2 = nn.Linear(2 * hidden_dim, 2 * hidden_dim)
        self.layer3 = nn.Linear(2 * hidden_dim, num_classes)

    def forward(self, x):
        x = F.relu(self.layer1(x))
        x = F.relu(self.layer2(x))
        x = self.layer3(x)
        return x

PUBLIC_INITIAL_PEERS = [
    # IPv4 DNS addresses
    "/dns/bootstrap1.petals.dev/tcp/31337/p2p/QmedTaZXmULqwspJXz44SsPZyTNKxhnnFvYRajfH7MGhCY",
    "/dns/bootstrap2.petals.dev/tcp/31338/p2p/QmQGTqmM7NKjV6ggU1ZCap8zWiyKR89RViDXiqehSiCpY5",
    # IPv6 DNS addresses
    "/dns6/bootstrap1.petals.dev/tcp/31337/p2p/QmedTaZXmULqwspJXz44SsPZyTNKxhnnFvYRajfH7MGhCY",
    "/dns6/bootstrap2.petals.dev/tcp/31338/p2p/QmQGTqmM7NKjV6ggU1ZCap8zWiyKR89RViDXiqehSiCpY5",
    # Reserved IPs
    "/ip4/159.89.214.152/tcp/31337/p2p/QmedTaZXmULqwspJXz44SsPZyTNKxhnnFvYRajfH7MGhCY",
    "/ip4/159.203.156.48/tcp/31338/p2p/QmQGTqmM7NKjV6ggU1ZCap8zWiyKR89RViDXiqehSiCpY5",
]

dht = DHT(
    initial_peers=PUBLIC_INITIAL_PEERS,
    host_maddrs=["/ip4/0.0.0.0/tcp/0", "/ip4/0.0.0.0/udp/0/quic"],
    start=True,
)
hidden_schema = BatchTensorDescriptor(
    64,
)
backends = {}
num_layers = 3
for i in range(num_layers):
    expert_name = f"expert.{i}"
    expert = ModuleBackend(
        name=expert_name,
        module=name_to_block["perceptron"](64),
        args_schema=(hidden_schema,),
        outputs_schema=(hidden_schema),
        max_batch_size=64,
        start=True,
    )
    backends[expert_name] = expert
server = Server(
    dht,
    backends,
)
server.run_in_background(timeout=5)

while True:
    print("waiting...")
    time.sleep(3)

Environment

OS: Arch Linux (x86_64) GCC version: (GCC) 14.2.1 20240910 Clang version: 18.1.8 CMake version: Could not collect Libc version: glibc-2.40

Python version: 3.12.6 (main, Sep 8 2024, 13:18:56) [GCC 14.2.1 20240805] (64-bit runtime) Python platform: Linux-6.11.1-arch1-1-x86_64-with-glibc2.40 Is CUDA available: True CUDA runtime version: 12.6.68 CUDA_MODULE_LOADING set to: LAZY GPU models and configuration: GPU 0: NVIDIA GeForce GTX 960 GPU 1: NVIDIA GeForce GTX 1070

Nvidia driver version: 560.35.03 cuDNN version: Probably one of the following: /usr/lib/libcudnn.so.9.2.1 /usr/lib/libcudnn_adv.so.9.2.1 /usr/lib/libcudnn_cnn.so.9.2.1 /usr/lib/libcudnn_engines_precompiled.so.9.2.1 /usr/lib/libcudnn_engines_runtime_compiled.so.9.2.1 /usr/lib/libcudnn_graph.so.9.2.1 /usr/lib/libcudnn_heuristic.so.9.2.1 /usr/lib/libcudnn_ops.so.9.2.1 HIP runtime version: N/A MIOpen runtime version: N/A Is XNNPACK available: True

CPU: Architecture: x86_64 CPU op-mode(s): 32-bit, 64-bit Address sizes: 39 bits physical, 48 bits virtual Byte Order: Little Endian CPU(s): 8 On-line CPU(s) list: 0-7 Vendor ID: GenuineIntel Model name: Intel(R) Core(TM) i7-4790K CPU @ 4.00GHz CPU family: 6 Model: 60 Thread(s) per core: 2 Core(s) per socket: 4 Socket(s): 1 Stepping: 3 CPU(s) scaling MHz: 96% CPU max MHz: 4400.0000 CPU min MHz: 800.0000 BogoMIPS: 7999.08 Flags: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc cpuid aperfmperf pni pclmulqdq dtes64 monitor ds_cpl est tm2 ssse3 sdbg fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm cpuid_fault epb pti ssbd ibrs ibpb stibp fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid xsaveopt dtherm ida arat pln pts md_clear flush_l1d L1d cache: 128 KiB (4 instances) L1i cache: 128 KiB (4 instances) L2 cache: 1 MiB (4 instances) L3 cache: 8 MiB (1 instance) NUMA node(s): 1 NUMA node0 CPU(s): 0-7 Vulnerability Gather data sampling: Not affected Vulnerability Itlb multihit: KVM: Mitigation: VMX unsupported Vulnerability L1tf: Mitigation; PTE Inversion Vulnerability Mds: Mitigation; Clear CPU buffers; SMT vulnerable Vulnerability Meltdown: Mitigation; PTI Vulnerability Mmio stale data: Unknown: No mitigations Vulnerability Reg file data sampling: Not affected Vulnerability Retbleed: Not affected Vulnerability Spec rstack overflow: Not affected Vulnerability Spec store bypass: Mitigation; Speculative Store Bypass disabled via prctl Vulnerability Spectre v1: Mitigation; usercopy/swapgs barriers and __user pointer sanitization Vulnerability Spectre v2: Mitigation; Retpolines; IBPB conditional; IBRS_FW; STIBP conditional; RSB filling; PBRSB-eIBRS Not affected; BHI Not affected Vulnerability Srbds: Mitigation; Microcode Vulnerability Tsx async abort: Not affected

Versions of relevant libraries: [pip3] numpy==2.1.2 [pip3] nvidia-cublas-cu12==12.1.3.1 [pip3] nvidia-cuda-cupti-cu12==12.1.105 [pip3] nvidia-cuda-nvrtc-cu12==12.1.105 [pip3] nvidia-cuda-runtime-cu12==12.1.105 [pip3] nvidia-cudnn-cu12==9.1.0.70 [pip3] nvidia-cufft-cu12==11.0.2.54 [pip3] nvidia-curand-cu12==10.3.2.106 [pip3] nvidia-cusolver-cu12==11.4.5.107 [pip3] nvidia-cusparse-cu12==12.1.0.106 [pip3] nvidia-nccl-cu12==2.20.5 [pip3] nvidia-nvjitlink-cu12==12.6.77 [pip3] nvidia-nvtx-cu12==12.1.105 [pip3] pytorch-lightning==2.4.0 [pip3] pytorch_optimizer==3.1.2 [pip3] torch==2.4.1 [pip3] torchmetrics==1.4.3 [pip3] triton==3.0.0 [conda] Could not collect

Vectorrent commented 1 month ago

So, it appears that using Server.create() to launch experts is much more reliable. While I can probably work with this new method, I remember now why I moved-away from it in the first place:

Server.create() makes an assumption that experts may only accept hidden_dim as an argument during initialization:

expert = name_to_block[expert_cls](hidden_dim)

I find this annoying, but maybe I'll find a clean way to work round the limitation.

Vectorrent commented 1 month ago

Okay, yeah - inheriting from Server and overwriting the create() method seems like a decent, not terrible solution for my needs. The other code is still buggy, but perhaps we were never supposed to use it that way. Will close this issue for now.

Vectorrent commented 1 month ago

I spoke too soon. The bootstrapping problem returned, even when using my customized Server.create() method. However, I think I've found the source of the problem.

For whatever reason, this piece of code is REQUIRED, to prevent issues during bootstrapping:

visible_maddrs_str = [str(a) for a in dht.get_visible_maddrs()]

You don't actually have to use this data. The mere act of calling dht.get_visible_maddrs() from within the server creation thread is enough to prevent bootstrapping issues. Without this code, bootstrap can fail up to 90% of the time.

Clearly, this a bug - and one that should be somewhat easy to fix.