forked from silencelamb/naked_llama
-
Notifications
You must be signed in to change notification settings - Fork 0
/
utils.py
37 lines (30 loc) · 1.27 KB
/
utils.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
import numpy as np
import torch
import json
from configuration_llama import LlamaConfig
def npy_to_tensor(npy_name):
# 将 NumPy 数组转换回 PyTorch 张量
loaded_numpy_array = np.load(npy_name)
loaded_tensor = torch.from_numpy(loaded_numpy_array)
loaded_tensor = loaded_tensor.to(torch.float32) # 将张量转换为 float32 类型
return loaded_tensor
def load_llama_config(config_file):
with open(config_file, "r") as f:
config_data = json.load(f)
configuration = LlamaConfig(**config_data)
return configuration
def get_attentioin_mask(start_pos, seq_length, ref_tensor):
if seq_length > 1:
mask = torch.full((seq_length, seq_length), float("-inf"), device=ref_tensor.device)
mask = torch.triu(mask, diagonal=1)
# When performing key-value caching, we compute the attention scores
# only for the new sequence. Thus, the matrix of scores is of size
# (seqlen, cache_len + seqlen), and the only masked entries are (i, j) for
# j > cache_len + i, since row i corresponds to token cache_len + i.
mask = torch.hstack([
torch.zeros((seq_length, start_pos), device=ref_tensor.device),
mask
]).type_as(ref_tensor)
else:
mask = None
return mask