forked from InfiniTensor/go-llama-go
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllama.py
More file actions
295 lines (195 loc) · 8.67 KB
/
llama.py
File metadata and controls
295 lines (195 loc) · 8.67 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
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
import dataclasses
import json
import math
from pathlib import Path
import torch
import torch.nn as nn
from safetensors.torch import load_file
import kernels
@dataclasses.dataclass
class ModelConfig:
head_dim: int
hidden_size: int
intermediate_size: int
num_attention_heads: int
num_hidden_layers: int
num_key_value_heads: int
rms_norm_eps: float
rope_theta: float
torch_dtype: str
vocab_size: int
class RMSNorm(nn.Module):
def __init__(self, hidden_size, eps):
super().__init__()
self.weight = nn.Parameter(torch.ones(hidden_size))
self.eps = eps
def forward(self, input):
# return (
# input
# * torch.rsqrt(input.pow(2).mean(dim=-1, keepdim=True) + self.eps)
# * self.weight
# )
# Replaced with Triton implementation of RMSNorm
return kernels.rms_norm_triton(input, self.weight, self.eps)
class MLP(nn.Module):
def __init__(self, hidden_size, intermediate_size):
super().__init__()
self.gate_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
self.up_proj = nn.Linear(hidden_size, intermediate_size, bias=False)
self.down_proj = nn.Linear(intermediate_size, hidden_size, bias=False)
self.silu = nn.SiLU()
def forward(self, input):
return self.down_proj(self.silu(self.gate_proj(input)) * self.up_proj(input))
def apply_rotary_position_embedding(input, sin_table, cos_table):
sin_table = sin_table[None, :, None, :]
cos_table = cos_table[None, :, None, :]
input_0 = input[..., : input.shape[-1] // 2]
input_1 = input[..., input.shape[-1] // 2 :]
input_0_rotated = input_0 * cos_table - input_1 * sin_table
input_1_rotated = input_0 * sin_table + input_1 * cos_table
return torch.cat((input_0_rotated, input_1_rotated), dim=-1)
def apply_scaled_dot_product_attention(query, key, value):
_, num_heads_q, seq_len_q, emb_dim = query.shape
_, num_heads_k, seq_len_k, _ = key.shape
_, num_heads_v, _, _ = value.shape
key = key.repeat_interleave(num_heads_q // num_heads_k, 1)
value = value.repeat_interleave(num_heads_q // num_heads_v, 1)
scale = 1 / math.sqrt(emb_dim)
attn_mask = torch.tril(
torch.full((seq_len_q, seq_len_k), True, device=query.device)
)
attn_output = torch.matmul(query, key.permute(0, 1, 3, 2)) * scale
attn_output = torch.where(attn_mask, attn_output, float("-inf"))
attn_output = torch.softmax(attn_output, dim=-1)
attn_output = torch.matmul(attn_output, value)
return attn_output
class Attention(nn.Module):
def __init__(self, config):
super().__init__()
self.head_dim = config.head_dim
self.hidden_size = config.hidden_size
self.num_attention_heads = config.num_attention_heads
self.num_key_value_heads = config.num_key_value_heads
self.q_proj = nn.Linear(
self.hidden_size, self.num_attention_heads * self.head_dim, bias=False
)
self.k_proj = nn.Linear(
self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False
)
self.v_proj = nn.Linear(
self.hidden_size, self.num_key_value_heads * self.head_dim, bias=False
)
self.o_proj = nn.Linear(
self.num_attention_heads * self.head_dim, self.hidden_size, bias=False
)
def forward(self, hidden_states, sin_table, cos_table):
batch_size, seq_len = hidden_states.shape[:2]
hidden_shape = (batch_size, seq_len, -1, self.head_dim)
query_states = self.q_proj(hidden_states).view(hidden_shape)
key_states = self.k_proj(hidden_states).view(hidden_shape)
value_states = self.v_proj(hidden_states).view(hidden_shape).permute(0, 2, 1, 3)
# query_states = apply_rotary_position_embedding(
# query_states, sin_table, cos_table
# ).permute(0, 2, 1, 3)
# key_states = apply_rotary_position_embedding(
# key_states, sin_table, cos_table
# ).permute(0, 2, 1, 3)
# Use Triton implementation of Rotary Embedding
kernels.apply_rotary_pos_emb_triton(query_states, cos_table, sin_table)
kernels.apply_rotary_pos_emb_triton(key_states, cos_table, sin_table)
query_states = query_states.permute(0, 2, 1, 3)
key_states = key_states.permute(0, 2, 1, 3)
# attn_output = apply_scaled_dot_product_attention(
# query_states, key_states, value_states
# )
# Use Triton implementation of Flash Attention
_, num_heads_q, seq_len_q, emb_dim = query_states.shape
_, num_heads_k, seq_len_k, _ = key_states.shape
_, num_heads_v, _, _ = value_states.shape
key_states = key_states.repeat_interleave(num_heads_q // num_heads_k, 1)
value_states = value_states.repeat_interleave(num_heads_q // num_heads_v, 1)
attn_output = kernels.flash_attention_triton(query_states, key_states, value_states)
return self.o_proj(
attn_output.permute(0, 2, 1, 3).reshape(batch_size, seq_len, -1)
)
class DecoderLayer(nn.Module):
def __init__(self, config):
super().__init__()
self.input_layernorm = RMSNorm(config.hidden_size, config.rms_norm_eps)
self.self_attn = Attention(config)
self.post_attention_layernorm = RMSNorm(config.hidden_size, config.rms_norm_eps)
self.mlp = MLP(config.hidden_size, config.intermediate_size)
def forward(self, hidden_states, sin_table, cos_table):
hidden_states += self.self_attn(
self.input_layernorm(hidden_states), sin_table, cos_table
)
hidden_states += self.mlp(self.post_attention_layernorm(hidden_states))
return hidden_states
def generate_sin_and_cos_tables(seq_len, emb_dim, base, dtype, device):
theta = base ** (
-2 * (torch.arange(emb_dim // 2, dtype=dtype, device=device) / emb_dim)
)
positions = torch.arange(seq_len, dtype=dtype, device=device).unsqueeze(1)
sin_table = torch.sin(positions * theta)
cos_table = torch.cos(positions * theta)
return sin_table, cos_table
class Model(nn.Module):
def __init__(self, config):
super().__init__()
self.head_dim = config.head_dim
self.hidden_size = config.hidden_size
self.num_hidden_layers = config.num_hidden_layers
self.rms_norm_eps = config.rms_norm_eps
self.rope_theta = config.rope_theta
self.torch_dtype = config.torch_dtype
self.vocab_size = config.vocab_size
self.embed_tokens = torch.nn.Embedding(self.vocab_size, self.hidden_size)
self.layers = nn.ModuleList(
DecoderLayer(config) for _ in range(self.num_hidden_layers)
)
self.norm = RMSNorm(self.hidden_size, self.rms_norm_eps)
def forward(self, input_ids):
hidden_states = self.embed_tokens(input_ids)
seq_len = hidden_states.shape[1]
sin_table, cos_table = generate_sin_and_cos_tables(
seq_len,
self.head_dim,
base=self.rope_theta,
dtype=getattr(torch, self.torch_dtype),
device=input_ids.device,
)
for i in range(self.num_hidden_layers):
hidden_states = self.layers[i](hidden_states, sin_table, cos_table)
return self.norm(hidden_states)
class ModelForCausalLM(nn.Module):
def __init__(self, config):
super().__init__()
self.model = Model(config)
self.lm_head = nn.Linear(config.hidden_size, config.vocab_size, bias=False)
def generate(self, input_ids, max_new_tokens=20):
for _ in range(max_new_tokens):
hidden_states = self.model(input_ids)
logits = self.lm_head(hidden_states[:, -1, :])
next = torch.argmax(logits, dim=-1).unsqueeze(-1)
input_ids = torch.cat((input_ids, next), dim=-1)
return input_ids
@staticmethod
def from_pretrained(model_path):
model_path = Path(model_path)
with open(model_path / "config.json") as f:
config = json.load(f)
if "head_dim" not in config:
config["head_dim"] = config["hidden_size"] // config["num_attention_heads"]
config = ModelConfig(
**{
key: value
for key, value in config.items()
if key in ModelConfig.__annotations__
}
)
model = ModelForCausalLM(config).to(getattr(torch, config.torch_dtype))
state_dict = load_file(model_path / "model.safetensors")
if "lm_head.weight" not in state_dict:
state_dict["lm_head.weight"] = state_dict["model.embed_tokens.weight"]
model.load_state_dict(state_dict)
return model