Update README.md
Browse files
README.md
CHANGED
|
@@ -60,34 +60,66 @@ Input → [Global Mem | Local Mem | Chunk Tokens] → Transformer → Output
|
|
| 60 |
|
| 61 |
```python
|
| 62 |
import torch
|
| 63 |
-
|
|
|
|
| 64 |
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
|
|
|
| 69 |
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
model = model.to(device)
|
| 73 |
|
| 74 |
-
|
| 75 |
-
|
|
|
|
|
|
|
|
|
|
| 76 |
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
|
| 81 |
-
# Inference
|
| 82 |
with torch.no_grad():
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
global_memory=
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 91 |
```
|
| 92 |
|
| 93 |
---
|
|
|
|
| 60 |
|
| 61 |
```python
|
| 62 |
import torch
|
| 63 |
+
import torch.nn.functional as F
|
| 64 |
+
import re
|
| 65 |
|
| 66 |
+
def sample_next_token(logits, temperature=0.1, top_p=0.9, repetition_penalty=1.2, input_ids=None):
|
| 67 |
+
# Tekrar cezası
|
| 68 |
+
if input_ids is not None:
|
| 69 |
+
for token_id in set(input_ids[0].tolist()):
|
| 70 |
+
logits[token_id] /= repetition_penalty
|
| 71 |
|
| 72 |
+
logits = logits / temperature
|
| 73 |
+
probs = F.softmax(logits, dim=-1)
|
|
|
|
| 74 |
|
| 75 |
+
sorted_probs, sorted_indices = torch.sort(probs, descending=True)
|
| 76 |
+
cumulative_probs = torch.cumsum(sorted_probs, dim=-1)
|
| 77 |
+
sorted_indices_to_remove = cumulative_probs - sorted_probs > top_p
|
| 78 |
+
sorted_probs[sorted_indices_to_remove] = 0
|
| 79 |
+
sorted_probs /= sorted_probs.sum()
|
| 80 |
|
| 81 |
+
next_token = torch.multinomial(sorted_probs, num_samples=1)
|
| 82 |
+
return sorted_indices[next_token]
|
| 83 |
+
|
| 84 |
+
def fix_spacing(text):
|
| 85 |
+
# Noktalama önündeki boşlukları sil
|
| 86 |
+
text = re.sub(r'\s+([.,!?;:)])', r'\1', text)
|
| 87 |
+
# Fazla boşlukları tek boşluğa indir
|
| 88 |
+
text = re.sub(r'\s+', ' ', text)
|
| 89 |
+
return text.strip()
|
| 90 |
+
|
| 91 |
+
prev_memory = torch.zeros(1, config.num_memory_tokens, config.d_model, device=device)
|
| 92 |
+
global_memory = torch.zeros(1, config.num_global_memory_tokens, config.d_model, device=device)
|
| 93 |
+
|
| 94 |
+
text = "Kullanıcı: Merhaba, nasılsın?\nAsistan:"
|
| 95 |
+
inputs = tokenizer(text, return_tensors="pt").to(device)
|
| 96 |
+
generated = inputs["input_ids"].clone()
|
| 97 |
|
|
|
|
| 98 |
with torch.no_grad():
|
| 99 |
+
for _ in range(100):
|
| 100 |
+
input_chunk = generated[:, -128:]
|
| 101 |
+
|
| 102 |
+
logits, prev_memory, global_memory = model(
|
| 103 |
+
input_ids=input_chunk,
|
| 104 |
+
prev_memory=prev_memory,
|
| 105 |
+
global_memory=global_memory
|
| 106 |
+
)
|
| 107 |
+
|
| 108 |
+
next_token = sample_next_token(
|
| 109 |
+
logits[0, -1, :],
|
| 110 |
+
temperature=0.1,
|
| 111 |
+
top_p=0.9,
|
| 112 |
+
repetition_penalty=1.3,
|
| 113 |
+
input_ids=generated
|
| 114 |
+
).unsqueeze(0)
|
| 115 |
+
|
| 116 |
+
generated = torch.cat([generated, next_token], dim=1)
|
| 117 |
+
|
| 118 |
+
if next_token.item() == tokenizer.eos_token_id:
|
| 119 |
+
break
|
| 120 |
+
|
| 121 |
+
output = tokenizer.decode(generated[0], skip_special_tokens=True, clean_up_tokenization_spaces=True)
|
| 122 |
+
print(fix_spacing(output))
|
| 123 |
```
|
| 124 |
|
| 125 |
---
|