You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@spark.apache.org by "harupy (via GitHub)" <gi...@apache.org> on 2023/03/15 05:00:14 UTC

[GitHub] [spark] harupy commented on a diff in pull request #40297: [SPARK-42412][WIP] Initial PR of Spark connect ML

harupy commented on code in PR #40297:
URL: https://github.com/apache/spark/pull/40297#discussion_r1136535013


##########
python/pyspark/sql/connect/ml/base.py:
##########
@@ -0,0 +1,327 @@
+#
+# 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.
+#
+
+from abc import ABCMeta, abstractmethod
+
+from pyspark.sql.connect.dataframe import DataFrame
+from pyspark.ml import Estimator, Model, Predictor, PredictionModel
+from pyspark.ml.wrapper import _PredictorParams
+from pyspark.ml.util import MLWritable, MLWriter, MLReadable, MLReader
+import pyspark.sql.connect.proto as pb2
+import pyspark.sql.connect.proto.ml_pb2 as ml_pb2
+import pyspark.sql.connect.proto.ml_common_pb2 as ml_common_pb2
+from pyspark.sql.connect.ml.serializer import deserialize, serialize_ml_params
+from pyspark.sql.connect import session as pyspark_session
+from pyspark.sql.connect.plan import LogicalPlan
+
+from pyspark.ml.util import inherit_doc
+from pyspark.ml.util import HasTrainingSummary as PySparkHasTrainingSummary
+
+
+@inherit_doc
+class ClientEstimator(Estimator, metaclass=ABCMeta):
+
+    @classmethod
+    def _algo_name(cls):
+        raise NotImplementedError()
+
+    @classmethod
+    def _model_class(cls):
+        raise NotImplementedError()
+
+    def _fit(self, dataset: DataFrame) -> Model:
+        client = dataset.sparkSession.client
+        dataset_relation = dataset._plan.plan(client)
+        estimator_proto = ml_common_pb2.MlStage(
+            name=self._algo_name(),
+            params=serialize_ml_params(self, client),
+            uid=self.uid,
+            type=ml_common_pb2.MlStage.ESTIMATOR,
+        )
+        fit_command_proto = ml_pb2.MlCommand.Fit(
+            estimator=estimator_proto,
+            dataset=dataset_relation,
+        )
+        req = client._execute_plan_request_with_metadata()
+        req.plan.ml_command.fit.CopyFrom(fit_command_proto)
+
+        resp = client._execute_ml(req)
+        return deserialize(resp, client, clazz=self._model_class())
+
+
+@inherit_doc
+class ClientPredictor(Predictor, ClientEstimator, _PredictorParams, metaclass=ABCMeta):
+    pass
+
+
+@inherit_doc
+class ClientModel(Model, metaclass=ABCMeta):
+
+    ref_id: str = None
+
+    def __del__(self):
+        client = pyspark_session._active_spark_session.client
+        del_model_proto = ml_pb2.MlCommand.DeleteModel(
+            model_ref_id=self.ref_id,
+        )
+        req = client._execute_plan_request_with_metadata()
+        req.plan.ml_command.delete_model.CopyFrom(del_model_proto)
+        client._execute_ml(req)
+
+    @classmethod

Review Comment:
   Can we use `asbstractmethod`?



-- 
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: reviews-unsubscribe@spark.apache.org

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


---------------------------------------------------------------------
To unsubscribe, e-mail: reviews-unsubscribe@spark.apache.org
For additional commands, e-mail: reviews-help@spark.apache.org