Skip to content

Commit e7dceba

Browse files
committed
Add coderabbit suggestions
Signed-off-by: Fynn Schmitt-Ulms <fschmitt@redhat.com>
1 parent 700fa68 commit e7dceba

3 files changed

Lines changed: 28 additions & 21 deletions

File tree

scripts/data_generation_offline.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -171,21 +171,21 @@ def parse_args():
171171
def get_existing_hidden_state_indices(output_path: Path) -> list[int]:
172172
"""Find existing `hs_i.safetensors` files (where i is the file index)"""
173173

174-
existing_file_indices = []
174+
existing_file_indices_set: set[int] = set()
175175

176176
if not output_path.exists():
177-
return existing_file_indices
177+
return []
178178

179179
for file_path in output_path.iterdir():
180180
if file_path.name.startswith("hs_") and file_path.name.endswith(".safetensors"):
181181
index_str = file_path.stem[3:] # Remove "hs_" prefix
182182
try:
183183
file_index = int(index_str)
184-
existing_file_indices.append(file_index)
184+
existing_file_indices_set.add(file_index)
185185
except ValueError:
186186
continue
187187

188-
return sorted(existing_file_indices)
188+
return sorted(existing_file_indices_set)
189189

190190

191191
def get_indices_to_process(
@@ -248,7 +248,7 @@ def check_safetensors_file(path: Path, tokens: list[int]):
248248
)
249249

250250

251-
async def worker(
251+
async def worker( # noqa: C901
252252
client,
253253
model: str,
254254
queue: "asyncio.Queue[dict[str, Any]]",

src/speculators/data_generation/vllm_client.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -137,9 +137,11 @@ async def wait_for_lock_async(lock_path, timeout=10.0, poll_interval=0.1):
137137
fd = os.open(lock_path, os.O_RDONLY)
138138
try:
139139
await asyncio.wait_for(_poll_lock_async(fd, poll_interval), timeout=timeout)
140-
finally:
140+
except BaseException:
141141
os.close(fd)
142-
os.remove(lock_path)
142+
raise
143+
os.close(fd)
144+
os.remove(lock_path)
143145

144146

145147
def wait_for_lock(lock_path, timeout=10.0, poll_interval=0.1):
@@ -149,16 +151,18 @@ def wait_for_lock(lock_path, timeout=10.0, poll_interval=0.1):
149151
while True:
150152
try:
151153
fcntl.flock(fd, fcntl.LOCK_EX | fcntl.LOCK_NB)
152-
return
154+
break
153155
except BlockingIOError:
154156
if time.monotonic() >= deadline:
155157
raise TimeoutError(
156158
f"Timed out waiting for lock: {lock_path}"
157159
) from None
158160
time.sleep(poll_interval)
159-
finally:
161+
except BaseException:
160162
os.close(fd)
161-
os.remove(lock_path)
163+
raise
164+
os.close(fd)
165+
os.remove(lock_path)
162166

163167

164168
@with_retries

src/speculators/train/data.py

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -285,19 +285,22 @@ def _maybe_generate_hs(self, index: int) -> dict[str, torch.Tensor] | None:
285285
timeout=self.request_timeout,
286286
max_retries=self.max_retries,
287287
)
288-
except Exception as e: # noqa: BLE001
289-
warnings.warn(str(e), stacklevel=1)
290-
return None
291288

292-
loaded_hs = load_file(hs_filepath)
289+
loaded_hs = load_file(hs_filepath)
293290

294-
match self.on_generate:
295-
case "cache":
296-
file_idx = self._map_to_file_idx(index)
297-
target_path = self.hidden_states_path / f"hs_{file_idx}.safetensors"
298-
shutil.move(hs_filepath, target_path)
299-
case "delete":
300-
Path(hs_filepath).unlink()
291+
match self.on_generate:
292+
case "cache":
293+
file_idx = self._map_to_file_idx(index)
294+
target_path = self.hidden_states_path / f"hs_{file_idx}.safetensors"
295+
shutil.move(hs_filepath, target_path)
296+
case "delete":
297+
Path(hs_filepath).unlink()
298+
except Exception as e: # noqa: BLE001
299+
warnings.warn(
300+
f"Failed to load/cache hidden states for sample {index}: {e}",
301+
stacklevel=1,
302+
)
303+
return None
301304

302305
return loaded_hs
303306

0 commit comments

Comments
 (0)