Spaces:
Sleeping
Sleeping
Download core/embedding.py from Debito/mamba-encoder-swarm_app: direct link, hf CLI and curl.
- Browser
- Download file 1.06 kB
-
https://huggingface.co/spaces/Debito/mamba-encoder-swarm_app/resolve/main/core/embedding.py
- Command line
-
hf download hf://spaces/Debito/mamba-encoder-swarm_app/core/embedding.py
-
curl -L -o embedding.py https://huggingface.co/spaces/Debito/mamba-encoder-swarm_app/resolve/main/core/embedding.py
1.06 kB
| # ============================================================================= | |
| # core/embedding.py | |
| # ============================================================================= | |
| import torch | |
| import torch.nn as nn | |
| import math | |
| from config import MambaConfig | |
| class MambaEmbedding(nn.Module): | |
| def __init__(self, config: MambaConfig): | |
| super().__init__() | |
| self.config = config | |
| # Token embeddings (no positional encoding needed for Mamba) | |
| self.token_embedding = nn.Embedding( | |
| config.vocab_size, | |
| config.d_model, | |
| dtype=config.dtype | |
| ) | |
| # Initialize embeddings | |
| nn.init.normal_(self.token_embedding.weight, std=0.02) | |
| def forward(self, input_ids: torch.Tensor) -> torch.Tensor: | |
| """ | |
| Args: | |
| input_ids: [batch_size, seq_len] | |
| Returns: | |
| embeddings: [batch_size, seq_len, d_model] | |
| """ | |
| embeddings = self.token_embedding(input_ids) | |
| return embeddings |