[Engine] Fix generate hanging issue after the first call (#1606)

This commit is contained in:
Byron Hsu
2024-10-07 21:26:56 -07:00
committed by GitHub
parent c5325aba75
commit e8613df071
2 changed files with 15 additions and 2 deletions

View File

@@ -691,8 +691,9 @@ class Engine:
lora_path=lora_path,
)
# make it synchronous
return asyncio.run(generate_request(obj, None))
# get the current event loop
loop = asyncio.get_event_loop()
return loop.run_until_complete(generate_request(obj, None))
def shutdown(self):
kill_child_process(os.getpid(), including_parent=False)

View File

@@ -28,6 +28,18 @@ class TestSRTBackend(unittest.TestCase):
print(out2)
assert out1 == out2, f"{out1} != {out2}"
def test_engine_multiple_generate(self):
# just to ensure there is no issue running multiple generate calls
prompt = "Today is a sunny day and I like"
model_path = DEFAULT_MODEL_NAME_FOR_TEST
sampling_params = {"temperature": 0, "max_new_tokens": 8}
engine = sgl.Engine(model_path=model_path, random_seed=42)
engine.generate(prompt, sampling_params)
engine.generate(prompt, sampling_params)
engine.shutdown()
if __name__ == "__main__":
unittest.main()