-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathllm_library_impl.py
More file actions
1401 lines (1135 loc) · 47.8 KB
/
llm_library_impl.py
File metadata and controls
1401 lines (1135 loc) · 47.8 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
"""
프로덕션급 Transformer LLM 라이브러리
- 실제 BPE 토크나이저
- Flash Attention 지원
- 완전한 KV-Cache 구현
- 다양한 최적화 기법
- 분산 학습 지원
- RLHF 준비
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
from torch.utils.data import Dataset, DataLoader
from torch.nn.parallel import DistributedDataParallel as DDP
from typing import Optional, Tuple, List, Dict, Any, Union
import math
import json
import os
import re
from collections import Counter, OrderedDict
from dataclasses import dataclass
import numpy as np
from pathlib import Path
# =====================================================
# 1. 프로덕션급 BPE 토크나이저
# =====================================================
class BPETokenizer:
"""실제 BPE (Byte Pair Encoding) 토크나이저 구현"""
def __init__(self, vocab_size: int = 50000):
self.vocab_size = vocab_size
self.encoder: Dict[str, int] = {}
self.decoder: Dict[int, str] = {}
self.bpe_ranks: Dict[Tuple[str, str], int] = {}
# Special tokens
self.special_tokens = {
'<PAD>': 0,
'<UNK>': 1,
'<BOS>': 2,
'<EOS>': 3,
'<MASK>': 4,
}
self.pad_token_id = 0
self.unk_token_id = 1
self.bos_token_id = 2
self.eos_token_id = 3
self.mask_token_id = 4
# Initialize with special tokens
self.encoder = self.special_tokens.copy()
self.decoder = {v: k for k, v in self.encoder.items()}
def train(self, texts: List[str], min_frequency: int = 2):
"""BPE 학습"""
print(f"Training BPE tokenizer on {len(texts)} texts...")
# 1. 문자 단위로 초기화
vocab = Counter()
for text in texts:
# 단어 분리 및 빈도 계산
words = text.split()
for word in words:
# 단어 끝 표시 추가
vocab[' '.join(list(word)) + ' </w>'] += 1
# 2. 빈도가 낮은 단어 제거
vocab = {word: freq for word, freq in vocab.items() if freq >= min_frequency}
# 3. 반복적으로 가장 빈번한 바이트 쌍 병합
num_merges = self.vocab_size - len(self.special_tokens)
for i in range(num_merges):
pairs = self._get_stats(vocab)
if not pairs:
break
best_pair = max(pairs, key=pairs.get)
vocab = self._merge_vocab(best_pair, vocab)
self.bpe_ranks[best_pair] = i
if (i + 1) % 1000 == 0:
print(f" Merge {i+1}/{num_merges}: {best_pair}")
# 4. 최종 vocabulary 구축
self._build_vocab(vocab)
print(f"Training complete. Vocabulary size: {len(self.encoder)}")
def _get_stats(self, vocab: Dict[str, int]) -> Counter:
"""인접한 심볼 쌍의 빈도 계산"""
pairs = Counter()
for word, freq in vocab.items():
symbols = word.split()
for i in range(len(symbols) - 1):
pairs[symbols[i], symbols[i + 1]] += freq
return pairs
def _merge_vocab(self, pair: Tuple[str, str], vocab: Dict[str, int]) -> Dict[str, int]:
"""vocabulary에서 특정 쌍 병합"""
new_vocab = {}
bigram = ' '.join(pair)
replacement = ''.join(pair)
for word in vocab:
new_word = word.replace(bigram, replacement)
new_vocab[new_word] = vocab[word]
return new_vocab
def _build_vocab(self, vocab: Dict[str, int]):
"""최종 vocabulary 구축"""
# 모든 서브워드 수집
subwords = set()
for word in vocab.keys():
subwords.update(word.split())
# Special tokens 이후부터 ID 할당
idx = len(self.special_tokens)
for subword in sorted(subwords):
if subword not in self.encoder:
self.encoder[subword] = idx
self.decoder[idx] = subword
idx += 1
if idx >= self.vocab_size:
break
def _bpe(self, token: str) -> str:
"""단일 토큰에 BPE 적용"""
if token in self.encoder:
return token
word = tuple(token) + ('</w>',)
pairs = self._get_pairs(word)
if not pairs:
return token
while True:
bigram = min(pairs, key=lambda pair: self.bpe_ranks.get(pair, float('inf')))
if bigram not in self.bpe_ranks:
break
first, second = bigram
new_word = []
i = 0
while i < len(word):
try:
j = word.index(first, i)
new_word.extend(word[i:j])
i = j
except ValueError:
new_word.extend(word[i:])
break
if word[i] == first and i < len(word) - 1 and word[i + 1] == second:
new_word.append(first + second)
i += 2
else:
new_word.append(word[i])
i += 1
word = tuple(new_word)
if len(word) == 1:
break
else:
pairs = self._get_pairs(word)
return ' '.join(word)
def _get_pairs(self, word: Tuple[str, ...]) -> set:
"""단어에서 인접한 심볼 쌍 추출"""
pairs = set()
prev_char = word[0]
for char in word[1:]:
pairs.add((prev_char, char))
prev_char = char
return pairs
def encode(self, text: str, add_special_tokens: bool = True) -> List[int]:
"""텍스트를 토큰 ID로 인코딩"""
tokens = []
if add_special_tokens:
tokens.append(self.bos_token_id)
# 단어로 분리
words = text.split()
for word in words:
# BPE 적용
bpe_tokens = self._bpe(word).split()
for token in bpe_tokens:
tokens.append(self.encoder.get(token, self.unk_token_id))
if add_special_tokens:
tokens.append(self.eos_token_id)
return tokens
def decode(self, token_ids: List[int], skip_special_tokens: bool = True) -> str:
"""토큰 ID를 텍스트로 디코딩"""
tokens = []
for token_id in token_ids:
if skip_special_tokens and token_id in self.special_tokens.values():
continue
token = self.decoder.get(token_id, '<UNK>')
tokens.append(token)
text = ''.join(tokens).replace('</w>', ' ')
return text.strip()
def save(self, path: str):
"""토크나이저 저장"""
data = {
'vocab_size': self.vocab_size,
'encoder': self.encoder,
'decoder': {int(k): v for k, v in self.decoder.items()},
'bpe_ranks': {f"{k[0]}|||{k[1]}": v for k, v in self.bpe_ranks.items()},
'special_tokens': self.special_tokens
}
with open(path, 'w', encoding='utf-8') as f:
json.dump(data, f, ensure_ascii=False, indent=2)
def load(self, path: str):
"""토크나이저 로드"""
with open(path, 'r', encoding='utf-8') as f:
data = json.load(f)
self.vocab_size = data['vocab_size']
self.encoder = data['encoder']
self.decoder = {int(k): v for k, v in data['decoder'].items()}
self.bpe_ranks = {tuple(k.split('|||')): v for k, v in data['bpe_ranks'].items()}
self.special_tokens = data['special_tokens']
# =====================================================
# 2. KV-Cache 완전 구현
# =====================================================
@dataclass
class KVCache:
"""Key-Value Cache for efficient autoregressive generation"""
k_cache: torch.Tensor # [batch, num_heads, seq_len, head_dim]
v_cache: torch.Tensor
seq_len: int = 0
@classmethod
def create(cls, batch_size: int, num_heads: int, max_len: int,
head_dim: int, dtype: torch.dtype, device: torch.device):
"""새로운 KV-Cache 생성"""
k_cache = torch.zeros(batch_size, num_heads, max_len, head_dim,
dtype=dtype, device=device)
v_cache = torch.zeros(batch_size, num_heads, max_len, head_dim,
dtype=dtype, device=device)
return cls(k_cache, v_cache, 0)
def update(self, k: torch.Tensor, v: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
"""캐시 업데이트 및 전체 K, V 반환"""
seq_len = k.size(2)
self.k_cache[:, :, self.seq_len:self.seq_len + seq_len] = k
self.v_cache[:, :, self.seq_len:self.seq_len + seq_len] = v
self.seq_len += seq_len
return (
self.k_cache[:, :, :self.seq_len],
self.v_cache[:, :, :self.seq_len]
)
def clear(self):
"""캐시 초기화"""
self.k_cache.zero_()
self.v_cache.zero_()
self.seq_len = 0
# =====================================================
# 3. 최적화된 Multi-Head Attention
# =====================================================
class OptimizedMultiHeadAttention(nn.Module):
"""Flash Attention 스타일 최적화 + KV-Cache 지원"""
def __init__(self, d_model: int, num_heads: int, dropout: float = 0.1,
bias: bool = True, use_flash: bool = True):
super().__init__()
assert d_model % num_heads == 0
self.d_model = d_model
self.num_heads = num_heads
self.head_dim = d_model // num_heads
self.scale = 1.0 / math.sqrt(self.head_dim)
self.use_flash = use_flash
# Fused QKV projection (더 효율적)
self.qkv_proj = nn.Linear(d_model, 3 * d_model, bias=bias)
self.out_proj = nn.Linear(d_model, d_model, bias=bias)
self.dropout = nn.Dropout(dropout)
self.attn_dropout = nn.Dropout(dropout)
def forward(
self,
x: torch.Tensor,
mask: Optional[torch.Tensor] = None,
cache: Optional[KVCache] = None,
use_cache: bool = False
) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
batch_size, seq_len, _ = x.shape
# Fused QKV projection
qkv = self.qkv_proj(x)
qkv = qkv.reshape(batch_size, seq_len, 3, self.num_heads, self.head_dim)
qkv = qkv.permute(2, 0, 3, 1, 4) # [3, batch, num_heads, seq_len, head_dim]
q, k, v = qkv[0], qkv[1], qkv[2]
# KV-Cache 처리
if use_cache and cache is not None:
k, v = cache.update(k, v)
# Flash Attention 또는 표준 Attention
if self.use_flash and hasattr(F, 'scaled_dot_product_attention'):
# PyTorch 2.0+ Flash Attention
attn_output = F.scaled_dot_product_attention(
q, k, v,
attn_mask=mask,
dropout_p=self.dropout.p if self.training else 0.0,
is_causal=(mask is None)
)
attn_weights = None
else:
# 표준 Attention
scores = torch.matmul(q, k.transpose(-2, -1)) * self.scale
if mask is not None:
scores = scores.masked_fill(mask == 0, float('-inf'))
attn_weights = F.softmax(scores, dim=-1)
attn_weights = self.attn_dropout(attn_weights)
attn_output = torch.matmul(attn_weights, v)
# Reshape and project
attn_output = attn_output.transpose(1, 2).contiguous()
attn_output = attn_output.reshape(batch_size, seq_len, self.d_model)
output = self.out_proj(attn_output)
output = self.dropout(output)
return output, attn_weights
# =====================================================
# 4. 개선된 Feed-Forward Network
# =====================================================
class GLUFeedForward(nn.Module):
"""GLU (Gated Linear Unit) 기반 FFN - GPT-3, LLaMA 스타일"""
def __init__(self, d_model: int, d_ff: int, dropout: float = 0.1,
activation: str = 'gelu'):
super().__init__()
# GLU는 2개의 linear layer 필요
self.w1 = nn.Linear(d_model, d_ff, bias=False)
self.w2 = nn.Linear(d_model, d_ff, bias=False)
self.w3 = nn.Linear(d_ff, d_model, bias=False)
self.dropout = nn.Dropout(dropout)
self.activation = {
'gelu': F.gelu,
'silu': F.silu, # Swish
'relu': F.relu
}[activation]
def forward(self, x: torch.Tensor) -> torch.Tensor:
# SwiGLU: (W1(x) * activation) ⊙ W2(x)
return self.w3(self.dropout(self.activation(self.w1(x)) * self.w2(x)))
# =====================================================
# 5. RoPE (Rotary Position Embedding)
# =====================================================
class RotaryPositionalEmbedding(nn.Module):
"""RoPE - LLaMA, GPT-NeoX 스타일"""
def __init__(self, dim: int, max_seq_len: int = 8192, base: int = 10000):
super().__init__()
self.dim = dim
self.max_seq_len = max_seq_len
self.base = base
# Precompute frequencies
inv_freq = 1.0 / (base ** (torch.arange(0, dim, 2).float() / dim))
self.register_buffer('inv_freq', inv_freq)
# Cache for efficiency
self._seq_len_cached = 0
self._cos_cached = None
self._sin_cached = None
def _update_cache(self, seq_len: int, device: torch.device, dtype: torch.dtype):
"""캐시 업데이트"""
if seq_len > self._seq_len_cached:
self._seq_len_cached = seq_len
t = torch.arange(seq_len, device=device, dtype=dtype)
freqs = torch.outer(t, self.inv_freq)
emb = torch.cat((freqs, freqs), dim=-1)
self._cos_cached = emb.cos()[None, None, :, :]
self._sin_cached = emb.sin()[None, None, :, :]
def rotate_half(self, x: torch.Tensor) -> torch.Tensor:
"""텐서를 반으로 나누고 회전"""
x1, x2 = x.chunk(2, dim=-1)
return torch.cat((-x2, x1), dim=-1)
def forward(self, q: torch.Tensor, k: torch.Tensor) -> Tuple[torch.Tensor, torch.Tensor]:
"""RoPE 적용"""
seq_len = q.shape[2]
self._update_cache(seq_len, q.device, q.dtype)
# Apply rotation
q_embed = (q * self._cos_cached[:, :, :seq_len]) + \
(self.rotate_half(q) * self._sin_cached[:, :, :seq_len])
k_embed = (k * self._cos_cached[:, :, :seq_len]) + \
(self.rotate_half(k) * self._sin_cached[:, :, :seq_len])
return q_embed, k_embed
# =====================================================
# 6. 최적화된 Transformer Block
# =====================================================
class OptimizedTransformerBlock(nn.Module):
"""최적화된 Transformer 블록 - RoPE, GLU, Pre-LN"""
def __init__(
self,
d_model: int,
num_heads: int,
d_ff: int,
dropout: float = 0.1,
use_rope: bool = True,
use_glu: bool = True,
use_flash: bool = True
):
super().__init__()
self.use_rope = use_rope
self.attention = OptimizedMultiHeadAttention(
d_model, num_heads, dropout, use_flash=use_flash
)
if use_glu:
self.feed_forward = GLUFeedForward(d_model, d_ff, dropout, activation='silu')
else:
self.feed_forward = nn.Sequential(
nn.Linear(d_model, d_ff),
nn.GELU(),
nn.Dropout(dropout),
nn.Linear(d_ff, d_model),
nn.Dropout(dropout)
)
self.norm1 = nn.LayerNorm(d_model)
self.norm2 = nn.LayerNorm(d_model)
if use_rope:
self.rope = RotaryPositionalEmbedding(d_model // num_heads)
def forward(
self,
x: torch.Tensor,
mask: Optional[torch.Tensor] = None,
cache: Optional[KVCache] = None,
use_cache: bool = False
) -> torch.Tensor:
# Pre-LN Self-Attention
residual = x
x = self.norm1(x)
attn_out, _ = self.attention(x, mask, cache, use_cache)
x = residual + attn_out
# Pre-LN Feed-Forward
residual = x
x = self.norm2(x)
ff_out = self.feed_forward(x)
x = residual + ff_out
return x
# =====================================================
# 7. 프로덕션급 GPT 모델
# =====================================================
class ProductionGPT(nn.Module):
"""프로덕션급 GPT 모델"""
def __init__(
self,
vocab_size: int,
d_model: int = 768,
num_heads: int = 12,
num_layers: int = 12,
d_ff: int = 3072,
max_seq_len: int = 2048,
dropout: float = 0.1,
pad_token_id: int = 0,
use_rope: bool = True,
use_glu: bool = True,
use_flash: bool = True,
tie_weights: bool = True
):
super().__init__()
self.d_model = d_model
self.vocab_size = vocab_size
self.pad_token_id = pad_token_id
self.max_seq_len = max_seq_len
self.num_layers = num_layers
self.num_heads = num_heads
self.use_rope = use_rope
# Token embedding
self.token_embedding = nn.Embedding(vocab_size, d_model)
# Positional embedding (RoPE를 사용하지 않을 경우)
if not use_rope:
self.pos_embedding = nn.Embedding(max_seq_len, d_model)
else:
self.pos_embedding = None
self.embed_dropout = nn.Dropout(dropout)
# Transformer blocks
self.blocks = nn.ModuleList([
OptimizedTransformerBlock(
d_model, num_heads, d_ff, dropout,
use_rope, use_glu, use_flash
)
for _ in range(num_layers)
])
# Final layer norm
self.final_norm = nn.LayerNorm(d_model)
# Output projection
self.output_projection = nn.Linear(d_model, vocab_size, bias=False)
# Weight tying
if tie_weights:
self.output_projection.weight = self.token_embedding.weight
# Initialize weights
self.apply(self._init_weights)
# Special scaled init for residual projections
for name, param in self.named_parameters():
if 'out_proj.weight' in name or 'w3.weight' in name:
nn.init.normal_(param, mean=0.0, std=0.02 / math.sqrt(2 * num_layers))
def _init_weights(self, module):
"""초기화"""
if isinstance(module, nn.Linear):
nn.init.normal_(module.weight, mean=0.0, std=0.02)
if module.bias is not None:
nn.init.zeros_(module.bias)
elif isinstance(module, nn.Embedding):
nn.init.normal_(module.weight, mean=0.0, std=0.02)
elif isinstance(module, nn.LayerNorm):
nn.init.ones_(module.weight)
nn.init.zeros_(module.bias)
def forward(
self,
input_ids: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
cache_list: Optional[List[KVCache]] = None,
use_cache: bool = False
) -> Tuple[torch.Tensor, Optional[List[KVCache]]]:
batch_size, seq_len = input_ids.shape
device = input_ids.device
# Token embedding
x = self.token_embedding(input_ids)
# Positional embedding (if not using RoPE)
if self.pos_embedding is not None:
positions = torch.arange(seq_len, device=device).unsqueeze(0)
x = x + self.pos_embedding(positions)
x = self.embed_dropout(x)
# Causal mask
if attention_mask is None:
causal_mask = self.create_causal_mask(seq_len, device)
else:
causal_mask = attention_mask.unsqueeze(1).unsqueeze(2)
causal_mask = causal_mask & self.create_causal_mask(seq_len, device)
# Pass through transformer blocks
new_cache_list = [] if use_cache else None
for i, block in enumerate(self.blocks):
cache = cache_list[i] if cache_list is not None else None
x = block(x, causal_mask, cache, use_cache)
if use_cache and cache is not None:
new_cache_list.append(cache)
# Final layer norm
x = self.final_norm(x)
# Project to vocabulary
logits = self.output_projection(x)
return logits, new_cache_list
@staticmethod
def create_causal_mask(seq_len: int, device: torch.device) -> torch.Tensor:
"""Causal mask 생성"""
mask = torch.tril(torch.ones(seq_len, seq_len, device=device)).bool()
return mask.unsqueeze(0).unsqueeze(0)
def get_num_params(self, non_embedding: bool = False) -> int:
"""파라미터 수 계산"""
n_params = sum(p.numel() for p in self.parameters())
if non_embedding:
n_params -= self.token_embedding.weight.numel()
if self.pos_embedding is not None:
n_params -= self.pos_embedding.weight.numel()
return n_params
# =====================================================
# 8. 고급 생성 전략
# =====================================================
class AdvancedGenerator:
"""고급 텍스트 생성 엔진"""
def __init__(self, model: ProductionGPT, tokenizer: BPETokenizer):
self.model = model
self.tokenizer = tokenizer
self.model.eval()
@torch.no_grad()
def generate(
self,
prompt: str,
max_new_tokens: int = 50,
temperature: float = 1.0,
top_k: Optional[int] = 50,
top_p: Optional[float] = 0.95,
repetition_penalty: float = 1.0,
num_beams: int = 1,
do_sample: bool = True,
use_cache: bool = True
) -> str:
"""텍스트 생성"""
# Encode prompt
input_ids = torch.tensor([self.tokenizer.encode(prompt)],
device=next(self.model.parameters()).device)
if num_beams > 1:
output_ids = self._beam_search(
input_ids, max_new_tokens, num_beams, temperature
)
else:
output_ids = self._sample_generate(
input_ids, max_new_tokens, temperature,
top_k, top_p, repetition_penalty, do_sample, use_cache
)
# Decode
generated_text = self.tokenizer.decode(output_ids[0].tolist())
return generated_text
def _sample_generate(
self,
input_ids: torch.Tensor,
max_new_tokens: int,
temperature: float,
top_k: Optional[int],
top_p: Optional[float],
repetition_penalty: float,
do_sample: bool,
use_cache: bool
) -> torch.Tensor:
"""샘플링 기반 생성"""
device = input_ids.device
batch_size = input_ids.size(0)
# Initialize cache
cache_list = None
if use_cache:
cache_list = [
KVCache.create(
batch_size,
self.model.num_heads,
self.model.max_seq_len,
self.model.d_model // self.model.num_heads,
input_ids.dtype,
device
)
for _ in range(self.model.num_layers)
]
generated = input_ids.clone()
for _ in range(max_new_tokens):
# Forward pass (only last token if using cache)
if use_cache and cache_list is not None and generated.size(1) > input_ids.size(1):
curr_input = generated[:, -1:]
else:
curr_input = generated
logits, cache_list = self.model(curr_input, cache_list=cache_list, use_cache=use_cache)
next_token_logits = logits[:, -1, :]
# Apply repetition penalty
if repetition_penalty != 1.0:
for i in range(batch_size):
for token_id in set(generated[i].tolist()):
if next_token_logits[i, token_id] < 0:
next_token_logits[i, token_id] *= repetition_penalty
else:
next_token_logits[i, token_id] /= repetition_penalty
# Apply temperature
next_token_logits = next_token_logits / temperature
# Top-k filtering
if top_k is not None:
indices_to_remove = next_token_logits < torch.topk(next_token_logits, top_k)[0][..., -1, None]
next_token_logits[indices_to_remove] = float('-inf')
# Top-p (nucleus) filtering
if top_p is not None:
sorted_logits, sorted_indices = torch.sort(next_token_logits, descending=True)
cumulative_probs = torch.cumsum(F.softmax(sorted_logits, dim=-1), dim=-1)
sorted_indices_to_remove = cumulative_probs > top_p
sorted_indices_to_remove[..., 1:] = sorted_indices_to_remove[..., :-1].clone()
sorted_indices_to_remove[..., 0] = 0
for i in range(batch_size):
indices_to_remove = sorted_indices[i][sorted_indices_to_remove[i]]
next_token_logits[i, indices_to_remove] = float('-inf')
# Sample or greedy
if do_sample:
probs = F.softmax(next_token_logits, dim=-1)
next_token = torch.multinomial(probs, num_samples=1)
else:
next_token = torch.argmax(next_token_logits, dim=-1, keepdim=True)
generated = torch.cat([generated, next_token], dim=1)
# Stop if EOS
if (next_token == self.tokenizer.eos_token_id).all():
break
return generated
def _beam_search(
self,
input_ids: torch.Tensor,
max_new_tokens: int,
num_beams: int,
temperature: float
) -> torch.Tensor:
"""Beam Search 생성"""
device = input_ids.device
batch_size = input_ids.size(0)
assert batch_size == 1, "Beam search only supports batch_size=1"
# Initialize beams
beam_scores = torch.zeros(num_beams, device=device)
beam_sequences = input_ids.repeat(num_beams, 1)
for _ in range(max_new_tokens):
# Forward pass for all beams
logits, _ = self.model(beam_sequences)
next_token_logits = logits[:, -1, :] / temperature
# Get log probabilities
log_probs = F.log_softmax(next_token_logits, dim=-1)
# Calculate scores for all possible next tokens
vocab_size = log_probs.size(-1)
scores = beam_scores.unsqueeze(1) + log_probs # [num_beams, vocab_size]
# Flatten to get top k across all beams
scores = scores.view(-1)
top_scores, top_indices = torch.topk(scores, num_beams)
# Convert flat indices back to beam and token indices
beam_indices = top_indices // vocab_size
token_indices = top_indices % vocab_size
# Update beam sequences
new_sequences = []
for beam_idx, token_idx in zip(beam_indices, token_indices):
seq = torch.cat([beam_sequences[beam_idx], token_idx.unsqueeze(0)])
new_sequences.append(seq)
beam_sequences = torch.stack(new_sequences)
beam_scores = top_scores
# Check if all beams ended
if (beam_sequences[:, -1] == self.tokenizer.eos_token_id).all():
break
# Return best beam
best_beam_idx = beam_scores.argmax()
return beam_sequences[best_beam_idx:best_beam_idx+1]
def generate_batch(
self,
prompts: List[str],
max_new_tokens: int = 50,
**kwargs
) -> List[str]:
"""배치 생성"""
# Encode all prompts
input_ids_list = [self.tokenizer.encode(p) for p in prompts]
max_len = max(len(ids) for ids in input_ids_list)
# Pad
device = next(self.model.parameters()).device
padded = torch.full(
(len(prompts), max_len),
self.tokenizer.pad_token_id,
dtype=torch.long,
device=device
)
for i, ids in enumerate(input_ids_list):
padded[i, :len(ids)] = torch.tensor(ids)
# Generate
output_ids = self._sample_generate(padded, max_new_tokens, **kwargs)
# Decode
results = []
for ids in output_ids:
text = self.tokenizer.decode(ids.tolist())
results.append(text)
return results
# =====================================================
# 9. 고급 학습 엔진
# =====================================================
class AdvancedTrainer:
"""프로덕션급 학습 엔진"""
def __init__(
self,
model: ProductionGPT,
train_dataloader: DataLoader,
val_dataloader: Optional[DataLoader] = None,
learning_rate: float = 3e-4,
weight_decay: float = 0.1,
betas: Tuple[float, float] = (0.9, 0.95),
warmup_steps: int = 2000,
max_steps: int = 100000,
grad_clip: float = 1.0,
gradient_accumulation_steps: int = 1,
mixed_precision: bool = True,
compile_model: bool = False,
device: str = "cuda"
):
self.model = model
self.train_dataloader = train_dataloader
self.val_dataloader = val_dataloader
self.device = device
self.grad_clip = grad_clip
self.gradient_accumulation_steps = gradient_accumulation_steps
self.mixed_precision = mixed_precision
self.max_steps = max_steps
# Model compilation (PyTorch 2.0+)
if compile_model and hasattr(torch, 'compile'):
self.model = torch.compile(self.model)
self.model.to(device)
# Optimizer with weight decay
decay_params = []
no_decay_params = []
for name, param in self.model.named_parameters():
if param.requires_grad:
if any(nd in name for nd in ['bias', 'norm', 'embedding']):
no_decay_params.append(param)
else:
decay_params.append(param)
optim_groups = [
{'params': decay_params, 'weight_decay': weight_decay},
{'params': no_decay_params, 'weight_decay': 0.0}
]
self.optimizer = torch.optim.AdamW(
optim_groups,
lr=learning_rate,
betas=betas,
fused=True if device == "cuda" else False
)
# Learning rate scheduler (cosine with warmup)
self.warmup_steps = warmup_steps
self.scheduler = self._get_lr_scheduler()
# Mixed precision
self.scaler = torch.cuda.amp.GradScaler() if mixed_precision else None
# Tracking
self.step = 0
self.best_val_loss = float('inf')
self.train_losses = []
self.val_losses = []
def _get_lr_scheduler(self):
"""Cosine decay with warmup"""
def lr_lambda(step):
if step < self.warmup_steps:
return step / self.warmup_steps
else:
progress = (step - self.warmup_steps) / (self.max_steps - self.warmup_steps)
return 0.5 * (1.0 + math.cos(math.pi * progress))
return torch.optim.lr_scheduler.LambdaLR(self.optimizer, lr_lambda)
def train_step(self, batch: Dict[str, torch.Tensor]) -> float:
"""단일 학습 스텝"""
input_ids = batch['input_ids'].to(self.device)
labels = batch['labels'].to(self.device)
if self.mixed_precision:
with torch.cuda.amp.autocast():
logits, _ = self.model(input_ids)
loss = F.cross_entropy(
logits.view(-1, self.model.vocab_size),
labels.view(-1),
ignore_index=-100
)
loss = loss / self.gradient_accumulation_steps
self.scaler.scale(loss).backward()
if (self.step + 1) % self.gradient_accumulation_steps == 0:
self.scaler.unscale_(self.optimizer)
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.grad_clip)
self.scaler.step(self.optimizer)
self.scaler.update()
self.optimizer.zero_grad()
self.scheduler.step()
else:
logits, _ = self.model(input_ids)
loss = F.cross_entropy(
logits.view(-1, self.model.vocab_size),
labels.view(-1),
ignore_index=-100
)
loss = loss / self.gradient_accumulation_steps
loss.backward()
if (self.step + 1) % self.gradient_accumulation_steps == 0:
torch.nn.utils.clip_grad_norm_(self.model.parameters(), self.grad_clip)
self.optimizer.step()
self.optimizer.zero_grad()
self.scheduler.step()
return loss.item() * self.gradient_accumulation_steps
@torch.no_grad()
def validate(self) -> float:
"""검증"""
if self.val_dataloader is None:
return 0.0
self.model.eval()
total_loss = 0
num_batches = 0
for batch in self.val_dataloader:
input_ids = batch['input_ids'].to(self.device)
labels = batch['labels'].to(self.device)
logits, _ = self.model(input_ids)
loss = F.cross_entropy(
logits.view(-1, self.model.vocab_size),