damccorm commented on code in PR #41405:
URL: https://github.com/apache/beam/pull/41405#discussion_r4672912815
##########
sdks/python/apache_beam/ml/inference/base.py:
##########
@@ -725,7 +725,13 @@ def load(self, key: str) -> _ModelLoadStats:
start_time = _to_milliseconds(time.time_ns())
shared_handle = multi_process_shared.MultiProcessShared(
mh.load_model, tag=tag)
- model_reference = shared_handle.acquire()
+ try:
+ model_reference = shared_handle.acquire()
+ except Exception:
+ # Forget the tag so that the next call loads the model again instead of
+ # returning a tag that has no model behind it.
+ del self._tag_map[key]
+ raise
self._proxy_map[tag] = (shared_handle, model_reference)
Review Comment:
Thanks for catching and fixing this! Instead of inserting
`self._tag_map[key]` before loading and rolling it back on exception, could we
defer setting `self._tag_map[key] = tag` until right after
`shared_handle.acquire()` succeeds (next to `self._proxy_map[tag] =
(shared_handle, model_reference)`) and adjust the LRU check above to
`self._max_models <= len(self._tag_map) and self._tag_map`?
```python
if key in self._tag_map:
self._tag_map.move_to_end(key)
return _ModelLoadStats(self._tag_map[key], None, None)
tag = uuid.uuid4().hex
mh = self._mh_map[key]
if (self._max_models is not None and
self._max_models <= len(self._tag_map) and self._tag_map):
# If we're about to exceed our LRU size, release the last used model.
tag_to_remove = self._tag_map.popitem(last=False)[1]
shared_handle, model_to_remove = self._proxy_map[tag_to_remove]
shared_handle.release(model_to_remove)
del self._proxy_map[tag_to_remove]
# Load the new model
memory_before = _get_current_process_memory_in_bytes()
start_time = _to_milliseconds(time.time_ns())
shared_handle = multi_process_shared.MultiProcessShared(
mh.load_model, tag=tag)
model_reference = shared_handle.acquire()
self._tag_map[key] = tag
self._proxy_map[tag] = (shared_handle, model_reference)
```
That keeps `_tag_map` and `_proxy_map` strictly in sync while `acquire()` is
in flight (avoiding a window on the multi-threaded `MultiProcessShared` manager
where a concurrent `load(key)` sees a tag with no model behind it or an
eviction/`update_model_handler` hits `KeyError` on `_proxy_map[tag]`), and also
covers failures outside `acquire()` (e.g., `self._mh_map[key]` or
`BaseException`) without needing a `try/except` rollback.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]