|
| 1 | +# Copyright 2026 Google LLC |
| 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 | +# https://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 | +"""Unit tests for train_dpo.py.""" |
| 16 | + |
| 17 | +import unittest |
| 18 | +from types import SimpleNamespace |
| 19 | +import pytest |
| 20 | + |
| 21 | +from maxtext.trainers.post_train.dpo import train_dpo |
| 22 | + |
| 23 | +pytestmark = [pytest.mark.post_training] |
| 24 | + |
| 25 | + |
| 26 | +class TrainDPOTest(unittest.TestCase): |
| 27 | + """Tests for train_dpo.py.""" |
| 28 | + |
| 29 | + @pytest.mark.cpu_only |
| 30 | + def test_validate_config_valid(self): |
| 31 | + config = SimpleNamespace( |
| 32 | + optimizer_memory_host_offload=False, |
| 33 | + num_vocab_tiling=1, |
| 34 | + ) |
| 35 | + # Should not raise any exception |
| 36 | + train_dpo.validate_config(config) |
| 37 | + |
| 38 | + @pytest.mark.cpu_only |
| 39 | + def test_validate_config_invalid_offload(self): |
| 40 | + config = SimpleNamespace( |
| 41 | + optimizer_memory_host_offload=True, |
| 42 | + num_vocab_tiling=1, |
| 43 | + ) |
| 44 | + with self.assertRaisesRegex(ValueError, "optimizer_memory_host_offload=True is not supported"): |
| 45 | + train_dpo.validate_config(config) |
| 46 | + |
| 47 | + @pytest.mark.cpu_only |
| 48 | + def test_validate_config_invalid_vocab_tiling(self): |
| 49 | + config = SimpleNamespace( |
| 50 | + optimizer_memory_host_offload=False, |
| 51 | + num_vocab_tiling=2, |
| 52 | + ) |
| 53 | + with self.assertRaisesRegex(ValueError, "Vocab Tiling is not supported with DPO"): |
| 54 | + train_dpo.validate_config(config) |
| 55 | + |
| 56 | + |
| 57 | +if __name__ == "__main__": |
| 58 | + unittest.main() |
0 commit comments