Repository navigation
[Python] Support RunInference models that can't be weakly referenced - #41407
schizophrenicmaniac wants to merge 7 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 58.60% 58.60%
Complexity 2292 2292
=========================================
Files 1133 1133
Lines 170086 170103 +17
Branches 1487 1487
=========================================
+ Hits 99685 99695 +10
- Misses 67860 67867 +7
Partials 2541 2541
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
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.