File size: 26,887 Bytes
c65503b |
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 |
"""
Boundary-Aware Intelligent Tokenizer Model
๋ฐ์ดํธ-๋ฌธ์ ๊ด๊ณ๋ฅผ ๋ช
์์ ์ผ๋ก ํ์ตํ๋ ๋ชจ๋ธ
"""
import torch
import torch.nn as nn
import torch.nn.functional as F
from typing import Dict, List, Optional, Tuple
import math
# Import necessary components from unified_model
from .unified_model import ByteEncoder, TransformerDecoder, CrossAttention, PositionalEncoding
class BoundaryAwareEncoder(nn.Module):
"""
๋ฐ์ดํธ-๋ฌธ์ ๊ฒฝ๊ณ๋ฅผ ๋ช
์์ ์ผ๋ก ํ์ตํ๋ ์ธ์ฝ๋
"""
def __init__(
self,
vocab_size: int = 260,
hidden_dims: List[int] = [512, 512, 640, 768, 768], # 384โ512๋ก ์ฆ๊ฐ
num_heads: int = 8,
dropout: float = 0.1,
max_seq_len: int = 512
):
super().__init__()
# 1. ๋ฐ์ดํธ ์๋ฒ ๋ฉ
self.byte_embedding = nn.Embedding(vocab_size, hidden_dims[0])
# 2. ๊ฒฝ๊ณ ์๋ฒ ๋ฉ (START, CONT, END, SPECIAL) - ๋ ํฐ ์ฐจ์
self.boundary_embedding = nn.Embedding(4, 128) # ๊ณ ์ 128์ฐจ์
# 3. ๋ฌธ์ ํ์
์๋ฒ ๋ฉ (ASCII, Korean, Chinese, etc.) - ๋ ํฐ ์ฐจ์
self.char_type_embedding = nn.Embedding(14, 128) # ๊ณ ์ 128์ฐจ์
# 4. ๋ฐ์ดํธ ์นด์ดํธ ์๋ฒ ๋ฉ (1-4 bytes) - UTF-8 ํจํด ์ค์
self.byte_count_embedding = nn.Embedding(5, 128) # ๊ณ ์ 128์ฐจ์
# 5. ๋ฌธ์ ์ธ๋ฑ์ค ์๋ฒ ๋ฉ (relative position within char)
self.char_position_embedding = nn.Embedding(4, 128) # ๊ณ ์ 128์ฐจ์
# ํตํฉ projection (๋ฐ์ดํธ ์๋ฒ ๋ฉ 512 + ๊ตฌ์กฐ ์๋ฒ ๋ฉ 512 = 1024)
structural_dim = 128 * 4 # boundary(128) + char_type(128) + byte_count(128) + char_pos(128)
self.input_projection = nn.Linear(hidden_dims[0] + structural_dim, hidden_dims[0])
# Positional encoding
self.pos_encoding = PositionalEncoding(hidden_dims[0], max_seq_len, dropout)
# Transformer layers (๊ธฐ์กด ๊ตฌ์กฐ ์ฌ์ฌ์ฉ)
self.layers = nn.ModuleList()
for i in range(len(hidden_dims)):
input_dim = hidden_dims[i-1] if i > 0 else hidden_dims[0]
output_dim = hidden_dims[i]
if input_dim != output_dim:
proj = nn.Linear(input_dim, output_dim)
else:
proj = None
layer = nn.TransformerEncoderLayer(
d_model=output_dim,
nhead=num_heads,
dim_feedforward=output_dim * 4,
dropout=dropout,
activation='gelu',
batch_first=True,
norm_first=True
)
self.layers.append(nn.ModuleDict({
'projection': proj,
'transformer': layer,
'norm': nn.LayerNorm(output_dim)
}))
# Hierarchical Merging Components (์๋ก ์ถ๊ฐ)
# ๊ฐ ๋ ์ด์ด๋ง๋ค ๋ณํฉ ๋ชจ๋ ์ถ๊ฐ - ํธ๋์คํฌ๋จธ๊ฐ ์ค์ค๋ก ๊ฒฐ์
self.merging_modules = nn.ModuleList()
for i in range(len(hidden_dims)):
dim = hidden_dims[i]
# Learned merging decision - no fixed ratios!
merge_module = nn.ModuleDict({
# ๊ฒฝ๊ณ ํ์ต์ ์ํ ๋ชจ๋
'boundary_detector': nn.Linear(dim, 3), # START, CONT, END
'merge_attention': nn.MultiheadAttention(dim, num_heads//2, dropout, batch_first=True),
'merge_gate': nn.Sequential(
nn.Linear(dim * 2, dim),
nn.ReLU(),
nn.Linear(dim, 1)
), # ๋ณํฉ ๊ฒฐ์ (ํ์ต์ผ๋ก ๊ฒฐ์ )
'merge_proj': nn.Linear(dim * 2, dim), # ๋ณํฉ ํ ํ๋ก์ ์
})
self.merging_modules.append(merge_module)
# ๊ฒฝ๊ณ ์์ธก ํค๋
self.boundary_predictor = nn.Linear(hidden_dims[-1], 4)
# ๋ฌธ์ ํ์
์์ธก ํค๋
self.char_type_predictor = nn.Linear(hidden_dims[-1], 14)
def forward(
self,
input_ids: torch.Tensor,
boundary_labels: Optional[torch.Tensor] = None,
char_types: Optional[torch.Tensor] = None,
byte_counts: Optional[torch.Tensor] = None,
char_indices: Optional[torch.Tensor] = None,
attention_mask: Optional[torch.Tensor] = None
) -> Dict[str, torch.Tensor]:
batch_size, seq_len = input_ids.shape
device = input_ids.device
# 1. ๋ฐ์ดํธ ์๋ฒ ๋ฉ
byte_emb = self.byte_embedding(input_ids) # [B, S, D]
# 2. ๊ฒฝ๊ณ ์ ๋ณด ์๋ฒ ๋ฉ (ํ์ต ์์๋ง)
if boundary_labels is not None:
boundary_emb = self.boundary_embedding(boundary_labels) # [B, S, D/4]
else:
# ์ถ๋ก ์: ๋ฐ์ดํธ ๊ฐ์ผ๋ก๋ถํฐ ๊ฒฝ๊ณ ์ถ์
# UTF-8 ํจํด:
# 0xxxxxxx (0-127): ASCII (START)
# 110xxxxx (192-223): 2-byte start
# 1110xxxx (224-239): 3-byte start
# 11110xxx (240-247): 4-byte start
# 10xxxxxx (128-191): continuation
estimated_boundaries = torch.zeros_like(input_ids)
# ASCII (0-127)
ascii_mask = input_ids < 128
estimated_boundaries[ascii_mask] = 1 # START
# Continuation bytes (128-191)
cont_mask = (input_ids >= 128) & (input_ids < 192)
estimated_boundaries[cont_mask] = 0 # CONT
# Multi-byte starters
mb_start_mask = input_ids >= 192
estimated_boundaries[mb_start_mask] = 1 # START
boundary_emb = self.boundary_embedding(estimated_boundaries)
# 3. ๋ฌธ์ ํ์
์๋ฒ ๋ฉ
if char_types is not None:
char_type_emb = self.char_type_embedding(char_types)
else:
# ์ถ๋ก ์: ๊ธฐ๋ณธ๊ฐ ์ฌ์ฉ
char_type_emb = self.char_type_embedding(torch.zeros_like(input_ids))
# 4. ๋ฐ์ดํธ ์นด์ดํธ ์๋ฒ ๋ฉ
if byte_counts is not None:
byte_count_emb = self.byte_count_embedding(torch.clamp(byte_counts, 0, 4))
else:
# ์ถ๋ก ์: ๋ฐ์ดํธ ํจํด์ผ๋ก ์ถ์
estimated_counts = torch.ones_like(input_ids)
# UTF-8 ํจํด์ผ๋ก ๋ฉํฐ๋ฐ์ดํธ ๊ธธ์ด ์ถ์
estimated_counts[input_ids >= 240] = 4 # 4-byte
estimated_counts[(input_ids >= 224) & (input_ids < 240)] = 3 # 3-byte
estimated_counts[(input_ids >= 192) & (input_ids < 224)] = 2 # 2-byte
byte_count_emb = self.byte_count_embedding(estimated_counts)
# 5. ๋ฌธ์ ๋ด ์์น ์๋ฒ ๋ฉ
if char_indices is not None:
# ๊ฐ์ ๋ฌธ์ ๋ด์์์ ์๋ ์์น ๊ณ์ฐ
char_positions = torch.zeros_like(char_indices)
for b in range(batch_size):
current_char = -1
position = 0
for i in range(seq_len):
if char_indices[b, i] != current_char:
current_char = char_indices[b, i]
position = 0
else:
position += 1
char_positions[b, i] = min(position, 3)
char_pos_emb = self.char_position_embedding(char_positions)
else:
char_pos_emb = self.char_position_embedding(torch.zeros_like(input_ids))
# 6. ๋ชจ๋ ์๋ฒ ๋ฉ ํตํฉ
# ๋ฐ์ดํธ ์๋ฒ ๋ฉ + ๊ตฌ์กฐ ์ ๋ณด
structural_emb = torch.cat([
boundary_emb,
char_type_emb,
byte_count_emb,
char_pos_emb
], dim=-1) # [B, S, D]
combined_emb = torch.cat([byte_emb, structural_emb], dim=-1) # [B, S, 2*D]
# Projection to original dimension
x = self.input_projection(combined_emb) # [B, S, D]
# Positional encoding
x = self.pos_encoding(x)
# Transformer layers with hierarchical merging
all_hidden_states = []
boundary_predictions = []
char_type_predictions = []
merge_info = [] # ๋ณํฉ ์ ๋ณด ์ ์ฅ
for i, layer_dict in enumerate(self.layers):
# Project if needed
if layer_dict['projection'] is not None:
x = layer_dict['projection'](x)
# Transformer layer
if attention_mask is not None:
# Ensure mask matches current sequence length
current_seq_len = x.size(1)
if attention_mask.size(1) != current_seq_len:
# Adjust mask to match current sequence length after merging
key_padding_mask = torch.zeros(x.size(0), current_seq_len, dtype=torch.bool, device=x.device)
# Copy valid mask values
valid_len = min(attention_mask.size(1), current_seq_len)
key_padding_mask[:, :valid_len] = (attention_mask[:, :valid_len] == 0)
else:
key_padding_mask = (attention_mask == 0)
x = layer_dict['transformer'](x, src_key_padding_mask=key_padding_mask)
else:
x = layer_dict['transformer'](x)
x = layer_dict['norm'](x)
# Store hidden state BEFORE merging (for proper gradient flow)
all_hidden_states.append(x.clone())
# Hierarchical Progressive Merging - ๊ณ์ธต์ ์ ์ง์ ๋ณํฉ
# Layer๋ณ๋ก ๋ค๋ฅธ ์์ค์ ๋ณํฉ ํ์ต (๋ฐ์ดํธโ๋ฌธ์โ๋จ์ดโ์ด์ )
if i < len(self.merging_modules) and self.merging_modules[i] is not None:
merge_module = self.merging_modules[i]
batch_size, seq_len, hidden_dim = x.shape
# Skip if already compressed too much
if seq_len < 4:
continue
# Layer 0: UTF-8 ๊ฒฝ๊ณ ๊ธฐ๋ฐ ๋ณํฉ (๋ฐ์ดํธ โ ๋ฌธ์)
if i == 0 and input_ids is not None:
# UTF-8 ๊ฒฝ๊ณ ๊ฐ์ง๋ฅผ ์ฌ์ฉํ ํ์คํ ๋ณํฉ
merge_decisions = torch.zeros(batch_size, seq_len - 1, device=x.device)
for b in range(batch_size):
for idx in range(seq_len - 1):
if idx < input_ids.shape[1] - 1:
current_byte = input_ids[b, idx].item()
next_byte = input_ids[b, idx + 1].item()
# Continuation byte (10xxxxxx) should merge with previous
if 128 <= next_byte < 192: # Next is continuation
merge_decisions[b, idx] = 1.0 # Merge with next
# Special tokens don't merge
elif current_byte >= 256 or next_byte >= 256:
merge_decisions[b, idx] = 0.0
# Also calculate merge_probs for logging
x_pairs = torch.cat([x[:, :-1], x[:, 1:]], dim=-1)
merge_scores = merge_module['merge_gate'](x_pairs).squeeze(-1)
merge_probs = torch.sigmoid(merge_scores)
# Use UTF-8 based decisions for layer 0
layer_merge_threshold = 0.5 # Not used but logged
else:
# Other layers: ํ์ต ๊ธฐ๋ฐ ๋ณํฉ
# 1. ํธ๋์คํฌ๋จธ๊ฐ ๋ณํฉ ๊ฒฝ๊ณ๋ฅผ ํ์ต
# ์ธ์ ํ ํฐ ์์ ๋ณํฉ ์ ์ ๊ณ์ฐ
x_pairs = torch.cat([x[:, :-1], x[:, 1:]], dim=-1) # [B, S-1, 2*D]
merge_scores = merge_module['merge_gate'](x_pairs).squeeze(-1) # [B, S-1]
merge_probs = torch.sigmoid(merge_scores) # 0~1 ํ๋ฅ
# 3. ๊ณ์ธต๋ณ ๋ณํฉ ๊ฐ๋ ์ค์ (ํ์ต ๊ฐ๋ฅ)
# ์ค๊ฐ ๋ ์ด์ด: ์ค๊ฐ ๋ณํฉ๋ฅ (๋ฌธ์โ๋จ์ด)
# ์ต์ข
๋ ์ด์ด: ๋์ ๋ณํฉ๋ฅ (๋จ์ดโ์ด์ )
layer_merge_threshold = 0.7 + (i / len(self.merging_modules)) * 0.2 # 0.7 โ 0.9
# 4. ๋ณํฉ ๊ฒฐ์ (ํ์ต๋ ํ๋ฅ ๊ธฐ๋ฐ)
merge_decisions = (merge_probs > layer_merge_threshold).float()
# 2. Self-attention์ผ๋ก ์ ์ญ ์ปจํ
์คํธ ํ์
attn_output, attn_weights = merge_module['merge_attention'](x, x, x)
# 5. ์ค์ ๋ณํฉ ์ํ (GPU ๋ณ๋ ฌ ์ฒ๋ฆฌ)
# ๋ณํฉ ๋ง์คํฌ ์์ฑ
merged_indices = []
merged_x = []
new_mask = []
# Efficient parallel merging using cumsum trick
# merge_decisions๊ฐ 1์ธ ์์น์์ ๋ค์ ํ ํฐ๊ณผ ๋ณํฉ
# group_ids๋ seq_len ํฌ๊ธฐ์ฌ์ผ ํจ (merge_decisions๋ seq_len-1)
group_ids = torch.zeros(batch_size, seq_len, device=x.device)
group_ids[:, 0] = 0
group_ids[:, 1:] = 1 - merge_decisions # ์ ๊ทธ๋ฃน ์์ ์์น
group_ids = group_ids.cumsum(dim=1).long() # ๊ทธ๋ฃน ID ํ ๋น
# ๊ฐ ๊ทธ๋ฃน์ ์ต๋ ID ์ฐพ๊ธฐ
max_groups = group_ids.max(dim=1)[0] + 1 # ๊ฐ ๋ฐฐ์น์ ๊ทธ๋ฃน ์
max_group_size = max_groups.max().item()
# ๊ทธ๋ฃน๋ณ aggregation (gradient-safe ๋ฐฉ๋ฒ)
# Use index_add instead of scatter for better gradient flow
new_x_list = []
new_mask_list = []
for b in range(batch_size):
# Create mapping from old to new indices
unique_groups, inverse_indices = torch.unique(group_ids[b], return_inverse=True)
num_groups = len(unique_groups)
# Initialize new tensor for this batch
batch_new_x = torch.zeros(num_groups, hidden_dim, device=x.device)
group_counts = torch.zeros(num_groups, device=x.device)
# Sum tokens belonging to same group
batch_new_x = batch_new_x.index_add(0, inverse_indices, x[b])
group_counts = group_counts.index_add(0, inverse_indices, torch.ones(seq_len, device=x.device))
# Average
batch_new_x = batch_new_x / group_counts.unsqueeze(-1).clamp(min=1)
new_x_list.append(batch_new_x)
new_mask_list.append(torch.ones(num_groups, device=x.device))
# Pad to same size for batching
max_new_len = max(t.size(0) for t in new_x_list)
padded_x_list = []
padded_mask_list = []
for batch_x, batch_mask in zip(new_x_list, new_mask_list):
pad_len = max_new_len - batch_x.size(0)
if pad_len > 0:
batch_x = torch.cat([batch_x, torch.zeros(pad_len, hidden_dim, device=x.device)], dim=0)
batch_mask = torch.cat([batch_mask, torch.zeros(pad_len, device=x.device)], dim=0)
padded_x_list.append(batch_x)
padded_mask_list.append(batch_mask)
new_x = torch.stack(padded_x_list)
valid_mask = torch.stack(padded_mask_list)
# Trim to actual size (important for gradient flow)
actual_len = valid_mask.sum(dim=1).max().long().item()
new_x = new_x[:, :actual_len]
valid_mask = valid_mask[:, :actual_len]
# Attention ์ ๋ณด ์ถ๊ฐ (์ ํ์ )
new_x = new_x + attn_output.mean(dim=1, keepdim=True).expand(-1, actual_len, -1) * 0.1
# Update x and attention_mask
x = new_x
attention_mask = valid_mask
# Note: DO NOT re-apply positional encoding after merging
# The transformer already learned position-aware representations
# Store merge mapping for cross-attention and decoder
# ์๋ณธ ์์น โ ๋ณํฉ ํ ์์น ๋งคํ ์ ์ฅ (๋์ฝ๋ ๋ณต์์ฉ)
merge_mapping = {
'original_positions': torch.arange(seq_len, device=x.device),
'merged_groups': group_ids,
'group_sizes': None # No longer using counts
}
# ์ ๋ณด ๊ธฐ๋ก (actual_len already computed above)
merge_info.append({
'layer': i,
'original_len': seq_len,
'merged_len': actual_len,
'compression_ratio': seq_len / max(actual_len, 1),
'merge_threshold': layer_merge_threshold,
'avg_merge_prob': merge_probs.mean().item(),
'merge_mapping': merge_mapping # ๋ณต์์ ์ํ ๋งคํ ์ ๋ณด
})
# ์ค๊ฐ ์ธต์์๋ ๊ฒฝ๊ณ ์์ธก (auxiliary loss) - ๋ง์ง๋ง ์ธต์์๋ง
if i == len(self.layers) - 1: # ๋ง์ง๋ง ์ธต์์๋ง ์์ธก
boundary_pred = self.boundary_predictor(x)
char_type_pred = self.char_type_predictor(x)
boundary_predictions.append(boundary_pred)
char_type_predictions.append(char_type_pred)
# Pool for sequence representation
if attention_mask is not None:
mask = attention_mask.unsqueeze(-1)
pooled = (x * mask).sum(dim=1) / mask.sum(dim=1).clamp(min=1e-9)
else:
pooled = x.mean(dim=1)
return {
'last_hidden_state': x,
'pooled_output': pooled,
'all_hidden_states': all_hidden_states,
'boundary_predictions': boundary_predictions, # ๊ฒฝ๊ณ ์์ธก (์ฌ๋ฌ ์ธต)
'char_type_predictions': char_type_predictions, # ๋ฌธ์ ํ์
์์ธก
'boundary_logits': self.boundary_predictor(x), # ์ต์ข
๊ฒฝ๊ณ ์์ธก
'char_type_logits': self.char_type_predictor(x), # ์ต์ข
๋ฌธ์ ํ์
์์ธก
'merge_info': merge_info, # ๋ณํฉ ์ ๋ณด (์๋ก ์ถ๊ฐ)
'attention_mask': attention_mask # ์
๋ฐ์ดํธ๋ ๋ง์คํฌ ๋ฐํ
}
class BoundaryAwareTokenizerModel(nn.Module):
"""
๋ฐ์ดํธ-๋ฌธ์ ๊ด๊ณ๋ฅผ ๋ช
์์ ์ผ๋ก ํ์ตํ๋ ํตํฉ ๋ชจ๋ธ
"""
def __init__(
self,
vocab_size: int = 260,
encoder_dims: List[int] = [512, 512, 640, 768, 768], # 384โ512๋ก ์ฆ๊ฐ
decoder_hidden: int = 768,
num_heads: int = 8,
num_decoder_layers: int = 6,
dropout: float = 0.1,
max_seq_len: int = 512
):
super().__init__()
# Boundary-aware encoder
self.encoder = BoundaryAwareEncoder(
vocab_size, encoder_dims, num_heads, dropout, max_seq_len
)
# Standard decoder (์ฌ์ฌ์ฉ)
self.decoder = TransformerDecoder(
vocab_size, decoder_hidden, num_heads, num_decoder_layers, dropout, max_seq_len
)
# Cross-attention (์ฌ์ฌ์ฉ)
self.cross_attention = CrossAttention(encoder_dims[-1], num_heads, dropout)
def forward(
self,
input_ids: torch.Tensor,
attention_mask: Optional[torch.Tensor] = None,
boundary_labels: Optional[torch.Tensor] = None,
char_types: Optional[torch.Tensor] = None,
byte_counts: Optional[torch.Tensor] = None,
char_indices: Optional[torch.Tensor] = None,
decoder_input_ids: Optional[torch.Tensor] = None,
labels: Optional[torch.Tensor] = None,
use_cross_attention: bool = True
) -> Dict[str, torch.Tensor]:
# 1. Boundary-aware encoding
encoder_outputs = self.encoder(
input_ids=input_ids,
boundary_labels=boundary_labels,
char_types=char_types,
byte_counts=byte_counts,
char_indices=char_indices,
attention_mask=attention_mask
)
encoder_hidden = encoder_outputs['last_hidden_state']
# 2. Decoding
# Pass the updated attention_mask from encoder (after merging)
encoder_mask = encoder_outputs.get('attention_mask', attention_mask)
# Use input_ids as decoder_input_ids for teacher forcing if not provided
if decoder_input_ids is None and input_ids is not None:
decoder_input_ids = input_ids
decoder_outputs = self.decoder(
encoder_hidden,
decoder_input_ids,
encoder_mask # Use encoder's updated mask
)
# 3. Cross-attention (optional)
cross_attn_outputs = None
relation_logits = None
if use_cross_attention and decoder_outputs['hidden_states'] is not None:
decoder_hidden = decoder_outputs['hidden_states']
cross_attn_outputs = self.cross_attention(
query=decoder_hidden,
key=encoder_hidden,
query_mask=None,
key_mask=attention_mask
)
relation_logits = cross_attn_outputs['relation_logits']
# Enhanced decoder with cross-attention
enhanced_decoder = decoder_hidden + cross_attn_outputs['cross_attention']
decoder_outputs['logits'] = self.decoder.output_projection(enhanced_decoder)
# 4. Loss calculation
total_loss = None
if labels is not None:
# Reconstruction loss
loss_fct = nn.CrossEntropyLoss(ignore_index=256) # PAD
recon_loss = loss_fct(
decoder_outputs['logits'].reshape(-1, decoder_outputs['logits'].size(-1)),
labels.reshape(-1)
)
total_loss = recon_loss
# Boundary prediction loss
if boundary_labels is not None and 'boundary_logits' in encoder_outputs:
boundary_logits = encoder_outputs['boundary_logits']
# Check if dimensions match
logits_size = boundary_logits.size(0) * boundary_logits.size(1)
labels_size = boundary_labels.numel()
if logits_size == labels_size:
boundary_loss_fct = nn.CrossEntropyLoss(ignore_index=3) # special
boundary_loss = boundary_loss_fct(
boundary_logits.reshape(-1, 4),
boundary_labels.reshape(-1)
)
total_loss = total_loss + boundary_loss * 0.3
# If encoder changed sequence length (due to merging), skip boundary loss
# This is expected behavior when boundary-aware merging is active
# Character type prediction loss
if char_types is not None and 'char_type_logits' in encoder_outputs:
char_type_logits = encoder_outputs['char_type_logits']
# Check if dimensions match
logits_size = char_type_logits.size(0) * char_type_logits.size(1)
labels_size = char_types.numel()
if logits_size == labels_size:
char_type_loss_fct = nn.CrossEntropyLoss(ignore_index=13) # special
char_type_loss = char_type_loss_fct(
char_type_logits.reshape(-1, 14),
char_types.reshape(-1)
)
total_loss = total_loss + char_type_loss * 0.2
# If encoder changed sequence length (due to merging), skip char type loss
# Auxiliary losses from intermediate layers
if encoder_outputs.get('boundary_predictions') and boundary_labels is not None:
# boundary_loss_fct๋ ์์์ ์ ์๋ ๊ฒฝ์ฐ์๋ง ์ฌ์ฉ
if 'boundary_loss_fct' in locals():
for boundary_pred in encoder_outputs['boundary_predictions']:
# Ensure batch sizes match
pred_batch_size = boundary_pred.size(0) * boundary_pred.size(1)
label_batch_size = boundary_labels.numel()
if pred_batch_size == label_batch_size:
aux_boundary_loss = boundary_loss_fct(
boundary_pred.reshape(-1, 4),
boundary_labels.reshape(-1)
)
total_loss = total_loss + aux_boundary_loss * 0.1
else:
# Skip if dimensions don't match (different layer sizes)
continue
return {
'loss': total_loss,
'logits': decoder_outputs['logits'],
'encoder_hidden_states': encoder_hidden,
'decoder_hidden_states': decoder_outputs['hidden_states'],
'boundary_logits': encoder_outputs['boundary_logits'],
'char_type_logits': encoder_outputs['char_type_logits'],
'boundary_predictions': encoder_outputs.get('boundary_predictions'),
'relation_logits': relation_logits,
'cross_attention': cross_attn_outputs['cross_attention'] if cross_attn_outputs else None
} |