shikhabhadoria commented on code in PR #41424:
URL: https://github.com/apache/beam/pull/41424#discussion_r4598427040
##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -374,12 +626,467 @@ def inference_output_type(self):
return Any
[email protected]_handler_type('PyTorch')
[email protected]_handler_type('PytorchModelHandlerTensor')
[email protected]_handler_type('PytorchModelHandlerKeyedTensor')
+class PytorchModelHandlerProvider(ModelHandlerProvider):
+ def __init__(
+ self,
+ state_dict_path: Optional[str] = None,
+ model_class: Optional[Union[dict[str, str], Callable[..., Any]]] = None,
+ model_params: Optional[dict[str, Any]] = None,
+ torch_script_model_path: Optional[str] = None,
+ device: str = 'CPU',
+ preprocess: Optional[dict[str, str]] = None,
+ postprocess: Optional[dict[str, str]] = None,
+ inference_fn: Optional[dict[str, str]] = None,
+ min_batch_size: Optional[int] = None,
+ max_batch_size: Optional[int] = None,
+ max_batch_duration_secs: Optional[int] = None,
+ large_model: bool = False,
+ keyed_tensor: bool = False,
+ input_field: Optional[Union[str, list[str]]] = None,
+ **kwargs):
+ if not torch_script_model_path and not (state_dict_path and model_class):
+ raise ValueError(
+ "PyTorch model handler requires either 'torch_script_model_path' or "
+ "both 'state_dict_path' and 'model_class'.")
+ clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+ model_class_obj = (
+ model_class
+ if callable(model_class) else self.parse_processing_transform(
+ model_class, 'model_class') if model_class else None)
+ inference_fn_obj = (
+ self.parse_processing_transform(inference_fn, 'inference_fn')
+ if inference_fn else None)
+
+ def _build_pytorch_handler():
+ try:
+ from apache_beam.ml.inference.pytorch_inference import
PytorchModelHandlerKeyedTensor
+ from apache_beam.ml.inference.pytorch_inference import
PytorchModelHandlerTensor
+ except ImportError:
+ raise ValueError(
+ 'Unable to import PyTorch ModelHandler. Please install torch.')
+ handler_cls = (
+ PytorchModelHandlerKeyedTensor
+ if keyed_tensor else PytorchModelHandlerTensor)
+ extra: dict[str, Any] = {}
+ if inference_fn_obj is not None:
+ extra['inference_fn'] = inference_fn_obj
+ return handler_cls(
+ state_dict_path=state_dict_path,
+ model_class=model_class_obj,
+ model_params=model_params,
+ torch_script_model_path=torch_script_model_path,
+ device=device,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model,
+ **extra,
+ **clean_kwargs)
+
+ _handler = _LazyYamlModelHandler(
+ _build_pytorch_handler,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+ super().__init__(
+ _handler,
+ preprocess,
+ postprocess,
+ input_field=input_field,
+ input_mode='torch')
+
+ @staticmethod
+ def validate(config):
+ if not config or (
+ not config.get('torch_script_model_path') and
+ not (config.get('state_dict_path') and config.get('model_class'))):
+ raise ValueError(
+ "PyTorch model handler requires either 'torch_script_model_path' or "
+ "both 'state_dict_path' and 'model_class'.")
+
+
[email protected]_handler_type('Sklearn')
[email protected]_handler_type('SklearnModelHandlerNumpy')
[email protected]_handler_type('SklearnModelHandlerPandas')
+class SklearnModelHandlerProvider(ModelHandlerProvider):
+ def __init__(
+ self,
+ model_uri: str = '',
+ model_file_type: str = 'pickle',
+ input_data_type: str = 'numpy',
+ preprocess: Optional[dict[str, str]] = None,
+ postprocess: Optional[dict[str, str]] = None,
+ inference_fn: Optional[dict[str, str]] = None,
+ min_batch_size: Optional[int] = None,
+ max_batch_size: Optional[int] = None,
+ max_batch_duration_secs: Optional[int] = None,
+ large_model: bool = False,
+ input_field: Optional[Union[str, list[str]]] = None,
+ **kwargs):
+ if not model_uri:
+ raise ValueError("Sklearn model handler requires 'model_uri'.")
+ clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+ inference_fn_obj = (
+ self.parse_processing_transform(inference_fn, 'inference_fn')
+ if inference_fn else None)
+
+ def _build_sklearn_handler():
+ try:
+ from apache_beam.ml.inference.sklearn_inference import ModelFileType
+ from apache_beam.ml.inference.sklearn_inference import
SklearnModelHandlerNumpy
+ from apache_beam.ml.inference.sklearn_inference import
SklearnModelHandlerPandas
+ except ImportError:
+ raise ValueError(
+ 'Unable to import Sklearn ModelHandler. Please install '
+ 'scikit-learn.')
+ file_type = (
+ ModelFileType.JOBLIB
+ if str(model_file_type).lower() == 'joblib' else
ModelFileType.PICKLE)
+ handler_cls = (
+ SklearnModelHandlerPandas if str(input_data_type).lower() == 'pandas'
+ else SklearnModelHandlerNumpy)
+ extra: dict[str, Any] = {}
+ if inference_fn_obj is not None:
+ extra['inference_fn'] = inference_fn_obj
+ return handler_cls(
+ model_uri=model_uri,
+ model_file_type=file_type,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model,
+ **extra,
+ **clean_kwargs)
+
+ _handler = _LazyYamlModelHandler(
+ _build_sklearn_handler,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+ super().__init__(
+ _handler,
+ preprocess,
+ postprocess,
+ input_field=input_field,
+ input_mode='numpy')
+
+ @staticmethod
+ def validate(config):
+ if not config or not config.get('model_uri'):
+ raise ValueError("Sklearn model handler requires 'model_uri'.")
+
+
[email protected]_handler_type('Custom')
[email protected]_handler_type('CustomModelHandler')
+class CustomModelHandlerProvider(ModelHandlerProvider):
+ def __init__(
+ self,
+ callable: Optional[Union[str, Callable[..., Any]]] = None, # pylint:
disable=redefined-builtin
+ path: Optional[str] = None,
+ name: Optional[str] = None,
+ inference_fn: Optional[Union[dict[str, str], str, Callable[...,
+ Any]]] = None,
+ load_model_fn: Optional[Union[dict[str, str], str, Callable[[],
+ Any]]] =
None,
+ model_kwargs: Optional[dict[str, Any]] = None,
+ preprocess: Optional[dict[str, str]] = None,
+ postprocess: Optional[dict[str, str]] = None,
+ min_batch_size: Optional[int] = None,
+ max_batch_size: Optional[int] = None,
+ max_batch_duration_secs: Optional[int] = None,
+ large_model: bool = False,
+ input_field: Optional[Union[str, list[str]]] = None,
+ **kwargs):
+ if not (callable or (path and name) or inference_fn):
+ raise ValueError(
+ "Custom model handler requires either 'callable', 'path' and 'name',
"
+ "or 'inference_fn'.")
+ clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+ builtins_callable = __builtins__['callable'] if isinstance(
+ __builtins__, dict) else __builtins__.callable
+
+ def _resolve_fn(spec, label):
+ if spec is None:
+ return None
+ if builtins_callable(spec):
+ return spec
+ if isinstance(spec, str):
+ return python_callable.PythonCallableWithSource(spec)
+ if isinstance(spec, dict):
+ return self.parse_processing_transform(spec, label)
+ raise ValueError(f"Invalid {label} specification: {spec}")
+
+ factory_spec = ({
+ 'callable': callable, 'path': path, 'name': name
+ } if (callable or path or name) else None)
+ factory_fn = (
+ callable
+ if builtins_callable(callable) else self.parse_processing_transform(
+ factory_spec, 'custom_model_handler') if factory_spec else None)
+ resolved_inference_fn = _resolve_fn(inference_fn, 'inference_fn')
+ resolved_load_fn = _resolve_fn(load_model_fn, 'load_model_fn')
+
+ def _build_custom_handler():
+ if factory_fn is not None:
+ init_kwargs = dict(model_kwargs or {})
+ init_kwargs.update(clean_kwargs)
+ return (
+ factory_fn if isinstance(factory_fn, ModelHandler) else factory_fn(
+ **init_kwargs))
+ return _FunctionModelHandler(
+ inference_fn=resolved_inference_fn,
+ load_model_fn=resolved_load_fn,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+
+ _handler = _LazyYamlModelHandler(
+ _build_custom_handler,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+ super().__init__(_handler, preprocess, postprocess,
input_field=input_field)
+
+ @staticmethod
+ def validate(config):
+ if not config or not (config.get('callable') or
+ (config.get('path') and config.get('name')) or
+ config.get('inference_fn')):
+ raise ValueError(
+ "Custom model handler requires either 'callable', 'path' and 'name',
"
+ "or 'inference_fn'.")
+
+
+class _SafePreprocessDoFn(beam.DoFn):
+ def __init__(
+ self,
+ preprocess_fn: Callable[[Any], Any],
+ dead_letter_tag: Optional[str] = None):
+ self._preprocess_fn = preprocess_fn
+ self._dead_letter_tag = dead_letter_tag
+
+ def process(self, row: Any, *args, **kwargs):
+ try:
+ yield (row, self._preprocess_fn(row))
+ except Exception as exn:
+ if self._dead_letter_tag:
+ yield pvalue.TaggedOutput(
+ self._dead_letter_tag,
+ (
+ row,
+ (
+ type(exn),
+ repr(exn),
+ traceback.format_exception(*sys.exc_info()))))
+ else:
+ raise
+
+
+class _PostprocessAndAttachDoFn(beam.DoFn):
+ def __init__(
+ self,
+ postprocess_fn: Callable[[Any], Any],
+ output_field: str,
+ flatten_predictions: bool = False,
+ unwrap_prediction_result: bool = False,
+ dead_letter_tag: Optional[str] = None):
+ self._postprocess_fn = postprocess_fn
+ self._output_field = output_field
+ self._flatten_predictions = flatten_predictions
+ self._unwrap_prediction_result = unwrap_prediction_result
+ self._dead_letter_tag = dead_letter_tag
+
+ @staticmethod
+ def _unwrap(pred: Any) -> Any:
+ val = pred.inference if isinstance(pred, PredictionResult) else pred
+ if (isinstance(val, (list, tuple)) and len(val) == 1 and
+ (isinstance(val[0], Mapping) or hasattr(val[0], '_asdict'))):
+ val = val[0]
+ return val
+
+ def process(self, element: tuple[Any, Any], *args, **kwargs):
+ row, raw_outcome = element
+ if self._dead_letter_tag is not None:
+ status, payload = raw_outcome
+ if status == _ERR:
+ yield pvalue.TaggedOutput(self._dead_letter_tag, (row, payload))
+ return
+ raw_pred = payload
+ else:
+ raw_pred = raw_outcome
+
+ try:
+ processed = self._postprocess_fn(raw_pred)
+ row_dict = dict(row._asdict()) if hasattr(row, '_asdict') else dict(row)
+
+ if self._flatten_predictions:
+ val = self._unwrap(processed)
+ if hasattr(val, '_asdict'):
+ val = val._asdict()
+ if isinstance(val, Mapping):
+ for k, v in val.items():
+ row_dict[f"{self._output_field}_{k}"] = _to_python_scalar(v)
+ else:
+ row_dict[self._output_field] = _to_python_scalar(val)
+ else:
+ if self._unwrap_prediction_result:
+ val = self._unwrap(processed)
+ if isinstance(val, Mapping):
+ val = beam.Row(**{k: _to_python_scalar(v) for k, v in val.items()})
+ else:
+ val = _to_python_scalar(val)
+ else:
+ val = processed
+ row_dict[self._output_field] = val
+
+ yield beam.Row(**row_dict)
+ except Exception as exn:
+ if self._dead_letter_tag:
+ yield pvalue.TaggedOutput(
+ self._dead_letter_tag,
+ (
+ row,
+ (
+ type(exn),
+ repr(exn),
+ traceback.format_exception(*sys.exc_info()))))
+ else:
+ raise
+
+
+class _YamlRunInferenceTransform(beam.PTransform):
+ def __init__(
+ self,
+ model_handler_provider: ModelHandlerProvider,
+ output_field: str,
+ inference_args: Optional[dict[str, Any]] = None,
+ flatten_predictions: bool = False,
+ output_type: Optional[Any] = None,
+ declarative_mode: bool = False):
+ self._provider = model_handler_provider
+ self._output_field = output_field
+ self._inference_args = inference_args
+ self._flatten_predictions = flatten_predictions
+ self._output_type = output_type
+ self._declarative_mode = declarative_mode
+ self._exception_handling_args: Optional[dict[str, Any]] = None
Review Comment:
Stripped YAML metadata from `output_type` via
`SafeLineLoader.strip_metadata` in `_YamlRunInferenceTransform.__init__` and
`_parse_type_spec`.
##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -66,20 +78,251 @@ def _list_submodules(package):
except ImportError:
MLTransform = None # type: ignore
+_OK = 'ok'
+_ERR = 'err'
+
+_TYPE_MAP: dict[str, Any] = {
+ 'str': str,
+ 'string': str,
+ 'int': int,
+ 'integer': int,
+ 'float': float,
+ 'double': float,
+ 'number': float,
+ 'bool': bool,
+ 'boolean': bool,
+ 'bytes': bytes,
+ 'any': Any,
+}
+
+
+def _parse_type_spec(spec: Any) -> Any:
+ if spec is None:
+ return Any
+ if isinstance(spec, type) or spec is Any:
+ return spec
+ if isinstance(spec, str):
+ return _TYPE_MAP.get(spec.lower(), Any)
+ if isinstance(spec, Mapping):
+ fields = [(str(k), _parse_type_spec(v)) for k, v in spec.items()]
+ return RowTypeConstraint.from_fields(fields)
+ return Any
+
+
+def _to_python_scalar(val: Any) -> Any:
+ if hasattr(val, 'item') and callable(val.item):
+ try:
+ return val.item()
+ except Exception:
+ pass
+ if hasattr(val, 'tolist') and callable(val.tolist):
+ try:
+ return val.tolist()
+ except Exception:
+ pass
+ return val
+
+
+class _WeakRefModelBox:
+ """Weak-referenceable wrapper for models that do not natively support
weakref."""
+ def __init__(self, model: Any):
+ self.model = model
+
+
+class _FunctionModelHandler(ModelHandler[Any, Any, Any]):
+ """ModelHandler backed by user-supplied load_model_fn and inference_fn."""
+ def __init__(
+ self,
+ inference_fn: Callable[..., Any],
+ load_model_fn: Optional[Callable[[], Any]] = None,
+ **kwargs):
+ super().__init__(**kwargs)
+ self._inference_fn = inference_fn
+ self._load_model_fn = load_model_fn
+
+ def load_model(self) -> Any:
+ if self._load_model_fn is not None:
+ return self._load_model_fn()
+ return None
+
+ def run_inference(
+ self,
+ batch: Sequence[Any],
+ model: Any,
+ inference_args: Optional[dict[str, Any]] = None) -> Iterable[Any]:
+ try:
+ return self._inference_fn(batch, model, inference_args)
+ except TypeError:
Review Comment:
Updated `_FunctionModelHandler` to inspect callable parameter arity via
`inspect.signature` rather than catching `TypeError` around invocation.
##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -374,12 +626,467 @@ def inference_output_type(self):
return Any
[email protected]_handler_type('PyTorch')
[email protected]_handler_type('PytorchModelHandlerTensor')
[email protected]_handler_type('PytorchModelHandlerKeyedTensor')
+class PytorchModelHandlerProvider(ModelHandlerProvider):
+ def __init__(
+ self,
+ state_dict_path: Optional[str] = None,
+ model_class: Optional[Union[dict[str, str], Callable[..., Any]]] = None,
+ model_params: Optional[dict[str, Any]] = None,
+ torch_script_model_path: Optional[str] = None,
+ device: str = 'CPU',
+ preprocess: Optional[dict[str, str]] = None,
+ postprocess: Optional[dict[str, str]] = None,
+ inference_fn: Optional[dict[str, str]] = None,
+ min_batch_size: Optional[int] = None,
+ max_batch_size: Optional[int] = None,
+ max_batch_duration_secs: Optional[int] = None,
+ large_model: bool = False,
+ keyed_tensor: bool = False,
+ input_field: Optional[Union[str, list[str]]] = None,
+ **kwargs):
+ if not torch_script_model_path and not (state_dict_path and model_class):
+ raise ValueError(
+ "PyTorch model handler requires either 'torch_script_model_path' or "
+ "both 'state_dict_path' and 'model_class'.")
+ clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+ model_class_obj = (
+ model_class
+ if callable(model_class) else self.parse_processing_transform(
+ model_class, 'model_class') if model_class else None)
+ inference_fn_obj = (
+ self.parse_processing_transform(inference_fn, 'inference_fn')
+ if inference_fn else None)
+
+ def _build_pytorch_handler():
+ try:
+ from apache_beam.ml.inference.pytorch_inference import
PytorchModelHandlerKeyedTensor
+ from apache_beam.ml.inference.pytorch_inference import
PytorchModelHandlerTensor
+ except ImportError:
+ raise ValueError(
+ 'Unable to import PyTorch ModelHandler. Please install torch.')
+ handler_cls = (
+ PytorchModelHandlerKeyedTensor
+ if keyed_tensor else PytorchModelHandlerTensor)
+ extra: dict[str, Any] = {}
+ if inference_fn_obj is not None:
+ extra['inference_fn'] = inference_fn_obj
+ return handler_cls(
+ state_dict_path=state_dict_path,
+ model_class=model_class_obj,
+ model_params=model_params,
+ torch_script_model_path=torch_script_model_path,
+ device=device,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model,
+ **extra,
+ **clean_kwargs)
+
+ _handler = _LazyYamlModelHandler(
+ _build_pytorch_handler,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+ super().__init__(
+ _handler,
+ preprocess,
+ postprocess,
+ input_field=input_field,
+ input_mode='torch')
+
+ @staticmethod
+ def validate(config):
+ if not config or (
+ not config.get('torch_script_model_path') and
+ not (config.get('state_dict_path') and config.get('model_class'))):
+ raise ValueError(
+ "PyTorch model handler requires either 'torch_script_model_path' or "
+ "both 'state_dict_path' and 'model_class'.")
+
+
[email protected]_handler_type('Sklearn')
[email protected]_handler_type('SklearnModelHandlerNumpy')
[email protected]_handler_type('SklearnModelHandlerPandas')
+class SklearnModelHandlerProvider(ModelHandlerProvider):
+ def __init__(
+ self,
+ model_uri: str = '',
+ model_file_type: str = 'pickle',
+ input_data_type: str = 'numpy',
+ preprocess: Optional[dict[str, str]] = None,
+ postprocess: Optional[dict[str, str]] = None,
+ inference_fn: Optional[dict[str, str]] = None,
+ min_batch_size: Optional[int] = None,
+ max_batch_size: Optional[int] = None,
+ max_batch_duration_secs: Optional[int] = None,
+ large_model: bool = False,
+ input_field: Optional[Union[str, list[str]]] = None,
+ **kwargs):
+ if not model_uri:
+ raise ValueError("Sklearn model handler requires 'model_uri'.")
+ clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+ inference_fn_obj = (
+ self.parse_processing_transform(inference_fn, 'inference_fn')
+ if inference_fn else None)
+
+ def _build_sklearn_handler():
+ try:
+ from apache_beam.ml.inference.sklearn_inference import ModelFileType
+ from apache_beam.ml.inference.sklearn_inference import
SklearnModelHandlerNumpy
+ from apache_beam.ml.inference.sklearn_inference import
SklearnModelHandlerPandas
+ except ImportError:
+ raise ValueError(
+ 'Unable to import Sklearn ModelHandler. Please install '
+ 'scikit-learn.')
+ file_type = (
+ ModelFileType.JOBLIB
+ if str(model_file_type).lower() == 'joblib' else
ModelFileType.PICKLE)
+ handler_cls = (
+ SklearnModelHandlerPandas if str(input_data_type).lower() == 'pandas'
+ else SklearnModelHandlerNumpy)
+ extra: dict[str, Any] = {}
+ if inference_fn_obj is not None:
+ extra['inference_fn'] = inference_fn_obj
+ return handler_cls(
+ model_uri=model_uri,
+ model_file_type=file_type,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model,
+ **extra,
+ **clean_kwargs)
+
+ _handler = _LazyYamlModelHandler(
+ _build_sklearn_handler,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+ super().__init__(
+ _handler,
+ preprocess,
+ postprocess,
+ input_field=input_field,
+ input_mode='numpy')
+
+ @staticmethod
+ def validate(config):
+ if not config or not config.get('model_uri'):
+ raise ValueError("Sklearn model handler requires 'model_uri'.")
+
+
[email protected]_handler_type('Custom')
[email protected]_handler_type('CustomModelHandler')
+class CustomModelHandlerProvider(ModelHandlerProvider):
+ def __init__(
+ self,
+ callable: Optional[Union[str, Callable[..., Any]]] = None, # pylint:
disable=redefined-builtin
+ path: Optional[str] = None,
+ name: Optional[str] = None,
+ inference_fn: Optional[Union[dict[str, str], str, Callable[...,
+ Any]]] = None,
+ load_model_fn: Optional[Union[dict[str, str], str, Callable[[],
+ Any]]] =
None,
+ model_kwargs: Optional[dict[str, Any]] = None,
+ preprocess: Optional[dict[str, str]] = None,
+ postprocess: Optional[dict[str, str]] = None,
+ min_batch_size: Optional[int] = None,
+ max_batch_size: Optional[int] = None,
+ max_batch_duration_secs: Optional[int] = None,
+ large_model: bool = False,
+ input_field: Optional[Union[str, list[str]]] = None,
+ **kwargs):
+ if not (callable or (path and name) or inference_fn):
+ raise ValueError(
+ "Custom model handler requires either 'callable', 'path' and 'name',
"
+ "or 'inference_fn'.")
+ clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+ builtins_callable = __builtins__['callable'] if isinstance(
+ __builtins__, dict) else __builtins__.callable
+
+ def _resolve_fn(spec, label):
+ if spec is None:
+ return None
+ if builtins_callable(spec):
+ return spec
+ if isinstance(spec, str):
+ return python_callable.PythonCallableWithSource(spec)
+ if isinstance(spec, dict):
+ return self.parse_processing_transform(spec, label)
+ raise ValueError(f"Invalid {label} specification: {spec}")
+
+ factory_spec = ({
+ 'callable': callable, 'path': path, 'name': name
+ } if (callable or path or name) else None)
+ factory_fn = (
+ callable
+ if builtins_callable(callable) else self.parse_processing_transform(
+ factory_spec, 'custom_model_handler') if factory_spec else None)
+ resolved_inference_fn = _resolve_fn(inference_fn, 'inference_fn')
+ resolved_load_fn = _resolve_fn(load_model_fn, 'load_model_fn')
+
+ def _build_custom_handler():
+ if factory_fn is not None:
+ init_kwargs = dict(model_kwargs or {})
+ init_kwargs.update(clean_kwargs)
+ return (
+ factory_fn if isinstance(factory_fn, ModelHandler) else factory_fn(
+ **init_kwargs))
+ return _FunctionModelHandler(
+ inference_fn=resolved_inference_fn,
+ load_model_fn=resolved_load_fn,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+
+ _handler = _LazyYamlModelHandler(
+ _build_custom_handler,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+ super().__init__(_handler, preprocess, postprocess,
input_field=input_field)
+
+ @staticmethod
+ def validate(config):
+ if not config or not (config.get('callable') or
+ (config.get('path') and config.get('name')) or
+ config.get('inference_fn')):
+ raise ValueError(
+ "Custom model handler requires either 'callable', 'path' and 'name',
"
+ "or 'inference_fn'.")
+
+
+class _SafePreprocessDoFn(beam.DoFn):
+ def __init__(
+ self,
+ preprocess_fn: Callable[[Any], Any],
+ dead_letter_tag: Optional[str] = None):
+ self._preprocess_fn = preprocess_fn
+ self._dead_letter_tag = dead_letter_tag
+
+ def process(self, row: Any, *args, **kwargs):
+ try:
+ yield (row, self._preprocess_fn(row))
+ except Exception as exn:
+ if self._dead_letter_tag:
+ yield pvalue.TaggedOutput(
+ self._dead_letter_tag,
+ (
+ row,
+ (
+ type(exn),
+ repr(exn),
+ traceback.format_exception(*sys.exc_info()))))
+ else:
+ raise
+
+
+class _PostprocessAndAttachDoFn(beam.DoFn):
+ def __init__(
+ self,
+ postprocess_fn: Callable[[Any], Any],
+ output_field: str,
+ flatten_predictions: bool = False,
+ unwrap_prediction_result: bool = False,
+ dead_letter_tag: Optional[str] = None):
+ self._postprocess_fn = postprocess_fn
+ self._output_field = output_field
+ self._flatten_predictions = flatten_predictions
+ self._unwrap_prediction_result = unwrap_prediction_result
+ self._dead_letter_tag = dead_letter_tag
+
+ @staticmethod
+ def _unwrap(pred: Any) -> Any:
+ val = pred.inference if isinstance(pred, PredictionResult) else pred
+ if (isinstance(val, (list, tuple)) and len(val) == 1 and
+ (isinstance(val[0], Mapping) or hasattr(val[0], '_asdict'))):
+ val = val[0]
+ return val
+
+ def process(self, element: tuple[Any, Any], *args, **kwargs):
+ row, raw_outcome = element
+ if self._dead_letter_tag is not None:
+ status, payload = raw_outcome
+ if status == _ERR:
+ yield pvalue.TaggedOutput(self._dead_letter_tag, (row, payload))
+ return
+ raw_pred = payload
+ else:
+ raw_pred = raw_outcome
+
+ try:
+ processed = self._postprocess_fn(raw_pred)
+ row_dict = dict(row._asdict()) if hasattr(row, '_asdict') else dict(row)
+
+ if self._flatten_predictions:
+ val = self._unwrap(processed)
+ if hasattr(val, '_asdict'):
+ val = val._asdict()
+ if isinstance(val, Mapping):
+ for k, v in val.items():
+ row_dict[f"{self._output_field}_{k}"] = _to_python_scalar(v)
+ else:
+ row_dict[self._output_field] = _to_python_scalar(val)
+ else:
+ if self._unwrap_prediction_result:
+ val = self._unwrap(processed)
+ if isinstance(val, Mapping):
+ val = beam.Row(**{k: _to_python_scalar(v) for k, v in val.items()})
+ else:
+ val = _to_python_scalar(val)
+ else:
+ val = processed
+ row_dict[self._output_field] = val
+
+ yield beam.Row(**row_dict)
+ except Exception as exn:
+ if self._dead_letter_tag:
+ yield pvalue.TaggedOutput(
+ self._dead_letter_tag,
+ (
+ row,
+ (
+ type(exn),
+ repr(exn),
+ traceback.format_exception(*sys.exc_info()))))
+ else:
+ raise
+
+
+class _YamlRunInferenceTransform(beam.PTransform):
+ def __init__(
+ self,
+ model_handler_provider: ModelHandlerProvider,
+ output_field: str,
+ inference_args: Optional[dict[str, Any]] = None,
+ flatten_predictions: bool = False,
+ output_type: Optional[Any] = None,
+ declarative_mode: bool = False):
+ self._provider = model_handler_provider
+ self._output_field = output_field
+ self._inference_args = inference_args
+ self._flatten_predictions = flatten_predictions
+ self._output_type = output_type
+ self._declarative_mode = declarative_mode
+ self._exception_handling_args: Optional[dict[str, Any]] = None
+
+ def with_exception_handling(self, **kwargs):
+ self._exception_handling_args = kwargs
+ return self
+
+ def _output_fields_for_inference(self) -> list[tuple[str, Any]]:
+ if self._flatten_predictions:
+ if isinstance(self._output_type, Mapping):
+ return [(f"{self._output_field}_{k}", _parse_type_spec(v))
+ for k, v in self._output_type.items()]
+ return [
+ (f"{self._output_field}_label", str),
+ (f"{self._output_field}_score", float),
+ ]
+ if self._output_type is not None:
+ return [(self._output_field, _parse_type_spec(self._output_type))]
+ return [(self._output_field, self._provider.inference_output_type())]
+
+ def infer_output_type(self, input_type):
Review Comment:
Updated `_output_fields_for_inference` to return `Any` when
`self._declarative_mode and not self._provider._has_custom_postprocess` unwraps
`PredictionResult`.
##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -66,20 +78,251 @@ def _list_submodules(package):
except ImportError:
MLTransform = None # type: ignore
+_OK = 'ok'
+_ERR = 'err'
+
+_TYPE_MAP: dict[str, Any] = {
+ 'str': str,
+ 'string': str,
+ 'int': int,
+ 'integer': int,
+ 'float': float,
+ 'double': float,
+ 'number': float,
+ 'bool': bool,
+ 'boolean': bool,
+ 'bytes': bytes,
+ 'any': Any,
+}
+
+
+def _parse_type_spec(spec: Any) -> Any:
+ if spec is None:
+ return Any
+ if isinstance(spec, type) or spec is Any:
+ return spec
+ if isinstance(spec, str):
+ return _TYPE_MAP.get(spec.lower(), Any)
+ if isinstance(spec, Mapping):
+ fields = [(str(k), _parse_type_spec(v)) for k, v in spec.items()]
+ return RowTypeConstraint.from_fields(fields)
+ return Any
+
+
+def _to_python_scalar(val: Any) -> Any:
+ if hasattr(val, 'item') and callable(val.item):
+ try:
+ return val.item()
+ except Exception:
+ pass
+ if hasattr(val, 'tolist') and callable(val.tolist):
+ try:
+ return val.tolist()
+ except Exception:
+ pass
+ return val
+
+
+class _WeakRefModelBox:
+ """Weak-referenceable wrapper for models that do not natively support
weakref."""
+ def __init__(self, model: Any):
+ self.model = model
+
+
+class _FunctionModelHandler(ModelHandler[Any, Any, Any]):
+ """ModelHandler backed by user-supplied load_model_fn and inference_fn."""
+ def __init__(
+ self,
+ inference_fn: Callable[..., Any],
+ load_model_fn: Optional[Callable[[], Any]] = None,
+ **kwargs):
+ super().__init__(**kwargs)
+ self._inference_fn = inference_fn
+ self._load_model_fn = load_model_fn
+
+ def load_model(self) -> Any:
+ if self._load_model_fn is not None:
+ return self._load_model_fn()
+ return None
+
+ def run_inference(
+ self,
+ batch: Sequence[Any],
+ model: Any,
+ inference_args: Optional[dict[str, Any]] = None) -> Iterable[Any]:
+ try:
+ return self._inference_fn(batch, model, inference_args)
+ except TypeError:
+ try:
+ return self._inference_fn(batch, model)
+ except TypeError:
+ return [self._inference_fn(item) for item in batch]
+
+
+class _LazyYamlModelHandler(ModelHandler[Any, Any, Any]):
+ """Defers heavy ModelHandler instantiation and imports to worker setup()."""
+ def __init__(
+ self,
+ handler_factory: Callable[[], ModelHandler[Any, Any, Any]],
+ *,
+ min_batch_size: Optional[int] = None,
+ max_batch_size: Optional[int] = None,
+ max_batch_duration_secs: Optional[int] = None,
+ large_model: bool = False,
+ model_copies: Optional[int] = None,
+ catch_errors: bool = False,
+ **kwargs):
+ super().__init__(
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model,
+ model_copies=model_copies,
+ **kwargs)
+ self._handler_factory = handler_factory
+ self._underlying: Optional[ModelHandler[Any, Any, Any]] = None
+ self._catch_errors = catch_errors
+
+ def with_error_catching(self, catch_errors: bool = True):
+ self._catch_errors = catch_errors
+ return self
+
+ def _get_underlying(self) -> ModelHandler[Any, Any, Any]:
Review Comment:
Delegated worker-side `set_environment_vars`, `get_metrics_namespace`,
`get_num_bytes`, and `update_model_path` on `_LazyYamlModelHandler` to the
underlying handler.
##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -374,12 +626,467 @@ def inference_output_type(self):
return Any
[email protected]_handler_type('PyTorch')
[email protected]_handler_type('PytorchModelHandlerTensor')
[email protected]_handler_type('PytorchModelHandlerKeyedTensor')
+class PytorchModelHandlerProvider(ModelHandlerProvider):
+ def __init__(
+ self,
+ state_dict_path: Optional[str] = None,
+ model_class: Optional[Union[dict[str, str], Callable[..., Any]]] = None,
+ model_params: Optional[dict[str, Any]] = None,
+ torch_script_model_path: Optional[str] = None,
+ device: str = 'CPU',
+ preprocess: Optional[dict[str, str]] = None,
+ postprocess: Optional[dict[str, str]] = None,
+ inference_fn: Optional[dict[str, str]] = None,
+ min_batch_size: Optional[int] = None,
+ max_batch_size: Optional[int] = None,
+ max_batch_duration_secs: Optional[int] = None,
+ large_model: bool = False,
+ keyed_tensor: bool = False,
+ input_field: Optional[Union[str, list[str]]] = None,
+ **kwargs):
+ if not torch_script_model_path and not (state_dict_path and model_class):
+ raise ValueError(
+ "PyTorch model handler requires either 'torch_script_model_path' or "
+ "both 'state_dict_path' and 'model_class'.")
+ clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+ model_class_obj = (
+ model_class
+ if callable(model_class) else self.parse_processing_transform(
+ model_class, 'model_class') if model_class else None)
+ inference_fn_obj = (
+ self.parse_processing_transform(inference_fn, 'inference_fn')
+ if inference_fn else None)
+
+ def _build_pytorch_handler():
+ try:
+ from apache_beam.ml.inference.pytorch_inference import
PytorchModelHandlerKeyedTensor
+ from apache_beam.ml.inference.pytorch_inference import
PytorchModelHandlerTensor
+ except ImportError:
+ raise ValueError(
+ 'Unable to import PyTorch ModelHandler. Please install torch.')
+ handler_cls = (
+ PytorchModelHandlerKeyedTensor
+ if keyed_tensor else PytorchModelHandlerTensor)
+ extra: dict[str, Any] = {}
+ if inference_fn_obj is not None:
+ extra['inference_fn'] = inference_fn_obj
+ return handler_cls(
+ state_dict_path=state_dict_path,
+ model_class=model_class_obj,
+ model_params=model_params,
+ torch_script_model_path=torch_script_model_path,
+ device=device,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model,
+ **extra,
+ **clean_kwargs)
+
+ _handler = _LazyYamlModelHandler(
+ _build_pytorch_handler,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+ super().__init__(
+ _handler,
+ preprocess,
+ postprocess,
+ input_field=input_field,
+ input_mode='torch')
+
Review Comment:
Registered dedicated `PytorchKeyedTensorModelHandlerProvider`
(`keyed_tensor=True`, `input_mode="torch_keyed"`) and
`SklearnPandasModelHandlerProvider` (`input_data_type="pandas"`,
`input_mode="pandas"`) subclasses and added `torch_keyed` and `pandas`
extraction modes in `_make_input_field_preprocess_fn`.
##########
sdks/python/apache_beam/yaml/yaml_ml.py:
##########
@@ -374,12 +626,467 @@ def inference_output_type(self):
return Any
[email protected]_handler_type('PyTorch')
[email protected]_handler_type('PytorchModelHandlerTensor')
[email protected]_handler_type('PytorchModelHandlerKeyedTensor')
+class PytorchModelHandlerProvider(ModelHandlerProvider):
+ def __init__(
+ self,
+ state_dict_path: Optional[str] = None,
+ model_class: Optional[Union[dict[str, str], Callable[..., Any]]] = None,
+ model_params: Optional[dict[str, Any]] = None,
+ torch_script_model_path: Optional[str] = None,
+ device: str = 'CPU',
+ preprocess: Optional[dict[str, str]] = None,
+ postprocess: Optional[dict[str, str]] = None,
+ inference_fn: Optional[dict[str, str]] = None,
+ min_batch_size: Optional[int] = None,
+ max_batch_size: Optional[int] = None,
+ max_batch_duration_secs: Optional[int] = None,
+ large_model: bool = False,
+ keyed_tensor: bool = False,
+ input_field: Optional[Union[str, list[str]]] = None,
+ **kwargs):
+ if not torch_script_model_path and not (state_dict_path and model_class):
+ raise ValueError(
+ "PyTorch model handler requires either 'torch_script_model_path' or "
+ "both 'state_dict_path' and 'model_class'.")
+ clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+ model_class_obj = (
+ model_class
+ if callable(model_class) else self.parse_processing_transform(
+ model_class, 'model_class') if model_class else None)
+ inference_fn_obj = (
+ self.parse_processing_transform(inference_fn, 'inference_fn')
+ if inference_fn else None)
+
+ def _build_pytorch_handler():
+ try:
+ from apache_beam.ml.inference.pytorch_inference import
PytorchModelHandlerKeyedTensor
+ from apache_beam.ml.inference.pytorch_inference import
PytorchModelHandlerTensor
+ except ImportError:
+ raise ValueError(
+ 'Unable to import PyTorch ModelHandler. Please install torch.')
+ handler_cls = (
+ PytorchModelHandlerKeyedTensor
+ if keyed_tensor else PytorchModelHandlerTensor)
+ extra: dict[str, Any] = {}
+ if inference_fn_obj is not None:
+ extra['inference_fn'] = inference_fn_obj
+ return handler_cls(
+ state_dict_path=state_dict_path,
+ model_class=model_class_obj,
+ model_params=model_params,
+ torch_script_model_path=torch_script_model_path,
+ device=device,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model,
+ **extra,
+ **clean_kwargs)
+
+ _handler = _LazyYamlModelHandler(
+ _build_pytorch_handler,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+ super().__init__(
+ _handler,
+ preprocess,
+ postprocess,
+ input_field=input_field,
+ input_mode='torch')
+
+ @staticmethod
+ def validate(config):
+ if not config or (
+ not config.get('torch_script_model_path') and
+ not (config.get('state_dict_path') and config.get('model_class'))):
+ raise ValueError(
+ "PyTorch model handler requires either 'torch_script_model_path' or "
+ "both 'state_dict_path' and 'model_class'.")
+
+
[email protected]_handler_type('Sklearn')
[email protected]_handler_type('SklearnModelHandlerNumpy')
[email protected]_handler_type('SklearnModelHandlerPandas')
+class SklearnModelHandlerProvider(ModelHandlerProvider):
+ def __init__(
+ self,
+ model_uri: str = '',
+ model_file_type: str = 'pickle',
+ input_data_type: str = 'numpy',
+ preprocess: Optional[dict[str, str]] = None,
+ postprocess: Optional[dict[str, str]] = None,
+ inference_fn: Optional[dict[str, str]] = None,
+ min_batch_size: Optional[int] = None,
+ max_batch_size: Optional[int] = None,
+ max_batch_duration_secs: Optional[int] = None,
+ large_model: bool = False,
+ input_field: Optional[Union[str, list[str]]] = None,
+ **kwargs):
+ if not model_uri:
+ raise ValueError("Sklearn model handler requires 'model_uri'.")
+ clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+ inference_fn_obj = (
+ self.parse_processing_transform(inference_fn, 'inference_fn')
+ if inference_fn else None)
+
+ def _build_sklearn_handler():
+ try:
+ from apache_beam.ml.inference.sklearn_inference import ModelFileType
+ from apache_beam.ml.inference.sklearn_inference import
SklearnModelHandlerNumpy
+ from apache_beam.ml.inference.sklearn_inference import
SklearnModelHandlerPandas
+ except ImportError:
+ raise ValueError(
+ 'Unable to import Sklearn ModelHandler. Please install '
+ 'scikit-learn.')
+ file_type = (
+ ModelFileType.JOBLIB
+ if str(model_file_type).lower() == 'joblib' else
ModelFileType.PICKLE)
+ handler_cls = (
+ SklearnModelHandlerPandas if str(input_data_type).lower() == 'pandas'
+ else SklearnModelHandlerNumpy)
+ extra: dict[str, Any] = {}
+ if inference_fn_obj is not None:
+ extra['inference_fn'] = inference_fn_obj
+ return handler_cls(
+ model_uri=model_uri,
+ model_file_type=file_type,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model,
+ **extra,
+ **clean_kwargs)
+
+ _handler = _LazyYamlModelHandler(
+ _build_sklearn_handler,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+ super().__init__(
+ _handler,
+ preprocess,
+ postprocess,
+ input_field=input_field,
+ input_mode='numpy')
+
+ @staticmethod
+ def validate(config):
+ if not config or not config.get('model_uri'):
+ raise ValueError("Sklearn model handler requires 'model_uri'.")
+
+
[email protected]_handler_type('Custom')
[email protected]_handler_type('CustomModelHandler')
+class CustomModelHandlerProvider(ModelHandlerProvider):
+ def __init__(
+ self,
+ callable: Optional[Union[str, Callable[..., Any]]] = None, # pylint:
disable=redefined-builtin
+ path: Optional[str] = None,
+ name: Optional[str] = None,
+ inference_fn: Optional[Union[dict[str, str], str, Callable[...,
+ Any]]] = None,
+ load_model_fn: Optional[Union[dict[str, str], str, Callable[[],
+ Any]]] =
None,
+ model_kwargs: Optional[dict[str, Any]] = None,
+ preprocess: Optional[dict[str, str]] = None,
+ postprocess: Optional[dict[str, str]] = None,
+ min_batch_size: Optional[int] = None,
+ max_batch_size: Optional[int] = None,
+ max_batch_duration_secs: Optional[int] = None,
+ large_model: bool = False,
+ input_field: Optional[Union[str, list[str]]] = None,
+ **kwargs):
+ if not (callable or (path and name) or inference_fn):
+ raise ValueError(
+ "Custom model handler requires either 'callable', 'path' and 'name',
"
+ "or 'inference_fn'.")
+ clean_kwargs = {k: v for k, v in kwargs.items() if not k.startswith('_')}
+ builtins_callable = __builtins__['callable'] if isinstance(
+ __builtins__, dict) else __builtins__.callable
+
+ def _resolve_fn(spec, label):
+ if spec is None:
+ return None
+ if builtins_callable(spec):
+ return spec
+ if isinstance(spec, str):
+ return python_callable.PythonCallableWithSource(spec)
+ if isinstance(spec, dict):
+ return self.parse_processing_transform(spec, label)
+ raise ValueError(f"Invalid {label} specification: {spec}")
+
+ factory_spec = ({
+ 'callable': callable, 'path': path, 'name': name
+ } if (callable or path or name) else None)
+ factory_fn = (
+ callable
+ if builtins_callable(callable) else self.parse_processing_transform(
+ factory_spec, 'custom_model_handler') if factory_spec else None)
+ resolved_inference_fn = _resolve_fn(inference_fn, 'inference_fn')
+ resolved_load_fn = _resolve_fn(load_model_fn, 'load_model_fn')
+
+ def _build_custom_handler():
+ if factory_fn is not None:
+ init_kwargs = dict(model_kwargs or {})
+ init_kwargs.update(clean_kwargs)
+ return (
+ factory_fn if isinstance(factory_fn, ModelHandler) else factory_fn(
+ **init_kwargs))
+ return _FunctionModelHandler(
+ inference_fn=resolved_inference_fn,
+ load_model_fn=resolved_load_fn,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+
+ _handler = _LazyYamlModelHandler(
+ _build_custom_handler,
+ min_batch_size=min_batch_size,
+ max_batch_size=max_batch_size,
+ max_batch_duration_secs=max_batch_duration_secs,
+ large_model=large_model)
+ super().__init__(_handler, preprocess, postprocess,
input_field=input_field)
+
+ @staticmethod
+ def validate(config):
+ if not config or not (config.get('callable') or
+ (config.get('path') and config.get('name')) or
+ config.get('inference_fn')):
+ raise ValueError(
+ "Custom model handler requires either 'callable', 'path' and 'name',
"
+ "or 'inference_fn'.")
+
+
+class _SafePreprocessDoFn(beam.DoFn):
+ def __init__(
+ self,
+ preprocess_fn: Callable[[Any], Any],
+ dead_letter_tag: Optional[str] = None):
+ self._preprocess_fn = preprocess_fn
+ self._dead_letter_tag = dead_letter_tag
+
+ def process(self, row: Any, *args, **kwargs):
+ try:
+ yield (row, self._preprocess_fn(row))
+ except Exception as exn:
+ if self._dead_letter_tag:
+ yield pvalue.TaggedOutput(
+ self._dead_letter_tag,
+ (
+ row,
+ (
+ type(exn),
+ repr(exn),
+ traceback.format_exception(*sys.exc_info()))))
+ else:
+ raise
+
+
+class _PostprocessAndAttachDoFn(beam.DoFn):
+ def __init__(
+ self,
+ postprocess_fn: Callable[[Any], Any],
+ output_field: str,
+ flatten_predictions: bool = False,
+ unwrap_prediction_result: bool = False,
+ dead_letter_tag: Optional[str] = None):
+ self._postprocess_fn = postprocess_fn
+ self._output_field = output_field
+ self._flatten_predictions = flatten_predictions
+ self._unwrap_prediction_result = unwrap_prediction_result
+ self._dead_letter_tag = dead_letter_tag
+
+ @staticmethod
+ def _unwrap(pred: Any) -> Any:
+ val = pred.inference if isinstance(pred, PredictionResult) else pred
+ if (isinstance(val, (list, tuple)) and len(val) == 1 and
+ (isinstance(val[0], Mapping) or hasattr(val[0], '_asdict'))):
+ val = val[0]
+ return val
+
+ def process(self, element: tuple[Any, Any], *args, **kwargs):
+ row, raw_outcome = element
+ if self._dead_letter_tag is not None:
+ status, payload = raw_outcome
+ if status == _ERR:
+ yield pvalue.TaggedOutput(self._dead_letter_tag, (row, payload))
+ return
+ raw_pred = payload
+ else:
+ raw_pred = raw_outcome
+
+ try:
+ processed = self._postprocess_fn(raw_pred)
+ row_dict = dict(row._asdict()) if hasattr(row, '_asdict') else dict(row)
+
+ if self._flatten_predictions:
+ val = self._unwrap(processed)
+ if hasattr(val, '_asdict'):
+ val = val._asdict()
+ if isinstance(val, Mapping):
+ for k, v in val.items():
+ row_dict[f"{self._output_field}_{k}"] = _to_python_scalar(v)
+ else:
+ row_dict[self._output_field] = _to_python_scalar(val)
+ else:
+ if self._unwrap_prediction_result:
+ val = self._unwrap(processed)
+ if isinstance(val, Mapping):
+ val = beam.Row(**{k: _to_python_scalar(v) for k, v in val.items()})
+ else:
+ val = _to_python_scalar(val)
+ else:
+ val = processed
+ row_dict[self._output_field] = val
+
+ yield beam.Row(**row_dict)
+ except Exception as exn:
+ if self._dead_letter_tag:
+ yield pvalue.TaggedOutput(
+ self._dead_letter_tag,
+ (
+ row,
+ (
+ type(exn),
+ repr(exn),
+ traceback.format_exception(*sys.exc_info()))))
+ else:
+ raise
+
+
+class _YamlRunInferenceTransform(beam.PTransform):
+ def __init__(
+ self,
+ model_handler_provider: ModelHandlerProvider,
+ output_field: str,
+ inference_args: Optional[dict[str, Any]] = None,
+ flatten_predictions: bool = False,
+ output_type: Optional[Any] = None,
+ declarative_mode: bool = False):
+ self._provider = model_handler_provider
+ self._output_field = output_field
+ self._inference_args = inference_args
+ self._flatten_predictions = flatten_predictions
+ self._output_type = output_type
+ self._declarative_mode = declarative_mode
+ self._exception_handling_args: Optional[dict[str, Any]] = None
+
+ def with_exception_handling(self, **kwargs):
+ self._exception_handling_args = kwargs
+ return self
+
+ def _output_fields_for_inference(self) -> list[tuple[str, Any]]:
+ if self._flatten_predictions:
+ if isinstance(self._output_type, Mapping):
+ return [(f"{self._output_field}_{k}", _parse_type_spec(v))
+ for k, v in self._output_type.items()]
+ return [
+ (f"{self._output_field}_label", str),
+ (f"{self._output_field}_score", float),
+ ]
+ if self._output_type is not None:
+ return [(self._output_field, _parse_type_spec(self._output_type))]
+ return [(self._output_field, self._provider.inference_output_type())]
+
+ def infer_output_type(self, input_type):
+ try:
+ if isinstance(input_type, RowTypeConstraint):
+ base_fields = list(input_type._fields)
+ else:
+ base_fields = list(named_fields_from_element_type(input_type))
+ except Exception:
+ return typehints.Any
+ if not base_fields:
+ return typehints.Any
+ return RowTypeConstraint.from_fields(
+ base_fields + self._output_fields_for_inference())
+
Review Comment:
Filtered out overwritten field names from `base_fields` before constructing
`RowTypeConstraint.from_fields`.
--
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]