|
| 1 | +# Copyright (c) Meta Platforms, Inc. and affiliates. |
| 2 | +# All rights reserved. |
| 3 | +# |
| 4 | +# This source code is licensed under the BSD-style license found in the |
| 5 | +# LICENSE file in the root directory of this source tree. |
| 6 | + |
| 7 | +import inspect |
| 8 | +import tempfile |
| 9 | +import unittest |
| 10 | +from dataclasses import asdict |
| 11 | + |
| 12 | +import yaml |
| 13 | + |
| 14 | +from forge.actors.policy import Policy, SamplingOverrides, WorkerConfig |
| 15 | +from vllm.engine.arg_utils import EngineArgs |
| 16 | + |
| 17 | + |
| 18 | +class TestPolicyConfig(unittest.TestCase): |
| 19 | + """Test suite for Policy configuration handling after PolicyConfig removal.""" |
| 20 | + |
| 21 | + def test_policy_default_initialization(self): |
| 22 | + """Test that Policy can be initialized with default values.""" |
| 23 | + policy = Policy() |
| 24 | + |
| 25 | + # Check that default factories work |
| 26 | + self.assertIsInstance(policy.worker_params, WorkerConfig) |
| 27 | + self.assertIsInstance(policy.sampling_overrides, SamplingOverrides) |
| 28 | + self.assertIsNone(policy.available_devices) |
| 29 | + |
| 30 | + # Check default values |
| 31 | + self.assertEqual(policy.worker_params.model, "meta-llama/Llama-3.1-8B-Instruct") |
| 32 | + self.assertEqual(policy.worker_params.tensor_parallel_size, 1) |
| 33 | + self.assertEqual(policy.worker_params.pipeline_parallel_size, 1) |
| 34 | + self.assertFalse(policy.worker_params.enforce_eager) |
| 35 | + |
| 36 | + self.assertEqual(policy.sampling_overrides.num_samples, 1) |
| 37 | + self.assertFalse(policy.sampling_overrides.guided_decoding) |
| 38 | + self.assertEqual(policy.sampling_overrides.max_tokens, 512) |
| 39 | + |
| 40 | + def test_policy_with_dict_configs(self): |
| 41 | + """Test Policy initialization with dictionary configs.""" |
| 42 | + worker_dict = { |
| 43 | + "model": "test-model-6789", |
| 44 | + "tensor_parallel_size": 7777, |
| 45 | + "pipeline_parallel_size": 8888, |
| 46 | + "enforce_eager": True, |
| 47 | + "vllm_args": {"max_model_len": 9999, "gpu_memory_utilization": 0.1234}, |
| 48 | + } |
| 49 | + |
| 50 | + sampling_dict = { |
| 51 | + "num_samples": 1357, |
| 52 | + "guided_decoding": True, |
| 53 | + "max_tokens": 2468, |
| 54 | + } |
| 55 | + |
| 56 | + policy = Policy( |
| 57 | + worker_params=worker_dict, |
| 58 | + sampling_overrides=sampling_dict, |
| 59 | + available_devices="test-gpu-device-abcd", |
| 60 | + ) |
| 61 | + |
| 62 | + # Check that dictionaries were converted to proper objects |
| 63 | + self.assertIsInstance(policy.worker_params, WorkerConfig) |
| 64 | + self.assertIsInstance(policy.sampling_overrides, SamplingOverrides) |
| 65 | + |
| 66 | + self.assertEqual(policy.worker_params.model, "test-model-6789") |
| 67 | + self.assertEqual(policy.worker_params.tensor_parallel_size, 7777) |
| 68 | + self.assertEqual(policy.worker_params.pipeline_parallel_size, 8888) |
| 69 | + self.assertTrue(policy.worker_params.enforce_eager) |
| 70 | + |
| 71 | + self.assertEqual(policy.sampling_overrides.num_samples, 1357) |
| 72 | + self.assertTrue(policy.sampling_overrides.guided_decoding) |
| 73 | + self.assertEqual(policy.sampling_overrides.max_tokens, 2468) |
| 74 | + |
| 75 | + def test_policy_yaml_config_loading(self): |
| 76 | + """Test loading Policy configuration from YAML file.""" |
| 77 | + yaml_content = """ |
| 78 | + worker_params: |
| 79 | + model: "yaml-test-model-9876" |
| 80 | + tensor_parallel_size: 1234 |
| 81 | + pipeline_parallel_size: 5678 |
| 82 | + enforce_eager: true |
| 83 | + vllm_args: |
| 84 | + max_model_len: 9876 |
| 85 | + gpu_memory_utilization: 0.1357 |
| 86 | +
|
| 87 | + sampling_overrides: |
| 88 | + num_samples: 2468 |
| 89 | + guided_decoding: true |
| 90 | + max_tokens: 1357 |
| 91 | +
|
| 92 | + available_devices: "yaml-test-device-xyz" |
| 93 | + """ |
| 94 | + |
| 95 | + with tempfile.NamedTemporaryFile(mode="w", suffix=".yaml", delete=False) as f: |
| 96 | + f.write(yaml_content) |
| 97 | + f.flush() |
| 98 | + |
| 99 | + # Load YAML and create Policy |
| 100 | + with open(f.name, "r") as yaml_file: |
| 101 | + config = yaml.safe_load(yaml_file) |
| 102 | + |
| 103 | + policy = Policy(**config) |
| 104 | + |
| 105 | + self.assertEqual(policy.worker_params.model, "yaml-test-model-9876") |
| 106 | + self.assertEqual(policy.worker_params.tensor_parallel_size, 1234) |
| 107 | + self.assertEqual(policy.worker_params.pipeline_parallel_size, 5678) |
| 108 | + self.assertTrue(policy.worker_params.enforce_eager) |
| 109 | + |
| 110 | + self.assertEqual(policy.sampling_overrides.num_samples, 2468) |
| 111 | + self.assertTrue(policy.sampling_overrides.guided_decoding) |
| 112 | + self.assertEqual(policy.sampling_overrides.max_tokens, 1357) |
| 113 | + |
| 114 | + self.assertEqual(policy.available_devices, "yaml-test-device-xyz") |
| 115 | + |
| 116 | + def test_invalid_worker_config_from_dict(self): |
| 117 | + """Test that WorkerConfig.from_dict handles invalid vllm_args gracefully.""" |
| 118 | + config_dict = { |
| 119 | + "model": "meta-llama/Llama-3.1-8B-Instruct", |
| 120 | + "vllm_args": "invalid_string_instead_of_dict", # This will be passed through as-is |
| 121 | + } |
| 122 | + |
| 123 | + worker_config = WorkerConfig.from_dict(config_dict) |
| 124 | + |
| 125 | + # The invalid vllm_args gets removed and default EngineArgs is used |
| 126 | + self.assertEqual(worker_config.model, "meta-llama/Llama-3.1-8B-Instruct") |
| 127 | + self.assertEqual(worker_config.vllm_args, None) |
| 128 | + |
| 129 | + |
| 130 | +if __name__ == "__main__": |
| 131 | + unittest.main() |
0 commit comments