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


##########
connector/connect/server/src/main/scala/org/apache/spark/sql/connect/ml/MLHandler.scala:
##########
@@ -0,0 +1,231 @@
+/*
+ * 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.
+ */
+
+package org.apache.spark.sql.connect.ml
+
+import scala.collection.JavaConverters._
+
+import org.apache.spark.connect.proto
+import org.apache.spark.ml.Model
+import org.apache.spark.ml.param.ParamMap
+import org.apache.spark.sql.DataFrame
+import org.apache.spark.sql.connect.common.LiteralValueProtoConverter
+import org.apache.spark.sql.connect.service.SessionHolder
+
+object MLHandler {
+
+  def handleMlCommand(
+      sessionHolder: SessionHolder,
+      mlCommand: proto.MlCommand): proto.MlCommandResponse = {
+    mlCommand.getMlCommandTypeCase match {
+      case proto.MlCommand.MlCommandTypeCase.FIT =>
+        val fitCommandProto = mlCommand.getFit
+        val estimatorProto = fitCommandProto.getEstimator
+        assert(estimatorProto.getType == proto.MlStage.StageType.ESTIMATOR)
+
+        val algoName = fitCommandProto.getEstimator.getName
+        val algo = AlgorithmRegistry.get(algoName)
+
+        val estimator = algo.initiateEstimator(estimatorProto.getUid)
+        MLUtils.setInstanceParams(estimator, estimatorProto.getParams)
+        val dataset = MLUtils.parseRelationProto(fitCommandProto.getDataset, 
sessionHolder)
+        val model = estimator.fit(dataset).asInstanceOf[Model[_]]
+        val refId = sessionHolder.mlCache.modelCache.register(model, algo)
+
+        proto.MlCommandResponse
+          .newBuilder()
+          .setModelInfo(
+            proto.MlCommandResponse.ModelInfo.newBuilder
+              .setModelRefId(refId)
+              .setModelUid(model.uid))
+          .build()
+
+      case proto.MlCommand.MlCommandTypeCase.FETCH_MODEL_ATTR =>
+        val getModelAttrProto = mlCommand.getFetchModelAttr
+        val modelEntry = 
sessionHolder.mlCache.modelCache.get(getModelAttrProto.getModelRefId)
+        val model = modelEntry._1
+        val algo = modelEntry._2
+        algo.getModelAttr(model, getModelAttrProto.getName).left.get
+
+      case proto.MlCommand.MlCommandTypeCase.FETCH_MODEL_SUMMARY_ATTR =>
+        val getModelSummaryAttrProto = mlCommand.getFetchModelSummaryAttr
+        val modelEntry =
+          
sessionHolder.mlCache.modelCache.get(getModelSummaryAttrProto.getModelRefId)
+        val model = modelEntry._1
+        val algo = modelEntry._2
+        // Create a copied model to avoid concurrently modify model params.
+        val copiedModel = model.copy(ParamMap.empty).asInstanceOf[Model[_]]
+        MLUtils.setInstanceParams(copiedModel, 
getModelSummaryAttrProto.getParams)
+
+        val datasetOpt = if (getModelSummaryAttrProto.hasEvaluationDataset) {
+          val evalDF = MLUtils.parseRelationProto(
+            getModelSummaryAttrProto.getEvaluationDataset,
+            sessionHolder)
+          Some(evalDF)
+        } else None

Review Comment:
   I didn't know that, thansk for the explanation



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


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to