jk-shah opened a new issue, #41351:
URL: https://github.com/apache/beam/issues/41351
### What happened?
Two related runtime issues affect custom error handling and `ModelHandler`
implementations in `apache_beam.ml.inference.RunInference`:
1. **`RunInference.with_exception_handling()` docstring vs runtime type
mismatch (`sdks/python/apache_beam/ml/inference/base.py:1795-1806` vs
`transforms/core.py:1624, 2550`)**:
- `RunInference.with_exception_handling()` documents `failed_inferences`,
`failed_preprocessing`, and `failed_postprocessing` as
`PCollection[tuple[failed batch, exception]]`.
- At runtime, `_ExceptionHandlingWrapperDoFn` (`transforms/core.py:2550`)
emits `(element, traceback.format_exc())` where the second tuple element is a
**`str`**, not an `Exception` instance — causing downstream dead-letter
transforms that inspect `exc.args` or `type(exc).__name__` to fail.
- *(Additionally, on batched inference, a single poison-pill element in a
batch of `N` fails all `N` elements and evicts/reloads the entire model in
memory per lines `1829-1830`.)*
2. **Custom `ModelHandler.load_model()` returning a `dict` crashes
`shared.Shared.acquire()` (`sdks/python/apache_beam/utils/shared.py:136`)**:
- Multi-artifact custom `ModelHandler`s commonly return `{'tokenizer':
tok, 'model': mdl}` from `load_model()`.
- `shared.Shared.acquire()` wraps the loaded object directly in
`weakref.ref(result)`, crashing immediately with:
`TypeError: cannot create weak reference to 'dict' object`.
Minimal reproducer:
```python
import apache_beam as beam
from apache_beam.ml.inference.base import ModelHandler, RunInference
class DictModelHandler(ModelHandler):
def load_model(self):
return {'weights': [1.0, 2.0]}
def run_inference(self, batch, model, inference_args=None):
return [sum(model['weights']) * x for x in batch]
with beam.Pipeline() as p:
_ = p | beam.Create([1, 2]) | RunInference(DictModelHandler())
# Raises TypeError: cannot create weak reference to 'dict' object
```
### Cause
`_ExceptionHandlingWrapperDoFn` formats `traceback.format_exc()` as `str`
while `RunInference.with_exception_handling()` documents `exception` objects;
and `shared.py` calls `weakref.ref(result)` without wrapping non-weakrefable
containers or raising an actionable error pointing to
`ModelHandler.load_model()`.
### 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
- [x] Component: Beam ML
- [ ] Component: Beam examples
- [ ] Component: Beam playground
- [ ] Component: Beam katas
- [ ] Component: Website
- [ ] Component: Infrastructure
- [ ] Component: Spark Runner
- [ ] Component: Flink Runner
- [ ] Component: Prism 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]