|
| 1 | +# Copyright 2024 The Kubeflow Authors. |
| 2 | +# |
| 3 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +# you may not use this file except in compliance with the License. |
| 5 | +# You may obtain a copy of the License at |
| 6 | +# |
| 7 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +# |
| 9 | +# Unless required by applicable law or agreed to in writing, software |
| 10 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +# See the License for the specific language governing permissions and |
| 13 | +# limitations under the License. |
| 14 | + |
| 15 | +import unittest |
| 16 | +from unittest.mock import Mock |
| 17 | + |
| 18 | +from kubeflow.trainer.utils import utils |
| 19 | +from kubeflow.trainer.types import types |
| 20 | + |
| 21 | + |
| 22 | +class TestCustomTrainerPythonFileSupport(unittest.TestCase): |
| 23 | + """Test cases for the new python_file and python_args functionality in CustomTrainer.""" |
| 24 | + |
| 25 | + def test_get_trainer_crd_from_custom_trainer_python_file_with_args(self): |
| 26 | + """Test get_trainer_crd_from_custom_trainer with python_file and python_args.""" |
| 27 | + runtime = Mock() |
| 28 | + trainer = types.CustomTrainer( |
| 29 | + python_file="train.py", |
| 30 | + python_args=["--epochs", "100", "--batch-size", "32"], |
| 31 | + num_nodes=2, |
| 32 | + resources_per_node={"gpu": "4"}, |
| 33 | + ) |
| 34 | + |
| 35 | + result = utils.get_trainer_crd_from_custom_trainer(runtime, trainer) |
| 36 | + |
| 37 | + self.assertEqual(result.num_nodes, 2) |
| 38 | + self.assertEqual(result.command, ["python"]) |
| 39 | + self.assertEqual(result.args, ["train.py", "--epochs", "100", "--batch-size", "32"]) |
| 40 | + |
| 41 | + def test_get_trainer_crd_from_custom_trainer_python_file_no_args(self): |
| 42 | + """Test get_trainer_crd_from_custom_trainer with python_file but no args.""" |
| 43 | + runtime = Mock() |
| 44 | + trainer = types.CustomTrainer( |
| 45 | + python_file="train.py", num_nodes=2, resources_per_node={"gpu": "4"} |
| 46 | + ) |
| 47 | + |
| 48 | + result = utils.get_trainer_crd_from_custom_trainer(runtime, trainer) |
| 49 | + |
| 50 | + self.assertEqual(result.num_nodes, 2) |
| 51 | + self.assertEqual(result.command, ["python"]) |
| 52 | + self.assertEqual(result.args, ["train.py"]) |
| 53 | + |
| 54 | + def test_get_trainer_crd_from_custom_trainer_mutual_exclusivity_both_specified(self): |
| 55 | + """Test that func and python_file cannot be specified together.""" |
| 56 | + runtime = Mock() |
| 57 | + trainer = types.CustomTrainer(func=lambda: None, python_file="train.py") |
| 58 | + |
| 59 | + with self.assertRaises(ValueError) as context: |
| 60 | + utils.get_trainer_crd_from_custom_trainer(runtime, trainer) |
| 61 | + |
| 62 | + self.assertIn( |
| 63 | + "Specify only one of func or python_file in CustomTrainer", str(context.exception) |
| 64 | + ) |
| 65 | + |
| 66 | + def test_get_trainer_crd_from_custom_trainer_mutual_exclusivity_neither_specified(self): |
| 67 | + """Test that either func or python_file must be specified.""" |
| 68 | + runtime = Mock() |
| 69 | + trainer = types.CustomTrainer() |
| 70 | + |
| 71 | + with self.assertRaises(ValueError) as context: |
| 72 | + utils.get_trainer_crd_from_custom_trainer(runtime, trainer) |
| 73 | + |
| 74 | + self.assertIn( |
| 75 | + "You must specify either func or python_file in CustomTrainer", str(context.exception) |
| 76 | + ) |
| 77 | + |
| 78 | + def test_get_trainer_crd_from_custom_trainer_with_func_unchanged(self): |
| 79 | + """Test that existing func functionality remains unchanged.""" |
| 80 | + runtime = Mock() |
| 81 | + runtime.trainer = Mock() |
| 82 | + runtime.trainer.command = ["python", "script.py"] |
| 83 | + |
| 84 | + def dummy_func(): |
| 85 | + pass |
| 86 | + |
| 87 | + trainer = types.CustomTrainer( |
| 88 | + func=dummy_func, func_args={"lr": 0.001}, num_nodes=2, resources_per_node={"gpu": "4"} |
| 89 | + ) |
| 90 | + |
| 91 | + with unittest.mock.patch( |
| 92 | + "kubeflow.trainer.utils.utils.get_command_using_train_func" |
| 93 | + ) as mock_get_command: |
| 94 | + mock_get_command.return_value = ["python", "script.py"] |
| 95 | + result = utils.get_trainer_crd_from_custom_trainer(runtime, trainer) |
| 96 | + |
| 97 | + self.assertEqual(result.num_nodes, 2) |
| 98 | + # Verify that the existing func path still works |
| 99 | + mock_get_command.assert_called_once() |
0 commit comments