File size: 4,814 Bytes
c4b9df5
 
 
 
6ae2e74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
cde7cc2
 
6ae2e74
cde7cc2
 
 
 
 
6ae2e74
cde7cc2
 
6ae2e74
cde7cc2
 
 
 
 
6ae2e74
cde7cc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae2e74
 
cde7cc2
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6ae2e74
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
---
language:
- tr
- en
license: apache-2.0
tags:
- turkish
- language-model
- custom-architecture
- memory-network
- causal-lm
pipeline_tag: text-generation
---

# SykoLLM-CMN V5.2 Beta

SykoLLM-CMN, Türkçe için sıfırdan tasarlanmış, özgün bir **Contextual Memory Network (CMN)** mimarisine sahip bir dil modelidir. Standart Transformer bloklarının yanına yerleştirilmiş yerel ve global bellek kapıları sayesinde, uzun bağlamları chunk'lara bölerek işler ve bilgiyi oturumlar boyunca taşıyabilir.

---

## Model Detayları

| Özellik | Değer |
|---|---|
| Mimari | Transformer + CMN (Contextual Memory Network) |
| Parametre Sayısı | ~100-300M |
| Diller | Türkçe, İngilizce |
| Bağlam Uzunluğu | 1024 token |
| Chunk Boyutu | 128 token |
| Katman Sayısı | 24 |
| Model Boyutu (d_model) | 768 |
| Attention Head | 6 |
| Lokal Bellek Token | 16 |
| Global Bellek Token | 32 |
| Vocab Size | 32.000 |
| Lisans | Apache 2.0 |

---

## CMN Mimarisi Nedir?

SykoLLM-CMN, standart decoder-only modellerden farklı olarak iki katmanlı bir bellek sistemi içerir:

- **Lokal Bellek (SykoMemoryGate):** Her chunk işlenirken bir önceki chunk'tan gelen bilgiyi taşır. Klasik GRU/LSTM kapı mantığına benzer şekilde çalışır — ne unutacağını, ne öğreneceğini öğrenir.

- **Global Bellek (SykoSmartMemoryGate):** Tüm sekansa ait özet bilgiyi tutar. Cross-attention kullanarak "bu oturumda benim için ne önemli?" diye sorar ve buna göre güncellenir.

Bu yapı sayesinde model, bağlamı chunk'lara bölerek işlerken uzun vadeli bağlamı kaybetmez.

```
Input → [Global Mem | Local Mem | Chunk Tokens] → Transformer → Output
                ↑                    ↑
         SmartMemoryGate        MemoryGate
         (cross-attention)      (forget/update)
```

---

## Kullanım

```python
import torch
import torch.nn.functional as F
import re

def sample_next_token(logits, temperature=0.1, top_p=0.9, repetition_penalty=1.2, input_ids=None):
    # Tekrar cezası
    if input_ids is not None:
        for token_id in set(input_ids[0].tolist()):
            logits[token_id] /= repetition_penalty

    logits = logits / temperature
    probs  = F.softmax(logits, dim=-1)

    sorted_probs, sorted_indices = torch.sort(probs, descending=True)
    cumulative_probs = torch.cumsum(sorted_probs, dim=-1)
    sorted_indices_to_remove = cumulative_probs - sorted_probs > top_p
    sorted_probs[sorted_indices_to_remove] = 0
    sorted_probs /= sorted_probs.sum()

    next_token = torch.multinomial(sorted_probs, num_samples=1)
    return sorted_indices[next_token]

def fix_spacing(text):
    # Noktalama önündeki boşlukları sil
    text = re.sub(r'\s+([.,!?;:)])', r'\1', text)
    # Fazla boşlukları tek boşluğa indir
    text = re.sub(r'\s+', ' ', text)
    return text.strip()

prev_memory   = torch.zeros(1, config.num_memory_tokens,        config.d_model, device=device)
global_memory = torch.zeros(1, config.num_global_memory_tokens, config.d_model, device=device)

text      = "Kullanıcı: Merhaba, nasılsın?\nAsistan:"
inputs    = tokenizer(text, return_tensors="pt").to(device)
generated = inputs["input_ids"].clone()

with torch.no_grad():
    for _ in range(100):
        input_chunk = generated[:, -128:]

        logits, prev_memory, global_memory = model(
            input_ids=input_chunk,
            prev_memory=prev_memory,
            global_memory=global_memory
        )

        next_token = sample_next_token(
            logits[0, -1, :],
            temperature=0.1,
            top_p=0.9,
            repetition_penalty=1.3,
            input_ids=generated
        ).unsqueeze(0)

        generated = torch.cat([generated, next_token], dim=1)

        if next_token.item() == tokenizer.eos_token_id:
            break

output = tokenizer.decode(generated[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
print(fix_spacing(output))
```

---

## Eğitim

- **Dataset:** [SykoSLM/DeepReason-Mix](https://huggingface.co/datasets/SykoSLM/DeepReason-Mix)
- **Eğitim Örneği:** 300.000
- **Epochs:** 2
- **Batch Size:** 32
- **Gradient Accumulation:** 4
- **Optimizer:** AdamW
- **LR Scheduler:** Cosine Annealing + Linear Warmup
- **Başlangıç LR:** 3e-4 → Min: 1e-6
- **Mixed Precision:** FP16
- **Donanım:** 2× NVIDIA T4 (Kaggle)

---

## Sınırlamalar

- Model hâlâ beta aşamasındadır, üretim ortamında kullanmadan önce test edin.
- Uzun yanıtlarda tekrar ve tutarsızlık görülebilir.
- Zararlı içerik filtrelemesi uygulanmamıştır, gerekirse kendi filtrenizi ekleyin.

---

## Lisans

Apache 2.0 — Ticari kullanıma açıktır. Detaylar için [LICENSE](LICENSE) dosyasına bakın.

---

## Yazar

**SykoSLM** — Türkçe doğal dil işleme için özgün mimari araştırmaları.