| 1 | # SPDX-License-Identifier: Apache-2.0 |
| 2 | # SPDX-FileCopyrightText: Copyright contributors to the vLLM project |
| 3 | import pytest |
| 4 | from transformers import GraniteConfig |
| 5 | |
| 6 | from vllm.model_executor.models.granite import granite_layer_attn_params |
| 7 | |
| 8 | from ...utils import check_logprobs_close |
| 9 | |
| 10 | MODELS = [ |
| 11 | # TODO(sang): Sliding window should be tested separately. |
| 12 | "ibm/PowerLM-3b", |
| 13 | "ibm/PowerMoE-3b", |
| 14 | "ibm-granite/granite-swash-2b", |
| 15 | "ibm-granite/granite-swash-3b-a600m", |
| 16 | ] |
| 17 | |
| 18 | |
| 19 | @pytest.mark.parametrize("model", MODELS) |
| 20 | @pytest.mark.parametrize("dtype", ["bfloat16"]) |
| 21 | @pytest.mark.parametrize("max_tokens", [64]) |
| 22 | @pytest.mark.parametrize("num_logprobs", [5]) |
| 23 | @pytest.mark.cpu_model |
| 24 | def test_models( |
| 25 | hf_runner, |
| 26 | vllm_runner, |
| 27 | example_prompts, |
| 28 | model: str, |
| 29 | dtype: str, |
| 30 | max_tokens: int, |
| 31 | num_logprobs: int, |
| 32 | ) -> None: |
| 33 | with hf_runner(model, dtype=dtype) as hf_model: |
| 34 | hf_outputs = hf_model.generate_greedy_logprobs_limit( |
| 35 | example_prompts, max_tokens, num_logprobs |
| 36 | ) |
| 37 | |
| 38 | with vllm_runner(model, dtype=dtype) as vllm_model: |
| 39 | vllm_outputs = vllm_model.generate_greedy_logprobs( |
| 40 | example_prompts, max_tokens, num_logprobs |
| 41 | ) |
| 42 | check_logprobs_close( |
| 43 | outputs_0_lst=hf_outputs, |
| 44 | outputs_1_lst=vllm_outputs, |
| 45 | name_0="hf", |
| 46 | name_1="vllm", |
| 47 | ) |
| 48 | |
| 49 | |
| 50 | def test_granite_swa_features_are_off_without_swa_config(): |
| 51 | """A plain Granite config must not pick up sliding windows or sinks.""" |
| 52 | config = GraniteConfig(num_hidden_layers=4) |
| 53 | theta = config.rope_parameters["rope_theta"] |
| 54 | |
| 55 | assert [granite_layer_attn_params(config, i) for i in range(4)] == [ |
| 56 | (None, theta, False) |
| 57 | ] * 4 |
| 58 | |
| 59 | |
| 60 | @pytest.mark.parametrize( |
| 61 | "attention_sinks, expected_sink", [(None, True), (False, False)] |
| 62 | ) |
| 63 | def test_granite_swa_features_resolve_per_layer(attention_sinks, expected_sink): |
| 64 | """`layer_types`/`layer_rope_theta` apply per layer; theta 0 means NoPE.""" |
| 65 | kwargs = {} if attention_sinks is None else {"attention_sinks": attention_sinks} |
| 66 | config = GraniteConfig( |
| 67 | num_hidden_layers=3, |
| 68 | sliding_window=128, |
| 69 | layer_types=["full_attention", "sliding_attention", "sliding_attention"], |
| 70 | layer_rope_theta=[10000.0, 0.0, 1000000.0], |
| 71 | **kwargs, |
| 72 | ) |
| 73 | |
| 74 | assert [granite_layer_attn_params(config, i) for i in range(3)] == [ |
| 75 | (None, 10000.0, expected_sink), |
| 76 | (128, 0.0, expected_sink), |
| 77 | (128, 1000000.0, expected_sink), |
| 78 | ] |