PowerMachine commited on
Commit
69a2f5a
·
verified ·
1 Parent(s): 4e7f6ee

Delete src/bigru_t/model/transformer_unit.py

Browse files
src/bigru_t/model/transformer_unit.py DELETED
@@ -1,68 +0,0 @@
1
- """transformer_unit.py — TransformerUnit: self-attention + FFN para 8 representações.
2
-
3
- Componente do u8cell_T (Lema 1). Processa as 8 saídas das BiGRU4 paralelas
4
- via self-attention multi-head, produzindo um vetor agregado.
5
-
6
- Diferenças vs. v13.9.2:
7
- - Pooling por média (não por Confidence Gate) — a seleção é feita no nível
8
- do UnifiedModel via softmax (Lema 1)
9
- - Pre-norm consistente (BUG-003 fix do v13.9.1 aplicado)
10
- - Sem RoPE (a entrada já é um embedding agregado, não uma sequência posicional)
11
- """
12
- from __future__ import annotations
13
-
14
- import torch
15
- import torch.nn as nn
16
- import torch.nn.functional as F
17
-
18
-
19
- class TransformerUnit(nn.Module):
20
- """Uma unidade Transformer (self-attention + FFN) para processar 8 representações.
21
-
22
- Args:
23
- d_model: dimensão do modelo (igual à saída projetada das BiGRU4)
24
- nhead: número de cabeças de atenção
25
- d_feedforward: dimensão interna do FFN (SwiGLU-style)
26
- dropout: probabilidade de dropout
27
-
28
- Forward:
29
- src: (batch, 8, d_model) → h: (batch, d_model)
30
- h é a média sobre as 8 posições pós-attention+FFN.
31
- """
32
-
33
- def __init__(self, d_model: int, nhead: int, d_feedforward: int, dropout: float = 0.1):
34
- super().__init__()
35
- self.d_model = d_model
36
- self.nhead = nhead
37
-
38
- # Self-attention multi-head (batch_first para consistência)
39
- self.self_attn = nn.MultiheadAttention(
40
- d_model, nhead, dropout=dropout, batch_first=True
41
- )
42
-
43
- # FFN: Linear → ReLU → Dropout → Linear (pre-norm style)
44
- self.linear1 = nn.Linear(d_model, d_feedforward)
45
- self.dropout = nn.Dropout(dropout)
46
- self.linear2 = nn.Linear(d_feedforward, d_model)
47
-
48
- # LayerNorms (pre-norm)
49
- self.norm1 = nn.LayerNorm(d_model)
50
- self.norm2 = nn.LayerNorm(d_model)
51
- self.dropout1 = nn.Dropout(dropout)
52
- self.dropout2 = nn.Dropout(dropout)
53
-
54
- def forward(self, src: torch.Tensor) -> torch.Tensor:
55
- """src: (batch, 8, d_model) → h: (batch, d_model)"""
56
- # Pre-norm self-attention
57
- normed = self.norm1(src)
58
- attn_out, _ = self.self_attn(normed, normed, normed, need_weights=False)
59
- src = src + self.dropout1(attn_out)
60
-
61
- # Pre-norm FFN
62
- normed = self.norm2(src)
63
- ff_out = self.linear2(self.dropout(F.relu(self.linear1(normed))))
64
- src = src + self.dropout2(ff_out)
65
-
66
- # Pooling por média sobre as 8 posições
67
- h = src.mean(dim=1) # (batch, d_model)
68
- return h