feat: continuous processing - add to GPU with fewer tasks (max 4 per GPU)

This commit is contained in:
Christian Medina
2026-07-03 00:26:38 -04:00
parent 8a30ec92c6
commit 4308fd3391

View File

@@ -47,10 +47,9 @@ def streaming_quantize(model_path: str, output_path: str):
config = AutoConfig.from_pretrained(model_path, trust_remote_code=True) config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
# Process 3 shards per GPU (6 total) - adjust based on GPU memory # Process continuously: max 4 shards per GPU, add to whichever GPU has room
shards_per_gpu = 3 max_per_gpu = 4
num_gpus = 2 num_gpus = 2
max_parallel = shards_per_gpu * num_gpus
def process_shard(idx, shard_file, gpu_id): def process_shard(idx, shard_file, gpu_id):
"""Process a single shard (called per-GPU).""" """Process a single shard (called per-GPU)."""
@@ -106,19 +105,61 @@ def streaming_quantize(model_path: str, output_path: str):
print(f"Starting from shard {start_idx+1}/16\n") print(f"Starting from shard {start_idx+1}/16\n")
# Process in batches from start_idx # Process continuously: submit to GPU with fewer active tasks
for i in range(start_idx, len(shards), max_parallel): import threading
batch = shards[i:i+max_parallel] gpu_locks = [threading.Lock() for _ in range(num_gpus)]
gpu_counts = [0] * num_gpus
with concurrent.futures.ThreadPoolExecutor(max_workers=max_parallel) as executor: def get_next_gpu():
futures = [] """Get the GPU with fewest active tasks (max 4 per GPU)."""
for j, shard_file in enumerate(batch): with gpu_locks[0]:
idx = i + j with gpu_locks[1]:
# Assign 4 shards per GPU: 0-3→GPU0, 4-7→GPU1, 8-11→GPU0, etc. if gpu_counts[0] <= gpu_counts[1] and gpu_counts[0] < max_per_gpu:
gpu_id = (idx // shards_per_gpu) % num_gpus return 0
futures.append(executor.submit(process_shard, idx, shard_file, gpu_id)) elif gpu_counts[1] < max_per_gpu:
for future in concurrent.futures.as_completed(futures): return 1
future.result() else:
return None # Both GPUs at max
# Create a queue of unprocessed shards
from collections import deque
remaining = deque(range(start_idx, len(shards)))
with concurrent.futures.ThreadPoolExecutor(max_workers=max_per_gpu * num_gpus) as executor:
futures = []
def submit_next():
"""Submit next shard to available GPU."""
gpu_id = get_next_gpu()
if gpu_id is None or not remaining:
return
idx = remaining.popleft()
shard_file = shards[idx]
with gpu_locks[gpu_id]:
gpu_counts[gpu_id] += 1
future = executor.submit(process_shard, idx, shard_file, gpu_id)
futures.append(future)
# When this future completes, release GPU slot and submit next
def on_complete(f):
with gpu_locks[gpu_id]:
gpu_counts[gpu_id] -= 1
submit_next() # Try to submit next shard
future.add_done_callback(on_complete)
# Start submitting
while remaining:
gpu_id = get_next_gpu()
if gpu_id is None:
break
submit_next()
# Wait for all to complete
concurrent.futures.wait(futures)
config.save_pretrained(output_path) config.save_pretrained(output_path)
print(f"\n✅ Done → {output_path}") print(f"\n✅ Done → {output_path}")