2121 DEFAULT_REQUEST_TIMEOUT ,
2222 ClientItem ,
2323 generate_hidden_states ,
24+ wait_for_lock ,
2425)
2526from speculators .train .noise_transforms import TransformTensors
2627
@@ -259,6 +260,11 @@ def _compute_approx_lengths(self) -> list[int]:
259260 def _maybe_load_hs_file (self , index : int ) -> dict [str , torch .Tensor ] | None :
260261 file_idx = self ._map_to_file_idx (index )
261262 candidate_path = self .hidden_states_path / f"hs_{ file_idx } .safetensors"
263+
264+ lock_path = str (candidate_path ) + ".lock"
265+ if Path (lock_path ).exists ():
266+ wait_for_lock (lock_path )
267+
262268 if candidate_path .exists ():
263269 return load_file (candidate_path )
264270
@@ -279,19 +285,22 @@ def _maybe_generate_hs(self, index: int) -> dict[str, torch.Tensor] | None:
279285 timeout = self .request_timeout ,
280286 max_retries = self .max_retries ,
281287 )
282- except Exception as e : # noqa: BLE001
283- warnings .warn (str (e ), stacklevel = 1 )
284- return None
285288
286- loaded_hs = load_file (hs_filepath )
289+ loaded_hs = load_file (hs_filepath )
287290
288- match self .on_generate :
289- case "cache" :
290- file_idx = self ._map_to_file_idx (index )
291- target_path = self .hidden_states_path / f"hs_{ file_idx } .safetensors"
292- shutil .move (hs_filepath , target_path )
293- case "delete" :
294- 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
295304
296305 return loaded_hs
297306
0 commit comments