tannnmayy opened a new issue, #40854:
URL: https://github.com/apache/beam/issues/40854

   ### What happened?
   
   **Environment:** Apache Beam 2.77.0 (also reproduced with master at 
cba73417cd), Python 3.14.4, Ubuntu (WSL2), `torch` not installed.
   
   In `sdks/python/apache_beam/ml/inference/base.py` 
([L68-L75](https://github.com/apache/beam/blob/cba73417cd23a9bde511bdfc1ed54b82f5978e48/sdks/python/apache_beam/ml/inference/base.py#L68-L75)),
 `import resource` shares a `try` block with the `ModelManager` import:
   
   ```python
   try:
     # pylint: disable=wrong-import-order, wrong-import-position
     import resource
   
     from apache_beam.ml.inference.model_manager import ModelManager
   except ImportError:
     resource = None  # type: ignore[assignment]
     ModelManager = None  # type: ignore[assignment]
   ```
   
   `model_manager.py` imports `torch` (and `scipy`). When `torch` is not 
installed, that import raises `ImportError`. The `except` branch then also sets 
`resource = None`, even on Linux/macOS where `import resource` succeeded. 
(Before #37506 this block only contained `import resource`.)
   
   As a result, 
[`_get_current_process_memory_in_bytes()`](https://github.com/apache/beam/blob/cba73417cd23a9bde511bdfc1ed54b82f5978e48/sdks/python/apache_beam/ml/inference/base.py#L2422-L2437)
 always returns `0` and logs `Resource module is not available for current 
platform, memory usage cannot be fetched.` twice per model load. So the 
`model_byte_size` RunInference metric is always 0 for pipelines running without 
torch, for example with the scikit-learn, XGBoost, ONNX, TensorFlow or 
remote-model handlers. Inference results themselves are not affected.
   
   **Reproduction** (Linux, `pip install apache-beam==2.77.0`, no torch):
   
   ```python
   import importlib
   import resource  # available on Linux
   
   try:
     importlib.import_module("apache_beam.ml.inference.model_manager")
   except ImportError as e:
     print("model_manager import failed:", e)  # No module named 'torch'
   
   from apache_beam.ml.inference import base
   print(base.resource is None)  # True
   print(base._get_current_process_memory_in_bytes())  # 0 (+ the warning)
   
   base.resource = resource
   print(base._get_current_process_memory_in_bytes())  # non-zero
   ```
   
   **Proposed fix:** give each import its own `try` block:
   
   ```python
   try:
     # pylint: disable=wrong-import-order, wrong-import-position
     import resource
   except ImportError:
     resource = None  # type: ignore[assignment]
   
   try:
     # pylint: disable=wrong-import-order, wrong-import-position
     from apache_beam.ml.inference.model_manager import ModelManager
   except ImportError:
     ModelManager = None  # type: ignore[assignment]
   ```
   
   I tried this locally on 2.77.0 (Linux, no torch). `base.resource` stays 
available, memory is measured (non-zero), and `ModelManager` still falls back 
to `None`. The fallback import added in #38936, for the code path that requires 
`ModelManager`, is unaffected.
   
   I'd be happy to open a PR with this change and a unit test.
   
   ### Issue Priority
   
   Priority: 2 (default / most bugs should be filed as P2)
   
   ### Issue Components
   
   - [x] Component: Python SDK
   - [ ] Component: Java SDK
   - [ ] Component: Go SDK
   - [ ] Component: Typescript SDK
   - [ ] Component: IO connector
   - [ ] Component: Beam YAML
   - [ ] Component: Beam examples
   - [ ] Component: Beam playground
   - [ ] Component: Beam katas
   - [ ] Component: Website
   - [ ] Component: Infrastructure
   - [ ] Component: Spark Runner
   - [ ] Component: Flink Runner
   - [ ] Component: Prism Runner
   - [ ] Component: Twister2 Runner
   - [ ] Component: Hazelcast Jet Runner
   - [ ] Component: Google Cloud Dataflow Runner


-- 
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]

Reply via email to