forked from Lightning-AI/litgpt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_api.py
219 lines (168 loc) · 7.36 KB
/
test_api.py
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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
from pathlib import Path
import os
import pytest
import re
import torch
from unittest.mock import MagicMock
from litgpt.api import LLM, calculate_number_of_devices
from litgpt.scripts.download import download_from_hub
from tests.conftest import RunIf
@pytest.fixture
def mock_llm():
llm = MagicMock(spec=LLM)
llm.model = MagicMock()
llm.preprocessor = MagicMock()
llm.prompt_style = MagicMock()
llm.checkpoint_dir = MagicMock()
llm.fabric = MagicMock()
return llm
def test_load_model(mock_llm):
assert isinstance(mock_llm, LLM)
assert mock_llm.model is not None
assert mock_llm.preprocessor is not None
assert mock_llm.prompt_style is not None
assert mock_llm.checkpoint_dir is not None
assert mock_llm.fabric is not None
def test_generate(mock_llm):
prompt = "What do Llamas eat?"
mock_llm.generate.return_value = prompt + " Mock output"
output = mock_llm.generate(prompt, max_new_tokens=10, temperature=0.8, top_k=5)
assert isinstance(output, str)
assert len(output) > len(prompt)
def test_stream_generate(mock_llm):
prompt = "What do Llamas eat?"
def iterator():
outputs = (prompt + " Mock output").split()
for output in outputs:
yield output
mock_llm.generate.return_value = iterator()
output = mock_llm.generate(prompt, max_new_tokens=10, temperature=0.8, top_k=5, stream=True)
result = "".join([out for out in output])
assert len(result) > len(prompt)
def test_generate_token_ids(mock_llm):
prompt = "What do Llamas eat?"
mock_output_ids = MagicMock(spec=torch.Tensor)
mock_output_ids.shape = [len(prompt) + 10]
mock_llm.generate.return_value = mock_output_ids
output_ids = mock_llm.generate(prompt, max_new_tokens=10, return_as_token_ids=True)
assert isinstance(output_ids, torch.Tensor)
assert output_ids.shape[0] > len(prompt)
def test_calculate_number_of_devices():
assert calculate_number_of_devices(1) == 1
assert calculate_number_of_devices([0, 1, 2]) == 3
assert calculate_number_of_devices(None) == 0
def test_llm_load_random_init(tmp_path):
download_from_hub(repo_id="EleutherAI/pythia-14m", tokenizer_only=True, checkpoint_dir=tmp_path)
torch.manual_seed(123)
llm = LLM.load(
model="pythia-160m",
init="random",
tokenizer_dir=Path(tmp_path/"EleutherAI/pythia-14m")
)
input_text = "some text text"
output_text = llm.generate(input_text, max_new_tokens=15)
ln = len(llm.preprocessor.tokenizer.encode(output_text)) - len(llm.preprocessor.tokenizer.encode(input_text))
assert ln <= 15
# The following below tests that generate works with different prompt lengths
# after the kv cache was set
input_text = "some text"
output_text = llm.generate(input_text, max_new_tokens=15)
ln = len(llm.preprocessor.tokenizer.encode(output_text)) - len(llm.preprocessor.tokenizer.encode(input_text))
assert ln <= 15
input_text = "some text text text"
output_text = llm.generate(input_text, max_new_tokens=15)
ln = len(llm.preprocessor.tokenizer.encode(output_text)) - len(llm.preprocessor.tokenizer.encode(input_text))
assert ln <= 15
def test_llm_load_hub_init(tmp_path):
torch.manual_seed(123)
llm = LLM.load(
model="EleutherAI/pythia-14m",
init="pretrained"
)
text_1 = llm.generate("text", max_new_tokens=10, top_k=1)
assert len(text_1) > 0
text_2 = llm.generate("text", max_new_tokens=10, top_k=1, stream=True)
assert text_1 == "".join(list(text_2))
def test_model_not_initialized(tmp_path):
llm = LLM.load(
model="EleutherAI/pythia-14m",
init="pretrained",
distribute=None
)
with pytest.raises(AttributeError, match=re.escape("The model is not initialized yet; use the .distribute() method to initialize the model.")):
llm.model
with pytest.raises(AttributeError, match=re.escape("The model is not initialized yet; use the .distribute() method to initialize the model.")):
llm.generate("text")
llm = LLM.load(
model="EleutherAI/pythia-14m",
tokenizer_dir="EleutherAI/pythia-14m",
init="random",
distribute=None
)
with pytest.raises(AttributeError, match=re.escape("The model is not initialized yet; use the .distribute() method to initialize the model.")):
llm.model
with pytest.raises(AttributeError, match=re.escape("The model is not initialized yet; use the .distribute() method to initialize the model.")):
llm.generate("text")
@RunIf(min_cuda_gpus=2)
def test_more_than_1_device_for_sequential_tp_gpu(tmp_path):
llm = LLM.load(
model="EleutherAI/pythia-14m",
)
llm.distribute(devices=2, generate_strategy="sequential")
assert isinstance(llm.generate("What do llamas eat?"), str)
if os.getenv("CI") != "true":
# this crashes the CI, maybe because of process forking; works fien locally though
llm.distribute(devices=2, generate_strategy="tensor_parallel")
assert isinstance(llm.generate("What do llamas eat?"), str)
with pytest.raises(NotImplementedError, match=f"Support for multiple devices is currently only implemented for generate_strategy='sequential'|'tensor_parallel'."):
llm.distribute(devices=2)
@RunIf(min_cuda_gpus=1)
def test_sequential_tp_incompatibility_with_random_weights(tmp_path):
llm = LLM.load(
model="EleutherAI/pythia-14m",
tokenizer_dir="EleutherAI/pythia-14m",
init="random"
)
for strategy in ("sequential", "tensor_parallel"):
with pytest.raises(NotImplementedError, match=re.escape("The LLM was initialized with init='random' but .distribute() currently only supports pretrained weights.")):
llm.distribute(devices=1, generate_strategy=strategy)
def test_sequential_tp_cpu(tmp_path):
llm = LLM.load(
model="EleutherAI/pythia-14m",
)
for strategy in ("sequential", "tensor_parallel"):
with pytest.raises(NotImplementedError, match=f"generate_strategy='{strategy}' is only supported for accelerator='cuda'|'gpu'."):
llm.distribute(
devices=1,
accelerator="cpu",
generate_strategy=strategy
)
@RunIf(min_cuda_gpus=1)
def test_quantization_is_applied(tmp_path):
llm = LLM.load(
model="EleutherAI/pythia-14m",
)
llm.distribute(devices=1, quantize="bnb.nf4", precision="bf16-true")
assert "NF4Linear" in str(type(llm.model.lm_head))
def test_fixed_kv_cache(tmp_path):
llm = LLM.load(
model="EleutherAI/pythia-14m",
)
llm.distribute(devices=1, fixed_kv_cache_size=100)
# Request too many tokens
with pytest.raises(NotImplementedError, match="max_seq_length 512 needs to be >= 9223372036854775809"):
output_text = llm.generate("hello world", max_new_tokens=2**63)
def test_invalid_accelerator(tmp_path):
llm = LLM.load(
model="EleutherAI/pythia-14m",
)
with pytest.raises(ValueError, match="Invalid accelerator"):
llm.distribute(accelerator="invalid")
def test_returned_benchmark_dir(tmp_path):
llm = LLM.load(
model="EleutherAI/pythia-14m",
)
text, bench_d = llm.benchmark(prompt="hello world")
assert isinstance(bench_d["Inference speed in tokens/sec"], float)
text, bench_d = llm.benchmark(prompt="hello world", stream=True)
assert isinstance(bench_d["Inference speed in tokens/sec"], float)