-
Notifications
You must be signed in to change notification settings - Fork 757
Expand file tree
/
Copy pathattention.py
More file actions
298 lines (263 loc) · 12.2 KB
/
Copy pathattention.py
File metadata and controls
298 lines (263 loc) · 12.2 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
296
297
298
"""
# Copyright (c) 2025 PaddlePaddle Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
"""
from __future__ import annotations
from typing import TYPE_CHECKING, Dict, Optional
import numpy as np
import paddle
from paddle import nn
from paddleformers.utils.log import logger
from fastdeploy.config import FDConfig
from fastdeploy.model_executor.layers.quantization.kv_cache import (
KvCacheQuantzationTypes,
)
from fastdeploy.model_executor.layers.quantization.quant_base import QuantMethodBase
if TYPE_CHECKING:
from fastdeploy.model_executor.forward_meta import ForwardMeta
import os
from safetensors import safe_open
from fastdeploy.model_executor.layers.utils import get_tensor
from fastdeploy.model_executor.utils import default_weight_loader, set_weight_attrs
class Attention(nn.Layer):
"""
The AttentionLayer.
"""
def __init__(
self,
fd_config: FDConfig,
layer_id: int,
v_head_dim: int = -1,
rope_type: str = "",
qkv_bias: Optional[paddle.Tensor] = None,
qkv_scale: Optional[paddle.Tensor] = None,
prefix: str = "",
out_scale: float = -1.0,
linear_shift: paddle.Tensor = None,
linear_smooth: paddle.Tensor = None,
use_neox_rotary_style: bool = False,
use_qk_norm: bool = False,
qk_norm_before_rope: bool = False,
rms_norm_eps: float = 1e-6,
with_sinks: bool = False,
) -> None:
"""
Initializes `LMLayer` with the given parameters.
Args:
fd_config (dict): The config of LM model.
layer_id (int): The id of current layer.
v_head_dim (int, optional): The head dim of value. Defaults to -1.
rope_type (str, optional): The type of RoPE. Defaults to "".
qkv_bias (Optional[paddle.Tensor], optional): The bias of QKV. Defaults to None.
qkv_scale (Optional[paddle.Tensor], optional): The scale of QKV. Defaults to None.
prefix (str, optional): The name of current layer. Defaults to "".
linear_shift (Optional[paddle.Tensor], optional): The shift of linear. Defaults to None.
linear_smooth (Optional[paddle.Tensor], optional): The smooth of linear. Defaults to None.
use_qk_norm (bool, optional): Whether to apply rmsnorm on QA after rope. Defaults to False.
qk_norm_before_rope (bool, optional): Whether to apply rmsnorm before rope (e.g., Qwen style). Defaults to False. if True, use_qk_norm should also be True.
rms_norm_eps (float, optional): The epsilon of RMSNorm. Defaults to 1e-6.
Raises:
ValueError: If the `v_head_dim` is less than 0.
"""
super().__init__()
self.fd_config = fd_config
self.num_heads: int = (
fd_config.model_config.num_attention_heads // fd_config.parallel_config.tensor_parallel_size
)
self.head_dim: int = fd_config.model_config.head_dim
self.layer_id: int = layer_id
num_key_value_heads = getattr(fd_config.model_config, "num_key_value_heads_list", None)
if num_key_value_heads is None:
num_key_value_heads = fd_config.model_config.num_key_value_heads
else:
num_key_value_heads = num_key_value_heads[self.layer_id]
self.kv_num_heads: int = max(
1,
int(num_key_value_heads) // fd_config.parallel_config.tensor_parallel_size,
)
self.v_head_dim: int = v_head_dim if v_head_dim > 0 else self.head_dim
self.rope_type: str = rope_type
self.qk_head_dim: int = self.head_dim
self.prefix: str = prefix
# not use
self.linear_shift: paddle.Tensor | None = linear_shift
self.linear_smooth: paddle.Tensor | None = linear_smooth
self.qkv_bias: paddle.Tensor | None = qkv_bias
self.qkv_scale: paddle.Tensor | None = qkv_scale
self._dtype = self._helper.get_default_dtype()
self.out_scale: float = out_scale
self.use_neox_rotary_style: bool = use_neox_rotary_style
self.with_sinks: bool = with_sinks
if fd_config.quant_config and hasattr(fd_config.quant_config, "kv_cache_quant_type"):
self.quant_method: QuantMethodBase = fd_config.quant_config.get_quant_method(self)
# set for RL model, as RL do not need load state dict
if fd_config.quant_config.kv_cache_quant_type == KvCacheQuantzationTypes.BLOCK_WISE_FP8:
self.cache_quant_type_str = "block_wise_fp8"
self.quant_max_bound = 448.0
self.quant_min_bound = -448.0
else:
self.quant_method = None
if self.quant_method is None:
logger.info(f"Attention is running in cache kv {self._dtype} mode")
else:
logger.info(f"Attention is running in cache kv {self.quant_method.cache_quant_config.quant_type} mode")
self.use_qk_norm = use_qk_norm
self.qk_norm_before_rope = qk_norm_before_rope
self.rms_norm_eps = rms_norm_eps
if self.use_qk_norm:
self.q_norm_key = f"{self.prefix}.q_norm"
self.k_norm_key = f"{self.prefix}.k_norm"
self.init_weight()
if self.with_sinks:
self.sinks = self.create_parameter(
shape=[self.num_heads],
dtype=self._dtype,
is_bias=False,
default_initializer=paddle.nn.initializer.Constant(0),
)
set_weight_attrs(
self.sinks,
{
"output_dim": True,
},
)
if (
hasattr(self.fd_config.model_config, "layer_types")
and self.fd_config.model_config.layer_types[self.layer_id] == "sliding_attention"
):
self.sliding_window = self.fd_config.model_config.sliding_window
else:
self.sliding_window = 0
if (
fd_config.plas_attention_config is not None
and fd_config.plas_attention_config.plas_encoder_top_k_left is not None
and fd_config.plas_attention_config.plas_encoder_top_k_right is not None
and fd_config.plas_attention_config.plas_decoder_top_k_left is not None
and fd_config.plas_attention_config.plas_decoder_top_k_right is not None
):
mlp_weight_path = os.path.join(
fd_config.model_config.model, fd_config.plas_attention_config.mlp_weight_name
)
self.plas_use_mlp = mlp_weight_path is not None and os.path.exists(mlp_weight_path)
plas_block_size = fd_config.plas_attention_config.plas_block_size
plas_max_seq_length = fd_config.plas_attention_config.plas_max_seq_length
if self.plas_use_mlp:
mlp_weight = {}
with safe_open(mlp_weight_path, framework="np", device="cpu") as f:
for key_name in f.keys():
weight = f.get_tensor(key_name)
weight = paddle.Tensor(weight, zero_copy=True)
weight = weight._copy_to(paddle.framework._current_expected_place(), False)
mlp_weight[key_name] = weight
if self.layer_id < fd_config.model_config.num_hidden_layers - 1:
self.attn_gate_weight = mlp_weight[
f"ernie.layers.{self.layer_id}.self_attn.attn_gate.weight"
].astype(paddle.get_default_dtype())[
fd_config.parallel_config.tensor_parallel_rank
* self.kv_num_heads : (fd_config.parallel_config.tensor_parallel_rank + 1)
* self.kv_num_heads
]
assert self.attn_gate_weight.shape[1] % plas_block_size == 0
self.cache_k_block_means = paddle.zeros(
[
fd_config.scheduler_config.max_num_seqs,
plas_max_seq_length // plas_block_size,
self.kv_num_heads,
self.head_dim,
],
dtype=paddle.get_default_dtype(),
)
def init_weight(self):
if self.quant_method is not None:
self.quant_method.create_weights(
self,
weight_loader=(
self.weight_loader if hasattr(self, "weight_loader") else default_weight_loader(self.fd_config)
),
)
if self.use_qk_norm:
self.q_norm_weight = self.create_parameter(
shape=[self.qk_head_dim],
dtype="float32",
is_bias=False,
default_initializer=paddle.nn.initializer.Constant(0),
)
self.k_norm_weight = self.create_parameter(
shape=[self.qk_head_dim],
dtype="float32",
is_bias=False,
default_initializer=paddle.nn.initializer.Constant(0),
)
def load_state_dict(self, state_dict: Dict[str, paddle.Tensor | np.ndarray]):
"""
Attention only have quant related scales not other parameters.
"""
if self.quant_method is not None:
self.quant_method.process_loaded_weights(self, state_dict)
if self.use_qk_norm:
q_norm_weight_tensor = paddle.to_tensor(get_tensor(state_dict.pop(self.q_norm_key + ".weight")))
k_norm_weight_tensor = paddle.to_tensor(get_tensor(state_dict.pop(self.k_norm_key + ".weight")))
self.q_norm_weight.set_value(q_norm_weight_tensor.astype("float32"))
self.k_norm_weight.set_value(k_norm_weight_tensor.astype("float32"))
if self.with_sinks:
sinks_tensor = paddle.to_tensor(get_tensor(state_dict.pop(f"{self.prefix}.sinks")))
self.sinks.set_value(sinks_tensor)
def weight_loader(self, param, loaded_weight, loaded_shard_id: Optional[str] = None):
if self.use_qk_norm and ("q_norm" in param.name or "k_norm" in param.name):
loaded_weight = get_tensor(loaded_weight).astype("float32")
param.copy_(loaded_weight, False)
return
loaded_weight = get_tensor(loaded_weight).cast(paddle.get_default_dtype())
if self.quant_method.cache_quant_config.has_zero_point: # cache_int4_zp
loaded_weight = 1.0 / loaded_weight
else:
loaded_weight = self.quant_method.cache_quant_config.max_bound / loaded_weight
param.copy_(loaded_weight, False)
def forward(
self,
q: paddle.Tensor = None,
k: paddle.Tensor = None,
v: paddle.Tensor = None,
qkv: paddle.Tensor = None,
compressed_kv: paddle.Tensor = None,
k_pe: paddle.Tensor = None,
forward_meta: ForwardMeta = None,
) -> paddle.Tensor:
"""
The forward function of attention layer.
args:
q: the query tensor
k: the key tensor
v: the value tensor
forward_meta: the forward meta data
compressed_kv: optional compressed key-value cache (for MLA)
k_pe: optional key positional encoding (for MLA)
"""
# ============ V1 KVCACHE Manager: Layer-by-layer swap wait ============
# Wait for swap-in of current layer before using cache
if forward_meta.layer_done_counter is not None:
forward_meta.layer_done_counter.wait_for_layer(self.layer_id)
attn_backend = forward_meta.attn_backend
if forward_meta.attn_backends is not None:
attn_backend = forward_meta.attn_backends[self.layer_id]
return attn_backend.forward(
q,
k,
v,
qkv,
compressed_kv,
k_pe,
self,
forward_meta,
)