[feat] support in-flight weight update (#10071)
Co-authored-by: 赵晨阳 <zhaochen20@outlook.com>
This commit is contained in:
@@ -98,19 +98,51 @@ class TestServerUpdateWeightsFromDisk(CustomTestCase):
|
||||
print(f"[Server Mode] Generated text: {response.json()['text']}")
|
||||
return response.json()["text"]
|
||||
|
||||
def run_decode_random(self, max_new_tokens=32):
|
||||
response = requests.post(
|
||||
self.base_url + "/generate",
|
||||
json={
|
||||
"text": f"Question: {random.randint(0, 100)},The capital of France is",
|
||||
"sampling_params": {
|
||||
"temperature": 0,
|
||||
"max_new_tokens": max_new_tokens,
|
||||
"ignore_eos": True,
|
||||
},
|
||||
},
|
||||
)
|
||||
return response.json()
|
||||
|
||||
def get_model_info(self):
|
||||
response = requests.get(self.base_url + "/get_model_info")
|
||||
model_path = response.json()["model_path"]
|
||||
print(json.dumps(response.json()))
|
||||
return model_path
|
||||
|
||||
def run_update_weights(self, model_path):
|
||||
def run_update_weights(self, model_path, flush_cache=True):
|
||||
response = requests.post(
|
||||
self.base_url + "/update_weights_from_disk",
|
||||
json={"model_path": model_path},
|
||||
json={
|
||||
"model_path": model_path,
|
||||
"flush_cache": flush_cache,
|
||||
},
|
||||
)
|
||||
ret = response.json()
|
||||
return ret
|
||||
|
||||
def pause_generation(self, mode):
|
||||
response = requests.post(
|
||||
self.base_url + "/pause_generation",
|
||||
json={"mode": mode},
|
||||
)
|
||||
ret = response.json()
|
||||
return ret
|
||||
|
||||
def continue_generation(self):
|
||||
response = requests.post(
|
||||
self.base_url + "/continue_generation",
|
||||
json={},
|
||||
)
|
||||
ret = response.json()
|
||||
print(json.dumps(ret))
|
||||
return ret
|
||||
|
||||
def test_update_weights(self):
|
||||
@@ -138,6 +170,42 @@ class TestServerUpdateWeightsFromDisk(CustomTestCase):
|
||||
updated_response = self.run_decode()
|
||||
self.assertEqual(origin_response[:32], updated_response[:32])
|
||||
|
||||
def test_update_weights_non_blocking(self):
|
||||
origin_model_path = self.get_model_info()
|
||||
print(f"[Server Mode] origin_model_path: {origin_model_path}")
|
||||
|
||||
pause_generation_modes = ["in_place", "retract"]
|
||||
for pause_generation_mode in pause_generation_modes:
|
||||
num_requests = 32
|
||||
with ThreadPoolExecutor(num_requests) as executor:
|
||||
futures = [
|
||||
executor.submit(self.run_decode_random, 1600)
|
||||
for _ in range(num_requests)
|
||||
]
|
||||
|
||||
# ensure the decode has been started
|
||||
time.sleep(2)
|
||||
|
||||
new_model_path = DEFAULT_SMALL_MODEL_NAME_FOR_TEST.replace(
|
||||
"-Instruct", ""
|
||||
)
|
||||
ret = self.pause_generation(pause_generation_mode)
|
||||
ret = self.run_update_weights(
|
||||
new_model_path, flush_cache=pause_generation_mode == "retract"
|
||||
)
|
||||
self.assertTrue(ret["success"])
|
||||
ret = self.continue_generation()
|
||||
|
||||
for future in as_completed(futures):
|
||||
self.assertNotEqual(
|
||||
future.result()["meta_info"]["finish_reason"]["type"], "abort"
|
||||
)
|
||||
|
||||
updated_model_path = self.get_model_info()
|
||||
print(f"[Server Mode] updated_model_path: {updated_model_path}")
|
||||
self.assertEqual(updated_model_path, new_model_path)
|
||||
self.assertNotEqual(updated_model_path, origin_model_path)
|
||||
|
||||
def test_update_weights_unexist_model(self):
|
||||
origin_model_path = self.get_model_info()
|
||||
print(f"[Server Mode] origin_model_path: {origin_model_path}")
|
||||
|
||||
@@ -18,6 +18,7 @@ import os
|
||||
import random
|
||||
import time
|
||||
import unittest
|
||||
from concurrent.futures import ThreadPoolExecutor
|
||||
|
||||
import numpy as np
|
||||
import requests
|
||||
@@ -68,6 +69,8 @@ def init_process(
|
||||
backend,
|
||||
checking_parameters,
|
||||
tie_word_embeddings,
|
||||
barrier,
|
||||
pause_generation_mode,
|
||||
):
|
||||
torch.cuda.set_device(rank)
|
||||
|
||||
@@ -81,6 +84,7 @@ def init_process(
|
||||
checking_parameters,
|
||||
tie_word_embeddings,
|
||||
state_dict_key_to_shape,
|
||||
barrier,
|
||||
)
|
||||
elif rank in [1, 2]:
|
||||
init_process_sgl(
|
||||
@@ -94,6 +98,8 @@ def init_process(
|
||||
state_dict_key_to_shape,
|
||||
backend,
|
||||
tp_size,
|
||||
barrier,
|
||||
pause_generation_mode,
|
||||
)
|
||||
|
||||
|
||||
@@ -106,6 +112,7 @@ def init_process_hf(
|
||||
checking_parameters,
|
||||
tie_word_embeddings,
|
||||
state_dict_key_to_shape,
|
||||
barrier,
|
||||
):
|
||||
# These two environment variables are very important
|
||||
# to avoid unexpected behaviors of CUDA and NCCL.
|
||||
@@ -162,6 +169,7 @@ def init_process_hf(
|
||||
group_name="test_parameter_update_group",
|
||||
)
|
||||
torch.cuda.synchronize()
|
||||
barrier.wait()
|
||||
time_begin_broadcast = time.perf_counter()
|
||||
|
||||
# The last parameter is lm_head.weight, which is tied
|
||||
@@ -208,6 +216,8 @@ def init_process_sgl(
|
||||
state_dict_key_to_shape,
|
||||
backend,
|
||||
tp_size,
|
||||
barrier,
|
||||
pause_generation_mode,
|
||||
):
|
||||
torch.cuda.set_device(rank)
|
||||
torch.cuda.synchronize()
|
||||
@@ -282,8 +292,25 @@ def init_process_sgl(
|
||||
},
|
||||
)
|
||||
|
||||
torch.cuda.synchronize()
|
||||
time_begin_update = time.perf_counter()
|
||||
if pause_generation_mode in ["in_place", "retract"]:
|
||||
|
||||
def run_decode(max_new_tokens=32):
|
||||
response = requests.post(
|
||||
url + "/generate",
|
||||
json={
|
||||
"text": f"Question: {random.randint(0, 100)},The capital of France is",
|
||||
"sampling_params": {
|
||||
"temperature": 0,
|
||||
"max_new_tokens": max_new_tokens,
|
||||
"ignore_eos": True,
|
||||
},
|
||||
},
|
||||
)
|
||||
return response.json()
|
||||
|
||||
with ThreadPoolExecutor(32) as executor:
|
||||
futures = [executor.submit(run_decode, 1000) for _ in range(32)]
|
||||
time.sleep(2)
|
||||
|
||||
# The last parameter is lm_head.weight, which is tied
|
||||
# with embed_tokens.weight. Actually, we only need
|
||||
@@ -300,6 +327,14 @@ def init_process_sgl(
|
||||
dtypes = [torch.bfloat16 if backend == "Engine" else "bfloat16"] * len(names)
|
||||
shapes = [state_dict_key_to_shape[parameter_name] for parameter_name in names]
|
||||
|
||||
if pause_generation_mode in ["in_place", "retract"]:
|
||||
requests.post(
|
||||
url + "/pause_generation",
|
||||
json={"mode": pause_generation_mode},
|
||||
)
|
||||
torch.cuda.synchronize()
|
||||
barrier.wait()
|
||||
time_begin_update = time.perf_counter()
|
||||
if backend == "Engine":
|
||||
engine.update_weights_from_distributed(
|
||||
names,
|
||||
@@ -315,10 +350,23 @@ def init_process_sgl(
|
||||
"dtypes": dtypes,
|
||||
"shapes": shapes,
|
||||
"group_name": "test_parameter_update_group",
|
||||
"flush_cache": not (pause_generation_mode == "in_place"),
|
||||
},
|
||||
)
|
||||
torch.cuda.synchronize()
|
||||
time_end_update = time.perf_counter()
|
||||
if pause_generation_mode in ["in_place", "retract"]:
|
||||
requests.post(
|
||||
url + "/continue_generation",
|
||||
json={},
|
||||
)
|
||||
|
||||
# discard unfinished requests to save test overhead
|
||||
time.sleep(2)
|
||||
requests.post(
|
||||
url + "/pause_generation",
|
||||
json={"mode": "abort"},
|
||||
)
|
||||
|
||||
# Measure the latency of broadcast/weights update.
|
||||
update_time = time_end_update - time_begin_update
|
||||
@@ -383,6 +431,7 @@ def test_update_weights_from_distributed(
|
||||
state_dict_key_to_shape,
|
||||
truncate_size,
|
||||
checking_parameters,
|
||||
pause_generation_mode=None,
|
||||
):
|
||||
tie_word_embeddings = (
|
||||
True if model_name == DEFAULT_SMALL_MODEL_NAME_FOR_TEST else False
|
||||
@@ -393,6 +442,7 @@ def test_update_weights_from_distributed(
|
||||
)
|
||||
param_queue = mp.Queue()
|
||||
results = {}
|
||||
barrier = mp.Barrier(1 + dp_size)
|
||||
|
||||
context = mp.spawn(
|
||||
init_process,
|
||||
@@ -406,6 +456,8 @@ def test_update_weights_from_distributed(
|
||||
backend,
|
||||
checking_parameters,
|
||||
tie_word_embeddings,
|
||||
barrier,
|
||||
pause_generation_mode,
|
||||
),
|
||||
nprocs=1 + dp_size,
|
||||
join=False,
|
||||
@@ -558,28 +610,50 @@ class TestUpdateWeightsFromDistributed(CustomTestCase):
|
||||
# test_suits : tp, dp, model_name, backend
|
||||
if is_in_ci():
|
||||
mode = random.choice(["Engine", "Server"])
|
||||
if mode == "Server":
|
||||
pause_generation_mode = random.choice(["in_place", "retract"])
|
||||
else:
|
||||
pause_generation_mode = None
|
||||
test_suits = [
|
||||
(1, 1, DEFAULT_SMALL_MODEL_NAME_FOR_TEST, mode),
|
||||
(1, 1, DEFAULT_SMALL_MODEL_NAME_FOR_TEST, mode, pause_generation_mode),
|
||||
]
|
||||
else:
|
||||
test_suits = [
|
||||
(1, 1, DEFAULT_SMALL_MODEL_NAME_FOR_TEST, "Engine"),
|
||||
(1, 1, DEFAULT_MODEL_NAME_FOR_TEST, "Sever"),
|
||||
(1, 1, DEFAULT_SMALL_MODEL_NAME_FOR_TEST, "Engine", None),
|
||||
(
|
||||
1,
|
||||
1,
|
||||
DEFAULT_MODEL_NAME_FOR_TEST,
|
||||
"Sever",
|
||||
random.choice(["in_place", "retract"]),
|
||||
),
|
||||
]
|
||||
|
||||
if torch.cuda.device_count() >= 4:
|
||||
test_suits.extend(
|
||||
[
|
||||
(2, 1, DEFAULT_SMALL_MODEL_NAME_FOR_TEST, "Engine"),
|
||||
(1, 2, DEFAULT_MODEL_NAME_FOR_TEST, "Server"),
|
||||
(2, 1, DEFAULT_SMALL_MODEL_NAME_FOR_TEST, "Engine", None),
|
||||
(
|
||||
1,
|
||||
2,
|
||||
DEFAULT_MODEL_NAME_FOR_TEST,
|
||||
"Server",
|
||||
random.choice(["in_place", "retract"]),
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
if torch.cuda.device_count() >= 5:
|
||||
test_suits.extend(
|
||||
[
|
||||
(2, 2, DEFAULT_SMALL_MODEL_NAME_FOR_TEST, "Engine"),
|
||||
(2, 2, DEFAULT_MODEL_NAME_FOR_TEST, "Server"),
|
||||
(2, 2, DEFAULT_SMALL_MODEL_NAME_FOR_TEST, "Engine", None),
|
||||
(
|
||||
2,
|
||||
2,
|
||||
DEFAULT_MODEL_NAME_FOR_TEST,
|
||||
"Server",
|
||||
random.choice(["in_place", "retract"]),
|
||||
),
|
||||
]
|
||||
)
|
||||
|
||||
@@ -615,7 +689,7 @@ class TestUpdateWeightsFromDistributed(CustomTestCase):
|
||||
"lm_head.weight",
|
||||
]
|
||||
|
||||
for tp_size, dp_size, model_name, backend in test_suits:
|
||||
for tp_size, dp_size, model_name, backend, pause_generation_mode in test_suits:
|
||||
test_update_weights_from_distributed(
|
||||
tp_size,
|
||||
dp_size,
|
||||
@@ -624,6 +698,7 @@ class TestUpdateWeightsFromDistributed(CustomTestCase):
|
||||
model_state_dict_shapes[model_name],
|
||||
truncate_size,
|
||||
checking_parameters,
|
||||
pause_generation_mode,
|
||||
)
|
||||
|
||||
|
||||
|
||||
@@ -1,12 +1,23 @@
|
||||
import gc
|
||||
import json
|
||||
import random
|
||||
import time
|
||||
import unittest
|
||||
from concurrent.futures import ThreadPoolExecutor, as_completed
|
||||
|
||||
import requests
|
||||
import torch
|
||||
|
||||
import sglang as sgl
|
||||
from sglang.srt.utils import MultiprocessingSerializer, kill_process_tree
|
||||
from sglang.srt.weight_sync.tensor_bucket import FlattenedTensorBucket
|
||||
from sglang.test.test_utils import DEFAULT_SMALL_MODEL_NAME_FOR_TEST, CustomTestCase
|
||||
from sglang.test.test_utils import (
|
||||
DEFAULT_SMALL_MODEL_NAME_FOR_TEST,
|
||||
DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
|
||||
DEFAULT_URL_FOR_TEST,
|
||||
CustomTestCase,
|
||||
popen_launch_server,
|
||||
)
|
||||
|
||||
|
||||
def test_update_weights_from_tensor(tp_size):
|
||||
@@ -167,6 +178,112 @@ class TestUpdateWeightsFromTensor(CustomTestCase):
|
||||
engine.shutdown()
|
||||
|
||||
|
||||
class TestServerUpdateWeightsFromTensorNonBlocking(CustomTestCase):
|
||||
@classmethod
|
||||
def setUpClass(cls):
|
||||
cls.model = DEFAULT_SMALL_MODEL_NAME_FOR_TEST
|
||||
cls.base_url = DEFAULT_URL_FOR_TEST
|
||||
cls.process = popen_launch_server(
|
||||
cls.model,
|
||||
cls.base_url,
|
||||
timeout=DEFAULT_TIMEOUT_FOR_SERVER_LAUNCH,
|
||||
other_args=["--max-running-requests", 8],
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def tearDownClass(cls):
|
||||
kill_process_tree(cls.process.pid)
|
||||
|
||||
def run_decode(self, max_new_tokens=32):
|
||||
response = requests.post(
|
||||
self.base_url + "/generate",
|
||||
json={
|
||||
"text": f"Question: {random.randint(0, 100)},The capital of France is",
|
||||
"sampling_params": {
|
||||
"temperature": 0,
|
||||
"max_new_tokens": max_new_tokens,
|
||||
"ignore_eos": True,
|
||||
},
|
||||
},
|
||||
)
|
||||
return response.json()
|
||||
|
||||
def get_model_info(self):
|
||||
response = requests.get(self.base_url + "/get_model_info")
|
||||
model_path = response.json()["model_path"]
|
||||
print(json.dumps(response.json()))
|
||||
return model_path
|
||||
|
||||
def pause_generation(self, mode):
|
||||
response = requests.post(
|
||||
self.base_url + "/pause_generation",
|
||||
json={"mode": mode},
|
||||
)
|
||||
ret = response.json()
|
||||
return ret
|
||||
|
||||
def continue_generation(self):
|
||||
response = requests.post(
|
||||
self.base_url + "/continue_generation",
|
||||
json={},
|
||||
)
|
||||
ret = response.json()
|
||||
return ret
|
||||
|
||||
def run_update_weights(self, named_tensors, flush_cache=True):
|
||||
response = requests.post(
|
||||
self.base_url + "/update_weights_from_tensor",
|
||||
json={
|
||||
"serialized_named_tensors": [
|
||||
MultiprocessingSerializer.serialize(named_tensors, output_str=True)
|
||||
],
|
||||
"flush_cache": flush_cache,
|
||||
},
|
||||
)
|
||||
ret = response.json()
|
||||
return ret
|
||||
|
||||
def test_update_weights(self):
|
||||
pause_generation_modes = ["in_place", "retract"]
|
||||
for pause_generation_mode in pause_generation_modes:
|
||||
num_requests = 32
|
||||
with ThreadPoolExecutor(num_requests) as executor:
|
||||
futures = [
|
||||
executor.submit(self.run_decode, 3000) for _ in range(num_requests)
|
||||
]
|
||||
|
||||
# ensure the decode has been started
|
||||
time.sleep(2)
|
||||
|
||||
param_names = [
|
||||
f"model.layers.{i}.mlp.up_proj.weight" for i in range(6, 16)
|
||||
]
|
||||
new_tensor = torch.full((16384, 2048), 1.5, device="cuda")
|
||||
named_tensors = [(x, new_tensor) for x in param_names]
|
||||
|
||||
ret = self.pause_generation(pause_generation_mode)
|
||||
ret = self.run_update_weights(
|
||||
named_tensors, flush_cache=pause_generation_mode == "retract"
|
||||
)
|
||||
self.assertTrue(ret["success"])
|
||||
ret = self.continue_generation()
|
||||
|
||||
for future in as_completed(futures):
|
||||
self.assertNotEqual(
|
||||
future.result()["meta_info"]["finish_reason"]["type"], "abort"
|
||||
)
|
||||
|
||||
for param_name in param_names[:3]:
|
||||
response = requests.post(
|
||||
self.base_url + "/get_weights_by_name",
|
||||
json={"name": param_name},
|
||||
)
|
||||
actual_values = torch.tensor(response.json())[0, :5]
|
||||
assert torch.allclose(
|
||||
actual_values, torch.tensor([1.5] * 5), atol=0.002
|
||||
), f"{actual_values=}"
|
||||
|
||||
|
||||
def _check_param(engine, param_name, expect_values):
|
||||
actual_values = torch.tensor(engine.get_weights_by_name(param_name))[0, :5]
|
||||
assert torch.allclose(
|
||||
|
||||
Reference in New Issue
Block a user