aigencydev commited on
Commit
71edddd
·
verified ·
1 Parent(s): 130b48f

Upload modeling_erk_linear.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. modeling_erk_linear.py +63 -0
modeling_erk_linear.py ADDED
@@ -0,0 +1,63 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ Erk-Linear — Erk-14B'nin 8 dikkat katmanini Gated DeltaNet'e damitan %20-lineer hibrit.
3
+
4
+ Yukleme:
5
+ # gerekli: pip install torch transformers flash-linear-attention safetensors huggingface_hub
6
+ from modeling_erk_linear import load_erk_linear
7
+ model, tokenizer = load_erk_linear() # Erk-14B tabanini + GDN agirliklarini indirir
8
+ out = model.generate(**tokenizer("Merhaba", return_tensors="pt").to(model.device))
9
+
10
+ Model, Qwen3-14B mimarisine dayanir; 8 katmanin softmax dikkati subquadratic Gated DeltaNet ile
11
+ degistirilmis, kalan 32 katman softmax "cipa" olarak korunmustur. Ayrinti: teknik rapor / GitHub.
12
+ """
13
+ import torch
14
+ import torch.nn as nn
15
+ from transformers import AutoModelForCausalLM, AutoTokenizer
16
+ from safetensors.torch import load_file
17
+ from huggingface_hub import hf_hub_download
18
+
19
+ BASE_MODEL = "ecloudtech/Erk-14B" # Qwen3-14B temelli Turkce model
20
+ REPO_ID = "ecloudtech/Erk-Linear"
21
+ GDN_LAYERS = [1, 3, 5, 7, 10, 36, 38, 39] # %20 lineer, yayilmis yerlesim
22
+
23
+
24
+ class _GDNAttention(nn.Module):
25
+ """Qwen3 self_attn cagri imzasiyla uyumlu Gated DeltaNet sarmalayici."""
26
+ def __init__(self, gdn):
27
+ super().__init__()
28
+ self.gdn = gdn
29
+
30
+ def forward(self, hidden_states, *args, **kwargs):
31
+ out = self.gdn(hidden_states)
32
+ y = out[0] if isinstance(out, tuple) else out
33
+ return (y, None)
34
+
35
+
36
+ def load_erk_linear(device="cuda", dtype=torch.bfloat16,
37
+ base_model=BASE_MODEL, repo_id=REPO_ID):
38
+ """Erk-Linear hibridini kurar ve (model, tokenizer) doner."""
39
+ from fla.layers import GatedDeltaNet # flash-linear-attention
40
+
41
+ model = AutoModelForCausalLM.from_pretrained(base_model, torch_dtype=dtype).to(device).eval()
42
+ H = model.config.hidden_size
43
+
44
+ gdn_path = hf_hub_download(repo_id=repo_id, filename="gdn_weights.safetensors")
45
+ state = load_file(gdn_path)
46
+
47
+ for li in GDN_LAYERS:
48
+ gdn = GatedDeltaNet(hidden_size=H, head_dim=128, num_heads=40,
49
+ use_gate=True, use_short_conv=True, mode="chunk")
50
+ prefix = f"L{li}."
51
+ layer_sd = {k[len(prefix):]: v for k, v in state.items() if k.startswith(prefix)}
52
+ gdn.load_state_dict(layer_sd)
53
+ gdn = gdn.to(device).to(dtype).eval()
54
+ model.model.layers[li].self_attn = _GDNAttention(gdn).to(device).to(dtype)
55
+
56
+ tokenizer = AutoTokenizer.from_pretrained(base_model)
57
+ return model, tokenizer
58
+
59
+
60
+ if __name__ == "__main__":
61
+ m, t = load_erk_linear()
62
+ ids = t("Türkiye'nin başkenti", return_tensors="pt").to(m.device)
63
+ print(t.decode(m.generate(**ids, max_new_tokens=12)[0], skip_special_tokens=True))