rithwik-db commented on code in PR #39146:
URL: https://github.com/apache/spark/pull/39146#discussion_r1063008896


##########
python/pyspark/ml/torch/tests/test_distributor.py:
##########
@@ -0,0 +1,195 @@
+#
+# 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 pyspark.testing.sqlutils import ReusedSQLTestCase
+import os
+from pyspark import SparkConf, SparkContext
+from pyspark.ml.torch.distributor import TorchDistributor
+from pyspark.sql import SparkSession
+from pyspark.testing.utils import SPARK_HOME
+import stat
+import tempfile
+import unittest
+
+
+class TorchDistributorBaselineUnitTests(unittest.TestCase):
+    def setUp(self) -> None:
+        conf = SparkConf()
+        self.sc = SparkContext("local[4]", conf=conf)
+        self.spark = SparkSession(self.sc)
+
+    def tearDown(self) -> None:
+        self.spark.stop()
+        self.sc.stop()
+
+    def test_validate_correct_inputs(self) -> None:
+        inputs = [
+            ("pytorch", 1, True, False),
+            ("pytorch", 100, True, False),
+            ("pytorch-lightning", 1, False, False),
+            ("pytorch-lightning", 100, False, False),
+        ]
+        for framework, num_processes, local_mode, use_gpu in inputs:
+            with self.subTest():
+                TorchDistributor(framework, num_processes, local_mode, use_gpu)
+
+    def test_validate_incorrect_inputs(self) -> None:
+        inputs = [
+            ("tensorflow", 1, True, False, ValueError, "framework"),
+            ("pytroch", 100, True, False, ValueError, "framework"),
+            ("pytorchlightning", 1, False, False, ValueError, "framework"),
+            ("pytorch-lightning", 0, False, False, ValueError, "positive"),
+        ]
+        for framework, num_processes, local_mode, use_gpu, error, message in 
inputs:
+            with self.subTest():
+                with self.assertRaisesRegex(error, message):
+                    TorchDistributor(framework, num_processes, local_mode, 
use_gpu)
+
+    def test_encryption_passes(self) -> None:
+        inputs = [
+            ("spark.ssl.enabled", "false", 
"pytorch.spark.distributor.ignoreSsl", "true"),
+            ("spark.ssl.enabled", "false", 
"pytorch.spark.distributor.ignoreSsl", "false"),
+            ("spark.ssl.enabled", "true", 
"pytorch.spark.distributor.ignoreSsl", "true"),
+        ]
+        for ssl_conf_key, ssl_conf_value, pytorch_conf_key, pytorch_conf_value 
in inputs:
+            with self.subTest():
+                self.spark.sparkContext._conf.set(ssl_conf_key, ssl_conf_value)
+                self.spark.sparkContext._conf.set(pytorch_conf_key, 
pytorch_conf_value)
+                distributor = TorchDistributor("pytorch", 1, True, False)
+                distributor._check_encryption()
+
+    def test_encryption_fails(self) -> None:
+        # this is the only combination that should fail
+        inputs = [("spark.ssl.enabled", "true", 
"pytorch.spark.distributor.ignoreSsl", "false")]
+        for ssl_conf_key, ssl_conf_value, pytorch_conf_key, pytorch_conf_value 
in inputs:
+            with self.subTest():
+                with self.assertRaisesRegex(Exception, "encryption"):
+                    self.spark.sparkContext._conf.set(ssl_conf_key, 
ssl_conf_value)
+                    self.spark.sparkContext._conf.set(pytorch_conf_key, 
pytorch_conf_value)
+                    distributor = TorchDistributor("pytorch", 1, True, False)
+                    distributor._check_encryption()
+
+    def test_get_num_tasks_fails(self) -> None:
+        inputs = [1, 5, 4]
+
+        # This is when the conf isn't set and we request GPUs
+        for num_processes in inputs:
+            with self.subTest():
+                with self.assertRaisesRegex(RuntimeError, "driver"):
+                    TorchDistributor("pytorch", num_processes, True, True)
+                with self.assertRaisesRegex(RuntimeError, "unset"):
+                    TorchDistributor("pytorch", num_processes, False, True)
+
+
+class TorchDistributorLocalUnitTests(unittest.TestCase):

Review Comment:
   More tests will be added to this class and the following class in later PRs.



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