-
Notifications
You must be signed in to change notification settings - Fork 247
/
llama.py
43 lines (36 loc) · 1.39 KB
/
llama.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
import torch
from .llm import LLMConfig, EXAMPLE_INPUTS_MODE
from transformers import LlamaForCausalLM, AutoTokenizer
import intel_extension_for_pytorch as ipex
class LLAMAConfig(LLMConfig):
def __init__(self, model_id):
self.name = "llama"
self.model_id = model_id
self.to_channels_last = False
self.example_inputs_mode = EXAMPLE_INPUTS_MODE.MASK_KV_POS
# for smooth quant
self.default_dataset = "NeelNanda/pile-10k"
self.use_global_past_key_value = True
self.use_ipex_autotune = True
def get_user_model(self, config, benchmark):
if benchmark:
try:
with ipex.OnDevice(dtype=torch.float, device="meta"):
self.model = LlamaForCausalLM._from_config(config)
except (RuntimeError, AttributeError):
self.model = LlamaForCausalLM.from_pretrained(
self.model_id,
config=config,
low_cpu_mem_usage=True,
torch_dtype=torch.half,
)
else:
self.model = LlamaForCausalLM.from_pretrained(
self.model_id,
config=config,
low_cpu_mem_usage=True,
torch_dtype=torch.float,
)
return self.model
def get_tokenizer(self):
return AutoTokenizer.from_pretrained(self.model_id)