-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkvpress_infer.py
38 lines (28 loc) · 994 Bytes
/
kvpress_infer.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
# importing the libaries and modules
from transformers import AutoModelForCausalLM, AutoTokenizer, pipeline
import torch
from kvpress import StreamingLLMPress
# loading the module in HF pipeline
# for now KVPress can only be used through high-level abstraction pipeline.
# I didn't test it on model.generate method yet
# feel free to try
checkpoint = "HuggingFaceTB/SmolLM-1.7B"
pipe = pipeline(
"kv-press-text-generation",
model=checkpoint,
torch_dtype="auto",
model_kwargs={
"max_length": 100,
"use_cache": True,
}
)
# Move the model to GPU
pipe.model.to('cuda')
# found 0.7 does better but better understanding will be a benchmark
# benchmark coming soon
context = "Alice and Bob were walking in the park when suddenly they encountered a mysterious figure."
question = "What happened next?"
press = StreamingLLMPress(compression_ratio=0.7)
# generating the results
answer = pipe(context, question=question, press=press)["answer"]
print(answer)