damccorm opened a new pull request, #41847:
URL: https://github.com/apache/beam/pull/41847

   Note: this PR was auto-generated by tooling. Before moving it out of draft, 
the author has done a full review of the PR, specifically evaluating it on the 
following criteria:
   
   - The PR is an appropriate fix for the issue (e.g. we're not changing a test 
to avoid a bug)
   - The PR is a simple fix to the issue (e.g. we're not adding infra when we 
could just modify a test)
   - The PR is not covering for other problems (e.g. if we're bumping timeouts, 
it is not because we're covering for a performance problem)
     
   Full details of the methodology of generation and review can be found here - 
https://docs.google.com/document/d/14lj7ECVov4T4EdwmIzNlWrRGE3lLDMOwX5xUMGzaLoc/edit?usp=sharing
   
   If you have concerns with the PR or with the underlying methodology used to 
generate this, please comment below - they will be addressed by the author not 
the tooling :)
   
   ## Summary
   Fixes `RuntimeError: Found no NVIDIA driver on your system` in 
`PytorchModelHandlerTensor` and `PytorchModelHandlerKeyedTensor` when 
`device='GPU'` falls back to CPU on a shared model across worker threads or 
processes (observed in 
`PytorchRunInferencePipelineTest.test_gpu_auto_convert_to_cpu` in [PreCommit 
Python ML 
#46676989108](https://github.com/apache/beam/actions/runs/46676989108), build 
scan https://develocity.apache.org/s/uo2dq54pe5nok).
   
   ## Root Cause
   `PytorchModelHandlerTensor.load_model()` and 
`PytorchModelHandlerKeyedTensor.load_model()` in 
`sdks/python/apache_beam/ml/inference/pytorch_inference.py` mutate 
`self._device = device` when `_load_model()` falls back from CUDA to CPU.
   
   However, `RunInference` shares the loaded model instance across worker 
threads via `Shared.acquire()` (and across processes via 
`MultiProcessShared(..., spawn_process=True)` when `large_model=True`). Only 
the single unpickled `ModelHandler` instance that executes `load_model()` 
updates its `self._device` to `torch.device('cpu')`. When a second bundle runs 
concurrently on another worker thread (e.g., `bundle inst003` in Prism loopback 
or multi-threaded Dataflow FnHarness workers), its sibling `ModelHandler` 
receives the cached CPU model without calling `load_model()`, keeps 
`self._device == torch.device('cuda')`, and fails in 
`_convert_to_device(batched_tensors, device)` 
(`examples.to(torch.device('cuda'))`).
   
   ## Changes
   - **`sdks/python/apache_beam/ml/inference/pytorch_inference.py`**:
     - Added `_resolve_device(model, device)`: when `device == 
torch.device('cuda')`, inspects the first parameter or buffer of 
`torch.nn.Module` (including TorchScript `ScriptModule`) and returns its device 
if non-CUDA (preserving `torch.device('cuda')` when already on CUDA); for 
stateless modules or non-`torch.nn.Module` proxies (such as 
`MultiProcessShared` `BaseProxy`, avoiding RPC generator pickling on 
`.parameters()`), falls back to `torch.device('cpu')` when `not 
_cuda_device_is_usable()`.
     - Updated `PytorchModelHandlerTensor.run_inference()` and 
`PytorchModelHandlerKeyedTensor.run_inference()` to resolve and cache 
`self._device = _resolve_device(model, self._device)`.
   - **`sdks/python/apache_beam/ml/inference/pytorch_inference_test.py`**:
     - Added `test_run_inference_gpu_handler_with_shared_cpu_model`, 
`test_run_inference_keyed_gpu_handler_with_shared_cpu_model`, and 
`test_resolve_device_stateless_and_proxy_models`.
   
   ## Verification
   - Verified syntax with `python3 -m py_compile` and ran 7 local stubbed unit 
assertions covering CPU parameters, CPU buffers, CUDA parameters, stateless 
modules, non-`nn.Module` proxies, and both handler `run_inference` methods 
(system Python lacks `torch`).
   - Triggered `.github/workflows/beam_PreCommit_Python_ML.yml` on this branch.
   
   ## Related Failures
   - https://github.com/apache/beam/actions/runs/46676989108
   
   ### Verification
   - Triggered `.github/workflows/beam_PreCommit_Python_ML.yml` workflow run on 
branch: [`46693137445`](https://github.com/apache/beam/actions/runs/46693137445)
   


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