fixtures.py 3.12 KB
Newer Older
chenzk's avatar
v1.0  
chenzk committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
# SPDX-FileCopyrightText: Copyright (c) 1993-2025 NVIDIA CORPORATION & AFFILIATES. All rights reserved.
# SPDX-License-Identifier: Apache-2.0


import pytest
import torch
from transformers import AutoModelForCausalLM, pipeline


def get_device():
    """Helper function that returns the appropriate device (GPU if available, otherwise CPU)"""
    return "cuda:0" if torch.cuda.is_available() else "cpu"


@pytest.fixture(scope="session")
def unit_test_model():
    model = AutoModelForCausalLM.from_pretrained("MaxJeblick/llama2-0b-unit-test").eval()
    return model.to(get_device())


@pytest.fixture(scope="session")
def unit_test_model_output_attention():
    model = AutoModelForCausalLM.from_pretrained("MaxJeblick/llama2-0b-unit-test", attn_implementation="eager").eval()
    return model.to(get_device())


@pytest.fixture(scope="session")
def danube_500m_model():
    model = AutoModelForCausalLM.from_pretrained("h2oai/h2o-danube3-500m-chat").eval()
    return model.to(get_device())


@pytest.fixture(scope="session")
def kv_press_unit_test_pipeline():
    return pipeline(
        "kv-press-text-generation",
        model="maxjeblick/llama2-0b-unit-test",
        device=get_device(),
    )


@pytest.fixture(scope="session")
def kv_press_danube_pipeline():
    return pipeline(
        "kv-press-text-generation",
        model="h2oai/h2o-danube3-500m-chat",
        device=get_device(),
    )


@pytest.fixture(scope="session")
def kv_press_adaptive_pipeline():
    """Flexible pipeline that uses GPU+flash attention if available, otherwise CPU"""
    device = get_device()
    ckpt = "meta-llama/Llama-3.2-1B-Instruct"

    # Use flash attention only if GPU is available
    model_kwargs = {}
    if torch.cuda.is_available():
        model_kwargs["attn_implementation"] = "flash_attention_2"

    pipe = pipeline(
        "kv-press-text-generation",
        model=ckpt,
        device=device,
        dtype="auto",
        model_kwargs=model_kwargs,
    )
    return pipe


@pytest.fixture(scope="class")
def kv_press_llama3_1_flash_attn_pipeline():
    device = "cuda:0"
    ckpt = "meta-llama/Llama-3.1-8B-Instruct"
    attn_implementation = "flash_attention_2"
    pipe = pipeline(
        "kv-press-text-generation",
        model=ckpt,
        device=device,
        model_kwargs={"attn_implementation": attn_implementation, "dtype": torch.bfloat16},
    )
    return pipe


@pytest.fixture(scope="class")
def kv_press_llama3_2_flash_attn_pipeline():
    device = "cuda:0"
    ckpt = "meta-llama/Llama-3.2-1B-Instruct"
    attn_implementation = "flash_attention_2"
    pipe = pipeline(
        "kv-press-text-generation",
        model=ckpt,
        device=device,
        model_kwargs={"attn_implementation": attn_implementation, "dtype": torch.bfloat16},
    )
    return pipe


@pytest.fixture(scope="class")
def kv_press_qwen3_flash_attn_pipeline():
    device = "cuda:0"
    ckpt = "Qwen/Qwen3-4B-Instruct-2507"
    attn_implementation = "flash_attention_2"
    pipe = pipeline(
        "kv-press-text-generation",
        model=ckpt,
        device=device,
        model_kwargs={"attn_implementation": attn_implementation, "dtype": torch.bfloat16},
    )
    return pipe