Text Generation
Transformers
Safetensors
Turkish
ozan_llm
feature-extraction
turkish
turkce
causal-lm
ozanllm
base-model
pretraining
custom_code
Instructions to use coderian/OzanLLM-40M with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use coderian/OzanLLM-40M with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("text-generation", model="coderian/OzanLLM-40M", trust_remote_code=True)# pip install -U transformers accelerate # Load model directly from transformers import AutoModel model = AutoModel.from_pretrained("coderian/OzanLLM-40M", trust_remote_code=True, device_map="auto") - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use coderian/OzanLLM-40M with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "coderian/OzanLLM-40M" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "coderian/OzanLLM-40M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker
docker model run hf.co/coderian/OzanLLM-40M
- SGLang
How to use coderian/OzanLLM-40M with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "coderian/OzanLLM-40M" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "coderian/OzanLLM-40M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "coderian/OzanLLM-40M" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "coderian/OzanLLM-40M", "prompt": "Once upon a time,", "max_tokens": 512, "temperature": 0.5 }' - Docker Model Runner
How to use coderian/OzanLLM-40M with Docker Model Runner:
docker model run hf.co/coderian/OzanLLM-40M
OzanLLM-40M: model kartini ve dosyalari guncelle
Browse files- README.md +187 -0
- config.json +18 -0
- configuration_ozanllm.py +30 -0
- generation_config.json +10 -0
- model.py +202 -0
- model.safetensors +3 -0
- tokenizer.json +0 -0
- tokenizer_config.json +10 -0
README.md
ADDED
|
@@ -0,0 +1,187 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
---
|
| 2 |
+
language:
|
| 3 |
+
- tr
|
| 4 |
+
license: apache-2.0
|
| 5 |
+
library_name: transformers
|
| 6 |
+
pipeline_tag: text-generation
|
| 7 |
+
datasets:
|
| 8 |
+
- tascib/turkish-llm-dataset
|
| 9 |
+
tags:
|
| 10 |
+
- turkish
|
| 11 |
+
- turkce
|
| 12 |
+
- causal-lm
|
| 13 |
+
- ozanllm
|
| 14 |
+
- base-model
|
| 15 |
+
- pretraining
|
| 16 |
+
inference: false
|
| 17 |
+
---
|
| 18 |
+
|
| 19 |
+
# OzanLLM-40M
|
| 20 |
+
|
| 21 |
+
Sıfırdan eğitilmiş **38.3M parametreli küçük bir Türkçe dil modeli**. Base model'dir:
|
| 22 |
+
instruction tuning yapılmamıştır; sohbet veya talimat takibi için değil, **metin
|
| 23 |
+
tamamlama** için tasarlanmıştır.
|
| 24 |
+
|
| 25 |
+
## Model Detayları
|
| 26 |
+
|
| 27 |
+
| | |
|
| 28 |
+
|---|---|
|
| 29 |
+
| Geliştirici | coderian |
|
| 30 |
+
| Yayın tarihi | Eylül 2026 |
|
| 31 |
+
| Dil | Türkçe (tr) |
|
| 32 |
+
| Lisans | Apache 2.0 |
|
| 33 |
+
| Mimari | Decoder-only Transformer (OzanLLM) |
|
| 34 |
+
| Parametre | 38.329.280 (38.3M) |
|
| 35 |
+
| Katman | 5 |
|
| 36 |
+
| Gizli boyut | 320 |
|
| 37 |
+
| Dikkat | Tek kafalı nedensel (causal) self-attention |
|
| 38 |
+
| Bağlam uzunluğu | 512 token |
|
| 39 |
+
| Kelime dağarcığı | 50.000 (BPE) |
|
| 40 |
+
| Konum kodlaması | Öğrenilebilir konum gömlemesi (512 × 320) |
|
| 41 |
+
| Aktivasyon | GELU (FFN genişliği 4× = 1.280) |
|
| 42 |
+
| Normalizasyon | Pre-norm LayerNorm |
|
| 43 |
+
| Çıktı katmanı | LM head (320 → 50.000), bağsız (untied) |
|
| 44 |
+
| KV cache | Yok (`use_cache=False`) |
|
| 45 |
+
| Hassasiyet | float32 |
|
| 46 |
+
|
| 47 |
+
## Mimari
|
| 48 |
+
|
| 49 |
+
- 5 adet Transformer bloğu; her blokta pre-norm LayerNorm ve residual bağlantılar
|
| 50 |
+
- Nedensel öz-dikkat: Q/K/V projeksiyonları → ölçekli iç çarpım (1/√320) → gelecek
|
| 51 |
+
pozisyonların maskelenmesi → softmax → 320 boyutlu çıktı
|
| 52 |
+
- FFN: 320 → 1.280 (GELU) → 320
|
| 53 |
+
- Girdi: token gömlemesi + öğrenilebilir konum gömlemesi toplamı
|
| 54 |
+
- Çıkış: son LayerNorm + LM head; kayıp, kaydırılmış çapraz entropidir
|
| 55 |
+
- Dropout ve ağırlık paylaşımı (weight tying) yok
|
| 56 |
+
|
| 57 |
+
## Hızlı Başlangıç
|
| 58 |
+
|
| 59 |
+
```bash
|
| 60 |
+
pip install -U transformers torch
|
| 61 |
+
```
|
| 62 |
+
|
| 63 |
+
```python
|
| 64 |
+
from transformers import AutoModelForCausalLM, AutoTokenizer
|
| 65 |
+
|
| 66 |
+
model_id = "coderian/OzanLLM-40M"
|
| 67 |
+
tokenizer = AutoTokenizer.from_pretrained(model_id)
|
| 68 |
+
model = AutoModelForCausalLM.from_pretrained(model_id, trust_remote_code=True)
|
| 69 |
+
|
| 70 |
+
prompt = "Türkiye'nin en kalabalık şehri"
|
| 71 |
+
inputs = tokenizer(prompt, return_tensors="pt")
|
| 72 |
+
cikti = model.generate(
|
| 73 |
+
**inputs,
|
| 74 |
+
max_new_tokens=64,
|
| 75 |
+
do_sample=True,
|
| 76 |
+
temperature=0.8,
|
| 77 |
+
top_p=0.9,
|
| 78 |
+
repetition_penalty=1.1, # küçük model tekrara düşmesin
|
| 79 |
+
)
|
| 80 |
+
print(tokenizer.decode(cikti[0], skip_special_tokens=True))
|
| 81 |
+
```
|
| 82 |
+
|
| 83 |
+
`trust_remote_code=True` gereklidir: model mimarisi standart transformers
|
| 84 |
+
sınıflarından biri olmadığı için kendi kodunu (`model.py`,
|
| 85 |
+
`configuration_ozanllm.py`) indirir. transformers 5.x ile yerel bir klasörden
|
| 86 |
+
yüklerken de bu bayrak gereklidir.
|
| 87 |
+
|
| 88 |
+
Modeli indirdiğiniz klasörden yüklemek için:
|
| 89 |
+
|
| 90 |
+
```python
|
| 91 |
+
model = AutoModelForCausalLM.from_pretrained("ozanllm", trust_remote_code=True)
|
| 92 |
+
```
|
| 93 |
+
|
| 94 |
+
## Üretim Notları
|
| 95 |
+
|
| 96 |
+
- Model belgeleri `[BOS] ... [EOS]` çerçevesiyle eğitildi; en iyi sonuç için prompt'un
|
| 97 |
+
başına `bos_token_id` (2) ekleyin.
|
| 98 |
+
- Bağlam 512 token ile sınırlıdır; bu sınırın üzerindeki girdiler desteklenmez.
|
| 99 |
+
- KV cache olmadığı için üretim token token ilerler ve uzun çıktılar yavaştır.
|
| 100 |
+
- 38M'lik bir modelde tekrara düşme sık görülür; `repetition_penalty≈1.1` önerilir.
|
| 101 |
+
- Örnekleme için `temperature≈0.8`, `top_p≈0.9` iyi çalışır.
|
| 102 |
+
|
| 103 |
+
## Eğitim
|
| 104 |
+
|
| 105 |
+
### Veri
|
| 106 |
+
|
| 107 |
+
| | |
|
| 108 |
+
|---|---|
|
| 109 |
+
| Veri seti | [tascib/turkish-llm-dataset](https://huggingface.co/datasets/tascib/turkish-llm-dataset) (CC BY-SA 4.0) |
|
| 110 |
+
| Kullanılan dilim | Akış (streaming) modunda alınan ilk 600.000 belge; yaklaşık 213M token |
|
| 111 |
+
| İşleme | Belgeler `[BOS] … [EOS]` olarak token'lanır ve 512'lik dizilere paketlenir; padding yoktur |
|
| 112 |
+
| Tokenizer | [coderian/QraXAi-Turkish-Tok-50K](https://huggingface.co/coderian/QraXAi-Turkish-Tok-50K) — BPE, 50.000 token, `[PAD]=0 [UNK]=1 [BOS]=2 [EOS]=3` |
|
| 113 |
+
|
| 114 |
+
### Hiperparametreler
|
| 115 |
+
|
| 116 |
+
| | |
|
| 117 |
+
|---|---|
|
| 118 |
+
| Optimizer adımı | 3.252 |
|
| 119 |
+
| Etkin batch | 128 dizi × 512 token = 65.536 token/adım |
|
| 120 |
+
| Toplam token | ≈213M |
|
| 121 |
+
| Optimizer | AdamW (β₁=0.9, β₂=0.95, weight decay 0.1) |
|
| 122 |
+
| Öğrenme oranı | 3e-4; 200 adım ısınma, ardından kosinüs ile 0.1×'e düşüş |
|
| 123 |
+
| Gradyan kırpma | 1.0 |
|
| 124 |
+
| Karışık hassasiyet | fp16 + GradScaler |
|
| 125 |
+
| Donanım | Kaggle, 2× Tesla T4 (otomatik DataParallel) |
|
| 126 |
+
| Çerçeve | PyTorch + Hugging Face transformers/datasets, safetensors |
|
| 127 |
+
|
| 128 |
+
Eğitim betiği tek dosyadır (`train.py`); model boyutları `EMBED_DIM=320`,
|
| 129 |
+
`N_LAYERS=5`, `SEQ_LEN=512`, `BATCH_SIZE=16`, `GRAD_ACCUM=8` ile ayarlanmıştır.
|
| 130 |
+
|
| 131 |
+
## Kullanım Amacı
|
| 132 |
+
|
| 133 |
+
Uygun kullanımlar:
|
| 134 |
+
|
| 135 |
+
- Türkçe metin tamamlama, taslak sürdürme, yaratıcı yazı denemeleri
|
| 136 |
+
- Türkçe NLP araştırmaları ve küçük model deneyleri
|
| 137 |
+
- Fine-tuning için başlangıç noktası (base model)
|
| 138 |
+
|
| 139 |
+
Uygun olmayan kullanımlar:
|
| 140 |
+
|
| 141 |
+
- Soru-cevap, sohbet, talimat takibi (instruction tuning yoktur)
|
| 142 |
+
- Olgusal doğruluk gerektiren üretim kullanımları
|
| 143 |
+
- Hukuki, tıbbi veya güvenlik açısından kritik karar sistemleri
|
| 144 |
+
|
| 145 |
+
## Sınırlamalar ve Önyargılar
|
| 146 |
+
|
| 147 |
+
- 38M parametre ve ≈213M token ile eğitilmiş küçük bir base modeldir; bilgisi,
|
| 148 |
+
akıcılığı ve tutarlılığı sınırlıdır.
|
| 149 |
+
- Uzun üretimlerde tekrara düşebilir veya konudan sapabilir.
|
| 150 |
+
- Eğitim verisi filtrelenmemiş web metni içerdiği için model önyargılı veya
|
| 151 |
+
istenmeyen ifadeler üretebilir; çıktılar kullanılmadan önce gözden geçirilmelidir.
|
| 152 |
+
- Türkçe dışındaki dillerde ve kod üretiminde güvenilir değildir.
|
| 153 |
+
- Resmî bir benchmark değerlendirmesi yapılmamıştır.
|
| 154 |
+
|
| 155 |
+
## Dosyalar
|
| 156 |
+
|
| 157 |
+
| Dosya | Açıklama |
|
| 158 |
+
|---|---|
|
| 159 |
+
| `model.safetensors` | Model ağırlıkları (float32, ~153 MB) |
|
| 160 |
+
| `config.json` | Model yapılandırması (`auto_map` ile özel kod bağlantıları) |
|
| 161 |
+
| `generation_config.json` | Varsayılan üretim ayarları (BOS/EOS/PAD id'leri) |
|
| 162 |
+
| `model.py` | OzanLLM mimarisi (`trust_remote_code`) |
|
| 163 |
+
| `configuration_ozanllm.py` | `GPTConfig` yapılandırma sınıfı |
|
| 164 |
+
| `tokenizer.json`, `tokenizer_config.json` | Tokenizer (50.000 token) |
|
| 165 |
+
| `README.md` | Bu model kartı |
|
| 166 |
+
|
| 167 |
+
## Atıf
|
| 168 |
+
|
| 169 |
+
```bibtex
|
| 170 |
+
@misc{ozanllm-40m,
|
| 171 |
+
title = {OzanLLM-40M: Turkish Base Language Model},
|
| 172 |
+
author = {coderian},
|
| 173 |
+
year = {2026},
|
| 174 |
+
url = {https://huggingface.co/coderian/OzanLLM-40M}
|
| 175 |
+
}
|
| 176 |
+
```
|
| 177 |
+
|
| 178 |
+
---
|
| 179 |
+
|
| 180 |
+
## English Summary
|
| 181 |
+
|
| 182 |
+
**OzanLLM-40M** is a 38.3M-parameter Turkish base language model trained from
|
| 183 |
+
scratch on the first 600k documents (~213M tokens) of the
|
| 184 |
+
`tascib/turkish-llm-dataset`. It is a 5-layer, 320-dim decoder-only Transformer
|
| 185 |
+
with single-head causal attention, learned absolute position embeddings and a
|
| 186 |
+
50k BPE tokenizer. It is a base model intended for Turkish text completion, not
|
| 187 |
+
for chat or instruction following. Use `trust_remote_code=True` to load it.
|
config.json
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"architectures": [
|
| 3 |
+
"OzanForCausalLM"
|
| 4 |
+
],
|
| 5 |
+
"auto_map": {
|
| 6 |
+
"AutoConfig": "configuration_ozanllm.GPTConfig",
|
| 7 |
+
"AutoModel": "model.OzanForCausalLM",
|
| 8 |
+
"AutoModelForCausalLM": "model.OzanForCausalLM"
|
| 9 |
+
},
|
| 10 |
+
"dtype": "float32",
|
| 11 |
+
"embed_dim": 320,
|
| 12 |
+
"max_seq_len": 512,
|
| 13 |
+
"model_type": "ozan_llm",
|
| 14 |
+
"n_layers": 5,
|
| 15 |
+
"transformers_version": "5.0.0",
|
| 16 |
+
"use_cache": false,
|
| 17 |
+
"vocab_size": 50000
|
| 18 |
+
}
|
configuration_ozanllm.py
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import PretrainedConfig
|
| 2 |
+
|
| 3 |
+
class GPTConfig(PretrainedConfig):
|
| 4 |
+
|
| 5 |
+
model_type = "ozan_llm"
|
| 6 |
+
|
| 7 |
+
# Hugging Face'te model kendi koduyla birlikte yuklenebilsin diye:
|
| 8 |
+
# config.json'a yazilir, kullanici trust_remote_code=True ile yukler.
|
| 9 |
+
auto_map = {
|
| 10 |
+
"AutoConfig": "configuration_ozanllm.GPTConfig",
|
| 11 |
+
"AutoModel": "model.OzanForCausalLM",
|
| 12 |
+
"AutoModelForCausalLM": "model.OzanForCausalLM",
|
| 13 |
+
}
|
| 14 |
+
|
| 15 |
+
def __init__(
|
| 16 |
+
self,
|
| 17 |
+
vocab_size=10000,
|
| 18 |
+
n_layers=6,
|
| 19 |
+
max_seq_len=512,
|
| 20 |
+
embed_dim=256,
|
| 21 |
+
use_cache=False,
|
| 22 |
+
**kwargs
|
| 23 |
+
):
|
| 24 |
+
super().__init__(**kwargs)
|
| 25 |
+
|
| 26 |
+
self.use_cache = use_cache
|
| 27 |
+
self.vocab_size = vocab_size
|
| 28 |
+
self.n_layers = n_layers
|
| 29 |
+
self.max_seq_len = max_seq_len
|
| 30 |
+
self.embed_dim = embed_dim
|
generation_config.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"_from_model_config": true,
|
| 3 |
+
"bos_token_id": 2,
|
| 4 |
+
"eos_token_id": 3,
|
| 5 |
+
"output_attentions": false,
|
| 6 |
+
"output_hidden_states": false,
|
| 7 |
+
"pad_token_id": 0,
|
| 8 |
+
"transformers_version": "5.0.0",
|
| 9 |
+
"use_cache": false
|
| 10 |
+
}
|
model.py
ADDED
|
@@ -0,0 +1,202 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from transformers import GenerationMixin, PreTrainedModel
|
| 2 |
+
from transformers.modeling_outputs import CausalLMOutput
|
| 3 |
+
import torch.nn as nn
|
| 4 |
+
import torch
|
| 5 |
+
import math
|
| 6 |
+
|
| 7 |
+
try:
|
| 8 |
+
from .configuration_ozanllm import GPTConfig
|
| 9 |
+
except ImportError:
|
| 10 |
+
from configuration_ozanllm import GPTConfig
|
| 11 |
+
|
| 12 |
+
class CausalSelfAttention(nn.Module):
|
| 13 |
+
|
| 14 |
+
def __init__(
|
| 15 |
+
self,
|
| 16 |
+
embed_dim
|
| 17 |
+
):
|
| 18 |
+
super().__init__()
|
| 19 |
+
|
| 20 |
+
self.q_proj = nn.Linear(embed_dim, embed_dim)
|
| 21 |
+
self.k_proj = nn.Linear(embed_dim, embed_dim)
|
| 22 |
+
self.v_proj = nn.Linear(embed_dim, embed_dim)
|
| 23 |
+
|
| 24 |
+
self.o_proj = nn.Linear(embed_dim, embed_dim)
|
| 25 |
+
|
| 26 |
+
def forward(self, x):
|
| 27 |
+
batch_size, seq_len, embed_dim = x.shape
|
| 28 |
+
|
| 29 |
+
Q = self.q_proj(x)
|
| 30 |
+
K = self.k_proj(x)
|
| 31 |
+
V = self.v_proj(x)
|
| 32 |
+
|
| 33 |
+
scores = Q @ K.transpose(-2,-1)
|
| 34 |
+
|
| 35 |
+
scores = scores / math.sqrt(embed_dim)
|
| 36 |
+
|
| 37 |
+
mask = torch.triu(
|
| 38 |
+
torch.ones(
|
| 39 |
+
seq_len,
|
| 40 |
+
seq_len,
|
| 41 |
+
device=x.device
|
| 42 |
+
),
|
| 43 |
+
diagonal=1
|
| 44 |
+
).bool()
|
| 45 |
+
|
| 46 |
+
scores = scores.masked_fill(mask, torch.finfo(scores.dtype).min)
|
| 47 |
+
|
| 48 |
+
attention_w = torch.softmax(
|
| 49 |
+
scores,
|
| 50 |
+
dim=-1
|
| 51 |
+
)
|
| 52 |
+
|
| 53 |
+
output = attention_w @ V
|
| 54 |
+
|
| 55 |
+
output = self.o_proj(output)
|
| 56 |
+
|
| 57 |
+
return output
|
| 58 |
+
|
| 59 |
+
class TransformerBlock(nn.Module):
|
| 60 |
+
|
| 61 |
+
def __init__(
|
| 62 |
+
self,
|
| 63 |
+
embed_dim
|
| 64 |
+
):
|
| 65 |
+
super().__init__()
|
| 66 |
+
|
| 67 |
+
self.ln1 = nn.LayerNorm(embed_dim)
|
| 68 |
+
|
| 69 |
+
self.attention = CausalSelfAttention(
|
| 70 |
+
embed_dim
|
| 71 |
+
)
|
| 72 |
+
|
| 73 |
+
self.ln2 = nn.LayerNorm(embed_dim)
|
| 74 |
+
|
| 75 |
+
self.ffn = nn.Sequential(
|
| 76 |
+
nn.Linear(
|
| 77 |
+
in_features=embed_dim,
|
| 78 |
+
out_features=4*embed_dim
|
| 79 |
+
),
|
| 80 |
+
|
| 81 |
+
nn.GELU(),
|
| 82 |
+
|
| 83 |
+
nn.Linear(
|
| 84 |
+
in_features=4*embed_dim,
|
| 85 |
+
out_features=embed_dim
|
| 86 |
+
)
|
| 87 |
+
)
|
| 88 |
+
|
| 89 |
+
def forward(self, x):
|
| 90 |
+
|
| 91 |
+
x = x + self.attention(
|
| 92 |
+
self.ln1(x)
|
| 93 |
+
)
|
| 94 |
+
|
| 95 |
+
x = x + self.ffn(
|
| 96 |
+
self.ln2(x)
|
| 97 |
+
)
|
| 98 |
+
|
| 99 |
+
return x
|
| 100 |
+
|
| 101 |
+
|
| 102 |
+
class OzanForCausalLM(PreTrainedModel, GenerationMixin):
|
| 103 |
+
|
| 104 |
+
config_class = GPTConfig
|
| 105 |
+
|
| 106 |
+
def __init__(
|
| 107 |
+
self,
|
| 108 |
+
config
|
| 109 |
+
):
|
| 110 |
+
|
| 111 |
+
super().__init__(config)
|
| 112 |
+
|
| 113 |
+
self.token_embedding = nn.Embedding(
|
| 114 |
+
config.vocab_size,
|
| 115 |
+
config.embed_dim
|
| 116 |
+
)
|
| 117 |
+
|
| 118 |
+
self.position_embedding = nn.Embedding(
|
| 119 |
+
config.max_seq_len,
|
| 120 |
+
config.embed_dim
|
| 121 |
+
)
|
| 122 |
+
|
| 123 |
+
self.transformer_blocks = nn.ModuleList([
|
| 124 |
+
TransformerBlock(config.embed_dim)
|
| 125 |
+
for _ in range(config.n_layers)
|
| 126 |
+
])
|
| 127 |
+
|
| 128 |
+
self.ln_f = nn.LayerNorm(
|
| 129 |
+
config.embed_dim
|
| 130 |
+
)
|
| 131 |
+
|
| 132 |
+
|
| 133 |
+
self.lm_head = nn.Linear(
|
| 134 |
+
config.embed_dim,
|
| 135 |
+
config.vocab_size,
|
| 136 |
+
bias=False
|
| 137 |
+
)
|
| 138 |
+
|
| 139 |
+
self.post_init()
|
| 140 |
+
|
| 141 |
+
def forward(
|
| 142 |
+
self,
|
| 143 |
+
input_ids,
|
| 144 |
+
labels=None,
|
| 145 |
+
**kwargs
|
| 146 |
+
):
|
| 147 |
+
|
| 148 |
+
batch_size, seq_len = input_ids.shape
|
| 149 |
+
|
| 150 |
+
if seq_len > self.config.max_seq_len:
|
| 151 |
+
raise ValueError(
|
| 152 |
+
f"Sequence length ({seq_len}) "
|
| 153 |
+
f"cannot be greater than "
|
| 154 |
+
f"max_seq_len ({self.config.max_seq_len})"
|
| 155 |
+
)
|
| 156 |
+
|
| 157 |
+
positions = torch.arange(
|
| 158 |
+
seq_len,
|
| 159 |
+
device=input_ids.device
|
| 160 |
+
)
|
| 161 |
+
|
| 162 |
+
token_emb = self.token_embedding(
|
| 163 |
+
input_ids
|
| 164 |
+
)
|
| 165 |
+
|
| 166 |
+
pos_emb = self.position_embedding(
|
| 167 |
+
positions
|
| 168 |
+
)
|
| 169 |
+
|
| 170 |
+
x = token_emb + pos_emb
|
| 171 |
+
|
| 172 |
+
for block in self.transformer_blocks:
|
| 173 |
+
x = block(x)
|
| 174 |
+
|
| 175 |
+
x = self.ln_f(x)
|
| 176 |
+
|
| 177 |
+
logits = self.lm_head(x)
|
| 178 |
+
|
| 179 |
+
loss = None
|
| 180 |
+
|
| 181 |
+
if labels is not None:
|
| 182 |
+
|
| 183 |
+
shift_logits = logits[
|
| 184 |
+
:, :-1, :
|
| 185 |
+
].contiguous()
|
| 186 |
+
|
| 187 |
+
shift_labels = labels[
|
| 188 |
+
:, 1:
|
| 189 |
+
].contiguous()
|
| 190 |
+
|
| 191 |
+
loss = nn.functional.cross_entropy(
|
| 192 |
+
shift_logits.view(
|
| 193 |
+
-1,
|
| 194 |
+
shift_logits.size(-1)
|
| 195 |
+
),
|
| 196 |
+
shift_labels.view(-1)
|
| 197 |
+
)
|
| 198 |
+
|
| 199 |
+
return CausalLMOutput(
|
| 200 |
+
loss=loss,
|
| 201 |
+
logits=logits
|
| 202 |
+
)
|
model.safetensors
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:b5863fe6bf3208cd8d2fb431d394d6be7fdcd4ec2d60f400625f4c60b7c0117a
|
| 3 |
+
size 153326288
|
tokenizer.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|
tokenizer_config.json
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"backend": "tokenizers",
|
| 3 |
+
"bos_token": "[BOS]",
|
| 4 |
+
"eos_token": "[EOS]",
|
| 5 |
+
"is_local": false,
|
| 6 |
+
"model_max_length": 1000000000000000019884624838656,
|
| 7 |
+
"pad_token": "[PAD]",
|
| 8 |
+
"tokenizer_class": "TokenizersBackend",
|
| 9 |
+
"unk_token": "[UNK]"
|
| 10 |
+
}
|