Repository navigation
[Python] Support RunInference models that can't be weakly referenced - #41407
jrmccluskey merged 8 commits into
Conversation
…and fix the DLQ docstring
There was a problem hiding this comment.
📋 Review Summary
This PR fixes a TypeError in RunInference when ModelHandler.load_model() returns an object that does not support weak references (such as a dict or list) by wrapping non-weak-referenceable models in a lightweight holder object before caching them in shared.Shared while preserving a strong reference on the DoFn. It also updates the with_exception_handling() docstring to accurately describe the dead-letter error info tuple and adds thorough unit tests for both changes.
🔍 General Feedback
- The
_WeakRefableModelHolderdesign cleanly handles both weak-referenceable and non-weak-referenceable models while properly maintaining reference lifetimes viaself._shared_modelon_RunInferenceDoFn. - The docstring update for
RunInference.with_exception_handling()accurately reflects the error tuple emitted byPTransformWithSideInputs.with_exception_handling(). - The added tests cover pipeline execution,
DoFnmodel reuse across garbage collection cycles, and the structure offailed_inferenceserror info.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @jrmccluskey for label python. Note: If you would like to opt out of this review, comment Available commands:
The PR bot will only process comments in the main thread (not review comments). |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## master #41407 +/- ##
============================================
- Coverage 59.77% 59.67% -0.11%
Complexity 5235 5235
============================================
Files 1472 1472
Lines 188707 188724 +17
Branches 3926 3926
============================================
- Hits 112804 112622 -182
- Misses 72479 72678 +199
Partials 3424 3424
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
jrmccluskey
left a comment
There was a problem hiding this comment.
I think these are two different problems and would be worth discussing separately. The weak references are a new feature, but the DLQ docstring probably needs to be discussed further. There' an argument for changing the docstring, there's also potential to change the behavior downstream (e.g. make it actually pass the exception down.) I'd rather the latter get split into another PR and the weak references be taken forward here
|
@jrmccluskey Makes sense. I've removed the docstring change and its test, so this PR now only covers the weak reference handling. |
# Conflicts: # CHANGES.md
# Conflicts: # CHANGES.md
# Conflicts: # CHANGES.md
# Conflicts: # CHANGES.md
# Conflicts: # CHANGES.md # sdks/python/apache_beam/ml/inference/base_test.py
|
/gemini review |
There was a problem hiding this comment.
📋 Review Summary
This PR resolves a TypeError in RunInference when ModelHandler.load_model() returns an object that cannot be weakly referenced (such as a dict or list) by wrapping non-weakly-referenceable models in _WeakRefableModelHolder before caching them via shared.Shared. It preserves a strong reference to the holder on _RunInferenceDoFn to maintain expected cache lifetimes and unwraps the underlying model before passing it to _SharedModelWrapper and run_inference.
🔍 General Feedback
- The
_WeakRefableModelHolderwrapper andself._shared_modelstrong reference on_RunInferenceDoFncleanly handle non-weakrefable models without alteringModelHandlersemantics or impactingMultiProcessShared/ModelManagercode paths. - The unit tests cover both end-to-end
RunInferenceexecution with adictmodel and model reuse acrossDoFninstances after garbage collection.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
Fixes #41351
RunInference caches the loaded model with
shared.Shared, which keeps a weak reference to it. If aModelHandler.load_model()returns something like a dict (a common way to bundle a tokenizer and a model), setup fails withTypeError: cannot create weak reference to 'dict' object._RunInferenceDoFn._load_modelnow checks whether the loaded model can be weakly referenced and, if not, caches it inside a small holder object. It unwraps the model before handing it torun_inference, so handlers still get the dict they returned. The DoFn keeps a strong reference to the cached holder so it isn't collected while in use. The multi-process and ModelManager paths don't go throughshared.Sharedand are unchanged.Added tests for running a pipeline with a dict model and for reusing a cached dict model across new DoFn instances. Also added a CHANGES.md entry.