Repository navigation
Conversation
`import resource` shared a try block with the ModelManager import, so when ModelManager's optional dependencies (torch) were missing, resource was set to None too and RunInference reported 0 for model memory. Fixes apache#40854
There was a problem hiding this comment.
📋 Review Summary
This PR cleanly resolves an issue where a failed import of ModelManager (e.g., when optional dependencies like torch are not installed) inadvertently set the resource module reference to None, breaking model_byte_size metric collection on POSIX platforms. Splitting the try...except ImportError blocks and verifying the behavior in an isolated subprocess test is the right approach.
🔍 General Feedback
- The separation of
import resourceandfrom apache_beam.ml.inference.model_manager import ModelManageris clean and preserves the fallback behavior for both modules independently. - The regression test in
base_test.pyaccurately simulates the missing dependency scenario using a fresh interpreter subprocess and properly skips on Windows.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
/gemini review |
There was a problem hiding this comment.
📋 Review Summary
This PR fixes a bug in apache_beam.ml.inference.base where a shared try...except ImportError block caused resource to be set to None whenever ModelManager failed to import (e.g., when optional dependencies like torch are absent). Separating the two try...except blocks and adding an isolated subprocess unit test cleanly resolves the issue.
🔍 General Feedback
- The separated import handling is clean and preserves platform fallback behavior for both
resource(on Windows) andModelManager(whentorch/scipyare not installed). - The regression test using
subprocess.runandsys.modules['apache_beam.ml.inference.model_manager'] = Noneis a great way to test module-level import state without polluting the test runner'ssys.modules.
💡 Tip: Comment /gemini review on this pull request to trigger a new Gemini code review.
|
Assigning reviewers: R: @shunping 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❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #40855 +/- ##
===========================================
+ Coverage 0 79.81% +79.81%
===========================================
Files 0 494 +494
Lines 0 83575 +83575
===========================================
+ Hits 0 66709 +66709
- Misses 0 16866 +16866
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:
|
| dofn._model.force_reset() | ||
|
|
||
|
|
||
| class ResourceImportTest(unittest.TestCase): |
There was a problem hiding this comment.
Welcome to Beam and thanks for your contribution! @tannnmayy
I think the test is trivial and I would just remove that.
Instead, have you considered adding a unit test to RunInference to see if we will get warnings if ModelMananger is not importable but resource is?
If it is doable and the test is simple (without using subprocess), I would rather have that.
There was a problem hiding this comment.
Thanks for the welcome! Makes sense. I removed the subprocess test and added test_no_resource_warning_when_model_manager_unavailable to RunInferenceBaseTest. It patches ModelManager to None, runs RunInference with FakeModelHandler, and checks that the "Resource module is not available" warning isn't logged. It's skipped on Windows, where resource doesn't exist.
Without torch, as in the default test environment, it fails on the old code and passes with the fix. With torch installed it passes either way, since the original bug happened at import time.
I also merged master and moved the CHANGES.md entry to 2.79.0, since 2.78.0 has been cut.
…e-import # Conflicts: # CHANGES.md
shunping
left a comment
There was a problem hiding this comment.
LGTM, thanks! I will merge once all of the tests are green.
Fixes #40854
import resourceand theModelManagerimport shared one try block inapache_beam/ml/inference/base.py. When ModelManager's dependencies (torch)are missing, the except branch also set
resource = None, so on Linux/macOSRunInference reported 0 for model memory (
model_byte_size) and logged"Resource module is not available..." twice per model load.
This splits the block so each import falls back independently, and adds a
RunInference test that patches
ModelManagertoNoneand checks that the"Resource module is not available" warning isn't logged.
Testing:
(Linux, Python 3.14).
Windows, where
resourcedoesn't exist).The fix and the regression test were written by me and I independently reviewed everything and ran the checks multiple times,
I did use my ai to help me navigate beam and to learn more about it, since this is my first time contributing to beam.