You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@beam.apache.org by "AnandInguva (via GitHub)" <gi...@apache.org> on 2023/02/06 04:20:35 UTC

[GitHub] [beam] AnandInguva commented on a diff in pull request #24965: XGBoost modelhandler implementation

AnandInguva commented on code in PR #24965:
URL: https://github.com/apache/beam/pull/24965#discussion_r1096930767


##########
sdks/python/apache_beam/ml/inference/xgboost_inference.py:
##########
@@ -0,0 +1,370 @@
+#
+# Licensed to the Apache Software Foundation (ASF) under one or more
+# contributor license agreements.  See the NOTICE file distributed with
+# this work for additional information regarding copyright ownership.
+# The ASF licenses this file to You under the Apache License, Version 2.0
+# (the "License"); you may not use this file except in compliance with
+# the License.  You may obtain a copy of the License at
+#
+#    http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+#
+
+import sys
+from abc import ABC
+from typing import Any
+from typing import Callable
+from typing import Dict
+from typing import Iterable
+from typing import Optional
+from typing import Sequence
+from typing import Union
+
+import datatable
+import numpy
+import pandas
+import scipy
+import xgboost
+
+from apache_beam.io.filesystems import FileSystems
+from apache_beam.ml.inference.base import ExampleT
+from apache_beam.ml.inference.base import ModelHandler
+from apache_beam.ml.inference.base import ModelT
+from apache_beam.ml.inference.base import PredictionResult
+from apache_beam.ml.inference.base import PredictionT
+
+__all__ = [
+    'XGBoostModelHandler',
+    'XGBoostModelHandlerNumpy',
+    'XGBoostModelHandlerPandas',
+    'XGBoostModelHandlerSciPy',
+    'XGBoostModelHandlerDatatable'
+]
+
+XGBoostInferenceFn = Callable[[
+    Sequence[object],
+    Union[xgboost.Booster, xgboost.XGBModel],
+    Optional[Dict[str, Any]]
+],
+                              Iterable[PredictionResult]]
+
+
+def default_xgboost_inference_fn(
+    batch: Sequence[object],
+    model: Union[xgboost.Booster, xgboost.XGBModel],
+    inference_args: Optional[Dict[str, Any]] = None):
+  inference_args = {} if not inference_args else inference_args
+
+  if type(model) == xgboost.Booster:
+    batch = (xgboost.DMatrix(array) for array in batch)
+  predictions = [model.predict(el, **inference_args) for el in batch]
+
+  return [PredictionResult(x, y) for x, y in zip(batch, predictions)]
+
+
+class XGBoostModelHandler(ModelHandler[ExampleT, PredictionT, ModelT], ABC):
+
+  def __init__(
+      self,
+      model_class: Union[Callable[..., xgboost.Booster],
+                         Callable[..., xgboost.XGBModel]],
+      model_state: str,
+      inference_fn: XGBoostInferenceFn = default_xgboost_inference_fn):
+    """Implementation of the ModelHandler interface for XGBoost.
+
+    Example Usage::
+
+        pcoll | RunInference(
+                    XGBoostModelHandler(
+                        model_class="XGBoost Model Class",
+                        model_state="my_model_state.json")))
+
+    See https://xgboost.readthedocs.io/en/stable/tutorials/saving_model.html
+    for details
+
+    Args:
+    model_class: class of the XGBoost model that defines the model
+      structure.
+    model_state: path to a json file that contains the model's
+      configuration.
+    inference_fn: the inference function to use during RunInference.
+      default=default_xgboost_inference_fn
+
+    **Supported Versions:** RunInference APIs in Apache Beam have been tested
+    with PyTorch 1.6.0 and 1.7.0

Review Comment:
   ```suggestion
       with XGBoost 1.6.0 and 1.7.0
   ```
   



-- 
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: github-unsubscribe@beam.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org