Threaded multi gpu inference

From wenmin-wu/ds-skills · View interactive version

Threaded multi gpu inference is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Run multiple LLM inference jobs in parallel using Python threads, each pinned to a separate GPU with staggered starts

How to use this skill

Skill file

Threaded Multi-GPU Inference

Overview

When you have multiple LLMs to run and multiple GPUs available, parallelize using Python threads with each model pinned to a dedicated GPU. Stagger thread starts by ~10 seconds to avoid simultaneous memory allocation spikes. Collect results into shared DataFrames. Cuts wall-clock time by N for N GPUs.

Quick Start

import threading
import time
import torch

def run_inference_on_gpu(model_path, gpu_id, data, output_dict, name):
    """Run LLM inference on a specific GPU."""
    device = f"cuda:{gpu_id}"
    model = load_model(model_path, device=device)
    tokenizer = load_tokenizer(model_path)
    
    results = []
    for batch in create_batches(data, batch_size=8):
        inputs = tokenizer(batch, return_tensors="pt", padding=True,
                          truncation=True).to(device)
        with torch.no_grad():
            outputs = model(**inputs)
        results.append(outputs.logits.cpu())
    
    output_dict[name] = torch.cat(results)

# Launch parallel inference
results = {}
gpu_assignments = [
    ("path/to/gemma", 0, "gemma"),
    ("path/to/qwen", 1, "qwen"),
]

threads = []
for model_path, gpu_id, name in gpu_assignments:
    t = threading.Thread(
        target=run_inference_on_gpu,
        args=(model_path, gpu_id, test_data, results, name)
    )
    threads.append(t)
    t.start()
    time.sleep(10)  # stagger to avoid OOM

for t in threads:
    t.join()
# results["gemma"], results["qwen"] now available

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.