|
| 1 | +# SPDX-FileCopyrightText: Copyright (c) 2024 NVIDIA CORPORATION & AFFILIATES. All rights reserved. |
| 2 | +# SPDX-License-Identifier: Apache-2.0 |
| 3 | +# |
| 4 | +# Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | +# you may not use this file except in compliance with the License. |
| 6 | +# You may obtain a copy of the License at |
| 7 | +# |
| 8 | +# http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +# |
| 10 | +# Unless required by applicable law or agreed to in writing, software |
| 11 | +# distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | +# See the License for the specific language governing permissions and |
| 14 | +# limitations under the License. |
| 15 | + |
| 16 | +"""Tests for EagleConfig model validators.""" |
| 17 | + |
| 18 | +import types |
| 19 | +import warnings |
| 20 | + |
| 21 | +import pytest |
| 22 | +from pydantic import ValidationError |
| 23 | + |
| 24 | +from modelopt.torch.speculative.config import EagleConfig |
| 25 | + |
| 26 | +# --- rope scaling consistency validator tests --- |
| 27 | + |
| 28 | + |
| 29 | +def test_rope_consistency_error_non_default_rope_type(): |
| 30 | + """Error when eagle_export_rope_scaling is set but training rope_type is not 'default'.""" |
| 31 | + cfg = { |
| 32 | + "eagle_export_rope_scaling": {"rope_type": "yarn", "factor": 32.0}, |
| 33 | + "eagle_architecture_config": {"rope_scaling": {"rope_type": "llama3"}}, |
| 34 | + } |
| 35 | + with pytest.raises(ValidationError, match="rope_type='llama3'"): |
| 36 | + EagleConfig.model_validate(cfg) |
| 37 | + |
| 38 | + |
| 39 | +def test_rope_consistency_error_non_default_rope_type_alt_key(): |
| 40 | + """Error when rope_scaling uses 'type' key instead of 'rope_type' (kimik2-style).""" |
| 41 | + cfg = { |
| 42 | + "eagle_export_rope_scaling": {"rope_type": "yarn", "factor": 32.0}, |
| 43 | + "eagle_architecture_config": {"rope_scaling": {"type": "yarn"}}, |
| 44 | + } |
| 45 | + with pytest.raises(ValidationError, match="rope_type='yarn'"): |
| 46 | + EagleConfig.model_validate(cfg) |
| 47 | + |
| 48 | + |
| 49 | +def test_rope_consistency_ok_default_rope_type(): |
| 50 | + """No error when training rope_type is 'default'.""" |
| 51 | + cfg = { |
| 52 | + "eagle_export_rope_scaling": {"rope_type": "yarn", "factor": 32.0}, |
| 53 | + "eagle_architecture_config": {"rope_scaling": {"rope_type": "default"}}, |
| 54 | + } |
| 55 | + EagleConfig.model_validate(cfg) |
| 56 | + |
| 57 | + |
| 58 | +def test_rope_consistency_ok_no_rope_scaling_in_arch(): |
| 59 | + """No error when eagle_architecture_config has no rope_scaling (defaults to 'default').""" |
| 60 | + cfg = { |
| 61 | + "eagle_export_rope_scaling": {"rope_type": "yarn", "factor": 32.0}, |
| 62 | + "eagle_architecture_config": {}, |
| 63 | + } |
| 64 | + EagleConfig.model_validate(cfg) |
| 65 | + |
| 66 | + |
| 67 | +def test_rope_consistency_ok_empty_export_rope(): |
| 68 | + """No error when eagle_export_rope_scaling is empty (disabled).""" |
| 69 | + cfg = { |
| 70 | + "eagle_export_rope_scaling": {}, |
| 71 | + "eagle_architecture_config": {"rope_scaling": {"rope_type": "llama3"}}, |
| 72 | + } |
| 73 | + EagleConfig.model_validate(cfg) |
| 74 | + |
| 75 | + |
| 76 | +# --- rope vs training_seq_len warning tests --- |
| 77 | + |
| 78 | + |
| 79 | +def _make_training_args(training_seq_len: int): |
| 80 | + return types.SimpleNamespace(training_seq_len=training_seq_len) |
| 81 | + |
| 82 | + |
| 83 | +def test_warn_rope_mismatch(): |
| 84 | + """Warning should fire when original_max_position_embeddings != training_seq_len.""" |
| 85 | + cfg = { |
| 86 | + "eagle_export_rope_scaling": { |
| 87 | + "rope_type": "yarn", |
| 88 | + "factor": 32.0, |
| 89 | + "original_max_position_embeddings": 2048, |
| 90 | + }, |
| 91 | + } |
| 92 | + with pytest.warns(UserWarning, match="differs from training_seq_len"): |
| 93 | + EagleConfig.model_validate(cfg, context={"training_args": _make_training_args(4096)}) |
| 94 | + |
| 95 | + |
| 96 | +def test_no_warn_rope_match(): |
| 97 | + """No warning when original_max_position_embeddings == training_seq_len.""" |
| 98 | + cfg = { |
| 99 | + "eagle_export_rope_scaling": { |
| 100 | + "rope_type": "yarn", |
| 101 | + "factor": 32.0, |
| 102 | + "original_max_position_embeddings": 2048, |
| 103 | + }, |
| 104 | + } |
| 105 | + with warnings.catch_warnings(): |
| 106 | + warnings.simplefilter("error") |
| 107 | + EagleConfig.model_validate(cfg, context={"training_args": _make_training_args(2048)}) |
| 108 | + |
| 109 | + |
| 110 | +def test_no_warn_without_context(): |
| 111 | + """No warning when context is not provided (e.g. inside convert chain).""" |
| 112 | + with warnings.catch_warnings(): |
| 113 | + warnings.simplefilter("error") |
| 114 | + EagleConfig.model_validate({}) |
| 115 | + |
| 116 | + |
| 117 | +def test_no_warn_missing_orig_max_pos(): |
| 118 | + """No warning when original_max_position_embeddings is absent from rope scaling config.""" |
| 119 | + cfg = {"eagle_export_rope_scaling": {}} |
| 120 | + with warnings.catch_warnings(): |
| 121 | + warnings.simplefilter("error") |
| 122 | + EagleConfig.model_validate(cfg, context={"training_args": _make_training_args(4096)}) |
| 123 | + |
| 124 | + |
| 125 | +def test_no_warn_empty_context(): |
| 126 | + """No warning when context dict has no training_args key.""" |
| 127 | + with warnings.catch_warnings(): |
| 128 | + warnings.simplefilter("error") |
| 129 | + EagleConfig.model_validate({}, context={}) |
0 commit comments