Skip to content

Commit

Permalink
fix lock for concurrent writes
Browse files Browse the repository at this point in the history
  • Loading branch information
bdubayah committed Sep 11, 2024
1 parent beccb7b commit c7fc110
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "truss"
version = "0.9.35rc1"
version = "0.9.35rc1dev1"
description = "A seamless bridge from model development to model delivery"
license = "MIT"
readme = "README.md"
Expand Down
19 changes: 14 additions & 5 deletions truss/templates/trtllm-briton/src/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import fcntl
import hashlib
import json
import math
import multiprocessing
import os
import signal
Expand Down Expand Up @@ -122,8 +123,11 @@ def __init__(self, **kwargs):
predict_concurrency = runtime.get("predict_concurrency", 1)
cpu_count = os.cpu_count()
self._max_fsm_workers = (
min(predict_concurrency, cpu_count) if cpu_count else predict_concurrency
min(predict_concurrency, math.ceil(cpu_count / 2))
if cpu_count
else predict_concurrency
)
print(f"Using {self._max_fsm_workers} workers for FSM schema generation")

def load(self):
if self._loaded:
Expand Down Expand Up @@ -424,10 +428,15 @@ def worker(vocab_size: int, end_id: int, schema: Dict[str, Any], output_path: Pa
vocab_size=vocab_size,
eos_token_id=end_id,
)
with open(output_path, "wb") as f:
fcntl.flock(f, fcntl.LOCK_EX)
f.write(states_to_tokens_pb.SerializeToString())
fcntl.flock(f, fcntl.LOCK_UN)
if not output_path.exists():
try:
fd = os.open(output_path, os.O_CREAT | os.O_EXCL | os.O_WRONLY)
with os.fdopen(fd, "wb") as f:
fcntl.flock(f, fcntl.LOCK_EX)
f.write(states_to_tokens_pb.SerializeToString())
fcntl.flock(f, fcntl.LOCK_UN)
except FileExistsError:
pass


def dummy_task():
Expand Down

0 comments on commit c7fc110

Please sign in to comment.