JonasGeiping commited on
Commit
23de8b3
·
verified ·
1 Parent(s): 305fa6f

Upload channel_embedding.py with huggingface_hub

Browse files
Files changed (1) hide show
  1. channel_embedding.py +33 -0
channel_embedding.py ADDED
@@ -0,0 +1,33 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """Channel embedding module for multi-stream models."""
2
+
3
+ import torch
4
+ from torch import nn
5
+
6
+
7
+ class ChannelEmbedding(nn.Module):
8
+ """Learnable per-channel embedding added to token hidden states.
9
+
10
+ Args:
11
+ num_channels: Number of channels (streams).
12
+ hidden_size: Model hidden dimension.
13
+ method: "additive" adds embedding to hidden states, "none" is a pass-through.
14
+ """
15
+
16
+ def __init__(self, num_channels: int, hidden_size: int, method: str = "additive"):
17
+ super().__init__()
18
+ self.method = method
19
+ self.num_channels = num_channels
20
+ if method == "additive":
21
+ self.embedding = nn.Embedding(num_channels, hidden_size)
22
+ elif method == "none":
23
+ pass
24
+ else:
25
+ raise ValueError(f"Unknown channel embedding method: {method}")
26
+
27
+ def forward(
28
+ self, hidden_states: torch.Tensor, channel_ids: torch.LongTensor
29
+ ) -> torch.Tensor:
30
+ if self.method == "none":
31
+ return hidden_states
32
+ channel_emb = self.embedding(channel_ids)
33
+ return hidden_states + channel_emb