Add lmul-attention version of Qwen/Qwen2-7B-Instruct
Browse files- .gitattributes +1 -0
- added_tokens.json +5 -0
- chat_template.jinja +6 -0
- config.json +28 -0
- lmul.py +88 -0
- merges.txt +0 -0
- model.safetensors +3 -0
- special_tokens_map.json +20 -0
- tokenizer.json +3 -0
- tokenizer_config.json +43 -0
- vocab.json +0 -0
.gitattributes
CHANGED
|
@@ -33,3 +33,4 @@ saved_model/**/* filter=lfs diff=lfs merge=lfs -text
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
|
|
|
|
|
| 33 |
*.zip filter=lfs diff=lfs merge=lfs -text
|
| 34 |
*.zst filter=lfs diff=lfs merge=lfs -text
|
| 35 |
*tfevents* filter=lfs diff=lfs merge=lfs -text
|
| 36 |
+
tokenizer.json filter=lfs diff=lfs merge=lfs -text
|
added_tokens.json
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"<|endoftext|>": 151643,
|
| 3 |
+
"<|im_end|>": 151645,
|
| 4 |
+
"<|im_start|>": 151644
|
| 5 |
+
}
|
chat_template.jinja
ADDED
|
@@ -0,0 +1,6 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{% for message in messages %}{% if loop.first and messages[0]['role'] != 'system' %}{{ '<|im_start|>system
|
| 2 |
+
You are a helpful assistant.<|im_end|>
|
| 3 |
+
' }}{% endif %}{{'<|im_start|>' + message['role'] + '
|
| 4 |
+
' + message['content'] + '<|im_end|>' + '
|
| 5 |
+
'}}{% endfor %}{% if add_generation_prompt %}{{ '<|im_start|>assistant
|
| 6 |
+
' }}{% endif %}
|
config.json
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"Qwen2ForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"attention_dropout": 0.0,
|
| 6 |
+
"bos_token_id": 151643,
|
| 7 |
+
"eos_token_id": 151645,
|
| 8 |
+
"hidden_act": "silu",
|
| 9 |
+
"hidden_size": 3584,
|
| 10 |
+
"initializer_range": 0.02,
|
| 11 |
+
"intermediate_size": 18944,
|
| 12 |
+
"max_position_embeddings": 32768,
|
| 13 |
+
"max_window_layers": 28,
|
| 14 |
+
"model_type": "qwen2",
|
| 15 |
+
"num_attention_heads": 28,
|
| 16 |
+
"num_hidden_layers": 28,
|
| 17 |
+
"num_key_value_heads": 4,
|
| 18 |
+
"rms_norm_eps": 1e-06,
|
| 19 |
+
"rope_scaling": null,
|
| 20 |
+
"rope_theta": 1000000.0,
|
| 21 |
+
"sliding_window": 131072,
|
| 22 |
+
"tie_word_embeddings": false,
|
| 23 |
+
"torch_dtype": "bfloat16",
|
| 24 |
+
"transformers_version": "4.52.4",
|
| 25 |
+
"use_cache": true,
|
| 26 |
+
"use_sliding_window": false,
|
| 27 |
+
"vocab_size": 152064
|
| 28 |
+
}
|
lmul.py
ADDED
|
@@ -0,0 +1,88 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""
|
| 2 |
+
PyTorch-native implementation of the L-Mul algorithm.
|
| 3 |
+
"""
|
| 4 |
+
from __future__ import annotations
|
| 5 |
+
|
| 6 |
+
import torch
|
| 7 |
+
|
| 8 |
+
__all__ = [
|
| 9 |
+
"l_mul_tensor",
|
| 10 |
+
"l_mul_attention",
|
| 11 |
+
]
|
| 12 |
+
|
| 13 |
+
def l_mul_tensor(
|
| 14 |
+
x: torch.Tensor, y: torch.Tensor, *, offset: float = 1.0 / 16.0
|
| 15 |
+
) -> torch.Tensor:
|
| 16 |
+
"""
|
| 17 |
+
Approximates `x * y` element-wise using the L-Mul algorithm.
|
| 18 |
+
"""
|
| 19 |
+
sign = torch.sign(x) * torch.sign(y)
|
| 20 |
+
|
| 21 |
+
x_abs = torch.abs(x)
|
| 22 |
+
y_abs = torch.abs(y)
|
| 23 |
+
|
| 24 |
+
# Decompose tensors into mantissa and exponent.
|
| 25 |
+
# torch.frexp gives mantissa in [0.5, 1.0)
|
| 26 |
+
mx, ex = torch.frexp(x_abs)
|
| 27 |
+
my, ey = torch.frexp(y_abs)
|
| 28 |
+
|
| 29 |
+
# The paper's logic implies a mantissa in [1.0, 2.0).
|
| 30 |
+
# We reconstruct this by multiplying the mantissa by 2 and adjusting the exponent.
|
| 31 |
+
mant_a = mx * 2.0
|
| 32 |
+
mant_b = my * 2.0
|
| 33 |
+
exp_a = ex - 1
|
| 34 |
+
exp_b = ey - 1
|
| 35 |
+
|
| 36 |
+
# Approximate multiplication using the L-Mul formula
|
| 37 |
+
result_mant = (mant_a - 1.0) + (mant_b - 1.0) + 1.0 + offset
|
| 38 |
+
result_exp = exp_a + exp_b
|
| 39 |
+
|
| 40 |
+
# Reconstruct the final number
|
| 41 |
+
result = torch.ldexp(result_mant, result_exp)
|
| 42 |
+
|
| 43 |
+
# Apply the correct sign and handle zero inputs
|
| 44 |
+
final_result = sign * result
|
| 45 |
+
final_result[x == 0] = 0
|
| 46 |
+
final_result[y == 0] = 0
|
| 47 |
+
|
| 48 |
+
return final_result
|
| 49 |
+
|
| 50 |
+
def l_mul_attention(
|
| 51 |
+
query: torch.Tensor,
|
| 52 |
+
key: torch.Tensor,
|
| 53 |
+
value: torch.Tensor,
|
| 54 |
+
mask: torch.Tensor | None = None,
|
| 55 |
+
dropout: torch.nn.Module | None = None
|
| 56 |
+
) -> tuple[torch.Tensor, torch.Tensor]:
|
| 57 |
+
"""
|
| 58 |
+
Scaled dot-product attention where matrix multiplications are replaced
|
| 59 |
+
by the L-Mul approximation for performance.
|
| 60 |
+
"""
|
| 61 |
+
d_k = query.size(-1)
|
| 62 |
+
|
| 63 |
+
# Approximate Q @ K.T using l_mul_tensor.
|
| 64 |
+
# This requires broadcasting and summing to perform the matrix multiplication.
|
| 65 |
+
scores = l_mul_tensor(
|
| 66 |
+
query.unsqueeze(-1),
|
| 67 |
+
key.transpose(-2, -1).unsqueeze(-3)
|
| 68 |
+
).sum(-2)
|
| 69 |
+
|
| 70 |
+
scores = scores / (d_k ** 0.5)
|
| 71 |
+
|
| 72 |
+
if mask is not None:
|
| 73 |
+
scores = scores.masked_fill(mask == 0, -1e9)
|
| 74 |
+
|
| 75 |
+
attn_probs = torch.nn.functional.softmax(scores, dim=-1)
|
| 76 |
+
if dropout is not None:
|
| 77 |
+
attn_probs = dropout(attn_probs)
|
| 78 |
+
|
| 79 |
+
# Approximate Attn @ V using l_mul_tensor
|
| 80 |
+
output = l_mul_tensor(
|
| 81 |
+
attn_probs.unsqueeze(-1),
|
| 82 |
+
value.unsqueeze(-3)
|
| 83 |
+
).sum(-2)
|
| 84 |
+
|
| 85 |
+
return output, attn_probs
|
| 86 |
+
|
| 87 |
+
|
| 88 |
+
|
merges.txt
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:59e8e631d083a50d3d456f4a241f9bdde3f5929d19f8a6f1a4e74d8568c720ee
|
| 3 |
+
size 15231272120
|
special_tokens_map.json
ADDED
|
@@ -0,0 +1,20 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"additional_special_tokens": [
|
| 3 |
+
"<|im_start|>",
|
| 4 |
+
"<|im_end|>"
|
| 5 |
+
],
|
| 6 |
+
"eos_token": {
|
| 7 |
+
"content": "<|im_end|>",
|
| 8 |
+
"lstrip": false,
|
| 9 |
+
"normalized": false,
|
| 10 |
+
"rstrip": false,
|
| 11 |
+
"single_word": false
|
| 12 |
+
},
|
| 13 |
+
"pad_token": {
|
| 14 |
+
"content": "<|endoftext|>",
|
| 15 |
+
"lstrip": false,
|
| 16 |
+
"normalized": false,
|
| 17 |
+
"rstrip": false,
|
| 18 |
+
"single_word": false
|
| 19 |
+
}
|
| 20 |
+
}
|
tokenizer.json
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:bcfe42da0a4497e8b2b172c1f9f4ec423a46dc12907f4349c55025f670422ba9
|
| 3 |
+
size 11418266
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,43 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"add_prefix_space": false,
|
| 3 |
+
"added_tokens_decoder": {
|
| 4 |
+
"151643": {
|
| 5 |
+
"content": "<|endoftext|>",
|
| 6 |
+
"lstrip": false,
|
| 7 |
+
"normalized": false,
|
| 8 |
+
"rstrip": false,
|
| 9 |
+
"single_word": false,
|
| 10 |
+
"special": true
|
| 11 |
+
},
|
| 12 |
+
"151644": {
|
| 13 |
+
"content": "<|im_start|>",
|
| 14 |
+
"lstrip": false,
|
| 15 |
+
"normalized": false,
|
| 16 |
+
"rstrip": false,
|
| 17 |
+
"single_word": false,
|
| 18 |
+
"special": true
|
| 19 |
+
},
|
| 20 |
+
"151645": {
|
| 21 |
+
"content": "<|im_end|>",
|
| 22 |
+
"lstrip": false,
|
| 23 |
+
"normalized": false,
|
| 24 |
+
"rstrip": false,
|
| 25 |
+
"single_word": false,
|
| 26 |
+
"special": true
|
| 27 |
+
}
|
| 28 |
+
},
|
| 29 |
+
"additional_special_tokens": [
|
| 30 |
+
"<|im_start|>",
|
| 31 |
+
"<|im_end|>"
|
| 32 |
+
],
|
| 33 |
+
"bos_token": null,
|
| 34 |
+
"clean_up_tokenization_spaces": false,
|
| 35 |
+
"eos_token": "<|im_end|>",
|
| 36 |
+
"errors": "replace",
|
| 37 |
+
"extra_special_tokens": {},
|
| 38 |
+
"model_max_length": 131072,
|
| 39 |
+
"pad_token": "<|endoftext|>",
|
| 40 |
+
"split_special_tokens": false,
|
| 41 |
+
"tokenizer_class": "Qwen2Tokenizer",
|
| 42 |
+
"unk_token": null
|
| 43 |
+
}
|
vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|