-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
49 lines (38 loc) · 1.27 KB
/
Copy pathmain.py
File metadata and controls
49 lines (38 loc) · 1.27 KB
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
import torch
import logging
from model import LlamaLM
from einops import rearrange
from transformers import PreTrainedTokenizer, AutoTokenizer
from load_checkpoint import load_model
logger = logging.getLogger(__name__)
logging.basicConfig(
level=logging.INFO, format="%(asctime)s - %(levelname)s - %(message)s"
)
def main():
if torch.backends.mps.is_available():
device = "mps"
else:
device = "cpu"
tokenizer: PreTrainedTokenizer = AutoTokenizer.from_pretrained(
"meta-llama/Llama-3.2-1B-Instruct"
)
input = "How are you?"
encoded = torch.tensor(tokenizer.encode(input), device=device)
encoded = rearrange(encoded, "(b seq) -> b seq", b=1)
print(encoded)
llama = load_model().to(device)
llama.eval()
logger.info("Start decoding...")
# TODO: pass an explicit attention_mask once we start padding/batching inputs.
# For this single unpadded prompt the outputs match the reference, but later
# batching and KV-cache work should not rely on Transformers inferring it.
res = llama.generate(
encoded,
max_new_tokens=16,
eos_token_id=tokenizer.eos_token_id,
do_sample=False,
use_kv_cache=False,
)
print(tokenizer.decode(res))
if __name__ == "__main__":
main()