|
| 1 | +from types import SimpleNamespace |
| 2 | + |
| 3 | +from torchspec.config.utils import generate_draft_model_config |
| 4 | + |
| 5 | + |
| 6 | +def test_generate_draft_model_config_preserves_rope_fields(monkeypatch): |
| 7 | + rope_scaling = { |
| 8 | + "type": "yarn", |
| 9 | + "factor": 64.0, |
| 10 | + "original_max_position_embeddings": 4096, |
| 11 | + "beta_fast": 32.0, |
| 12 | + "beta_slow": 1.0, |
| 13 | + "mscale": 1.0, |
| 14 | + "mscale_all_dim": 1.0, |
| 15 | + } |
| 16 | + text_config = SimpleNamespace( |
| 17 | + vocab_size=32000, |
| 18 | + hidden_size=4096, |
| 19 | + num_attention_heads=32, |
| 20 | + num_key_value_heads=8, |
| 21 | + intermediate_size=14336, |
| 22 | + max_position_embeddings=262144, |
| 23 | + rope_theta=5000000, |
| 24 | + rope_scaling=rope_scaling, |
| 25 | + rms_norm_eps=1e-6, |
| 26 | + hidden_act="silu", |
| 27 | + bos_token_id=1, |
| 28 | + eos_token_id=2, |
| 29 | + torch_dtype="bfloat16", |
| 30 | + ) |
| 31 | + target_config = SimpleNamespace(text_config=text_config) |
| 32 | + |
| 33 | + class DummyTokenizer: |
| 34 | + def __len__(self): |
| 35 | + return 32000 |
| 36 | + |
| 37 | + monkeypatch.setattr( |
| 38 | + "torchspec.config.utils.AutoConfig.from_pretrained", |
| 39 | + lambda *args, **kwargs: target_config, |
| 40 | + ) |
| 41 | + monkeypatch.setattr( |
| 42 | + "torchspec.config.utils.AutoTokenizer.from_pretrained", |
| 43 | + lambda *args, **kwargs: DummyTokenizer(), |
| 44 | + ) |
| 45 | + |
| 46 | + draft_config = generate_draft_model_config("dummy-model") |
| 47 | + |
| 48 | + assert draft_config["max_position_embeddings"] == 262144 |
| 49 | + assert draft_config["rope_theta"] == 5000000 |
| 50 | + assert draft_config["rope_scaling"] == rope_scaling |
| 51 | + |
| 52 | + |
| 53 | +def test_generate_draft_model_config_copies_rope_scaling(monkeypatch): |
| 54 | + rope_scaling = {"type": "yarn", "factor": 8.0, "original_max_position_embeddings": 8192} |
| 55 | + text_config = SimpleNamespace( |
| 56 | + vocab_size=32000, |
| 57 | + hidden_size=2048, |
| 58 | + num_attention_heads=16, |
| 59 | + num_key_value_heads=4, |
| 60 | + intermediate_size=8192, |
| 61 | + max_position_embeddings=65536, |
| 62 | + rope_theta=1000000, |
| 63 | + rope_scaling=rope_scaling, |
| 64 | + rms_norm_eps=1e-6, |
| 65 | + hidden_act="silu", |
| 66 | + bos_token_id=1, |
| 67 | + eos_token_id=2, |
| 68 | + torch_dtype="bfloat16", |
| 69 | + ) |
| 70 | + target_config = SimpleNamespace(text_config=text_config) |
| 71 | + |
| 72 | + class DummyTokenizer: |
| 73 | + def __len__(self): |
| 74 | + return 32000 |
| 75 | + |
| 76 | + monkeypatch.setattr( |
| 77 | + "torchspec.config.utils.AutoConfig.from_pretrained", |
| 78 | + lambda *args, **kwargs: target_config, |
| 79 | + ) |
| 80 | + monkeypatch.setattr( |
| 81 | + "torchspec.config.utils.AutoTokenizer.from_pretrained", |
| 82 | + lambda *args, **kwargs: DummyTokenizer(), |
| 83 | + ) |
| 84 | + |
| 85 | + draft_config = generate_draft_model_config("dummy-model") |
| 86 | + rope_scaling["factor"] = 999.0 |
| 87 | + |
| 88 | + assert draft_config["rope_scaling"]["factor"] == 8.0 |
| 89 | + |
| 90 | + |
| 91 | +def test_generate_draft_model_config_fills_yarn_defaults(monkeypatch): |
| 92 | + rope_scaling = {"type": "yarn", "factor": 8.0, "original_max_position_embeddings": 8192} |
| 93 | + text_config = SimpleNamespace( |
| 94 | + vocab_size=32000, |
| 95 | + hidden_size=2048, |
| 96 | + num_attention_heads=16, |
| 97 | + num_key_value_heads=4, |
| 98 | + intermediate_size=8192, |
| 99 | + max_position_embeddings=65536, |
| 100 | + rope_theta=1000000, |
| 101 | + rope_scaling=rope_scaling, |
| 102 | + rms_norm_eps=1e-6, |
| 103 | + hidden_act="silu", |
| 104 | + bos_token_id=1, |
| 105 | + eos_token_id=2, |
| 106 | + torch_dtype="bfloat16", |
| 107 | + ) |
| 108 | + target_config = SimpleNamespace(text_config=text_config) |
| 109 | + |
| 110 | + class DummyTokenizer: |
| 111 | + def __len__(self): |
| 112 | + return 32000 |
| 113 | + |
| 114 | + monkeypatch.setattr( |
| 115 | + "torchspec.config.utils.AutoConfig.from_pretrained", |
| 116 | + lambda *args, **kwargs: target_config, |
| 117 | + ) |
| 118 | + monkeypatch.setattr( |
| 119 | + "torchspec.config.utils.AutoTokenizer.from_pretrained", |
| 120 | + lambda *args, **kwargs: DummyTokenizer(), |
| 121 | + ) |
| 122 | + |
| 123 | + draft_config = generate_draft_model_config("dummy-model") |
| 124 | + |
| 125 | + assert draft_config["rope_scaling"]["beta_fast"] == 32.0 |
| 126 | + assert draft_config["rope_scaling"]["beta_slow"] == 1.0 |
| 127 | + assert draft_config["rope_scaling"]["mscale"] == 1.0 |
| 128 | + assert draft_config["rope_scaling"]["mscale_all_dim"] == 0.0 |
0 commit comments